I’m doing the 3rd demo program in the openGL bible. This is the second machine I’ve complied it on and I’m getting the same result. The program is suppose to bounce the square around and when it hits an edge it reverses direction. However, mine is stuck in the center. I’ve been going over this for about a day now. i can’t spot the mistake.
// Inital sqaure position and size
GLfloat x1 = 0.0f;
GLfloat y1 = 0.0f;
GLfloat rsize = 25;
// Step size in x and y directions
// (number of pixels to move each time)
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;
// Keep track of windows changing width and height
// Called to draw a scene
// Cear the window with current clearing color
// Set current drawing color to red
// R G B
glColor3f(1.0f, 0.0f, 0.0f);
// Draw a filled rectantgle with current color
glRectf(x1, y1, x1 + rsize, y1 - rsize);
// Flush drawing commands and swap
// Set up the rendering state
// Set clear color to blue
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
// Called by glut library when the window has changed size
void ChangeSize(GLsizei w, GLsizei h)
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinates system
// Establish clipping volume (Left, Right, bottom, top, near, far)
aspectRatio = (GLfloat)w / (GLfloat)h;
if (w <= h)
glOrtho (-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
glOrtho (-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0);
// Called by glut library when idle (window not being resized or moved)
void TimerFunction(int value)
// Reverse direction when you reach left or right edge.
if(x1 > windowWidth-rsize || x1 < -windowWidth)
xstep = -xstep;
// Reverse direction when you reach top or bottom edge.
if(y1 > windowHeight || y1 < -windowHeight + rsize)
ystep = -ystep;
// Acutally move the square
x1 += xstep;
y1 += ystep;
// Check bounds. This is in case the window is made smaller while the rectangle
// is bouncing and the rectangle suddenly finds itself outside the new clipping volume
if(x1 > (windowWidth-rsize + xstep))
x1 = windowWidth-rsize-1;
else if(x1 < -(windowWidth + xstep))
x1 = - windowWidth -1;
if(y1 > (windowHeight + ystep))
y1 = windowHeight-1;
else if(y1 < -(windowHeight - rsize + ystep))
y1 = -windowHeight + rsize -1;
// redraw the scene with new coordinates
glutTimerFunc(33, TimerFunction, 1);
// Main program entry point
int main(int argc, char* argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutTimerFunc(33, TimerFunction, 1);
I know. I’m a new to C++ and I don’t understand a lot of the stuff they call to that OpenGL calls to. I just started reading the bible 3 days ago. I have 4 weeks left of prgraming C++ 1 left. I get the fact that most of the stuff were dealing with are just functions.
I did however figure it out. But what if I want it to go ahead and write so it automatically go to the borders? I’m assuming 100 x 100 is a perfect square which is why it short changed the Width paramature. So I changed the code accordly.
I guess what I’m asking is there a way to make it reference
instead of spending all that time maunally resizing it??? I mean you could easly make a screen saver with this code and if you allowed it to automatically ajust to the screen size you definatly can’t make changes to live code on the fly.
Ok…I think I’ve figured out the problem. I think they are assuming you have a programing knowledge with these examples and I’m getting lost real fast cause they missing code fragments because they are just giving you just the new content<updates only not the full code>.
I’ve entered chapter 3 of the book and they give code snippetts and assume you know how to fill in the rest. They assume you use the previous code and add on to it but they are leaving the freedom up to you to modify that code accordingly. Let me explain to you what’s going on here and maybe you point me in the right direction.
I am being stupid and I’m learning almost every aspect to game making on my own. I have no history with this type of stuff but it’s my passion and I do it in my spare time. I’ve given myself 5 years to learn what I want. Here’s what I’m presently doing.
I’m currently using <ug hate to say this> Microsoft Visual C++ 2008. I have openGL, Glee, Glew & glut installed. I’m currently enrolled for C++ at a local college. I have a fair grasp of C++ but lack the expirence with it. So I’m making a lot of mistakes and missing simple things I should know.
by the 5th year manage to make a Game engine I can use on windows, mac <hopefully Linux> using currently technology to call to the graphics portion and shaders. I believe OpenGL can help realize the graphics end of my goals. I will learn about the rest as I go.
What I want:
I need something that will teach the in’s and out of OpenGL with tutorials so I can practice the code read up on the functions and get a better understanding of how OpenGL can help me in the future.
5 years is more than enough, if you’re hardcore about the thing.
Since you chose C++, be sure to get experience with it, because it’s more fundamental than OpenGL itself, if you want to write sth. Also be sure to understand that OpenGL is just a graphics API (just, coughs), and that in order to make a game engine, many ^ n aspects are needed. Linear algebra, vector math, graphics theory are just for the graphics alone, without talking about sound, networking & other stuff. And don’t plan to build the Grand Engine before you’ve tried some 100s of stuff.
You can find millions of tutorials for OpenGL on the net, but be sure to understand also the concepts behind the tutorials, and how they work. The concepts & the theory why everything works like it does are more important, so be sure to select carefully your sources.
Anyway, one of my fav sites when I started was Lighthouse3D.
Hey Nico since you were nice enough to assist me I figured I’d follow up with you and let you know I did some probing. After a little work I discovered they have the source code to their work here on the openGL site. After getting the source code all my answers where answered. there are 4 lines of code they omitted from the book which allows the program to work flawlessly. Below is the code.
if (w <= h)
windowWidth = 100;
windowHeight = 100 / aspectRatio;
glOrtho (-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0);
windowWidth = 100 * aspectRatio;
windowHeight = 100;
glOrtho (-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0);
Actually after carefully inspecting it they completly redid the Changesize function.
Well what I find more interesting is that all the examples in the book use global functions. All the changes that were made were done inside the fuction. Plus they complete changed the code to the ChangeSize Function. So I don’t feel so stupid now I couldn’t figure it out. But it kinda sorta does make sense to intialize them inside the function. Like you I’m use to intializing outside the function but I’m straight out of class.