src/SkyDome.cpp

Go to the documentation of this file.
00001 /* Crown and Cutlass
00002  * Sky Dome Object Code
00003  */
00004 
00005 #include <string>
00006 #include "Log.h"
00007 #include "ccmath.h"
00008 #include "Texture.h"
00009 #include "SkyDome.h"
00010 
00011 using namespace std;
00012 
00013 SkyDome::SkyDome(float radius, int slices, int stacks, string textureName) {
00014   m_colorTexture = new Texture(textureName);
00015 
00016   m_list = glGenLists(1);
00017   glNewList(m_list, GL_COMPILE);
00018 
00019   DrawDome(radius, slices, stacks);
00020 
00021   glEndList();
00022 }
00023 
00024 SkyDome::~SkyDome() {
00025   // Free the list
00026   glDeleteLists(m_list, 1);
00027 
00028   delete m_colorTexture;
00029 }
00030 
00031 void SkyDome::DrawDome(float radius, int slices, int stacks) {
00032   const float centerRad = degreesToRadians(5);
00033   int i;
00034 
00035   GLfloat material[4];
00036   float tempX, tempY, tempZ;
00037 
00038   float dSlice = TWO_PI / slices;
00039   float dStack = (M_PI_2 - centerRad) / (stacks - 1);
00040   //float maxY = getHeight(radius, 0);
00041 
00042   material[0] = 1.0;
00043   material[1] = 1.0;
00044   material[2] = 1.0;
00045   material[3] = 1.0;
00046 
00047   glDepthMask(GL_FALSE);
00048 
00049   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, material);
00050   m_colorTexture->BindTexture();
00051 
00052   glNormal3f(0, 1, 0);
00053 
00054   for (i = 0; i < slices; ++i) {
00055     glBegin(GL_TRIANGLE_STRIP);
00056     for (int j = 0; j < stacks; ++j) {
00057       tempX = GetX((i+1) * dSlice, (j * dStack) + centerRad, radius);
00058       tempY = GetHeight(radius, (j * dStack) + centerRad);
00059       tempZ = GetZ((i+1) * dSlice, (j * dStack) + centerRad, radius);
00060       //glTexCoord2f(tempY / maxY, 0);
00061       glTexCoord2f((tempX/radius + 1) / 2, 0);
00062       glVertex3f(tempX, tempY, tempZ);
00063 
00064       tempX = GetX(i * dSlice, (j * dStack) + centerRad, radius);
00065       // TempY doesn't change
00066       tempZ = GetZ(i * dSlice, (j * dStack) + centerRad, radius);
00067       //glTexCoord2f(tempY / maxY, 0);
00068       glTexCoord2f((tempX/radius + 1) / 2, 0);
00069       glVertex3f(tempX, tempY, tempZ);
00070     }
00071     glEnd();
00072   }
00073 
00074   glBegin(GL_TRIANGLE_FAN);
00075   // This is the very center of the dome, so it's tex coord is .5, .5
00076   glTexCoord2f(0.5, 0);
00077   //glTexCoord2f(1, 0);
00078   glVertex3f(0, GetHeight(radius, 0), 0);
00079   for (i = 0; i <= slices; ++i) {
00080     tempX = GetX(i * dSlice, centerRad, radius);
00081     tempY = GetHeight(radius, centerRad);
00082     tempZ = GetZ(i * dSlice, centerRad, radius);
00083     //glTexCoord2f(tempY / maxY, 0);
00084     glTexCoord2f((tempX/radius + 1) / 2, 0);
00085     glVertex3f(tempX, tempY, tempZ);
00086   }
00087   glEnd();
00088 
00089   glDepthMask(GL_TRUE);
00090 }
00091 
00092 void SkyDome::Draw(float time) {
00093   if (time >= 24) {
00094     Log::s_log->Message("Sky dome drawn with time (%f) >= 24", time);
00095     time = 0;
00096   }
00097 
00098   glMatrixMode(GL_TEXTURE);
00099   glTranslatef(0, time/24.0, 0);
00100 
00101   glCallList(m_list);
00102 
00103   glLoadIdentity();
00104   glMatrixMode(GL_MODELVIEW);
00105 }
00106 
00107 float SkyDome::GetX(float angle, float radiusAngle, float domeRadius) {
00108   return sin(radiusAngle) * cos(angle) * domeRadius;
00109 }
00110 
00111 float SkyDome::GetZ(float angle, float radiusAngle, float domeRadius) {
00112   return sin(radiusAngle) * sin(angle) * domeRadius;
00113 }
00114 
00115 float SkyDome::GetHeight(float domeRadius, float pointRadiusAngle) {
00116   return (cos(pointRadiusAngle) * domeRadius) / 2;
00117 }

Generated on Mon Jan 8 22:34:12 2007 for CrownandCutlass by  doxygen 1.4.7