DGL https://delphigl.com/forum/ |
|
What's the easiest way for me to work in pixels with OpenGL? https://delphigl.com/forum/viewtopic.php?f=19&t=6343 |
Seite 1 von 1 |
Autor: | dpm_dpmartin [ Sa Feb 03, 2007 17:21 ] |
Betreff des Beitrags: | What's the easiest way for me to work in pixels with OpenGL? |
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... Code:
Code:
So, when I used the 2D orthographic mode, I used: Code:
Something like: Code:
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! |
Autor: | dpm_dpmartin [ Sa Feb 03, 2007 18:54 ] |
Betreff des Beitrags: | Rephrase... |
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?". |
Autor: | i0n0s [ Sa Feb 03, 2007 20:18 ] |
Betreff des Beitrags: | |
Look for gluUnProject. It only works if you're pointing on an object. If you're pointing in the deep space the result will be crap. |
Autor: | dpm_dpmartin [ Sa Feb 03, 2007 20:50 ] |
Betreff des Beitrags: | Yup, I'm stuck... |
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. Code:
|
Autor: | i0n0s [ So Feb 04, 2007 01:35 ] |
Betreff des Beitrags: | |
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. |
Autor: | dpm_dpmartin [ So Feb 04, 2007 01:53 ] |
Betreff des Beitrags: | gluUnProject... |
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... Code:
Code:
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? |
Autor: | i0n0s [ So Feb 04, 2007 02:13 ] | ||
Betreff des Beitrags: | Re: gluUnProject... | ||
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 ![]()
|
Autor: | dpm_dpmartin [ So Feb 04, 2007 11:34 ] |
Betreff des Beitrags: | Thanks! |
Well, you're right - that definitely works. 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. |
Autor: | i0n0s [ So Feb 04, 2007 19:07 ] |
Betreff des Beitrags: | |
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. |
Seite 1 von 1 | Alle Zeiten sind UTC + 1 Stunde |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |