2D & 3D in OpenGL at the same time.

Started by
3 comments, last by ElPeque 21 years, 7 months ago
Being used to the old school linear frame buffer, like the one while using the mode 13h in DOS (heh), i sometimes find myself in trouble trying to understand the way OpenGL displays stuff and the matrices, etc. My problem is this, when i init the opengl window with something like this... glViewport(0, 0, Width, Height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, Width, 0, Height); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); then dwaing 2D stuff can be done like this. glColor3f(1.0f, 1.0f, 1.0f); glBegin(GL_TRIANGLES); glVertex2i(400, 300); glVertex2i(42, 342); glVertex2i(225, 75); glEnd(); It looks pretty much like in 13h.if you are at 800x600 then you know that 400x300 is the middle of the screen. And whein i init lthe window with something like... glViewport(rWindow.left, rWindow.top, rWindow.right, rWindow.bottom); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)rWindow.right/(GLfloat)rWindow.bottom,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); ShowCursor(FALSE); Then i can draw in 3D. And draw with something like... glLoadIdentity(); glTranslatef(0.0f + b, 0.0f + c, -20.0f + a); glColor3f(1.0f, 1.0f, 0.0f); glBegin(GL_TRIANGLES); glVertex3d(0.0f, 0.0f, 0.0f); glVertex3d(0.5f, -0.5f, 0.0f); glVertex3d(0.0f, -1.0f, 0.0f); glEnd(); But in this way there is no way to know where you are drawing it. What if what i want to draw is the mouse cursor? Im pretty confused about this. And if I try do do something like glColor3f(1.0f, 1.0f, 1.0f); glBegin(GL_TRIANGLES); glVertex2i(400, 300); glVertex2i(42, 342); glVertex2i(225, 75); glEnd(); with the second initialization, i get no drawing at all. I could use a little help here... Thanks a lot in advance, and sorry for the incompetence =O) __________________ Alejandro Martínez
__________________Alejandro Martínez
Advertisement
The thing to remember when in perspective mode is that the vertex coordinates are in world space (or local object space depending on how you manage the rendering). Because of this, thinking in terms of screen coordinates does you no good. The calls to glViewport & gluPerspective will determine how many points of world space will be visible on the screen at any given time.

The thing is, you do not have to limit yourself to one type of projection for the entire app. Typically, if you want to render, say, a 2D GUI on top of a 3D seen, you first set up the perpective view at the beginning of your render loop, render the 3D scene in world space, change projection to orthographic, then render the 2D components using screen coordinates.

Remember, when in perspective mode you tell OpenGL where in the 3D world to draw the verts. OpenGL will handle converting those coords to screen space.
Wow. Thanks a lot. It had been quite a while since i last had such a clear explanation.

Thanks again... =O)

__________________
Alejandro Martínez
__________________Alejandro Martínez
oooooh!!!!!
Please realize that the choice of the projMat has nothing to do with 2D or 3D. Vertices are transformed from the world into screen space with this matrix. The only, the one and only difference between orthogonal and perspective matrices, is that the orthogonal screen space is a cube, and the perspective screen space is a canonical fustrum! Get that?
I think with glVertex2* the z coordinate is set to a default value, depending on your depth clipping, primitives with glVertex2* can be clipped away. It is also important to specify an z coord for vertices with the orthogonal matrix.

Greetings....
There are 10 kinds of people,those who understand binaryand those who not.
But as long as you set the Ortho projection with min/max z values of -1.0/1.0 (which gluOrtho2D does for you) then you can think of things in terms of screen space and have no need to worry about z coords. (0,0) will be in the top/bottom left corner of the screen (depending upon you set it up) and the max x/y coords will be the width/height (if you set it up that way). So in that sense, it has everything to do with 2D/3D. It would be possible to have two different renderers, on using a 2D API like DDraw and the other using OGL with ortho projection set up appropriately, and have your game load one at run time. You could use the same code to draw to the screen with either renderer.

This topic is closed to new replies.

Advertisement