/////////////////////////////////////////////////////////////////////// // Draw random inhabitants and the rotating torus/sphere duo void DrawInhabitants(GLint nShadow) { static GLfloat yRot = 0.0f; // Rotation angle for animation GLint i; if(nShadow == 0) { yRot += 0.5f; glColor4f(1.0f, 1.0f, 1.0f, 1.0f); } else glColor4f(0.00f, 0.00f, 0.00f, .6f); // Shadow color // Draw the randomly located spheres glBindTexture(GL_TEXTURE_2D, textureObjects[SPHERE_TEXTURE]); for(i = 0; i < NUM_SPHERES; i++) { glPushMatrix(); spheres[i].ApplyActorTransform(); if(iMethod == 0) gltDrawSphere(0.1f, 40, 20); else glCallList(sphereList); glPopMatrix(); } glPushMatrix(); glTranslatef(0.0f, 0.1f, -2.5f); glPushMatrix(); glRotatef(-yRot * 2.0f, 0.0f, 1.0f, 0.0f); glTranslatef(1.0f, 0.0f, 0.0f); if(iMethod == 0) gltDrawSphere(0.1f, 40, 20); else glCallList(sphereList); glPopMatrix(); if(nShadow == 0) { // Torus alone will be specular glMaterialfv(GL_FRONT, GL_SPECULAR, fBrightLight); } glRotatef(yRot, 0.0f, 1.0f, 0.0f); glBindTexture(GL_TEXTURE_2D, textureObjects[TORUS_TEXTURE]); if(iMethod == 0) gltDrawTorus(0.35, 0.15, 61, 37); else glCallList(torusList); glMaterialfv(GL_FRONT, GL_SPECULAR, fNoLight); glPopMatrix(); }
// Called to draw scene void RenderScene(void) { // Clear the window glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); frameCamera.ApplyCameraTransform(); // Move the camera about // Sky Box is manually textured glActiveTexture(GL_TEXTURE0); glDisable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE1); glEnable(GL_TEXTURE_CUBE_MAP); glDisable(GL_TEXTURE_GEN_S); glDisable(GL_TEXTURE_GEN_T); glDisable(GL_TEXTURE_GEN_R); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); DrawSkyBox(); // Use texgen to apply cube map glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glEnable(GL_TEXTURE_GEN_R); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glActiveTexture(GL_TEXTURE0); glEnable(GL_TEXTURE_2D); glPushMatrix(); glTranslatef(0.0f, 0.0f, -3.0f); glActiveTexture(GL_TEXTURE1); glMatrixMode(GL_TEXTURE); glPushMatrix(); // Invert camera matrix (rotation only) and apply to // texture coordinates M3DMatrix44f m, invert; frameCamera.GetCameraOrientation(m); m3dInvertMatrix44(invert, m); glMultMatrixf(invert); glColor3f(1.0f, 1.0f, 1.0f); gltDrawSphere(0.75f, 41, 41); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glPopMatrix(); // Do the buffer Swap glutSwapBuffers(); }
void makeplanets(double red, double green, double blue, double PLANET_INCLINATION, double PLANET_ORBIT, double PLANET_RADIUS, double EARTHRADIUS, double planet_period, float speed, float rotation, float tilt, float EARTH_DEGREES ) { // glEnable(GL_TEXTURE_CUBE_MAP); glEnable(GL_TEXTURE_2D); GLUquadric *orbit = gluNewQuadric(); gluQuadricDrawStyle(orbit, GLU_LINE); GLUquadric *planet = gluNewQuadric(); glColor3f(255,255,255); //eod * 365 = beta Earth orbit degree per frame glPushMatrix(); glRotatef(PLANET_INCLINATION,1,0,0); glRotatef(90,1,0,0); gluDisk(orbit,PLANET_ORBIT*AU,PLANET_ORBIT*AU,30,30); glColor3ub(red,green,blue); glPopMatrix(); glRotatef(speed*.05,0,1,0); //sets the planets inclination relative to the Earth glRotatef(PLANET_INCLINATION,0,0,1); //****sets the planets rotation****/ glRotatef(-90,1,0,0);//xaxis //translate to position //rotatez //sets the planets distatance from the sun relative to the Earth glTranslatef(PLANET_ORBIT*AU,0,0); glRotatef(tilt,1,0,0); glRotatef(rotation,0,0,1); //Creates planet with Earth Radius gltDrawSphere(PLANET_RADIUS*EARTH_RADIUS,30,30); //glRotatef(rotation,1,0,0); //glRotatef((cos (PI/90)),(sin (PI/90)),0,0 );//alpha //glRotatef(beta,1,0,0); //gluSphere(planet,PLANET_RADIUS*EARTH_RADIUS,30,30); glDisable(GL_TEXTURE_2D); //sets the planets inclination relative to the Earth //glRotatef(PLANET_INCLINATION,0,0,1); }
/////////////////////////////////////////////////////////////////////////////// // Setup. Create font/bitmaps, load textures, create display lists void SetupRC(HDC hDC) { M3DVector3f vPoints[3] = {{ 0.0f, -0.4f, 0.0f }, { 10.0f, -0.4f, 0.0f }, { 5.0f, -0.4f, -5.0f } }; int iSphere; int i; // Setup the Font characteristics HFONT hFont; LOGFONT logfont; logfont.lfHeight = -20; logfont.lfWidth = 0; logfont.lfEscapement = 0; logfont.lfOrientation = 0; logfont.lfWeight = FW_BOLD; logfont.lfItalic = FALSE; logfont.lfUnderline = FALSE; logfont.lfStrikeOut = FALSE; logfont.lfCharSet = ANSI_CHARSET; logfont.lfOutPrecision = OUT_DEFAULT_PRECIS; logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS; logfont.lfQuality = DEFAULT_QUALITY; logfont.lfPitchAndFamily = DEFAULT_PITCH; strcpy(logfont.lfFaceName,"Arial"); // Create the font and display list hFont = CreateFontIndirect(&logfont); SelectObject (hDC, hFont); //Create display lists for glyphs 0 through 128 nFontList = glGenLists(128); wglUseFontBitmaps(hDC, 0, 128, nFontList); DeleteObject(hFont); // Don't need original font anymore // Grayish background glClearColor(fLowLight[0], fLowLight[1], fLowLight[2], fLowLight[3]); // Clear stencil buffer with zero, increment by one whenever anybody // draws into it. When stencil function is enabled, only write where // stencil value is zero. This prevents the transparent shadow from drawing // over itself glStencilOp(GL_INCR, GL_INCR, GL_INCR); glClearStencil(0); glStencilFunc(GL_EQUAL, 0x0, 0x01); // Cull backs of polygons glCullFace(GL_BACK); glFrontFace(GL_CCW); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); // Setup light parameters glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fNoLight); glLightfv(GL_LIGHT0, GL_AMBIENT, fLowLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, fBrightLight); glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); // Calculate shadow matrix M3DVector4f pPlane; m3dGetPlaneEquation(pPlane, vPoints[0], vPoints[1], vPoints[2]); m3dMakePlanarShadowMatrix(mShadowMatrix, pPlane, fLightPos); // Mostly use material tracking glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glMateriali(GL_FRONT, GL_SHININESS, 128); // Randomly place the sphere inhabitants for(iSphere = 0; iSphere < NUM_SPHERES; iSphere++) { // Pick a random location between -20 and 20 at .1 increments spheres[iSphere].SetOrigin((float)((rand() % 400) - 200) * 0.1f, 0.0f, (float)((rand() % 400) - 200) * 0.1f); } // Set up texture maps glEnable(GL_TEXTURE_2D); glGenTextures(NUM_TEXTURES, textureObjects); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // Load teach texture for(i = 0; i < NUM_TEXTURES; i++) { GLbyte *pBytes; GLint iWidth, iHeight, iComponents; GLenum eFormat; glBindTexture(GL_TEXTURE_2D, textureObjects[i]); // Load this texture map pBytes = gltLoadTGA(szTextureFiles[i], &iWidth, &iHeight, &iComponents, &eFormat); gluBuild2DMipmaps(GL_TEXTURE_2D, iComponents, iWidth, iHeight, eFormat, GL_UNSIGNED_BYTE, pBytes); free(pBytes); // Trilinear mipmapping glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); } // Get window position function pointer if it exists glWindowPos2i = (PFNGLWINDOWPOS2IPROC)wglGetProcAddress("glWindowPos2i"); // Get swap interval function pointer if it exists wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT"); if(wglSwapIntervalEXT != NULL && startupOptions.bVerticalSync == TRUE) wglSwapIntervalEXT(1); // If multisampling was available and was selected, enable if(startupOptions.bFSAA == TRUE && startupOptions.nPixelFormatMS != 0) glEnable(GL_MULTISAMPLE_ARB); // If sepearate specular color is available, make torus shiney if(gltIsExtSupported("GL_EXT_separate_specular_color")) glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); // Initialize the timers QueryPerformanceFrequency(&CounterFrequency); QueryPerformanceCounter(&FPSCount); CameraTimer = FPSCount; // Build display lists for the torus and spheres // (You could do one for the ground as well) lTorusList = glGenLists(2); lSphereList = lTorusList + 1; glNewList(lTorusList, GL_COMPILE); gltDrawTorus(0.35f, 0.15f, 61, 37); glEndList(); glNewList(lSphereList, GL_COMPILE); gltDrawSphere(0.3f, 31, 16); glEndList(); }
////////////////////////////////////////////////////////////////// // This function does any needed initialization on the rendering // context. void SetupRC() { M3DVector3f vPoints[3] = {{ 0.0f, -0.4f, 0.0f }, { 10.0f, -0.4f, 0.0f }, { 5.0f, -0.4f, -5.0f }}; int iSphere; int i; // Grayish background glClearColor(fLowLight[0], fLowLight[1], fLowLight[2], fLowLight[3]); // Clear stencil buffer with zero, increment by one whenever anybody // draws into it. When stencil function is enabled, only write where // stencil value is zero. This prevents the transparent shadow from drawing // over itself glStencilOp(GL_INCR, GL_INCR, GL_INCR); glClearStencil(0); glStencilFunc(GL_EQUAL, 0x0, 0x01); // Cull backs of polygons glCullFace(GL_BACK); glFrontFace(GL_CCW); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glEnable(GL_MULTISAMPLE_ARB); // Setup light parameters glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fNoLight); glLightfv(GL_LIGHT0, GL_AMBIENT, fLowLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, fBrightLight); glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); // Calculate shadow matrix M3DVector4f pPlane; m3dGetPlaneEquation(pPlane, vPoints[0], vPoints[1], vPoints[2]); m3dMakePlanarShadowMatrix(mShadowMatrix, pPlane, fLightPos); // Mostly use material tracking glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glMaterialfv(GL_FRONT, GL_SPECULAR, fBrightLight); glMateriali(GL_FRONT, GL_SHININESS, 128); // Randomly place the sphere inhabitants for(iSphere = 0; iSphere < NUM_SPHERES; iSphere++) { // Pick a random location between -20 and 20 at .1 increments spheres[iSphere].SetOrigin(((float)((rand() % 400) - 200) * 0.1f), 0.0, (float)((rand() % 400) - 200) * 0.1f); } // Set up texture maps glEnable(GL_TEXTURE_2D); glGenTextures(NUM_TEXTURES, textureObjects); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); for(i = 0; i < NUM_TEXTURES; i++) { GLbyte *pBytes; GLint iWidth, iHeight, iComponents; GLenum eFormat; glBindTexture(GL_TEXTURE_2D, textureObjects[i]); // Load this texture map pBytes = gltLoadTGA(szTextureFiles[i], &iWidth, &iHeight, &iComponents, &eFormat); gluBuild2DMipmaps(GL_TEXTURE_2D, iComponents, iWidth, iHeight, eFormat, GL_UNSIGNED_BYTE, pBytes); free(pBytes); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); } // Create display lists groundList = glGenLists(3); sphereList = groundList + 1; torusList = sphereList + 1; // Create sphere display list glNewList(sphereList, GL_COMPILE); gltDrawSphere(0.1f, 40, 20); glEndList(); // Create torus display list glNewList(torusList, GL_COMPILE); gltDrawTorus(0.35, 0.15, 61, 37); glEndList(); // Create the ground display list glNewList(groundList, GL_COMPILE); DrawGround(); glEndList(); }
void RenderScene(void) { GLUquadric *orbitQuad = gluNewQuadric(); gluQuadricDrawStyle(orbitQuad , GLU_LINE ); GLUquadric *planetQuad = gluNewQuadric(); GLUquadricObj *pObj; // Quadric Object pObj = gluNewQuadric(); gluQuadricNormals(pObj, GLU_SMOOTH); GLfloat horizontalMovement=1; GLfloat verticalMovement=0; horizontalMovement=cos(xRotationAngle*PI/180); verticalMovement=-sin(xRotationAngle*PI/180); if (lookDown) { xRotationAngle+=1; if (xRotationAngle>=90) xRotationAngle=90; } if (lookUp) { xRotationAngle-=1; if (xRotationAngle<=-90) xRotationAngle=-90; } if (lookRight) { yRotationAngle+=1; if (yRotationAngle>=360) yRotationAngle=0; } if (lookLeft) { yRotationAngle-=1; if (yRotationAngle<=-360) yRotationAngle=0; } if (walkForward) { zTranslation+=cos(yRotationAngle*PI/180)*horizontalMovement; xTranslation-=sin(yRotationAngle*PI/180)*horizontalMovement; yTranslation-=verticalMovement; } if (walkBackward) { zTranslation-=cos(yRotationAngle*PI/180)*horizontalMovement; xTranslation+=sin(yRotationAngle*PI/180)*horizontalMovement; yTranslation+=verticalMovement; } if (strafeRight) { zTranslation+=cos((yRotationAngle+90)*PI/180); xTranslation-=sin((yRotationAngle+90)*PI/180); } if (strafeLeft) { zTranslation-=cos((yRotationAngle+90)*PI/180); xTranslation+=sin((yRotationAngle+90)*PI/180); } if (setback) { zTranslation=0; xTranslation=0; yTranslation=0; } // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Reset Model view matrix stack glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(xRotationAngle,1,0,0); glRotatef(zRotationAngle,0,0,1); glRotatef(yRotationAngle,0,1,0); glDisable(GL_LIGHTING); glBindTexture(GL_TEXTURE_2D, textureObjects[CUBE_MAP_TEX]); DrawSkyBox(); glEnable(GL_LIGHTING); glTranslatef(xTranslation,yTranslation,zTranslation); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glScalef(10,10,10); glDisable(GL_LIGHTING); //sun glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glColor3f(1,1,0); glBindTexture(GL_TEXTURE_2D, textureObjects[SUN]); gltDrawSphere(0.65,100,100);//radius .15 GLfloat lightPos[] = {0.0f,0.0f,0.0f,1.0f}; glLightfv(GL_LIGHT0, GL_POSITION, lightPos); glEnable(GL_LIGHTING); //glRotatef(90,1,0,0); glPushMatrix(); glBindTexture(GL_TEXTURE_2D, textureObjects[ASTERIODS]); //glutWireTorus(2,6,0,4); gltDrawTorus(6,2,6,2); glPopMatrix(); //glRotatef(-90,1,0,0); //Draw Jupiter - orange glPushMatrix(); glBindTexture(GL_TEXTURE_2D, textureObjects[JUPITER]); makeplanets(249,100,0,JUPITER_INCLINATION,JUPITER_ORBIT,JUPITER_RADIUS,EARTH_RADIUS,JUPITER_PERIOD, jupiter_speed, jupiter_rotation, JUPITER_TILT, EARTH_DEGREES_PER_FRAME); glPopMatrix(); //Draw Venus - greenish blue glPushMatrix(); glBindTexture(GL_TEXTURE_2D, textureObjects[VENUS]); makeplanets(43,146,164,VENUS_INCLINATION,VENUS_ORBIT,VENUS_RADIUS,EARTH_RADIUS,VENUS_PERIOD, venus_speed, venus_rotation, VENUS_TILT, EARTH_DEGREES_PER_FRAME); glPopMatrix(); //Draw Mercury - graywhite glPushMatrix(); glBindTexture(GL_TEXTURE_2D, textureObjects[MERCURY]); makeplanets(191,164,142,MERCURY_INCLINATION,MERCURY_ORBIT,MERCURY_RADIUS,EARTH_RADIUS,MERCURY_PERIOD, mercury_speed, mercury_rotation, MERCURY_TILT, EARTH_DEGREES_PER_FRAME); glPopMatrix(); //Draw Uranus - dark blue glPushMatrix(); glBindTexture(GL_TEXTURE_2D, textureObjects[URANUS]); makeplanets(120,112,224,URANUS_INCLINATION,URANUS_ORBIT,URANUS_RADIUS,EARTH_RADIUS,URANUS_PERIOD, uranus_speed, uranus_rotation, URANUS_TILT, EARTH_DEGREES_PER_FRAME); glPopMatrix(); //Draw Neptune -light blue glPushMatrix(); glBindTexture(GL_TEXTURE_2D, textureObjects[NEPTUNE]); makeplanets(53,45,206,NEPTUNE_INCLINATION,NEPTUNE_ORBIT,NEPTUNE_RADIUS,EARTH_RADIUS,NEPTUNE_PERIOD, neptune_speed, neptune_rotation, NEPTUNE_TILT, EARTH_DEGREES_PER_FRAME); glPopMatrix(); //Draw Mars - pink glPushMatrix(); glBindTexture(GL_TEXTURE_2D, textureObjects[MARS]); makeplanets(255,43,43,MARS_INCLINATION,MARS_ORBIT,MARS_RADIUS,EARTH_RADIUS,MARS_PERIOD, mars_speed, mars_rotation, MARS_TILT, EARTH_DEGREES_PER_FRAME); glPopMatrix(); //Draw Saturn - yellow whitegl glBindTexture(GL_TEXTURE_2D, textureObjects[SATURN]); glPushMatrix(); makeplanets(255,255,145,SATURN_INCLINATION,SATURN_ORBIT,SATURN_RADIUS,EARTH_RADIUS,SATURN_PERIOD, saturn_speed, saturn_rotation, SATURN_TILT, EARTH_DEGREES_PER_FRAME); glPopMatrix(); //draw the Earth - BLUE GREEN glPushMatrix(); glBindTexture(GL_TEXTURE_2D, textureObjects[EARTH]); makeplanets(43,146,164,EARTH_INCLINATION,EARTH_ORBIT,EARTH_RADIUS,EARTH_RADIUS,EARTH_PERIOD, earth_speed, earth_rotation, EARTH_TILT, EARTH_DEGREES_PER_FRAME); glRotatef(-90,1,0,0); glBindTexture(GL_TEXTURE_2D, textureObjects[MOON]); makeplanets(191,164,142,MOON_INCLINATION,MOON_ORBIT,MOON_RADIUS,EARTH_RADIUS,MOON_PERIOD, moon_speed, moon_rotation, MOON_TILT, EARTH_DEGREES_PER_FRAME); //glBindTexture(GL_TEXTURE_2D, textureObjects[CLOUDS]); //gltDrawSphere(0.8,40,40); glPopMatrix(); // Flush drawing commands glutSwapBuffers(); // Set color shading model to flat glShadeModel(GL_SMOOTH); }