Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Problems with online tutorial

  1. #1
    Newbie Newbie
    Join Date
    Jun 2013
    Posts
    2

    Problems with online tutorial

    Hi everybody,

    i am trying to learn OpenGl for some days now but i still get a problem after this tutorial.

    youtube[dot]com/watch?v=qniXE1wi0Po

    when i try to start the program with the example source code, the cube isnīt rotating smoothly, it stucks and itīs jerky.

    can somebody help me with this problem?

    here is the source code:

    Code :
    #include <GL\glut.h>
     GLfloat xRotated, yRotated, zRotated;
    void init(void)
    {
    glClearColor(0,0,0,0);
     
    }
     
    void DrawCube(void)
    {
     
         glMatrixMode(GL_MODELVIEW);
        // clear the drawing buffer.
        glClear(GL_COLOR_BUFFER_BIT);
       glLoadIdentity();
            glTranslatef(0.0,0.0,-10.5);
        glRotatef(xRotated,1.0,0.0,0.0);
        // rotation about Y axis
        glRotatef(yRotated,0.0,1.0,0.0);
        // rotation about Z axis
        glRotatef(zRotated,0.0,0.0,1.0);
      glBegin(GL_QUADS);        // Draw The Cube Using quads
        glColor3f(0.0f,1.0f,0.0f);    // Color Blue
        glVertex3f( 1.0f, 1.0f,-1.0f);    // Top Right Of The Quad (Top)
        glVertex3f(-1.0f, 1.0f,-1.0f);    // Top Left Of The Quad (Top)
        glVertex3f(-1.0f, 1.0f, 1.0f);    // Bottom Left Of The Quad (Top)
        glVertex3f( 1.0f, 1.0f, 1.0f);    // Bottom Right Of The Quad (Top)
        glColor3f(1.0f,0.5f,0.0f);    // Color Orange
        glVertex3f( 1.0f,-1.0f, 1.0f);    // Top Right Of The Quad (Bottom)
        glVertex3f(-1.0f,-1.0f, 1.0f);    // Top Left Of The Quad (Bottom)
        glVertex3f(-1.0f,-1.0f,-1.0f);    // Bottom Left Of The Quad (Bottom)
        glVertex3f( 1.0f,-1.0f,-1.0f);    // Bottom Right Of The Quad (Bottom)
        glColor3f(1.0f,0.0f,0.0f);    // Color Red    
        glVertex3f( 1.0f, 1.0f, 1.0f);    // Top Right Of The Quad (Front)
        glVertex3f(-1.0f, 1.0f, 1.0f);    // Top Left Of The Quad (Front)
        glVertex3f(-1.0f,-1.0f, 1.0f);    // Bottom Left Of The Quad (Front)
        glVertex3f( 1.0f,-1.0f, 1.0f);    // Bottom Right Of The Quad (Front)
        glColor3f(1.0f,1.0f,0.0f);    // Color Yellow
        glVertex3f( 1.0f,-1.0f,-1.0f);    // Top Right Of The Quad (Back)
        glVertex3f(-1.0f,-1.0f,-1.0f);    // Top Left Of The Quad (Back)
        glVertex3f(-1.0f, 1.0f,-1.0f);    // Bottom Left Of The Quad (Back)
        glVertex3f( 1.0f, 1.0f,-1.0f);    // Bottom Right Of The Quad (Back)
        glColor3f(0.0f,0.0f,1.0f);    // Color Blue
        glVertex3f(-1.0f, 1.0f, 1.0f);    // Top Right Of The Quad (Left)
        glVertex3f(-1.0f, 1.0f,-1.0f);    // Top Left Of The Quad (Left)
        glVertex3f(-1.0f,-1.0f,-1.0f);    // Bottom Left Of The Quad (Left)
        glVertex3f(-1.0f,-1.0f, 1.0f);    // Bottom Right Of The Quad (Left)
        glColor3f(1.0f,0.0f,1.0f);    // Color Violet
        glVertex3f( 1.0f, 1.0f,-1.0f);    // Top Right Of The Quad (Right)
        glVertex3f( 1.0f, 1.0f, 1.0f);    // Top Left Of The Quad (Right)
        glVertex3f( 1.0f,-1.0f, 1.0f);    // Bottom Left Of The Quad (Right)
        glVertex3f( 1.0f,-1.0f,-1.0f);    // Bottom Right Of The Quad (Right)
      glEnd();            // End Drawing The Cube
    glFlush();
    }
     
     
    void animation(void)
    {
     
         yRotated += 0.01;
         xRotated += 0.02;
        DrawCube();
    }
     
     
    void reshape(int x, int y)
    {
        if (y == 0 || x == 0) return;  //Nothing is visible then, so return
        //Set a new projection matrix
        glMatrixMode(GL_PROJECTION);  
        glLoadIdentity();
        //Angle of view:40 degrees
        //Near clipping plane distance: 0.5
        //Far clipping plane distance: 20.0
     
        gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
        glMatrixMode(GL_MODELVIEW);
        glViewport(0,0,x,y);  //Use the whole window for rendering
    }
     
    int main(int argc, char** argv){
     
    glutInit(&argc, argv);
    //we initizlilze the glut. functions
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(DrawCube);
    glutReshapeFunc(reshape);
    //Set the function for the animation.
    glutIdleFunc(animation);
    glutMainLoop();
    return 0;
    }

  2. #2
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    173
    Right off the bat I can see one issue that might be the source of your problem.
    You call 'DrawCube' inside the 'Animation' function.
    That's not supposed to be how OGL/GLUT works.
    'DrawCube' is designated as the primary rendering routine in 'main' with the
    statement 'glutDisplayFunc(DrawCube)'.
    Therefore GLUT automatically calls 'DrawCube' after the Idle Function is executed.
    You do not have to explicitly call 'DrawCube' from within 'Animation'.
    In fact this will screw things up because 'DrawCube' is now being called twice -
    once by GLUT (because it's the DisplayFunc), and once from 'Animation'.

    Try commenting out the call to 'DrawCube' inside 'Animation' and see if that helps.

  3. #3
    Newbie Newbie
    Join Date
    Jun 2013
    Posts
    2
    iīve tried your idea but without the "drawCube" Methode the cube isnīt rotating...


    i think the code must be right because it is an old online tutorial and i am certainly not the first one learning opengl with this tutorial ;-)

  4. #4
    Newbie Newbie
    Join Date
    Jun 2013
    Posts
    2

    Fixed it

    Hey there I updated your source code to a program that's about 75% less sucky

    Code :
    #include <GL\glut.h>
     GLfloat xRotated, yRotated, zRotated;
    void init(void)
    {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Changed to reflect floating point numbers
     
    }
     
    void DrawCube(void)
    {
     
         glMatrixMode(GL_MODELVIEW);
        // clear the drawing buffer.
        glClear(GL_COLOR_BUFFER_BIT);
       glLoadIdentity();
            glTranslatef(0.0,0.0,-10.5);
        glRotatef(xRotated,1.0,0.0,0.0);
        // rotation about Y axis
        glRotatef(yRotated,0.0,1.0,0.0);
        // rotation about Z axis
        glRotatef(zRotated,0.0,0.0,1.0);
     
      glBegin(GL_QUADS);        // Draw The Cube Using quads
        glColor3f(0.0f,1.0f,0.0f);    // Color Blue
        glVertex3f( 1.0f, 1.0f,-1.0f);    // Top Right Of The Quad (Top)
        glVertex3f(-1.0f, 1.0f,-1.0f);    // Top Left Of The Quad (Top)
        glVertex3f(-1.0f, 1.0f, 1.0f);    // Bottom Left Of The Quad (Top)
        glVertex3f( 1.0f, 1.0f, 1.0f);    // Bottom Right Of The Quad (Top)
        glColor3f(1.0f,0.5f,0.0f);    // Color Orange
        glVertex3f( 1.0f,-1.0f, 1.0f);    // Top Right Of The Quad (Bottom)
        glVertex3f(-1.0f,-1.0f, 1.0f);    // Top Left Of The Quad (Bottom)
        glVertex3f(-1.0f,-1.0f,-1.0f);    // Bottom Left Of The Quad (Bottom)
        glVertex3f( 1.0f,-1.0f,-1.0f);    // Bottom Right Of The Quad (Bottom)
        glColor3f(1.0f,0.0f,0.0f);    // Color Red    
        glVertex3f( 1.0f, 1.0f, 1.0f);    // Top Right Of The Quad (Front)
        glVertex3f(-1.0f, 1.0f, 1.0f);    // Top Left Of The Quad (Front)
        glVertex3f(-1.0f,-1.0f, 1.0f);    // Bottom Left Of The Quad (Front)
        glVertex3f( 1.0f,-1.0f, 1.0f);    // Bottom Right Of The Quad (Front)
        glColor3f(1.0f,1.0f,0.0f);    // Color Yellow
        glVertex3f( 1.0f,-1.0f,-1.0f);    // Top Right Of The Quad (Back)
        glVertex3f(-1.0f,-1.0f,-1.0f);    // Top Left Of The Quad (Back)
        glVertex3f(-1.0f, 1.0f,-1.0f);    // Bottom Left Of The Quad (Back)
        glVertex3f( 1.0f, 1.0f,-1.0f);    // Bottom Right Of The Quad (Back)
        glColor3f(0.0f,0.0f,1.0f);    // Color Blue
        glVertex3f(-1.0f, 1.0f, 1.0f);    // Top Right Of The Quad (Left)
        glVertex3f(-1.0f, 1.0f,-1.0f);    // Top Left Of The Quad (Left)
        glVertex3f(-1.0f,-1.0f,-1.0f);    // Bottom Left Of The Quad (Left)
        glVertex3f(-1.0f,-1.0f, 1.0f);    // Bottom Right Of The Quad (Left)
        glColor3f(1.0f,0.0f,1.0f);    // Color Violet
        glVertex3f( 1.0f, 1.0f,-1.0f);    // Top Right Of The Quad (Right)
        glVertex3f( 1.0f, 1.0f, 1.0f);    // Top Left Of The Quad (Right)
        glVertex3f( 1.0f,-1.0f, 1.0f);    // Bottom Left Of The Quad (Right)
        glVertex3f( 1.0f,-1.0f,-1.0f);    // Bottom Right Of The Quad (Right)
      glEnd();            // End Drawing The Cube
     
      glutSwapBuffers();      //Removed glFlush() command
      glutPostRedisplay();    //added this to swap buffers since we are now in double buffered mode.
     
    }
     
     
    void animation(void)
    {
     
         yRotated += 0.1f; //It moved slow when I fixed it so I made it a little faster.
         xRotated += 0.2f;
        DrawCube();
    }
     
     
    void reshape(int x, int y)
    {
        if (y == 0 || x == 0) return;  //Nothing is visible then, so return
        //Set a new projection matrix
        glMatrixMode(GL_PROJECTION);  
        glLoadIdentity();
        //Angle of view:40 degrees
        //Near clipping plane distance: 0.5
        //Far clipping plane distance: 20.0
     
        gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
        glMatrixMode(GL_MODELVIEW);
        glViewport(0,0,x,y);  //Use the whole window for rendering
    }
     
    int main(int argc, char** argv){
     
    glutInit(&argc, argv);
    //we initizlilze the glut. functions
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA); //Switched from GLUT_SINGLE to GLUT_DOUBLE so have double buffering| switched GLUT_RGB to GLUT_RGBA 
    glutInitWindowPosition(100, 100);
    glutCreateWindow("THERE I FIXED IT");
    init();
     
    //Set the function for the animation.
    glutDisplayFunc(animation);   //Removed glutIdleFunc, You should only use that for operations when the screen isn't drawing
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
    }

    The code itself is commented with the changes but the biggest no-no was drawing with a combination of glFlush() and singlebuffering. Second, your program
    was trying to draw and animate in between drawing processes. Your program was most likely not able to complete vertex operations before your double glFlush() calls. The
    way it works now is that you now have two buffers (places in video memory)
    for final drawing to the output device. The scene is rendered and displayed, all the while the next frame is rendered to the back buffer. When the next frame is ready,
    the buffers are switched and the process starts all over again. Voila, you have animation.

    To make the program better, I would advise you to learn how to do things without the fixed function pipeline. As a beginner, it is good to know how things were done, but
    modern openGL programs are going to require shaders (little programs written in C) for EVERYTHING.
    Last edited by R-Skully; 06-09-2013 at 03:39 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
  •