How to do certain things with OpenGL in 2D?

Started by
2 comments, last by TheChubu 9 years, 11 months ago

I have researched the internet on this topic. Several just conluded with using glOrtho with 2 dimensions. Another talked about vertexes. That's about all I found as I researched.

I understand there's easier libraries, and I used SFML that does all the OpenGL rendering all for you, but I see myself using OpenGL:ES down the road, and so I think it might be educational using OpenGL for 2D. That way it's not a big jump going to OpenGL:ES, being depenent on abstractions.

I'm more curious on the following:

1) How sprites would be loaded using "surfaces" (arrays). Would this be done in software, or is there an extension for this?

2) How to copy and draw only a portion of a sprite sheet.

3) Avoiding sprite masks. I remember I loaded a 2d sprite and I had to make a mask for it. That felt redundent. Maybe there's a way in softare to avoid making masks?

4) How to draw sprites with (0,0) being at the top-left corner of the screen, though I'm assuming glOrtho does this for you. It's been too long to remember.

Basically, I want to write my own 2D library so jumping to OpenGL:ES will be easier in the long run.

Advertisement

1) "surfaces" is probably a term you picked up from SDL. The GL equivalent is a texture... You'll simply probably need to use some existing image loading library such as SOIL, DevIL, etc.

2) glCopyTexSubImage2D, although you might probably actually just want to be rendering only a portion of a larger texture without wasting a copy operation... In which case you simply have to specify the correct texture coordinates

3) N/A to GL

4) If you're wanting to use GL, then it's probably logical to stick to it's natural coordinate state.. I *think* SDL2 probably uses OpenGL as a backend for it's 2D rendering, so maybe it even uses ES on mobile... But, not sure. Otherwise, I'm guessing it's certain parameters to glOrtho2D.. Also, if you simply change the origin, the direction will still be "not what you expect". (Down is negative), so you'll have to change the origin AND direction.

Your questions are unclear. You talk about high level concepts such as "Sprites", "Sprite Sheets", "Sprite Masks" etc. None of these concepts are present in OpenGL. OpenGL deals with graphic primitives, such as points, lines, triangles, quads. It deals with textures.

Here is my attempt to answer your questions, though I am by no means a graphics programmer:
1) It isn't clear what you mean by "loaded". If you mean from disk, OpenGL doesn't help you here - but there are other libraries you can use.
2) If you create a texture from a sprite sheet, you can specify texture co-ordinates when drawing
3) You can use an image format with an alpha channel. Otherwise, nominate a particular colour as "transparent" and when the image is loaded from disk, create a copy with an alpha channel populated based on whether the pixel matches this "transparent" colour, before uploading as a texture. You might be able to achieve something similar with a shader.
4) glOrtho2D can help, but ultimately it is only a bit of math to transform "world" co-ordinates to "screen" co-ordinates, and you might want to keep them separate anyway. For example, in my (small) games I often go for a -1 to 1 screen co-ordinate system, with the origin in the centre.

1) Make a 3D renderer.

2) Drop the Z coordinate.

3) ???

4) Profit.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

This topic is closed to new replies.

Advertisement