PDA

View Full Version : 2d work and 3d



Bruno
04-30-2000, 03:06 PM
Hi
could someone explain to me how does the opengl interacts with 2d and 3d?
For example if i wan't to make a bar in 2d on the top of the screen, and then a cube rotating, what do i have to do?
I know it has something to do with the glortho and glmatrixmode, but i really don't understand....
thanks
Bruno

TheGecko
05-02-2000, 09:09 PM
Heh...good luck getting 2D to work in OpenGL without your framerate dropping to say 1 frame per 10 seconds http://www.opengl.org/discussion_boards/ubb/tongue.gif

john
05-02-2000, 09:38 PM
what? 2d is just 3d but without the depth. and, no, that's not a silly thing to say.

(opengl 2D is drawing on a PLANE with z=0 in three dimensions. same thing....)

cheers
John

MikeC
05-03-2000, 12:51 AM
John's right, 2D need not be a performance issue in OpenGL if you do it right. The only real problem is if you're coming from DOS and are used to having direct access to the framebuffer. In which case, you need to get over it, because those days are gone.

Humus
05-03-2000, 06:08 AM
Use this 2 functions:

void Enter2dMode(){
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0,1,1,0,1,-1);
}

void Exit2dMode(){
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}

After calling Enter2dMode() you have (0,0) at upper left corner of screen, and (1,1) at lower right corner. Now you can use glVertex2f() to specify 2d coords. After you've done your 2d work you can return to previous state with Exit2dMode().

Bruno
05-03-2000, 12:05 PM
thanks a lot

Bruno

john
05-03-2000, 03:36 PM
Humus is proposing a unit square reference frame for 2D work in OpenGL. This does have it's merits, because it's sometimes more logical/easier to think of placement as a proportion of the window.

An alternative is to map the projection matrix to the pixels, so you'd have left=top=0 and right=width and bottom=height of the screen. That way you can say glVertex(5,5) maps to the pixel 5,5 of the screen.

Both techniques, i would argue, have their place.