Using OpenGL for 2D game.

Started by
10 comments, last by psyjax 20 years, 6 months ago
Hey folks, I''m an SDL user and I also posted this question to the mailing list, but I figured this would be a good place to ask as well. In any case: I wanted to know what would be the best approach to my current game project which I am considering using OpenGL for.My game is an RPG which shifts between two modes, an Isometric perspective like Diablo, and a nice 3D dungeon crawl mode (using GL), much like the old Eye of the Beholder games. I''m considering doing both the 2D (isometric) portion and the 3D portion using OpenGL. But I am wondering if I will see good speed results this way? Will it be faster to do the 2d Part using regular blitting through SDL(a great 2D graphics library for those who don''t know), or OpenGL? One of the main reasons I want to use OpenGL for this portion, is for the simplicity in implementing lighting, scaling, rotation, etc. Also, I want to do neat 3d spell effects, the whole gamut of nice 3d eye candy on a 2d game board. What would be the best method for going about this in OpenGL? Would the best method be to render the 2D tile map onto an OpenGL surface every frame, as you would in regular 2D? Or, am I better off just sticking to 2D methods all together? I have never attempted something like this before, so excuse my ignorance. Any suggestions, or pros and cons you could give me would be gerat, as I am still in the initial design stages. I don''t wanna commit to something less I know it will work Thanks alot!
Advertisement
GL for 2d stuff works great, or at least for the stuff I''ve tried.

What you might want to play with is Orth projection and set the camera on the z axis (with y as up). this way you can work using just x and y coords like a 2d screen.

Is this clear, or should I try this again?

Armand
Like the AP said, you can render in 2D if you change the current projection matrix to the orthographic projection, instead of the perspective projection. Here are two useful functions for switching between 2D and 3D rendering:

// Enables ortho mode for 2D renderingvoid Enable2D(){    int viewPort[4];    glGetIntegerv(GL_VIEWPORT, viewPort);    glMatrixMode(GL_PROJECTION);    glPushMatrix();    glLoadIdentity();    glOrtho(0, viewPort[2], 0, viewPort[3], -1, 1);    glMatrixMode(GL_MODELVIEW);    glPushMatrix();    glLoadIdentity();}//////////////////////////////////////////////////////// Disables ortho mode and returns back to // perspective projection mode for 3D renderingvoid Disable2D(){    glMatrixMode(GL_PROJECTION);    glPopMatrix();    glMatrixMode(GL_MODELVIEW);    glPopMatrix();}


So whenever you want to render in 2D simply call 'Enable2D()', and then when you're finished your 2D rendering, return back to 3D rendering with 'Disable2D()'. Note that when you're in 2D mode, you supply 2D coordinates such as 'glVertex2f(x, y)' instead of 'glVertex3f(x, y, z)'.

[edited by - chacha on October 9, 2003 12:31:08 AM]
just a minor comment on this method. If u plan to do 2d in opengl, itrs tru ortho is the way to go. But if u wanna harness some of the additional power of opengl for effects which MAY not be limited to a 2D plane (say a 3d-explosion seen from birds-eye-view) then u may need to tweak the ortho settings.

chacha''s code example specifies the ortho''s Z-near and Z-far parameters as -1, 1 which is also wat happend if u use gluOrtho2D(...). But u may not want to limit your z-range between those values. U can still have depth for purposes of any special features by increasing the range for Z. Dun worry though, ocz the ortho projection will maintain the top-down-like view irrespective of the z-range.

Also, you CAN still use glVertex3f if u want, but its pointless unless u really need the z-coords. And if u need to use it anyway, u''ll probably need to change the z-range for projection anyway
- To learn, we share... Give some to take some -
If you want to use glOrtho() go ahead, and use glVertex3f() if you want. The Z axis is still their and you can move objects on Z if you want. Problem is objects will not be perspective corrected, again objects appear the same regardless of distance on Z. glOrtho is the way to go with 2D games because the screen coordinates map 1to1 with the screen resolution. If you do use glOrtho() and use glVertex3f() and use the Z axis and plan on moving objects on Z make sure you expand near and far planes to somethign like 2000 -2000 so you can see your objects or else they will be cut in half or not show up at all.
Why not making it isometric 3D instead?

Height Map Editor | Eternal Lands | Fast User Directory
quote:Original post by CraZeE
chacha's code example specifies the ortho's Z-near and Z-far parameters as -1, 1 which is also wat happend if u use gluOrtho2D(...). But u may not want to limit your z-range between those values. U can still have depth for purposes of any special features by increasing the range for Z. Dun worry though, ocz the ortho projection will maintain the top-down-like view irrespective of the z-range.

Also, you CAN still use glVertex3f if u want, but its pointless unless u really need the z-coords. And if u need to use it anyway, u'll probably need to change the z-range for projection anyway

Well said, I forgot to mention the alternatives.

[edited by - chacha on October 10, 2003 3:18:52 AM]
quote:Original post by Raduprv
Why not making it isometric 3D instead?


I did actually

I already built the engine using full 3D polygons. But I decidede that tiles are alot prettier.

Take a look at the tiles I wan't to use:

http://www.starmars.com/tiles.png

and here is what the engine curently looks like (full 3d):

http://www.starmars.com/worldScreen.png

Granted, with some better models etc. Im sure the 3d one would look nice and spiffy But that's not the look I wan't! I wan't nice pre-rendered sprites and tiles in this game view.

Thank you all for your suggestions by the way. I am deffinetly going to use GL, and GLOrtho.

One more question, when rendering the tiles, how should it be done? Should I render them to individual Quads and then let OpenGL handle them as polygons, or is there a betterm faster method?

Remember, I wan't to take advantage of lighting, rotation, etc.

Thanks alot!

EDIT: What's the code for hyperlinks on this board? :D

[edited by - psyjax on October 10, 2003 11:05:47 AM]
You'll be feeding GL your sprites as textured quads, yes. Of course, you still have access to all the other stuff, so you're welcome to use triangles etc. Quads just generally make the most sense. The only project that springs to mind that uses 2D OpenGL extensively is Chromium, makbe you'd like to have a glance at the way the author of Chromium handled GL.

As for links, see the faq.


[edited by - jcspray on October 10, 2003 5:59:33 PM]
quote:Original post by psyjax
quote:Original post by Raduprv
Why not making it isometric 3D instead?


I did actually


Good
Look at some screenshots here (from my game), just to give you some insight on what you can do with the OpenGL ortho mode:

http://www.eternal-lands.com/index.php?main=screenshots.php

Height Map Editor | Eternal Lands | Fast User Directory

This topic is closed to new replies.

Advertisement