Created
November 22, 2011 06:10
-
-
Save davidwparker/1385020 to your computer and use it in GitHub Desktop.
OpenGL Screencast 16: Animation part 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "screencasts.h" | |
| /* | |
| * main() | |
| * ---- | |
| * Start up GLUT and tell it what to do | |
| */ | |
| int main(int argc,char* argv[]) | |
| { | |
| initializeGlobals(); | |
| /* screencast specific variables */ | |
| windowName = "OpenGL screenscasts 16: Animation part 1"; | |
| screencastID = 16; | |
| toggleAxes = 0; | |
| glutInit(&argc,argv); | |
| glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); | |
| glutInitWindowSize(windowWidth,windowHeight); | |
| glutInitWindowPosition(450,350); | |
| glutCreateWindow(windowName); | |
| glutDisplayFunc(display); | |
| glutReshapeFunc(displayReshape); | |
| glutKeyboardFunc(windowKey); | |
| glutSpecialFunc(windowSpecial); | |
| // glutIdleFunc. Executed when there is no other event to be handled (event queue is empty). | |
| //glutIdleFunc(windowIdle); | |
| // glutVisibilityFunc. Sets the visibility callback for the current window | |
| // GLUT considers a window visible if any pixel of the window is visible | |
| // or any pixel of descendant window is visible on screen. | |
| // state = GLUT_NOT_VISIBLE | GLUT_VISIBLE | |
| glutVisibilityFunc(windowVisible); | |
| initializeTextures(); | |
| redisplayAll(); | |
| glutMainLoop(); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "screencasts.h" | |
| /* redraw the screen approximately every 10ms */ | |
| void windowIdle(void) | |
| { | |
| static int lastTime = 0; | |
| // glutGet retrieves OpenGL state. See list at http://www.opengl.org/documentation/specs/glut/spec3/node70.html | |
| int time = glutGet(GLUT_ELAPSED_TIME); | |
| double t = time/1000.0; | |
| if ((lastTime == 0 || time >= lastTime + 10) && toggleAnimation) { | |
| lastTime = time; | |
| // do some animation here | |
| if (toggleLight) lightPh = fmod(90*t,360.0); | |
| rotateCube(); | |
| redisplayAll(); | |
| } | |
| } | |
| /* If window is visible, set idle function */ | |
| void windowVisible(int visible) | |
| { | |
| glutIdleFunc(visible == GLUT_VISIBLE ? windowIdle : NULL); | |
| } | |
| void rotateCube(void) | |
| { | |
| if (cubeRotation <= 360) | |
| cubeRotation += 1; | |
| else | |
| cubeRotation = 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment