src/Ocean.cpp

Go to the documentation of this file.
00001 /* Crown and Cutlass
00002  * Ocean Object Code
00003  */
00004 
00005 #include <cstdio>
00006 #include "GLee.h"
00007 #include "ccmath.h"
00008 #include <vector>
00009 #include "Log.h"
00010 #include "Texture.h"
00011 #include "normals.h"
00012 #include "WaveEmitter.h"
00013 #include "Ocean.h"
00014 
00015 #define OCEAN_TEXSCALE 6
00016 
00017 using namespace std;
00018 
00019 Ocean::Ocean(int sizeX, int sizeZ) {
00020   m_width = sizeX;
00021   m_height = sizeZ;
00022 
00023   m_count = 0;
00024 
00025   // Set up the texture
00026   glEnable(GL_TEXTURE_2D);
00027   m_texture = new Texture("water.png");
00028 
00029   // Set up the waves texture
00030   m_waveTexture = new Texture("surf.png");
00031 
00032   // Set up the material;
00033   
00034   m_material[0] = 0.23;
00035   m_material[1] = 0.35;
00036   m_material[2] = 0.7;
00037   m_material[3] = 0.45;
00038   /*
00039   material[0] = 1.0;
00040   material[1] = 1.0;
00041   material[2] = 1.0;
00042   material[3] = 0.45;
00043   */
00044 
00045   // Generate the display list
00046   m_list = glGenLists(1);
00047   if (m_list == 0) {
00048     printf("Could not generate waves list\n");
00049   }
00050   glNewList(m_list, GL_COMPILE);
00051 
00052   // Draw the actual water
00053   // Use the material
00054   glMaterialfv(GL_FRONT, GL_AMBIENT, m_material);
00055   glMaterialfv(GL_FRONT, GL_DIFFUSE, m_material);
00056 
00057   // Enable the texture
00058   m_texture->BindTexture();
00059 
00060   glNormal3f(0, 1, 0);
00061 
00062   glBegin(GL_QUADS);
00063   glTexCoord2f(0, 0);
00064   glVertex3f(-1*m_width/2, 0, -1*m_height/2);
00065   glTexCoord2f(0, m_height/OCEAN_TEXSCALE);
00066   glVertex3f(-1*m_width/2, 0, m_height/2);
00067   glTexCoord2f(m_width/OCEAN_TEXSCALE, m_height/OCEAN_TEXSCALE);
00068   glVertex3f(m_width/2, 0, m_height/2);
00069   glTexCoord2f(m_width/OCEAN_TEXSCALE, 0);
00070   glVertex3f(m_width/2, 0, -1*m_height/2);
00071   glEnd();
00072 
00073   // Done with list
00074   glEndList();
00075 
00076   // Generate the wave list
00077   WaveEmitter::GenerateWaveList();
00078 }
00079 
00080 Ocean::~Ocean() {
00081   // The wave emitters will be deleted by the quad tree
00082   m_waveEmitters.clear();
00083 
00084   WaveEmitter::DeleteActiveWaves();
00085 
00086   WaveEmitter::DeleteWaveList();
00087 
00088   // Delete the display list
00089   glDeleteLists(m_list, 1);
00090 
00091   // Delete the texture
00092   delete m_texture;
00093   delete m_waveTexture;
00094 
00095   Log::s_log->Message("Ocean deleted");
00096 }
00097 
00098 void Ocean::Draw() {
00099   float mult = sin(m_count);
00100   float mult2 = cos(m_count);
00101 
00102   GLfloat shift = (.25 * mult) / OCEAN_TEXSCALE;
00103   GLfloat shift2 = (.25 * mult2 * (m_width/m_height)) / OCEAN_TEXSCALE;
00104 
00105   glBlendFunc(GL_SRC_ALPHA, GL_ONE);
00106   m_waveTexture->BindTexture();
00107   WaveEmitter::DrawActiveWaves();
00108   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00109 
00110   // Get ready to manipulate the texture matrix
00111   glMatrixMode(GL_TEXTURE);
00112   glPushMatrix();
00113 
00114   // Move around the texture
00115   glTranslatef(shift, shift2, 0);
00116 
00117   // Draw the actual waves
00118   glCallList(m_list);
00119 
00120   // Reset the matrix
00121   glPopMatrix();
00122   glMatrixMode(GL_MODELVIEW);
00123 }
00124 
00125 void Ocean::Update(unsigned int ticks) {
00126   unsigned int numEmitters = m_waveEmitters.size();
00127 
00128   m_count += .002 * ticks;
00129   if (m_count > TWO_PI) m_count -= TWO_PI;
00130 
00131   for(unsigned int i = 0; i < numEmitters; i++) {
00132     m_waveEmitters[i]->Update(ticks);
00133   }
00134 
00135   WaveEmitter::UpdateActiveWaves(ticks);
00136 }
00137 
00138 void Ocean::AddEmitter(WaveEmitter *emitter) {
00139   // This just puts a pointer to a wave emitter into the vector
00140   m_waveEmitters.push_back(emitter);
00141 }

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