Why itsn't rotate ??

Hello ,

I drew a cube and I tried to rotate it but it doesn’t work ? I don’t know why ?

This is the code :
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>

//// Cube
float angle = 0.0f;

GLvoid DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// Calculate the rotating angle for the first cube.
angle = angle + 0.1f;
if(angle >= 360.0f) angle = 0.0f;

glPushMatrix(); //save matrix
glTranslatef(0.0,0.0,-6.0);
glRotatef(angle, 1.0f, 0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glutSolidCube(1.5f);
glPopMatrix(); //restore matrix

glFlush();
// glutSwapBuffers();
}

int main(int argc, char **argv)
{

/* Initialisation and window creation */

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE| GLUT_DEPTH);
glutInitWindowSize(400,400); glutInitWindowPosition(0,0);
glutCreateWindow(“Cube”);
/* A general OpenGL initialization function. Sets all of the initial parameters. */

glClearColor(0.0, 0.0, 0.0, 0.0);
glViewport(0, 0, 400, 400); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0,1,0.1,100.0); glMatrixMode(GL_MODELVIEW);

glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);

glutDisplayFunc(DrawGLScene);

glutMainLoop();
}

Thanks in advance for any help …

[This message has been edited by glcrazy (edited 01-26-2004).]

I’m not up on my GLUT, but shouldn’t you be using glutIdleFunc instead of glutDisplayFunc?

Might be a red herring, but I would have thought that the display function isn’t being called more than once because a redisplay hasn’t been triggered.

While you’re fixing the code with Dobbie’s suggestions, put in a glutSwapBuffers();
call after each frame if you’re double buffering, or you’ll always be drawing to the buffer you’re not looking at.


GLvoid DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
glLoadIdentity(); 

// Calculate the rotating angle for the first cube. 
angle = angle + 0.1f;
if(angle &gt;= 360.0f) angle = 0.0f;

glPushMatrix(); //save matrix 
glTranslatef(0.0,0.0,-6.0); 
glRotatef(angle, 1.0f, 0.0f, 1.0f); 
glColor3f(0.0f, 1.0f, 0.0f);
glutSolidCube(1.5f); 
glPopMatrix(); //restore matrix

glFlush();

// Must be enabled
glutSwapBuffers();
}


int main(int argc, char **argv) 
{ 

/* Initialisation and window creation */

glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE| GLUT_DEPTH); 
glutInitWindowSize(400,400); glutInitWindowPosition(0,0); 
glutCreateWindow("Cube"); 
/* A general OpenGL initialization function. Sets all of the initial parameters. */

glClearColor(0.0, 0.0, 0.0, 0.0); 
glViewport(0, 0, 400, 400); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0,1,0.1,100.0); glMatrixMode(GL_MODELVIEW); 

glShadeModel(GL_SMOOTH); 
glEnable(GL_DEPTH_TEST); 

glutDisplayFunc(DrawGLScene); 

// Settings this, will call DrawGLScene when the application is in idle mode (not receiving any commands)
glutIdleFunc(DrawGLScene); 

glutMainLoop(); 
}

You are calling swapbuffers, but you have set only a single buffer in your window creation. Should be GLUT_DOUBLE not GLUT_SINGLE in the glutInitDisplayMode.

glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE| GLUT_DEPTH);

Also this should not be in your main routine but your draw routine or reshape.

glClearColor(0.0, 0.0, 0.0, 0.0);
glViewport(0, 0, 400, 400); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0,1,0.1,100.0); glMatrixMode(GL_MODELVIEW);

[This message has been edited by nexusone (edited 01-26-2004).]

Thanks to all of you

I used glutIdleFunc as cfmdobbie2 and cenix said and enabled glutSwapBuffers as dovkruger and cenix said and changed to GLUT_DOUBLE as nexusone said … and it’s work

Thank you very much again

Yeah, go team!

Hi, team

Rendering should really happen in the display callback, as it initially was. The idle function can trigger the display function via the event system, but it should not do any rendering itself. An easy example to explain this would be a minimzed window. You don’t need to render anything then, and indeed glut won’t call the display callback when minimized (not even if you explicitly request it to be called).

The idle callback however will be called over and over again when minimized. Putting the rendering code there is - quite frankly - a waste of time.

The viewport and projection should be set in the reshape callback. They both depend on window size, so this is the most natural place to handle it.

void
reshape_callback(int width,int height)
{
    glViewPort(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    float aspect_ratio;
    if (height==0) aspect_ratio=1.0f;
    else           aspect_ratio=float(width)/height;

    gluPerspective(45.0,aspect_ratio,0.1,100.0);
    glMatrixMode(GL_MODELVIEW);
}

void
idle_callback()
{
   glutPostRedisplay();
}

void
DrawGLScene()
{
   //render
   <...>
   glutSwapBuffers();
}

int
main(int argc, char **argv) 
{
    <...>
    glutDisplayFunc(DrawGLScene);
    glutReshapeFunc(reshape_callback);
    glutIdleFunc(idle_callback);
    glutMainLoop();
}

[This message has been edited by zeckensack (edited 01-27-2004).]