Archived

This topic is now archived and is closed to further replies.

naboo

2D engine using OpenGL

Recommended Posts

What about a tutorial explaining how to do a 2D engine (draw_sprite, blit_surface, ...) using 3D acceleration with OpenGL, how to use pixel coords with OpenGL and thing like this? Edited by - naboo on April 21, 2001 12:51:17 PM

Share this post


Link to post
Share on other sites
Hmm, that''s actually a good idea for a tutorial to write, maybe I could do that...

A real quick explanation (for you, incase you don''t already know how to do it) would go like this: Turn off your depth buffer, set up an orthographic projection matrix, use glVertex2i instead of 3f, and sort from back to front before display. Pretty easy, huh?

If you want to see how I do it, feel free to download my engine''s source code. It is no master peice, but it should work ok (I get over 100 fps all the time, with a TNT2). My code is still more or less a framework still, I still have a lot to add.

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/

Share this post


Link to post
Share on other sites
I use glVertex2d..., well for making GL_TRIANGLE_STRIPs. What''s the difference with glVertex2i? I''m assuming, if this is it, glVertex2f is for floats and glVertex2i is for integers, but glVertex2d... doubles right?!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
yeah, but what about writing a pixel to 100,200 for example.. u cant do that with opengl, you have to calculate this, which is a lot of overhead

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster

yeah, but what about writing a pixel to 100,200 for example.. u cant do that with opengl, you have to calculate this, which is a lot of overhead


Sure you can. That''s why you use glOrtho and glViewport.

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/

Share this post


Link to post
Share on other sites
quote:
Original post by Null and Void

A real quick explanation (for you, incase you don''t already know how to do it) would go like this: Turn off your depth buffer, set up an orthographic projection matrix, use glVertex2i instead of 3f, and sort from back to front before display. Pretty easy, huh?




Lots of thx, Null and Void .

Share this post


Link to post
Share on other sites
>>yeah, but what about writing a pixel to 100,200 for example.. u cant do that with opengl, you have to calculate this, which is a lot of overhead <<

very easy check the faq http://www.frii.com/~martz/oglfaq section 9

9.030 How do I draw 2D controls over
my 3D rendering?
The basic strategy is to set up a 2D
projection for drawing controls. You can do this either on
top of your 3D rendering or in overlay planes. If you do so
on top of a 3D rendering, you''ll need to redraw the controls
at the end of every frame (immediately before swapping
buffers). If you draw into the overlay planes, you only need
to redraw the controls if you''re updating them.
To set up a 2D projection, you need to change
the Projection matrix. Normally, it''s convenient to set up
the projection so one world coordinate unit is equal to one
screen pixel, as follows:
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0, windowWidth, 0, windowHeight);
gluOrtho2D() sets up a Z range of -1 to 1, so
you need to use one of the glVertex2*() functions to ensure
your geometry isn''t clipped by the zNear or zFar
clipping planes.
Normally, the ModelView matrix is set to the
identity when drawing 2D controls, though you may find it
convenient to do otherwise (for example, you can draw
repeated controls with interleaved translation matrices).
If exact pixelization is required, you might
want to put a small translation in the ModelView matrix, as
shown below:
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.375, 0.375, 0.);
If you''re drawing on top of a 3D-depth
buffered image, you''ll need to somehow disable depth testing
while drawing your 2D geometry. You can do this by calling glDisable(GL_DEPTH_TEST) or glDepthFunc (GL_ALWAYS).
Depending on your application, you might also simply clear
the depth buffer before starting the 2D rendering. Finally,
drawing all 2D geometry with a minimum Z coordinate is also a
solution.
After the 2D projection is established as
above, you can render normal OpenGL primitiv

http://members.xoom.com/myBollux

Share this post


Link to post
Share on other sites