PDA

View Full Version : Why itsn't rotate ??



glcrazy
01-26-2004, 11:37 AM
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).]

01-26-2004, 12:59 PM
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.

dovkruger
01-26-2004, 01:30 PM
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.

01-26-2004, 02:08 PM
[CODE]
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();

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

nexusone
01-26-2004, 02:38 PM
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).]

glcrazy
01-27-2004, 09:31 AM
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 http://www.opengl.org/discussion_boards/ubb/smile.gif

Thank you very much again

01-27-2004, 09:53 AM
Yeah, go team! http://www.opengl.org/discussion_boards/ubb/wink.gif

zeckensack
01-27-2004, 10:30 AM
Hi, team http://www.opengl.org/discussion_boards/ubb/biggrin.gif

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).]