• ### Announcements

#### Archived

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

# OpenGL Using OpenGL for 2D game.

## Recommended Posts

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!

##### Share on other sites
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

##### Share on other sites
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]

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
Why not making it isometric 3D instead?

Height Map Editor | Eternal Lands | Fast User Directory

##### Share on other sites
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]

##### Share on other sites
quote:
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]

##### Share on other sites
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]

##### Share on other sites
quote:
Original post by psyjax
quote:
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

##### Share on other sites
Wow! looks really nice. I agree it looks very good, but it''s not exactly what I wan''t for my project. I want a nice Iso view kinda like civilization III. With 3D effects on top.

Most of you seem to suggest mapping the tiles onto Quads then blitting them to screen. This would be easy for square tiles, but what about isometric raster tiles? Like the ones I show in my screenshot above.

This one is giving me a run for my money :D

##### Share on other sites
I highly suggest you to go in real 3D, but isometric, like I do. So map the tiles on quads.
The reason behind this is that it will be much easier for you to keep the consistency of the world, rather than render every thing via a different method.
Second, giving the user the ability to rotate the camera can''t hurt

Height Map Editor | Eternal Lands | Fast User Directory

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627682
• Total Posts
2978622
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 9
• 14
• 12
• 10
• 12