Perspective Projection Opengl
-
hi all Currently i am doing an 3d Graphics application , which allows to calculate the center of gravity of an object. The user can control the foreces upon the object. (stuffs like that).. Here is my problem ------------------- My application using perspective projection. So While creating an object (for Ex: consider a rectangle) using mouse , the objects is not correctly positoned with mouse cursor. ie when the user click and drag in the window , the object size is different than what he/she just dragged. I am using 45 degree the angle of view and depth is from 1 to 500 Any !deas :) ? please give it to me warm regards krishnan
If u can Dream... U can do it
-
hi all Currently i am doing an 3d Graphics application , which allows to calculate the center of gravity of an object. The user can control the foreces upon the object. (stuffs like that).. Here is my problem ------------------- My application using perspective projection. So While creating an object (for Ex: consider a rectangle) using mouse , the objects is not correctly positoned with mouse cursor. ie when the user click and drag in the window , the object size is different than what he/she just dragged. I am using 45 degree the angle of view and depth is from 1 to 500 Any !deas :) ? please give it to me warm regards krishnan
If u can Dream... U can do it
Not 100% sure, but I think you can use gluProject/gluUnProject:
double modelMatrix[16]; double projMatrix[16]; int viewPort[4]; glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix); glGetDoublev(GL_PROJECTION_MATRIX, projMatrix); glGetIntegerv(GL_VIEWPORT, viewPort); double x, y, z; gluUnProject(viewPort[0] + mousex, viewPort[1] + mousey, 0.0f, modelMatrix, projMatrix, viewPort, &x, &y, &z);
Might have to play around with the Y coordinate, it may be inverted.
- S 50 cups of coffee and you know it's on!
-
Not 100% sure, but I think you can use gluProject/gluUnProject:
double modelMatrix[16]; double projMatrix[16]; int viewPort[4]; glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix); glGetDoublev(GL_PROJECTION_MATRIX, projMatrix); glGetIntegerv(GL_VIEWPORT, viewPort); double x, y, z; gluUnProject(viewPort[0] + mousex, viewPort[1] + mousey, 0.0f, modelMatrix, projMatrix, viewPort, &x, &y, &z);
Might have to play around with the Y coordinate, it may be inverted.
- S 50 cups of coffee and you know it's on!
Thanks for the reply & code One more thing to clarify ( currently i am thinking about it). Pls go through this. i think you know the function gluPersPective(...,near,Far); For a perspective projection the coordiantes are normalised. ie (-1,0) , (1,0)is equal to left to right through the Orgin .. is it ? I want to know the units of this near and Far parameters :confused: ? is that also normalised coordinates ? Thanks in advance krishnan
If u can Dream... U can do it
-
Thanks for the reply & code One more thing to clarify ( currently i am thinking about it). Pls go through this. i think you know the function gluPersPective(...,near,Far); For a perspective projection the coordiantes are normalised. ie (-1,0) , (1,0)is equal to left to right through the Orgin .. is it ? I want to know the units of this near and Far parameters :confused: ? is that also normalised coordinates ? Thanks in advance krishnan
If u can Dream... U can do it
krishnadevank wrote:
For a perspective projection the coordiantes are normalised. ie (-1,0) , (1,0)is equal to left to right through the Orgin .. is it ?
Yes, if your glScale is setup normally. But because of perspective, (-1, 0, 0)- (1, 0, 0) will not visually equal (0, 0, 1)-(0, 0, -1).
krishnadevank wrote:
I want to know the units of this near and Far parameters ? is that also normalised coordinates ?
OpenGL is inherently unit-less - you define what 1 means (i.e. it could be an inch, foot, meter, or an A.U.) The near and far planes, have to do with the z-buffer, which is non-linear. Objects up close have more precision. Google on gluPerspective, z-fighting and the depth buffer for more info. Here's some info (not a very pretty site): http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html[^] (My old computer died, otherwise I'd provide you with a better link :)).
- S 50 cups of coffee and you know it's on!
-
krishnadevank wrote:
For a perspective projection the coordiantes are normalised. ie (-1,0) , (1,0)is equal to left to right through the Orgin .. is it ?
Yes, if your glScale is setup normally. But because of perspective, (-1, 0, 0)- (1, 0, 0) will not visually equal (0, 0, 1)-(0, 0, -1).
krishnadevank wrote:
I want to know the units of this near and Far parameters ? is that also normalised coordinates ?
OpenGL is inherently unit-less - you define what 1 means (i.e. it could be an inch, foot, meter, or an A.U.) The near and far planes, have to do with the z-buffer, which is non-linear. Objects up close have more precision. Google on gluPerspective, z-fighting and the depth buffer for more info. Here's some info (not a very pretty site): http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html[^] (My old computer died, otherwise I'd provide you with a better link :)).
- S 50 cups of coffee and you know it's on!
Once again Thanks :rose: Suppose i would setup the my projection with 45 as fov and near = 1 and far = 500. According to my knowledge the default eye location is at Orgin and looking towards in to the screen. so if i drawn a polygon/rectangle like this (Covering the entire screen. ) , it maynot be visible. glBegin(GL_POLYGON); glVertex3f(-1,1,0); glVertex3f(1,1,0); glVertex3f(1,-1,0); glVertex3f(-1,-1,0); glEnds(); For making this visible(Convering the entire screen ) how much exact translation i want to do in Z direction . ? Here i am confused with the parameters of gluPersPective function. What you thinks ?
If u can Dream... U can do it
-
Once again Thanks :rose: Suppose i would setup the my projection with 45 as fov and near = 1 and far = 500. According to my knowledge the default eye location is at Orgin and looking towards in to the screen. so if i drawn a polygon/rectangle like this (Covering the entire screen. ) , it maynot be visible. glBegin(GL_POLYGON); glVertex3f(-1,1,0); glVertex3f(1,1,0); glVertex3f(1,-1,0); glVertex3f(-1,-1,0); glEnds(); For making this visible(Convering the entire screen ) how much exact translation i want to do in Z direction . ? Here i am confused with the parameters of gluPersPective function. What you thinks ?
If u can Dream... U can do it
Not sure you can ever get an exact z position, but you could try backing up on the z-axis by a small increment just outside of the near plane, like glTranslatef(0, 0, -1.001). You might have better results switching into ortho mode, using glOrtho.
- S 50 cups of coffee and you know it's on!
-
Not sure you can ever get an exact z position, but you could try backing up on the z-axis by a small increment just outside of the near plane, like glTranslatef(0, 0, -1.001). You might have better results switching into ortho mode, using glOrtho.
- S 50 cups of coffee and you know it's on!
-
Not sure you can ever get an exact z position, but you could try backing up on the z-axis by a small increment just outside of the near plane, like glTranslatef(0, 0, -1.001). You might have better results switching into ortho mode, using glOrtho.
- S 50 cups of coffee and you know it's on!
Hi steve I tried with a value -1.0 for tranlation. I think there still exists some problems. I put a point on the screen using GL_POINT. When the point location was at (0,0) every thing loooks fine , ie the point was on the orgin(0,0,0). then i tried with value (1,0,0) , and translation in z direction glTranslate(0,0,-1). But the result is intersting. it vanished from the screen. ( :(( ) . Then i checked it by resizing the window , then it became visible at some time. There must be problem with aspect ratio i think.(the asepect ration i put was width/height). Any thoughts ? Thanks krishnan
If u can Dream... U can do it