Refresh Open GL in GLUT
I'm going through the NeHe tutorials, just started really. Anyway, I'm on the tutorial where you first actually have some rotating going on, unfortunately my view only refreshes when I resize the window. Here's all my code:
How can I make it refresh regularly. I guess this would be the beginning of an 'engine'?
Code:
#include <GLUT/glut.h>
// Constants -----------------------------------------------------------------
#define kWindowWidth 400
#define kWindowHeight 300
// Function Prototypes -------------------------------------------------------
GLvoid InitGL(GLvoid);
GLvoid DrawGLScene(GLvoid);
GLvoid ReSizeGLScene(int Width, int Height);
// Main ----------------------------------------------------------------------
GLfloat rtri; // Angle For The Triangle ( NEW )
GLfloat rquad; // Angle For The Quad ( NEW )
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (kWindowWidth, kWindowHeight);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
InitGL();
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
glutMainLoop();
return 0;
}
// Init ----------------------------------------------------------------------
GLvoid InitGL(GLvoid)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f,(GLfloat)kWindowWidth/(GLfloat)kWindowHeight,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW);
}
// DrawGLScene ---------------------------------------------------------------
GLvoid DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glRotatef(rtri,0.0f,1.0f,0.0f); // Rotate The Triangle On The Y axis ( NEW )
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 6.0
glRotatef(rquad,1.0f,0.0f,0.0f); // Rotate The Quad On The X axis ( NEW )
glColor3f(0.5f,0.5f,1.0f); // Set The Color To Blue One Time Only
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd(); // Done Drawing The Quad
rtri+=0.2f; // Increase The Rotation Variable For The Triangle ( NEW )
rquad-=0.15f; // Decrease The Rotation Variable For The Quad ( NEW )
glFlush();
}
// ReSizeGLScene ------------------------------------------------------------
GLvoid ReSizeGLScene(int Width, int Height)
{
glViewport (0, 0, (GLsizei) Width, (GLsizei) Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat) Width / (GLfloat) Height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}How can I make it refresh regularly. I guess this would be the beginning of an 'engine'?
Code:
// Function Prototype
GLvoid IdleFunc(GLvoid);
// Callback registration (along with glutDisplayFunc etc)
glutIdleFunc(IdleFunc);
// this is the actual idle function
GLvoid IdleFunc(GLvoid)
{
glutPostRedisplay();
}
First, if you've requested a double buffered context (GLUT_DOUBLE) then you need to swap the buffers to display anything. So use glutSwapBuffers() instead of glFlush(). The only reason you are seeing anything in your window is because GLUT forces a swap during window resize (the window is resized once upon creation.)
Then, to make this animate, you need to tell GLUT that you want to call DrawGLScene repeatedly. You could use glutIdleFunc(), or just add glutPostRedisplay() after glutSwapBuffers().
Then, to make this animate, you need to tell GLUT that you want to call DrawGLScene repeatedly. You could use glutIdleFunc(), or just add glutPostRedisplay() after glutSwapBuffers().
Thank you, very helpful, unfortunately glutSwapBuffers and post redisplay weren't yet mentioned in the tutorial, but now I know.
You can find an example on how to setup a window, an OpenGL context, capture Carbon events, all without GLUT, and load textures from JPG, PNG, TGA files, and play AIFF sounds with CoreAudio on the source code at: http://webpages.charter.net/utopiaplanet...Struct.dmg
All our games that used GLUT were converted not to use it, as we received quite a few complaints that the games were not rendering on certain hardware configurations running the lastest versions of OS X.
What, specifically, is broken? Full screen mode? On what hardware?
GLUT is just NSGL inside.
GLUT is just NSGL inside.
It seems to be something related to the fullscreen mode. The screen would show entirely black or just partially rendered. It was discussed on another thread a few weeks ago. I don't remember exactly what the cause is.
I didn't keep track of the hardware either. I just know that Danlabgames received quite a few complaints via e-mail or on Macupdate and VersionTracker about this issue and it was gone once we updated the games and got rid of GLUT.
I didn't keep track of the hardware either. I just know that Danlabgames received quite a few complaints via e-mail or on Macupdate and VersionTracker about this issue and it was gone once we updated the games and got rid of GLUT.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Low-level control of refresh rate | nfunit | 4 | 3,109 |
Dec 7, 2006 07:34 PM Last Post: OneSadCookie |
|
| WindowServer Causing Implicit Refresh? | Matrix | 4 | 2,841 |
Sep 5, 2004 02:06 PM Last Post: OneSadCookie |
|

