src/WaveEmitter.cpp

Go to the documentation of this file.
00001 /* Crown and Cutlass
00002  * Wave Emitter Object Code
00003  */
00004 /*
00005 #if defined (WIN32)
00006 #define WIN32_LEAN_AND_MEAN
00007 #include <windows.h>
00008 #endif
00009 */
00010 #include <iostream>
00011 #include "GLee.h"
00012 #include <vector>
00013 #include <list>
00014 #include "Log.h"
00015 #include "image.h"
00016 #include "Point.h"
00017 #include "Terrain.h"
00018 #include "BoundBox.h"
00019 #include "Wave.h"
00020 #include "WaveEmitter.h"
00021 
00022 using namespace std;
00023 
00024 #define WAVE_RATE .002
00025 #define EMIT_RATE 3300
00026 #define EMIT_DIFF_PERCENT .2
00027 #define EMIT_RATIO .2
00028 
00029 class WaveLoc {
00030  public:
00031   float m_x;
00032   float m_z;
00033   float m_angle;
00034 };
00035 
00036 GLuint WaveEmitter::s_waveList = 0;
00037 list <Wave*> WaveEmitter::s_activeWaves;
00038 
00039 WaveEmitter::WaveEmitter(Terrain *terrain, BoundBox *box){
00040   // List of points on shore
00041   list< Point* > shorePoints;
00042   Point *temp;
00043   float tAngle = 0.0;
00044   WaveLoc *tLoc;
00045 
00046   m_ticksToNextEmit = (int) (EMIT_RATE*(rand()/(RAND_MAX+1.0)));
00047 
00048   //counter = 0.0;
00049 
00050   m_isVisible = false;
00051 
00052   // Populate the shore line list
00053   terrain->LocateShoreLine(box, &shorePoints);
00054 
00055   while (!shorePoints.empty()) {
00056     // First, need to read the front element of list
00057     temp = shorePoints.front();
00058     // Next, remove that first element
00059     shorePoints.pop_front();
00060 
00061     // Find shore angle and move point to proper distance
00062     if (terrain->FindNearestShoreAngle(temp, &tAngle)) {
00063       // Set tLoc from point and add it to inactive queue
00064       tLoc = new WaveLoc();
00065       tLoc->m_x = temp->m_x;
00066       tLoc->m_z = temp->m_z;
00067       tLoc->m_angle = tAngle;
00068       m_waveLocations.push_back(tLoc);
00069     }
00070 
00071     // Finally, delete the Point the list element pointed to
00072     delete temp;
00073   }
00074 }
00075 
00076 WaveEmitter::~WaveEmitter(){
00077   // Delete the wave locations in vector
00078   WaveLoc *temp;
00079   while (!m_waveLocations.empty()) {
00080     temp = m_waveLocations.back();
00081     m_waveLocations.pop_back();
00082     delete temp;
00083   }
00084 }
00085 
00086 void WaveEmitter::Update(unsigned int ticks) {
00087   int numToGenerate;
00088 
00089   m_ticksToNextEmit -= ticks;
00090 
00091   if (m_ticksToNextEmit <= 0) {
00092     // Update ticksToNextEmit
00093     // NOTE: I add EMIT_RATE and ticksToNextEmit so that it keeps the emit rate exact (I hope)
00094     m_ticksToNextEmit = GetNextEmit() + m_ticksToNextEmit;
00095 
00096     // Add a new wave (only if visible???)
00097     if (m_isVisible) {
00098       numToGenerate = 1 + (int) (m_waveLocations.size() * EMIT_RATIO * (rand()/(RAND_MAX+1.0)));
00099       for (int i = 0; i < numToGenerate; i++) {
00100         GenerateWave();
00101       }
00102     }
00103   }
00104 
00105   m_isVisible = false;
00106 }
00107 
00108 void WaveEmitter::SetVisible() {
00109   m_isVisible = true;
00110 }
00111 
00112 int WaveEmitter::GetNextEmit() {
00113   int diff = (int) (EMIT_DIFF_PERCENT * EMIT_RATE);
00114   float randNum = (2.0 * (rand() / (RAND_MAX + 1.0))) - 1;
00115 
00116   return EMIT_RATE + (int) (diff * randNum);
00117 }
00118 
00119 void WaveEmitter::GenerateWaveList() {
00120   // Generate the surf list
00121   s_waveList = glGenLists(1);
00122   if (s_waveList == 0) {
00123     // Should throw an exception
00124     return;
00125   }
00126 
00127   glNewList(s_waveList, GL_COMPILE);
00128 
00129   // Draw the actual surf
00130   glBegin(GL_QUADS);
00131   glTexCoord2f(0, 1);
00132   glVertex3f(1, 0, -.3);
00133   glTexCoord2f(1, 1);
00134   glVertex3f(-1, 0, -.3);
00135   glTexCoord2f(1, 0);
00136   glVertex3f(-1, 0, .3);
00137   glTexCoord2f(0, 0);
00138   glVertex3f(1, 0, .3);
00139   glEnd();
00140 
00141   glEndList();
00142 }
00143 
00144 void WaveEmitter::DeleteWaveList() {
00145   glDeleteLists(s_waveList, 1);
00146 }
00147 
00148 void WaveEmitter::GenerateWave() {
00149   WaveLoc *temp;
00150   int index;
00151 
00152   index = (int) (m_waveLocations.size() * (rand() / (RAND_MAX + 1.0)));
00153 
00154   temp = m_waveLocations[index];
00155 
00156   s_activeWaves.push_back(new Wave(temp->m_x, temp->m_z, temp->m_angle));
00157 }
00158 
00159 int WaveEmitter::NumWaveLocations() {
00160   return m_waveLocations.size();
00161 }
00162 
00163 void WaveEmitter::DrawActiveWaves() {
00164   Wave *temp;
00165   list< Wave* >::iterator iter;
00166   list< Wave* >::iterator listEnd;
00167 
00168   if (s_activeWaves.size() <= 0) return;
00169 
00170   glEnable(GL_COLOR_MATERIAL);
00171   // Disable depth writes
00172   glDepthMask(GL_FALSE);
00173 
00174   iter = s_activeWaves.begin();
00175   listEnd = s_activeWaves.end();
00176   while(iter != listEnd) {
00177     temp = *iter;
00178     glPushMatrix();
00179     temp->PrepDraw();
00180     glCallList(s_waveList);
00181     glPopMatrix();
00182 
00183     ++iter;
00184   }
00185 
00186   // Reenable depth testing
00187   glDepthMask(GL_TRUE);
00188   glDisable(GL_COLOR_MATERIAL);
00189 }
00190 
00191 void WaveEmitter::UpdateActiveWaves(unsigned int ticks) {
00192   Wave *temp;
00193   list< Wave* >::iterator iter;
00194   list< Wave* >::iterator listEnd;
00195 
00196   if (s_activeWaves.size() <= 0) return;
00197 
00198   temp = s_activeWaves.front();
00199   while (!s_activeWaves.empty() && !temp->IsActive()) {
00200     s_activeWaves.pop_front();
00201     delete temp;
00202     temp = s_activeWaves.front();
00203   }
00204 
00205   iter = s_activeWaves.begin();
00206   listEnd = s_activeWaves.end();
00207   while(iter != listEnd) {
00208     temp = *iter;
00209     temp->Update(ticks);
00210 
00211     ++iter;
00212   }
00213 }
00214 
00215 void WaveEmitter::DeleteActiveWaves() {
00216   Wave *temp;
00217 
00218   while (!s_activeWaves.empty()) {
00219     temp = s_activeWaves.front();
00220     s_activeWaves.pop_front();
00221     delete temp;
00222   }
00223 }

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