Draw #include "main.h" #include "draw.h" #include <math.h> /*----------------------- Variable definition------------------------------float fps; float angle; float dist; // Frames per second // Rotation angle of ship // Ship translation float float float float float float float float … // Rotationswinkel Erde um Sonne // Eigenrotation der Erde // Rotationswinkel Mond um Sonne mercuryRotationAngle; ownMercuryRotationAngle; venusRotationAngle; ownVenusRotationAngle; earthRotationAngle; ownEarthRotationAngle; moonRotationAngle; ownMoonRotationAngle; */ // Rotationswinkel Erde um Sonne // Eigenrotation der Erde // Rotationswinkel Mond um Sonne float PI = 3.1415965; /*----------------------------- Draw 3D Objects-------------------------*/ void draw3D() { glLoadIdentity(); // Load a new matrix camera(); // Calculate and set cameraview and cameraposition float lightPos[4] = { 0.0f, 0.0f , 0.0f, 1.0f }; glLightfv(GL_LIGHT0, GL_POSITION, lightPos); incAnimationVars(); // Licht-Position definieren // Licht-Position übergeben // Animations Variablen neu berechnen // Dem Schiff dynamisch folgen Aufgabe 2.5 //gluLookAt(20, 20, 20, 0, 0, dist, 0, 1, 0); //------------Draw Ship-----------------glBindTexture(GL_TEXTURE_2D, texture[0]); glEnable(GL_TEXTURE_2D); glPushMatrix(); calculateShip(); drawShip(); glDisable(GL_TEXTURE_2D); glPopMatrix(); // Texture für Schiff in TEXTURE Ausgabe // 2D Texturierung einschalten // Ship zeichnen //-------- Draw Sterne ------------// drawStars(); //-------- Draw Sonne -------------// drawSun(); //-------- Draw Planeten ----------// //glPolygonMode(GL_FRONT, GL_LINE); //glPolygonMode(GL_BACK, GL_LINE); drawMercury(mercuryRotationAngle, ownMercuryRotationAngle); drawVenus(venusRotationAngle, ownVenusRotationAngle); drawEarth(earthRotationAngle, ownEarthRotationAngle, moonRotationAngle, ownMoonRotationAngle); drawMars(marsRotationAngle, ownMarsRotationAngle); drawJupiter(jupiterRotationAngle, ownJupiterRotationAngle); drawSaturn(saturnRotationAngle, ownSaturnRotationAngle, titanRotationAngle, ownTitanRotationAngle); drawUranus(uranusRotationAngle, ownUranusRotationAngle); drawPluto(plutoRotationAngle, ownPlutoRotationAngle); //--------- Draw grid -------------// //////////////////// glPushMatrix(); glTranslatef( 0.0f, -3.0f, 0.0f); drawGrid(); glPopMatrix(); // // // // Save matrix Translate grid in the y-axis Draw a grid on the ground Restore matrix } /**************************************************/ /* Increment Animation Variables */ /**************************************************/ void incAnimationVars() { angle = angle + 60.0f/fps ; dist = dist + 10.0f/fps; earthRotationAngle ownEarthRotationAngle moonRotationAngle ownMoonRotationAngle ... if if if if if if … = = = = // deltaAngle = 60/fps; 60 degrees per second // 20 units per second earthRotationAngle + 10.0f/fps; ownEarthRotationAngle + 60.0f/fps; moonRotationAngle + 120.0f/fps; ownMoonRotationAngle + 120.0f/fps; (angle >= 360){angle = 0;}// reset angle, 360 degrees = 0 degrees (dist > 100){dist = -50;} (earthRotationAngle >= 360){earthRotationAngle = 0;} (ownEarthRotationAngle >= 360){ownEarthRotationAngle = 0;} (moonRotationAngle >= 360){moonRotationAngle = 0;} (ownMoonRotationAngle >= 360){ownMoonRotationAngle = 0;} } /*---------------------------- Draw Grid -------------------------------- */ void drawGrid() { GLfloat mat_ambient_diffuse[]= {0.0f, 0.0f, 0.35f}; GLfloat mat_specular[]= {0.5f, 0.5f, 0.5f}; GLfloat mat_shininess[]= {50.0}; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_ambient_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); for(float i = -50; i <= 50; i += 1) { glBegin(GL_LINES); glVertex3f(-50, 0, i); glVertex3f(50, 0, i); glVertex3f(i, 0, -50); glVertex3f(i, 0, 50); glEnd(); // Draw a 1x1 grid along the X and Z axis // Start drawing some lines // Do the horizontal lines (along the X) // Do the vertical lines (along the Z) } } /* -----------------------------Draw Screen ------------------------------ void draw_screen( ) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and the depth buffer draw3D(); countFrames(); SDL_GL_SwapBuffers( ); // Swap the buffers } /*------------------ Count Frames void countFrames() { ------------------------------------ */ */ static double thisFrame, lastFrame; static int count = 0; // Define placeholders for frametimes // Define placeholder to count frames thisFrame = SDL_GetTicks(); count++; // Get time // Count frames if((thisFrame-lastFrame) > 10){ // Check if 10 millis are over fps = 1000.0f*(float)count/(float)(thisFrame-lastFrame); // Calculate frames per second lastFrame=thisFrame; // Set frametime value count = 0; // Reset count value } } Main #include "main.h" #include "draw.h" /*------------------------- Variable definition---------------------------- */ KeyFlag keyFlag; int width = 1024; int height = 768; // Placeholder for pressed keys // Dimensions of our window bool fullscreen = false; GLuint texture[14]; // Fullscreen or windowed mode /*------------------------- Exit------------------------------------------ */ void quit_program( int code ) { SDL_Quit( ); exit( code ); } // Quit SDL and restore previous video settings // Exit program /*------------------------- Poll Keyevents -----------------------------void process_events( ) { SDL_Event event; // Used for camera control ////////////////////////// while( SDL_PollEvent( &event ) ) { switch( event.type ) { case SDL_QUIT: quit_program( 0 ); break; case SDL_KEYDOWN: // SDL event placeholder // Grab all the events off the queue // Handle quit requests (like Ctrl-c) // Handle each keydown switch( event.key.keysym.sym ) { case SDLK_ESCAPE: quit_program( 0 ); // Quit program break; case SDLK_RIGHT: keyFlag.right = true; break; … */ // Set keyflags default: break; } break; case SDL_KEYUP: switch( event.key.keysym.sym ) { case SDLK_RIGHT: keyFlag.right = false; break; … // Handle each keyup // Set keyflags case SDL_VIDEORESIZE: width = event.resize.w; height = event.resize.h; SDL_Quit(); init_SDL(); init_OpenGL(); break; // // // // // // Handle resize events Set event width value Set event height vlaue Quit SDL Restart SDL Restart OpenGL default: break; } break; } } } bool init_OpenGL( ) { float ratio = (float) width / (float) height; // Calculate and store the aspect ratio of the display glMatrixMode( GL_PROJECTION ); gluPerspective( 60.0, ratio, 0.1, 1024.0 ); glMatrixMode( GL_MODELVIEW ); glEnable(GL_DEPTH_TEST); // // // // Change to the projection matrix Set view perspective Change to the modelview matrix Enable hidden surface removal glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); createTexture(texture, "Textures/MeineTextur.bmp", 0); createTexture(texture, "Textures/Sun.bmp", 1); createTexture(texture, "Textures/Mercury.bmp", 2); … float lightAmbient[] = {0.2f, 0.2f, 0.2f}; float lightDiffuse[] = {0.8f, 0.8f, 0.8f}; float lightSpecular[] = {1.0f, 1.0f, 1.0f}; initStars(); glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular); glShadeModel(GL_SMOOTH); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); return true; } bool init_SDL() { // Enable smooth shading // Turn on light0 // Enable lighting const SDL_VideoInfo* info = NULL; // Information about the current video settings int bpp = 0; int flags = 0; // Color depth in bits of our window // Flags we will pass into SDL_SetVideoMode if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) // First, initialize SDL's video subsystem (video only) { fprintf( stderr, "Video initialization failed: %s\n", SDL_GetError( ) ); quit_program( 1 ); // Failed, exit } info = SDL_GetVideoInfo( ); // Get some video information if( !info ) // This should probably never happen { fprintf( stderr, "Video query failed: %s\n", SDL_GetError( ) ); return false; } bpp = info->vfmt->BitsPerPixel; SDL_GL_SetAttribute( blue color-part SDL_GL_SetAttribute( SDL_GL_SetAttribute( SDL_GL_SetAttribute( SDL_GL_SetAttribute( doublebuffering // Get color depth SDL_GL_RED_SIZE, 8 ); // Sets the color-depth of the red, green and SDL_GL_GREEN_SIZE, 8 ); SDL_GL_BLUE_SIZE, 8 ); SDL_GL_DEPTH_SIZE, 16 ); SDL_GL_DOUBLEBUFFER, 1 ); // to 8bit (standard today) flags = SDL_OPENGL; flags = flags | SDL_RESIZABLE; if (fullscreen) { flags = flags | SDL_FULLSCREEN; } // Set depth buffer // Sets wether to enable or disable // Set flags for SDL OpenGL // Set flag for fullscreen or windowed mode if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 ) // Set the video mode { fprintf( stderr, "Video mode set failed: %s\n", SDL_GetError( ) ); return false; } return true; } int main( int argc, char* argv[] ) { if(!init_SDL()) // If intialising of SDL fails -> quit the program with error code 1 {quit_program( 1 );} if(!init_OpenGL()) // If intialising of OpenGL fails -> quit the program with error code 1 { quit_program( 1 );} while(true) { draw_screen(); process_events( ); } quit_program(0); while condition... } // Repeat forever // Draw your graphics // Process any ocuring events // You shouldn't get here. Only if someone changes the Camera #include "main.h" #include "draw.h" void camera() { static bool initCamera = true; static GLfloat cam_matrix[16]; if (initCamera){ // Only on the fist loop glPushMatrix(); glLoadIdentity(); // load identity matrix gluLookAt(0, 0, 20, 0, 0, 0, 0, 1, 0); glGetFloatv(GL_MODELVIEW_MATRIX, cam_matrix); // save glPopMatrix(); initCamera = false; } //glLoadMatrixf(cam_matrix); glLoadIdentity(); if(keyFlag.down && !keyFlag.ctrl) { glTranslatef(0.0f, 0.0f, -0.1f); } if(keyFlag.up && !keyFlag.ctrl) { glTranslatef(0.0f, 0.0f, 0.1f); } … if(keyFlag.down && keyFlag.ctrl) { glRotatef(2.0f, 1.0f, 0.0f, 0.0f); } … glMultMatrixf(cam_matrix); glGetFloatv(GL_MODELVIEW_MATRIX, cam_matrix); } Draw Planet #include "main.h" #include "draw.h" void drawPlanet( float radius, int textureID, GLfloat mat_ambient[], GLfloat mat_diffuse[], GLfloat mat_specular[], GLfloat mat_shininess[], GLfloat mat_emission[] ) { glBindTexture(GL_TEXTURE_2D, texture[textureID]); glEnable(GL_TEXTURE_2D); glMaterialfv(GL_FRONT_AND_BACK, glMaterialfv(GL_FRONT_AND_BACK, glMaterialfv(GL_FRONT_AND_BACK, glMaterialfv(GL_FRONT_AND_BACK, glMaterialfv(GL_FRONT_AND_BACK, GLUquadricObj *q; q = gluNewQuadric(); GL_AMBIENT, mat_ambient); GL_DIFFUSE, mat_diffuse); GL_SPECULAR, mat_specular); GL_SHININESS, mat_shininess); GL_EMISSION, mat_emission); // Variable for a quadric object // Create a new quadric gluQuadricNormals(q, GL_SMOOTH); gluQuadricTexture(q, true); gluSphere(q, radius, 32, 16); // Draw the sphere gluDeleteQuadric(q); glDisable(GL_TEXTURE_2D);} Draw Earth void drawEarth( float RotationAngle, float ownRotationAngle, float trabantRotationAngle, float ownTrabantRotationAngle) { GLfloat mat_ambient[]= {0.2f, 0.2f, 0.2f}; GLfloat mat_diffuse[]= {0.8f, 0.8f, 0.8f}; GLfloat mat_specular[]= {0.5, 0.5, 0.5}; GLfloat mat_shininess[]= {10.0}; GLfloat mat_emission[]= {0.0, 0.0, 0.0}; glPushMatrix(); float earthX = sin( RotationAngle* (PI/180) ) * 6.0f; float earthZ = cos( RotationAngle* (PI/180) ) * 6.0f; glTranslatef(earthX, 0.0f, earthZ); glRotatef(23.0f, 0.0f, 0.0f, 1.0f ); glPushMatrix(); glRotatef(ownRotationAngle, 0.0f, 1.0f, 0.0f glRotatef(-90.0f, 1.0f, 0.0f, 0.0f ); ); drawPlanet(0.3f, 4, mat_ambient, mat_diffuse, mat_specular, mat_shininess, mat_emission ); glPopMatrix(); drawMoon(trabantRotationAngle, ownTrabantRotationAngle); glPopMatrix(); }