Jump to content
  • Advertisement
Sign in to follow this  
peanutman

OpenGL 2d with OpenGL

This topic is 4054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am pretty new to OpenGL, but I do have a history with DirectX. I am a bit lost, and do not know what i am looking for. I want to make a game, tile based, in 2d BUT my game would require a LOT of live rotating of images (turrets pointing at a target, etc etc), 360 degree movement for the character, ... I read that live rotating is a big hit on performance, and the alternative, caching the prerotated images, would just take too much memory for the things I want to do. So I was wondering, is it possible to use 3d rendering for a 2d game... eg, what if I could blit the character on a square polygon, and rotate the polygon as needed? It would be cool, because i would be able to do stuff with the lighting I think? I have no idea if this is possible, or how to do it. So does anyone have any advice? or maybe point me to the good direction, maybe even a tutorial/example? Thanks for your time, Reinout

Share this post


Link to post
Share on other sites
Advertisement
If you're talking about manually compute the rotation and perform it on your objects then yes it will cost some performance, but you have to do a lot. With Opengl, when you want to do something like that, you can send the transformation matrix and vertices then the hardware will do the transformation. For instance you have a turrent you want to turn 60 degree clockwise or 10 turrets, then you send the transformation for rotating 60 degree clockwise and the vertices for the turrets. It's pretty much similar to Directx. I wouldn't worry about it, unless you're planning on manually transforming thousands of vertices.

Share this post


Link to post
Share on other sites
Not only is it possible to create a 2D game using a 3D API like OpenGL or Direct3D; these days its a good idea.

Most modern 3D graphics cards have pretty poor support for 2D APIs like DirectDraw, and will often fall back to executing the rendering code on the CPU for non-trivial effects like scaling or rotation. I've also witnessed strange driver-related problems with some cards that map 2D operations onto the 3D hardware and other wierdness.

On top of poor support, 2D APIs are quite restrictive compared to their 3D counterparts. Arbitrary rotation (around all 3 axis), Depth/stencil buffers, and shaders are among the chief benefits of using a 3D API, and enable visual effects and a certain elegance of code that no 2D API simultaneously, or even seperately IMHO, matches.

There is a different mindset to developing your 2D game for the 3D pipeline. The most prominant shift in thinking is switching from the top-to-bottom-left-to-right drawing mentality towards the batched drawing mentality. On 3D hardware, its better to batch tiles by their material (A collection of common visual attributes, such as bound textures, shaders, etc) than to draw each tile in sequential order, changing the material with (more or less) each step.

There's a book called Focus on 2D in Direct3D which covers the topic. IIRC, it doesn't go into more advanced theory like optimal batching, so if you're already familiar with DirectX, it may not contain much benefit for you. The gist of using a 3D API to write a 2D game is simply: Write a 3D engine, set orthographic projection, thats it. The fact that the engine is 2D will allow you to make certain assumptions (and optimizations, accordingly) -- for example, you'll always know which direction is the up-vector, and culling the scene becomes trivial.


Good luck!

Share this post


Link to post
Share on other sites
A short definition of OpenGL is a 2D and 3D graphics API.
OpenGL provides 2D specific functions for you, for example, glDrawPixels() to draw 2D image on the screen, and glVertex2f() to draw a vertex in 2D space. So, you can do whatever you want using OpenGL.

Share this post


Link to post
Share on other sites
You received a couple of good replies already, but a few more cents from me:

* set up an orthogonal view (this means you have no perspective; your frustum is a box)
* render (textured) quads and triangles, the depth becomes the priority in ortho
* you can simply use matrices for all your 2D objects, allowing you to translate, skew, rotate and scale them

Some neat tricks you can utilize all of a sudden (which are often harder to do using old style bitblit 2D):

* fragment- and vertex shaders allow for real nice 2D effects
* full screen filters, like bloom, motion blur
* linear interpolation of vertex colors, so you can easily add gradients to your sprites
* rotation around different axii (not just the axis that points into your screen)

It is important not to switch material states very often to allow batching. So when you have a tiled background, make sure you render a huge triangle strip that fetches the tiles from a single texture by setting proper UV texture coordinates for the vertices.

Bottom line: 3D acceleration will certainly accelerate your 2D and allow new effects that were never possible before. It will require you to approach things a bit differently though.

Share this post


Link to post
Share on other sites
cool, thanks for all the great replies
I will try to do it as suggested above (textured quads).

Still have a lot to learn before i can do that though.
If anyone can point out some usefull functions to look up, feel free ;)

thanks for the replies

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!