Results 1 to 3 of 3

Thread: Trouble with my first OpenGL program

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2001
    Posts
    8

    Trouble with my first OpenGL program

    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 (guess they want me to buy it, no thx )

    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);
    }

  2. #2
    Senior Member Regular Contributor
    Join Date
    Nov 2000
    Location
    State College, PA
    Posts
    186

    Re: Trouble with my first OpenGL program

    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

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2001
    Posts
    8

    Re: Trouble with my first OpenGL program

    Yeah, it worked now. Thanks Chris.

Similar Threads

  1. New to Opengl, having trouble with program.
    By 3DozenWhales in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 07-25-2013, 10:14 AM
  2. Trouble with first OpenCL program
    By Wolfheart in forum OpenCL
    Replies: 10
    Last Post: 02-02-2012, 03:56 AM
  3. trouble running a program
    By in forum OpenGL: User Software
    Replies: 0
    Last Post: 12-31-2002, 03:32 PM
  4. trouble running program ...please help.
    By c_anindya in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 11-12-2002, 10:07 AM
  5. vertex program trouble
    By SirKnight in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 06-01-2001, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean