PDA

View Full Version : coordination relation between openGL and Windows(MFC)



arrowpig
11-24-2003, 04:35 AM
In a windows application based on MFC,
void CGLEnabledView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if ( 0 < cx && 0 < cy )
{
m_dAspectRatio=double(cx)/double(cy);
glViewport(0,0,cx,cy);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,m_dAspectRatio,0.1f, 10.0f);
glTranslatef(0.0f,0.0f,-4.f);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
.......
};
}
when i debug into the above ,i got cx=612,cy=281.while the actual draw code is:
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
i found on the screen,the length of the line drawed is several logical pixels instead of one logical pixel. Please tell me the relation between openGL and Windows.
Thanks.

dvm
11-24-2003, 04:45 AM
glViewport only maps your existing frustum (the one created with gluPerspective onto the window. The frustum has six planes, front, rear, bottom, top, left and right. Anything outside the frustum is culled (not drawed). You can define a frustum any size you want and glViewport maps the front plane of the frustum to the window. So if you created a frustum say with glFrustum(-50, 50, -50, 50, 10, 100); your front plane would have dimensions x1,y1 = -50, -50, x2,y2 = 50, 50 (lower left and upper right). Now you need to translate your objects 10 units to the back, because the frustum is 10 units from the center of the coordinates (zNear = 10). You can also use gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0); Now your ready to go. The upper right corner of your window is 50, 50 and the lower right is -50, -50. You can also search this forum for glFrustum and find some interesting information on how you can calculate the frustum from gluPerspective. Hope this helps!

arrowpig
11-24-2003, 06:39 AM
Originally posted by moucard:
glViewport only maps your existing frustum (the one created with gluPerspective onto the window. The frustum has six planes, front, rear, bottom, top, left and right. Anything outside the frustum is culled (not drawed). You can define a frustum any size you want and glViewport maps the front plane of the frustum to the window. So if you created a frustum say with glFrustum(-50, 50, -50, 50, 10, 100); your front plane would have dimensions x1,y1 = -50, -50, x2,y2 = 50, 50 (lower left and upper right). Now you need to translate your objects 10 units to the back, because the frustum is 10 units from the center of the coordinates (zNear = 10). You can also use gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0); Now your ready to go. The upper right corner of your window is 50, 50 and the lower right is -50, -50. You can also search this forum for glFrustum and find some interesting information on how you can calculate the frustum from gluPerspective. Hope this helps!

Thanks for your reply,but i made a calculation.probably i'm wrong again,gluPerspective(40.0,612.0/281.0,0.1f,10.0f);so the height of the front plane is what? 0.1*tan(20*3.14/180)?

nexusone
11-24-2003, 07:13 AM
The relationship between your 3D world and your screen pixels will vary based on you world setup.

Remember what you see on the screen is a bitmap rendering of your 3D world, your 3D world is created using vectors.

Viewport tell openGL what size of bitmap(screen window size) to render to.

Your ortho/prespective settings tells openGL how much of the 3D world to include in the rendering process.

How in perspective mode it is a bit tricky to get the exact pixel/vector relationship since it's relationship changes as the depth does. I have only done it with ortho mode, for mouse drawing.




[This message has been edited by nexusone (edited 11-24-2003).]

dvm
11-25-2003, 03:41 AM
You had me going through a small stack of paperwork on my desk(always a mess!) to findthe paper but here it is: (it's based very much on an older posting from someone else in this forum, the link is:
www.opengl.org/discussion_boards/ubb/Forum2/HTML/000559.html (http://www.opengl.org/discussion_boards/ubb/Forum2/HTML/000559.html) )



a = aspect ratio;
fov = field of view
left = -1 * zNear * tan((a * fov) / 2);
right = zFar * tan((a * fov) / 2) / (zFar / zNear);
bottom = -1 * zNear * tan(fov / 2)
top = far * tan(fov / 2) / (zFar / zNear);

Also the fov has to be in radians as I think you guessed it. The formula for that is fov(rad) = fov(degrees) * PI / 180. Also if someone can answer this; the right and top coordinates of glFrustum refer to the front or the back clipping plane? On some books I've seen they refer to the front and others on the back.

zeckensack
11-25-2003, 04:42 AM
Originally posted by arrowpig:


void CGLEnabledView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if ( 0 < cx &amp;&amp; 0 < cy )
{
m_dAspectRatio=double(cx)/double(cy);
glViewport(0,0,cx,cy);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,m_dAspectRatio,0.1f, 10.0f);
glTranslatef(0.0f,0.0f,-4.f);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();Not that it would matter much, but the two lines marked in bold are redundant. You can just remove them.


Originally posted by moucard:
Also if someone can answer this; the right and top coordinates of glFrustum refer to the front or the back clipping plane? On some books I've seen they refer to the front and others on the back.Front.