PDA

View Full Version : Help with superBible chapter 2



thrive
06-20-2008, 10:13 AM
Hi I'm hopeing someone here can help me with one of the examples in the OpenGL super-bible version 4. Its in chapter two and is where a cube is made to move around a window and bounce off the sides. I've checked and double checked my code copying directly from the book but all I get is a box that flashes back and forth in the middle continuously. I know the problem has to do with the program not knowing the height and width of the clipping volume. But my attempts to fix it have failed.

My code is as followed:
//The only difference between this and the book is that I called the variables something else because when I compiled those variables were already in the math header file or something.


#include "../../../src/shared/gltools.h"

GLfloat my_x = 0.0f;
GLfloat my_y = 0.0f;
GLfloat rsize = 25;

GLfloat my_xstep = 1.0f;
GLfloat my_ystep = 1.0f;

GLfloat my_h;
GLfloat my_w;

void RenderScene(void)
{
//clear with the clear color in setup
glClear(GL_COLOR_BUFFER_BIT);
//set to red
glColor3f(0.5f, 0.0f,0.0f);

glRectf(my_x, my_y, my_x+rsize, my_y-rsize);

glutSwapBuffers();
}
//called when the window isn't working
void TimerFunction(int value)
{
//reverse directions
if(my_x > my_w-rsize || my_x < -my_w)
my_xstep = -my_xstep;
if( my_y > my_h || my_y < -my_h+rsize)
my_ystep = -my_ystep;

//actually move
my_x +=my_xstep;
my_y +=my_ystep;

//check the bounds
if( my_x > (my_w-rsize + my_xstep))
my_x = my_w -rsize-1;
else if(my_x < -(my_w + my_xstep))
my_x = -my_w -1;

if(my_y > (my_h + my_ystep))
my_y = my_h-1;
else if( my_y < -(my_h-rsize +my_ystep))
my_y = -my_h + rsize -1;

glutPostRedisplay();
glutTimerFunc(33,TimerFunction, 1);
}

void SetupRC(void)
{
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat aspectRatio;

if(h==0)
h=1;

glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

aspectRatio = (GLfloat)w / (GLfloat) h;
//this if statement keeps the square from growing in only one direction by enlarging the coordinate system in that direction
if(w <= h) //what is the limiting factor?
{// makes bigger
glOrtho(-100.0, 100.0, -100/aspectRatio, 100.0/aspectRatio, 1.0, -1.0);
my_h = 100/aspectRatio;
my_w = 100;
}
else
{
glOrtho(-100.0 * aspectRatio, 100.0 * aspectRatio, -100, 100.0, 1.0, -1.0);
my_w = aspectRatio*100;
my_h = 100;
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[]) //beggining of any c program
{
glutInit(&argc, argv); //beggining of GLUT initialization
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); //sets up how the window will display
//other options are double buffered and color index(out of date)
glutInitWindowSize( 600, 600 );
glutCreateWindow("Bounce"); //obviously creates the window

glutDisplayFunc(RenderScene); //this says that "renderscene" is called whenever the window is drawn

glutReshapeFunc(ChangeSize);
glutTimerFunc(33, TimerFunction, 1);

SetupRC(); //the function under which we do all set up for openGL
glutMainLoop(); //This doesnt quit untill window is closed (or mas os x tells it to quit)
// processes all operating specific messages and keystrokes
return 0;
}

thrive
06-20-2008, 10:45 AM
Found the problem. Sorry for the waist of time.

satan
06-21-2008, 10:56 AM
Then post your solution so that other users can benefit from it.