int main() { //The first part of this code will by now be pretty standard to you: cout << "This is Tut4-lists2 example running" << endl; //Step 1 is to initialize SDL. Absolutely critical that your SDL application starts with this! if (SDL_Init(SDL_INIT_VIDEO) != 0){ cout << "Error initializing SDL" << endl; return -1; } //Step 2 is to get a screen/window on which we can do some graphics. //create a Videoclass to control the video with SDLVideoclass SDLmain(640,480); if (SDLmain.InitVideo()){ cout << "Video up and running" << endl; }else{ cout << "Error initializing Video" << endl; return 0; } //get the events up and running SDLEventclass SDLevent; //some GL setup routines glEnable(GL_DEPTH_TEST); //enable depth buffer glShadeModel(GL_SMOOTH); //use smooth shading mode. //setup camera: glMatrixMode(GL_PROJECTION); gluPerspective(45.0f,1.33,0.1f,100.0f); //setup the camera //setup modelview matrix: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClearColor(0.0,0.0,0.2,0.0); //Ok lets start with the display list: makelists(); GLfloat rot = 0.0; Uint32 curticks,prevticks; GLfloat timescale; curticks = SDL_GetTicks(); //the heights of the piramids GLfloat height1,height2; int mod2; height1 = 2.0; height2 = 2.0; mod2 = FALSE; cout << "Use Left and Right to rotate piramids" << endl; cout << "Change height of piramid 1 with A and Z" << endl; cout << "Change height of piramid 2 with S and X" << endl; //main loop: while (!SDLevent.MustQuit()){ SDLevent.ProcessEvent(); //main loop 1 - events //timescale: prevticks = curticks; curticks = SDL_GetTicks(); timescale = GLfloat(curticks-prevticks)/1000.0; //check rotation keys if (SDLevent.GetKey(SDLK_LEFT)) rot += timescale*90; if (SDLevent.GetKey(SDLK_RIGHT)) rot -= timescale*90; //check height change keys //piramid1 - use immediate recompile of displaylist method if (SDLevent.GetKey(SDLK_a)) if (height1 < 6.0){ height1 += timescale*1.5; updatelist(displaylists,height1); } if (SDLevent.GetKey(SDLK_z)) if (height1 > 0.0){ height1 -= timescale*1.5; updatelist(displaylists,height1); } //piramid2 - recompile only if needed with GL_COMPILE_AND_EXECUTE method if (SDLevent.GetKey(SDLK_s)) if (height2 < 6.0){ height2 += timescale*1.5; mod2 = TRUE; } if (SDLevent.GetKey(SDLK_x)) if (height2 > 0.0){ height2 -= timescale*1.5; mod2 = TRUE; } //main loop 2 - graphics glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //we draw 2 piramids: //piramid 1: to the left glLoadIdentity(); glTranslatef(-5.0,0.0,-20.0); glRotatef(rot,0.0,1.0,0.0); //call the display list to draw the piramid rather than the procedure. glCallList(displaylists); //piramid 2: to the right glLoadIdentity(); glTranslatef(5.0,0.0,-20.0); glRotatef(-rot,0.0,1.0,0.0); //call the display list to draw the piramid rather than the procedure. if (mod2){ //the height2 changed! glNewList(displaylists+1,GL_COMPILE_AND_EXECUTE); drawpiramid(height2); glEndList(); }else{ glCallList(displaylists+1); } SDL_GL_SwapBuffers(); } SDL_Quit(); return 0; }
int main() { cout << "This is Tut6-fog1 example running" << endl; //Step 1 is to initialize SDL. Absolutely critical that your SDL application starts with this! if (SDL_Init(SDL_INIT_VIDEO) != 0){ cout << "Error initializing SDL" << endl; return -1; } //Step 2 is to get a screen/window on which we can do some graphics. //create a Videoclass to control the video with SDLVideoclass SDLmain(640,480); if (SDLmain.InitVideo()){ cout << "Video up and running" << endl; }else{ cout << "Error initializing Video" << endl; return 0; } //get the events up and running SDLEventclass SDLevent; setup(); GLfloat rotx,roty; GLfloat distance; rotx = 0; roty = 0; distance = 10; Uint32 curticks,prevticks; GLfloat timescale; curticks = SDL_GetTicks(); cout << "Move the cube further and closer with the UP/DOWN arrow keys" << endl; cout << "Press ESC to quit" << endl; while (!SDLevent.MustQuit()){ SDLevent.ProcessEvent(); //events: //timescale prevticks = curticks; curticks = SDL_GetTicks(); timescale = GLfloat(curticks-prevticks)/1000.0; //rotation rotx += timescale*60; roty += timescale*15; if (SDLevent.GetKey(SDLK_UP)) if (distance < 21) distance += timescale*4; if (SDLevent.GetKey(SDLK_DOWN)) if (distance > 4) distance -= timescale*4; //graphics: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //setup modelview matrix: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-distance); glRotatef(rotx,1,0,0); glRotatef(roty,0,1,0); drawbox(); SDL_GL_SwapBuffers(); } SDL_Quit(); return 0; }