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.
You need to set the projection matrix inside the reshape function (
resize()), which also automatically solves the problem of the user resizing the window:And then in your draw function, make sure that the matrix mode is model-view:
Other problems with your code:
glutPostRedisplay()at the end ofdraw(). This is going to make your CPU run at 100%. You can instead useglutTimerFunc()to still have updates every some number of milliseconds.processMouse(), you’re usingwsprintf()on an array ofchars:wsprintf()takes an array of wide characters (wchar_t), so you should make the local variablesof typewchar_t[], or usesprintf()andMessageBoxA()instead ofwsprintf()andMessageBoxW()(to whichMessageBox()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 largexvalue. Finally, you should also usesnprintf()/wsnprintf()instead ofsprintf()/wsprintf()to protect against the buffer overflow.