Well, I've had good help from here so far and I thought I'd ask a question that's been on my mind since starting with OpenGL. The question is around coordinates and sizing and the fact that they're usually GLFloats and appear to be between small values like -1.0 and 1.0... as I've been coding simple 2D / VCL programs (with limited graphics) for years, I'm obviously much better suited to working in unit of pixels and I'm wondering what's the simplest way to go about this in OpenGL (but not in 2D orthographic - as I've done that already for 2D stuff).
I'm building up a concept of 3D Album Cover picking for addition to my HMusicCentre application so I'm only a little way down the experimental path so far - but I have the drawing working and the picking working. Here I include a link to a .zip file here that contains all the required files to simply compile it in Delphi 7... there's also the small .exe included - here: http://www.hmusiccentre.org.uk/download ... icking.zip - it's only 360K, so if anyone helpful wanted to get hold it and see what I'm doing that'd be great.
Here's how it looks... my Album Covers spin around and wobble a bit, then - when the mouse goes over them - they lean at a shallower angle so that the user can see what they are more clearly - that's the idea anyway. I'm drawing my quads using the GLFloat coordinates, but measuring where my mouse is using Pixels - not a great idea I am sure.
At the moment, I'm displaying my OpenGL stuff directly to a TForm that is around 1000 x 600 pixels in size, and I'm setting up OpenGL with the following code...
...which I guess is pretty standard stuff. Then I draw my Quads with a function that takes a number of GLFloats for position, angles, rotation and size...
...what I'd like to be able to do is set up my program so that I can address my (X,Y,Z) coordinates and sizes etc. with pixels... obviously Angles will need to be as they are now. It's all in the source code and is kind of self-explanatory I think.
So, when I used the 2D orthographic mode, I used:
Code:
glOrtho (0,pOGL.Width,0,pOGL.Height,-10,10);
glMatrixMode (GL_MODELVIEW);
...which makes sense to me... with 3D I'll admit that I've got a little confused. Should I be using something like glViewport or gluPerspective or a combination of both? How would I set up my OpenGL application so that I'm drawing directly to a TForm in 3D and I have the entire TForm area to draw to and I can address my coordinates in pixels?
...which I've seen in another example... is that on the right track? Also - is this approach inadvisable for some reason that I'm not aware of? Should I be sticking to my small GLFloats and finding another way to recognise where my mouse is on the screen without using Delphi's OnMouseOver event?
P.S. I understand that different coordinates systems are used by these technologies as well... with 1,1 being top-left in Delphi and 1,1 being bottom-left in OpenGL... I think!
Hmm... is my question best asked: "How can I have a 1:1 relationship between OpenGL coordinates and screen / canvas / form / handle pixels in 3D?" or even "Can I actually have a 1:1 relationship between pixels and OpenGL coordinates / units in 3D?".
I think I might have to go back to some OpenGL basics because I think I am lacking some understanding of the simple stuff. I know how to use pixels as my units when using orthographic projection - I was a little surprised to find that I could not do this so simply with perspective... I mean, surely if I'm defining a vertex at -0.2,0.3 I should be able to define a vertex at the relevant position on my screen if I understand the pixels, say 20,40 or something like that. That's what's getting me confused... and I think it's all to do with how I'm setting up OpenGL, using glFrustrum or gluPerspective.
I'll do a bit of research into gluUnProject as well... see if I can make sense of this. My code below is the simplest display a triangle on screen example I could come up with - in my perfect world, I wouldn't be constrained to passing in units to the DrawTriangleByUnits procedure, I could pass in pixels and still end up with my triangle being drawn, but with much more idea of where I wanted it to be, rather than using fractions of a screen.
Registriert: Sa Jan 01, 2005 17:11 Beiträge: 2067
Programmiersprache: C++
In theory you can calculate this by the values given to gluPerspective. Nevertheless I would say, that it will be better to draw a big rectangle at that deep you want you objects and use gluUnProject with the edges of your window to get these coordinates. Will be the easiest but not niced way.
I had a quick look at gluUnProject which seems to convert Windows screen coordinates to OpenGL coordinates - sounded like something I wanted (although I must say, it sounds like a lot of code for something I'd hoped would be part of the set-up process that I'd just missed). Anyway, I got hold of a function just to give it a go...
...and then in my TForm's OnMouseDown event I used the following code to see if I could convert an X and Y mouse positional value to something that appeared relevant in OpenGL coordinates...
...to be honest, it didn't make much sense to me. Even though I left my mouse in the same place and dismissed the message box with the Return key, the OpenGL coordinates reported by the function seemed to vary wildy. I got constant values for the X and Y mouse coordinates - as you would expect - but massive differences in the reported OpenGL coordinates - sometimes flipping between positive and negative without me even moving the mouse.
I guess what I'd originally hoped for... a way to just use pixel coordinates in OpenGL, without being in orthographic mode, isn't either possible - or there's certainly a good reason for why people don't use it... I just wonder what that reason is. As I had mentioned, at the beginning, working in pixels is just more natural for me... maybe I'll have to work in these units after all...
So, that said, is there a way to change the granularity of the OpenGL units... like, when you set up a program, the default values seem to be from -1 to 1... is there a way (or is there absolutely no reason to do this) to have say -10 to 10?
Registriert: Sa Jan 01, 2005 17:11 Beiträge: 2067
Programmiersprache: C++
dpm_dpmartin hat geschrieben:
I guess what I'd originally hoped for... a way to just use pixel coordinates in OpenGL, without being in orthographic mode, isn't either possible - or there's certainly a good reason for why people don't use it... I just wonder what that reason is. As I had mentioned, at the beginning, working in pixels is just more natural for me... maybe I'll have to work in these units after all...
If you change windowresolution or using fullscreen on different pc you have to take care of different windowwidths. When using OpenGL units you don't have to care at all.
dpm_dpmartin hat geschrieben:
So, that said, is there a way to change the granularity of the OpenGL units... like, when you set up a program, the default values seem to be from -1 to 1... is there a way (or is there absolutely no reason to do this) to have say -10 to 10?
You can use glScalef(x, y, z) to scale each axis.
Attached to this post is a sample where a cube is drawn at the position of the mouse. The origin of that code is from Sulaco. At the bottom of that page, the last tutorial from him
You made a very interesting comment, to me, about the fact that using the OpenGL unit (or fractions of the screen, i.e. 0.1) means that you don't have to really care about what resolution your user is running in - if they're running in 800x600 then OpenGL will scale everything correctly and if they're running in 1024x768, again - everything will be scaled according to the fractions of the screen.
I get this. However, it kind of goes exactly against what I was wanting to do really. In my program, I also encourage my users to run in whatever screen resolution they wish to - but running in a higher resolution means that they will get more Album covers on their screen... because they define the size of the Album Covers they wish to do - in pixels. So, as the screen real-estate grows. This part of my program is what I'm looking to covert across to OpenGL...
The following example picture show what I mean I hope, although they're all at 1024x768 in the program, the user can choose how big their Album Covers are... if they select a size, then change their resolution, the amount they see will differ according to how many HMusicCentre can fit nicely onto the screen.
HMusicCentre with a cover size (in pixels of 216):
HMusicCentre with a cover size (in pixels of 130):
HMusicCentre with a cover size (in pixels of 95):
I'm sure there's a clever way of getting around this. I don't want to use orthographic projection because I do want to do some things in 3D (my Album Covers) and make things rotate and translate (and potentially scale). I can't do that kind of stuff in 3D with glOrtho (0,pOGL.Width,0,pOGL.Height,-10,10); can I, even though there is a depth part to the call... as opposed to gluOrtho2D which doesn't have that at all?
At the moment, my plan is to start work in a non-orthographic projection mode... my next question then comes down to whether I can 'limit' the amount of perspective while still getting the 3D parts, for example - I would like my Album Covers arranged on the screen to look as 'flat' as possible... but I might want the current Album playing to have its texture (and other Albums by the same Artist) placed on the faces of a cube that comes closer to the user and rotates - I'd still need to see perspective on that.
Registriert: Sa Jan 01, 2005 17:11 Beiträge: 2067
Programmiersprache: C++
Ok, before reading you second topic I answer to this:
I have thought a bit and found a result:
If you're looking at a wall covered with song covers, only the covers in center of your view will be squares. Right or left of them, the covers are getting rectancles. This is because of the perspective view and here is your problem: You want to force 2D in 3D what is not thought for. But for a solution you can mix 2D and 3D. There is no problem with drawing in 2D, then switching to 3D and continue drawing. No problem at all and will solve your problem.
Or as an alternative you can draw a "real" room with covers on the wall where the user can look around and select covers.
Mitglieder in diesem Forum: 0 Mitglieder und 2 Gäste
Du darfst keine neuen Themen in diesem Forum erstellen. Du darfst keine Antworten zu Themen in diesem Forum erstellen. Du darfst deine Beiträge in diesem Forum nicht ändern. Du darfst deine Beiträge in diesem Forum nicht löschen. Du darfst keine Dateianhänge in diesem Forum erstellen.