PDA

View Full Version : Trouble with my first OpenGL program



SilentStrike
01-14-2001, 08:10 AM
I am just starting to Learn OpenGL. I read through the first 3 chapters of the Open GL Super Bible online, but unfortunetely, it doesn't let me get access to the CD that comes with it http://www.opengl.org/discussion_boards/ubb/frown.gif (guess they want me to buy it, no thx http://www.opengl.org/discussion_boards/ubb/wink.gif )

Anyway, I am trying to modify the 3rd section of code with the AuxKeyFunc function so that I can move a rectangle down when I press the d key. However, nothing happens when I press the key down. What is wrong with my code?

// bounce.c
// Bouncing square

#include <windows.h> // Standard windows include
#include <gl\gl.h> // OpenGL library
#include <gl\glaux.h> // AUX library
#include <iostream.h>

GLfloat windowHeight = 250.0f;
GLfloat windowWidth = 250.0f;

GLfloat x1 = 100;
GLfloat y1 = 100;
GLfloat rsize = 50;

void CALLBACK MoveDown(void) {
cout << "Yup, you pressed d" << endl;
if (y1 - rsize > 0)
y1--;
}

void CALLBACK ChangeSize(GLsizei w, GLsizei h)
{
// Prevent a divide by zero, when window is too short
// (you canít make a window of zero width)
if(h == 0)
h = 1;

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Reset the coordinate system before modifying
glLoadIdentity();

// Keep the square square, this time, save calculated
// width and height for later use
if (w <= h)
{
windowHeight = 250.0f*h/w;
windowWidth = 250.0f;
}
else
{
windowWidth = 250.0f*w/h;
windowHeight = 250.0f;
}

// Set the clipping volume
glOrtho(0.0f, windowWidth, 0.0f, windowHeight, 1.0f, -1.0f);
}

// Called by AUX library to update window
void CALLBACK RenderScene(void)
{
// Set background clearing color to blue
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);

// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);

// Set drawing color to red, and draw rectangle at
// current position.
glColor3f(1.0f, 0.0f, 0.0f);
glRectf(x1, y1, x1 + rsize, y1 + rsize);


glFlush();
auxSwapBuffers();
}


// Main body of program
void main(void)
{
// AUX window setup and initialization
auxInitDisplayMode(AUX_DOUBLE | AUX_RGBA);
auxInitPosition(100,100,250,250);
auxInitWindow("Simple 2D Animation");

// Set function to call when window is resized
auxReshapeFunc(ChangeSize);

// Start main loop
auxMainLoop(RenderScene);

//Move Rect down when D is pressed
auxKeyFunc(AUX_d, MoveDown);
}

chennes
01-14-2001, 08:23 AM
Although I've never read the Superbible (I stick to the red and blue books), I would assume that once you enter the main event loop, nothing else in main() gets executed until after the program terminates the loop (i.e. - quits). Are you getting a response to the button pressed event (you've got a print statemnet in there - is it executing?)? If not, try moving the last line...

instead of:
// Start main loop
auxMainLoop(RenderScene);

//Move Rect down when D is pressed
auxKeyFunc(AUX_d, MoveDown);

try:
//Move Rect down when D is pressed
auxKeyFunc(AUX_d, MoveDown);

// Start main loop
auxMainLoop(RenderScene);

HTH,
Chris

SilentStrike
01-14-2001, 08:30 AM
Yeah, it worked now. Thanks Chris.