Sign in to follow this  
Tom Backton

OpenGL 2D Rendering with OpenGL 3.0/3.1/3.2

Recommended Posts

I have an image. A texture object. I need to draw it many (let's say 50) times per frame. There is an array containing position and rotation of each instance. The is also the origin data (the same for all images) as global variables (x and y). This is how one image is drawn: 1. translate by (posX, posY) 2. rotate by angle 3. draw the image with bottom-left corner at (-originX, -originY) The question is what is the best way to draw 50 images using shaders and buffer objects, and whether it's faster than immediate mode or display lists. The position and angle are changed by user input, AI and/or physics. The origin is constant. With VBOs, there are generally 2 methods: update the VBO and call glDrawArrays once or use a loop that calls glDrawArrays 50 times. In the seond method I can update 4 VBO vertices with buffer mapping or send attribs with glVertexAttrib or replace glDrawArrays with glBegin/glEnd. the origin is constant so I can send it once with glVertexAttrib. which is the best? And how should I write the VBO (interleaved vertices or separate VBO for dynamic data and static data)? OpenGL isn't suited for 2D, is it? I found many 2d rendering examples, but all of them use immediate mode. I want to use shaders and buffer objects (for effects) and I'm not sure how to "port" the examples to the new style of OpenGL 3.0 and above. [Edited by - Tom Backton on February 20, 2010 10:55:07 AM]

Share this post


Link to post
Share on other sites

"the best way to draw 50 images"

Do it in immediate mode and optimise later. 50 images won't stress the API even slightly. Make it work in the simple cases before optimising.


"I want to use shaders and buffer objects (for effects)"

Which work fine in immediate mode. Do this stuff *simply* to start with. The hard part about programming is not making things go fast enough. If it's not fast enough now, wait six months. That's easy. Not tying yourself in knots prematurely optimising is the hard part. Do the simplest thing that will work.


"OpenGL isn't suited for 2D, is it?"

OpenGL works just fine doing 2D. Why? Because 2D renderings are a subset of doing stuff in 3D. How on *EARTH* would it work fine for 3D but have a problem with 2D rendering?

Where does this stuff come from??? I keep hearing this junk: "Oh, opengl can't do transparency", "opengl isn't cross platform", "opengl isn't supported on mac", "opengl's lighting is too slow for real-time rendering". Who is actually starting all these rumours? Are there really that many DirectX fanboys out there whose only entertainment comes from starting weirdass rumours about opengl? FFS!!

Share this post


Link to post
Share on other sites
Quote:
Original post by Katie
Where does this stuff come from??? I keep hearing this junk: "Oh, opengl can't do transparency", "opengl isn't cross platform", "opengl isn't supported on mac", "opengl's lighting is too slow for real-time rendering". Who is actually starting all these rumours? Are there really that many DirectX fanboys out there whose only entertainment comes from starting weirdass rumours about opengl? FFS!!


Well, for one, I heard that OpenGL is a proprietary closed graphics standard. I don't really like OpenGL because of that. What's even worse is that they apparently don't like space characters, otherwise it would have been called Open GL. What's up with that? I'm pretty sure they've given the space character a bad reputation for no reason. Stupid OpenGL, good space character.

Kidding. :)

Tom, have you tried to use some basic 3D samples that use shaders? Have you tried using the orthographic projection camera? Combining the two techniques won't be difficult once you get the hang of them. Maybe try NeHe's samples. The "kidding" example that I linked to above uses a shader to render a quad in 2D. Unfortunately it uses a framebuffer (rather than drawing to the screen), so it might be too complex to be useful. However, it does give the basic idea.

Share this post


Link to post
Share on other sites
Thanks for the advice!

I'm aware of the first rule of optimization (don't do it...). When I wrote the base code, I used OpenGL 1.1 so the only options were immediate mode, display lists and RAM vertex arrays. But later I moved to OpenGL 2.1, which added shaders and buffer objects. But there were still DLs, immediate mode and VA. The problem appeared when I tried to "conform" to the new model - port the code to OpenGL 3.2, core profile. So the question is...what is the best way to do 2D rendering in OpenGL 3.2? (maybe 3.2 isn't a good choice for 2D, but I'd still like to know, as 3.2 has some useful features) And is there a performance loss when using very small batched with VBO drawing commands (e.g. four vertices with glDrawArrays) ?

Share this post


Link to post
Share on other sites
I know how to draw triangles. That's not the problem. The question is whether using glDrawArrays for 4 vertices (2 triangles) is the best approach in OpenGL 3.2. (you can read my previous posts...)

Share this post


Link to post
Share on other sites
It will be about as fast as the equivalent immediate mode command, in most cases should be quicker since you are passing only one command to the driver instead of 4 or more, i know you are trying to avoid some optimization that may lock you in to a form early but what i do is

Allocate/Init VBO enough for n-number of objects/tiles/sprites
create Object with beginning-index and ending-index of created vertexes
update vertex data as needed
Draw Objects using referenced vertexes index
Clean up at the end

Share this post


Link to post
Share on other sites
I see... but there's still a problem. Performance of some operations depends on the hardware. One of the things I'd like to know is how non-interleaved vertices, VBO-per-vertex-attribute and non-sequential writing to the VBO during mapping affect performance. Also, my graphics card supports OpenGL 2.1' so I can't try all the drawing and vertex specification features of OpenGL 3.2 (many extensions are supported by the card, but many aren't). Also, other cards behave differently. I have an NVIDIA card; On ATI's cards the effect on performance is probably not the same...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this