12
\$\begingroup\$

For self education, I'm writing a 2D platformer engine in C++ using SDL / OpenGL. I initially began with pure SDL using the tutorials on sdltutorials.com and lazyfoo.net, but I'm now using SDL to create an OpenGL rendering context and then render with OpenGL functions (specifically immediate mode but I'm learning about VAOs/VBOs). I am using SDL only for interface, audio, etc.

SDL uses a coordinate system with the origin in the upper left of the screen and the positive y-axis pointing down. It's easy to set up my orthographic projection in OpenGL to mirror this.

I know that texture coordinates are a right-hand system with values from 0 to 1 -- flipping the texture vertically before rendering (well, flip the file before loading) yields textures that render correctly... which is fine if I'm drawing the entire texture, but ultimately I'll be using tilesets and can imagine problems.

What should I be concerned about in terms of rendering when I do this?

If anybody has any advice or they've done this themselves and can point out future pitfalls, that would be great, but really any thoughts would be appreciated.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Are you using SDL to render (as in SDL's blitting functions), or are you using SDL to create an OpenGL rendering context, then using OpenGL functions to render? \$\endgroup\$ Commented Sep 14, 2012 at 21:50
  • 1
    \$\begingroup\$ I'm using SDL to create an OpenGL rendering context and using OpenGL functions to render. I'll clarify this in the question. \$\endgroup\$
    – derivative
    Commented Sep 14, 2012 at 22:16
  • 1
    \$\begingroup\$ Maybe if you're going to use an physics system, better see which coordinate system it uses (righ handed or left handed, Y-UP or Y-DOWN) and keep using the same. Of course you can do things like inverting Gravity, but I would try to use the same to make thngs easier. \$\endgroup\$ Commented Sep 15, 2012 at 12:13

2 Answers 2

8
\$\begingroup\$

"What should I be concerned about in terms of rendering when I do this?" - not much. A co-ordinate system is just a convention, not a hard-and-fast rule, and this is a perfectly legal and valid thing to do. Just make sure that you are consistent in your own usage and everything will work fine.

\$\endgroup\$
2
  • \$\begingroup\$ Great! I had a hunch self consistency was the only thing I needed to worry about, but this is new territory for me. \$\endgroup\$
    – derivative
    Commented Sep 15, 2012 at 20:54
  • 1
    \$\begingroup\$ I should add that dear old Quake did this back in 1996 and it worked just fine. \$\endgroup\$ Commented Sep 16, 2012 at 2:48
5
\$\begingroup\$

When working in 2D with OpenGL, I often use the top-left corner as the origin. This can be done with:

glOrtho(0,      // left
        width,  // right
        height, // bottom
        0,      // top
        0,      // zNear
        1       // zFar
        );

However, there are some pitfalls when changing the origin to the upper-left.

One pitfall I've run into more than once: glScissor(), which is used for clipping, always uses lower-left coordinates.

Its signature is:

void glScissor( GLint x, GLint y, GLsizei width, GLsizei height )

where x, y specify the lower left corner of the scissor box.

More generally, any functions that operate in window coordinates all use the lower left as the origin. This includes functions like gluProject, gluUnProject, glCopyPixels, etc.

But some functions like glDrawPixels use glRasterPos for positioning, and glRasterPos uses object (i.e. transformed) coordinates.

If you're running on Linux, and know how to use man, you might want to grab a copy of the OpenGL manpages here: ftp://ftp.sgi.com/sgi/opengl/doc/ I find them a real time-saver.

Also, I notice you say "flip the file before loading" textures. This is not necessary. Instead of flipping the image when loading it, just flip the texture coordinates to go from 1.0 to 0.0 when rendering.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .