Results 1 to 6 of 6

Thread: "Bounce" example from SuperBible (3th)

  1. #1

    "Bounce" example from SuperBible (3th)

    Hello!

    I am rewriting the second example (Bounce) of SuperBible (3th) in Qt. But my square doesn't move! This is the original example from the disk of the book:

    Code :
    // Bounce.c
    // Demonstrates a simple animated rectangle program with GLUT
    // OpenGL SuperBible, 3rd Edition
    // Richard S. Wright Jr.
    // rwright@starstonesoftware.com
     
    #include "../../Common/OpenGLSB.h"	// System and OpenGL Stuff
     
     
     
    // Initial square position and size
    GLfloat x = 0.0f;
    GLfloat y = 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
    GLfloat windowWidth;
    GLfloat windowHeight;
     
    ///////////////////////////////////////////////////////////
    // Called to draw scene
    void RenderScene(void)
    	{
    	// Clear the window with current clearing color
    	glClear(GL_COLOR_BUFFER_BIT);
     
       	// Set current drawing color to red
    	//		   R	 G	   B
    	glColor3f(1.0f, 0.0f, 0.0f);
     
    	// Draw a filled rectangle with current color
    	glRectf(x, y, x + rsize, y - rsize);
     
        // Flush drawing commands and swap
        glutSwapBuffers();
    	}
     
    ///////////////////////////////////////////////////////////
    // 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(x > windowWidth-rsize || x < -windowWidth)
            xstep = -xstep;
     
        // Reverse direction when you reach top or bottom edge
        if(y > windowHeight || y < -windowHeight + rsize)
            ystep = -ystep;
     
    	// Actually move the square
        x += xstep;
        y += 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(x > (windowWidth-rsize + xstep))
            x = windowWidth-rsize-1;
    	else if(x < -(windowWidth + xstep))
    		x = -windowWidth -1;
     
        if(y > (windowHeight + ystep))
            y = windowHeight-1; 
    	else if(y < -(windowHeight - rsize + ystep))
    		y = -windowHeight + rsize - 1;
     
     
     
         // Redraw the scene with new coordinates
        glutPostRedisplay();
        glutTimerFunc(33,TimerFunction, 1);
        }
     
     
    ///////////////////////////////////////////////////////////
    // Setup the rendering state
    void SetupRC(void)
        {
        // Set clear color to blue
        glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
        }
     
     
    ///////////////////////////////////////////////////////////
    // Called by GLUT library when the window has chanaged size
    void ChangeSize(int w, int h)
        {
        GLfloat aspectRatio;
     
        // Prevent a divide by zero
        if(h == 0)
            h = 1;
     
        // Set Viewport to window dimensions
        glViewport(0, 0, w, h);
     
        // Reset coordinate system
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
     
        // Establish clipping volume (left, right, bottom, top, near, far)
        aspectRatio = (GLfloat)w / (GLfloat)h;
        if (w <= h) 
            {
            windowWidth = 100;
            windowHeight = 100 / aspectRatio;
            glOrtho (-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0);
            }
        else 
            {
            windowWidth = 100 * aspectRatio;
            windowHeight = 100;
            glOrtho (-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0);
            }
     
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        }
     
    ///////////////////////////////////////////////////////////
    // Main program entry point
    int main(int argc, char* argv[])
    	{
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    	glutInitWindowSize(800,600);
            glutCreateWindow("Bounce");
    	glutDisplayFunc(RenderScene);
            glutReshapeFunc(ChangeSize);
    	glutTimerFunc(33, TimerFunction, 1);
     
    	SetupRC();
     
    	glutMainLoop();
     
            return 0;
        }

    This is my code in Qt:
    Code :
     
    #include "Scene.h"
     
    Scene::Scene( QWidget *parent ) :
        QGLWidget( parent ),
        m_x( 0.0f ),
        m_y( 0.0f ),
        m_rsize( 25.0f )
    {
        m_timer = new QTimer( this );
        connect( m_timer, SIGNAL( timeout() ),
                 this, SLOT( slotMoveRect() ) );
        m_timer->start( 100 );
    }
     
    void Scene::slotMoveRect()
    {
        // Reverse direction when you reach left or right edge
        if( m_x > m_windowWidth - m_rsize || m_x < -m_windowWidth ) {
            m_xstep = -m_xstep;
        }
     
        // Reverse direction when you reach top or bottom edge
        if( m_y > m_windowHeight || m_y < -m_windowHeight + m_rsize ) {
            m_ystep = -m_ystep;
        }
     
        // Actually move the square
        m_x += m_xstep;
        m_y += m_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( m_x > ( m_windowWidth-m_rsize + m_xstep ) ) {
            m_x = m_windowWidth-m_rsize-1;
        } else if( m_x < -( m_windowWidth + m_xstep ) ) {
            m_x = -m_windowWidth - 1;
        }
     
        if( m_y > ( m_windowHeight + m_ystep ) ) {
            m_y = m_windowHeight-1;
        } else if( m_y < -( m_windowHeight - m_rsize + m_ystep ) ) {
            m_y = -m_windowHeight + m_rsize - 1;
        }
     
        updateGL();
    }
     
    void Scene::initializeGL()
    {
        glClearColor( 0.0f, 0.0f, 1.0f, 1.0f );
    }
     
    void Scene::paintGL()
    {
        // Clear the window with current clearing color
        glClear( GL_COLOR_BUFFER_BIT );
     
        // Set current drawing color to red
        glColor3f( 1.0f, 0.0f, 0.0f );
     
        // Draw a filled rectangle with current color
        glRectf( m_x, m_y, m_x + m_rsize, m_y - m_rsize );
    }
     
    void Scene::resizeGL( int w, int h )
    {
        // Prevent a divide by zero
        if ( h == 0 ) {
            h = 1;
        }
     
        // Set Viewport to window dimensions
        glViewport( 0, 0, w, h );
     
        // Reset coordinate system
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
     
        // Establish clipping volume (left, right, bottom, top, near, far)
        GLfloat aspectRatio = ( GLfloat ) w / ( GLfloat ) h;
     
        if ( w <= h ) {
            m_windowWidth = 100.0f;
            m_windowHeight = 100.0f / aspectRatio;
            glOrtho( -100.0, 100.0, -m_windowHeight, m_windowHeight, 1.0, -1.0 );
        } else {
            m_windowWidth = 100.0 * aspectRatio;
            m_windowHeight = 100.0;
            glOrtho( -m_windowWidth, m_windowWidth, -100.0, 100.0, 1.0, -1.0 );
        }
     
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    }

    Thank you!

  2. #2
    Senior Member Regular Contributor
    Join Date
    Dec 2010
    Location
    Oakville, ON, CA
    Posts
    165
    I do not see a call swapping the buffers after the rectangle is drawn ( at the end of Scene :: PaintGL(){...} ).

  3. #3
    I see the blinking and this message:

    QOpenGLContext::swapBuffers() called without corresponding makeCurrent()

  4. #4
    Senior Member Regular Contributor
    Join Date
    Dec 2010
    Location
    Oakville, ON, CA
    Posts
    165
    Well, that is not a reason to avoid calling that function.
    It looks like window is not initialized properly (the call to wglMakeCurrent() is not made for the created OpenGL rendering context).

  5. #5
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896
    QGLWidget (the class of 8Observer8's Scene class from) is supposed to take care of making the context current before calling paintGL and swapping buffers afterwards (unless explicitly disabled with setAutoBufferSwap(false)).

    8Observer8: Since your original problem was that the square is not moving: is your slotMoveRect function actually called, i.e. set a breakpoint in it when running under a debugger. While there you can also check the coordinates to see if they are reasonable after the update.

  6. #6
    I found mistake! I forgot to initialize m_xstep and m_ystep:
    Code :
    Scene::Scene( QWidget *parent ) :
        QGLWidget( parent ),
        m_x( 0.0f ),
        m_y( 0.0f ),
        m_rsize( 25.0f ),
        m_xstep( 1.0f ),
        m_ystep( 1.0f )
    {
        m_timer = new QTimer( this );
        connect( m_timer, SIGNAL( timeout() ),
                 this, SLOT( slotMoveRect() ) );
        m_timer->start( 33 );
    }

    I don't need to swap buffers in Qt

    P.S. carsten neumann, thank you very much

Similar Threads

  1. OpenGL SuperBible 5E, Triangle program background "scattered"
    By kyle.brechin in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 03-05-2014, 02:21 AM
  2. Replies: 1
    Last Post: 10-26-2012, 02:19 AM
  3. Silly question regards "Superbible" camera class
    By pixelita in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-23-2011, 09:25 AM
  4. OpenGL Superbible 5th ed. Problem with "Move"
    By BajiBoxer in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-03-2011, 03:57 AM
  5. "best" linux dist for a "Gelato" render farm?
    By ArtificialArtOfficial in forum OpenGL: Linux
    Replies: 10
    Last Post: 12-23-2007, 01:02 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