tags:

views:

231

answers:

3

Hello, I am creating a simple 2D OpenGL application but I seem to be experiencing some camera issues. When I draw a rectangle at (20,20) it is drawn at (25,20) or so. When I draw it at (100, 20) it is drawn at 125 or so. For some reasons everything is being shifted to the right by a few %.

I have pasted a trimmed down version here http://pastebin.com/m56491c4c

Is there something wrong with the way I am setting up GLUT? I know it's not my objects doing anything weird since the same thing happens when I disable them.

Thanks in advance.

A: 

You seem to be calling your glOrtho2D on your ModelView matrix. I doubt that that's the problem (since I guess in this case, your Projection should be the identity), but you should still call it on your Projection matrix instead.

You should also print out w and h in your resize call, just to make sure that your window size is actually what you think it is (I don't know how glut works, but glutInitWindowSize() may include borders, which would mess things up).

I've already tried setting it up in the order you suggested without any luck. I've messed around with the outputs and am fairly confident that the window size is correct. All out of ideas.
Alexander
+3  A: 

You need to set the projection matrix inside the reshape function (resize()), which also automatically solves the problem of the user resizing the window:

void resize(int w, int h)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0, w, h, 0);
}

And then in your draw function, make sure that the matrix mode is model-view:

void draw()
{
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  ...
}

Other problems with your code:

  • You probably shouldn't be calling glutPostRedisplay() at the end of draw(). This is going to make your CPU run at 100%. You can instead use glutTimerFunc() to still have updates every some number of milliseconds.
  • In processMouse(), you're using wsprintf() on an array of chars: wsprintf() takes an array of wide characters (wchar_t), so you should make the local variable s of type wchar_t[], or use sprintf() and MessageBoxA() instead of wsprintf() and MessageBoxW() (to which MessageBox() expands as a macro when compiling a Unicode application, which I'm assuming you're doing). You're also vulnerable to a buffer overflow -- you should use a buffer of at least 12 characters, even though realistically you'll never be passed a very large x value. Finally, you should also use snprintf()/wsnprintf() instead of sprintf()/wsprintf() to protect against the buffer overflow.
Adam Rosenfield
Thanks, that fixed it! Thanks for your other suggestions. I know it's messy code, I just wanted a quick working sample to post. Thanks again!
Alexander
A: 

It depends on what system you are working on, but usually most windows coordinate systems start at the bottom left corner and count up to the top and to the right. In this case your gluOrth02D call would be wrong.

You Have:

gluOrtho2D(0, appWidth, appHeight, 0);

Which has the top of the window mapping to the bottom, vice-verse.

Most of the time it's:

gluOrtho2D(0, appWidth, 0, appHeight);

As I said it depends on the system, platform your working with. I can only speak for most linux implementations.

Just something else to check out just in case it affecting your bug.

Brian Gianforcaro