This is the complete code I am using. The sphere does not spin like I would expect it to. Normally I program in Java so maybe it is my c++ programming and not GLUT.
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <iostream>
using namespace std;
static float angle = 0;
void init(void)
{
GLfloat mat_specular[] = { 1.0, .5f, .5f, .5f };
GLfloat mat_shininess[] = { 15.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
cout << angle;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(90, 1, 0, 0);
glRotatef(angle, 0, 0, 1);
glutSolidSphere (.5, 24, 24);
glPopMatrix();
glFlush ();
angle += 1;
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho (-1.5*(GLfloat)w/(GLfloat)h, 1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
It seems from my debug line ‘cout << angle;’ that glut is only calling the display function once.
I am using Microsoft Visual C++ 2010 with GLUT
There are a few options:
You can manually call
glutPostRedisplayto ask the main loop to call your display function again, as documented here.Or, you can set it to automatically call your display as often as possible by setting it as the idle func with
glutIdleFunc, as documented here.If you’re going with the Idle Func, I would definitely suggest you to create a function such as
gameUpdateorsimulationUpdatewhere you manipulate your angle and THEN callglutPostRedisplayso that the changes can be rendered.