HydroSDL/oGL_graphics/wave_z_compute_byHand.cpp

Go to the documentation of this file.
00001 
00009 struct seaDatas
00010 {
00011         float *z;
00012         float *R;
00013         float *G;
00014         float *B;
00015         int N;
00016         float L;
00017 };
00018 
00019 
00020 #include <math.h>
00021 
00022 #include "Sea.h"
00023 
00024 
00025 #define pi 3.14159265358979
00026 #define LUTLEN 64
00027 #define LUTLENF 64.0f
00028 
00029 
00030 float sinLUT[LUTLEN]; 
00031 float cosLUT[LUTLEN]; 
00032 
00033 seaDatas seaDat;
00034 seaDatas *sd = &seaDat;
00035 
00036 
00037 
00038 
00040 void fillWaveLuts()
00041 {
00042         int k;
00043         for (k=0; k<LUTLEN; k++)
00044         {
00045                 sinLUT[k] = sin(((float)k)*(2.0f*pi)/(LUTLENF-1.0f));
00046                 cosLUT[k] = cos(((float)k)*(2.0f*pi)/(LUTLENF-1.0f));
00047         }
00048 }
00049 
00050 
00052 
00056 __inline float Wcos(float xarg)
00057 {
00058         float x;
00059         x = xarg/(2*pi);
00060         if (x<0) x=-x;
00061         x = x - (float (int (x)));
00062         x *= LUTLENF;
00063         return cosLUT[int(x)];
00064 }
00065 
00066 
00068 
00072 __inline float Wsin(float xarg)
00073 {
00074         return Wcos(xarg-pi/2.0f);
00075 }
00076 
00077 
00079 
00082 void wave_init()//graphicDatas *gd)
00083 {
00084         long k;
00085         int N;
00086 
00087         sd->N = 64;
00088         sd->L = 150.0;//100
00089 
00090         N = sd->N;
00091 
00092         sd->z = new float [N*N];
00093         sd->R = new float [N*N];
00094         sd->G = new float [N*N];
00095         sd->B = new float [N*N];
00096 
00097         for (k=0; k<N*N; k++) sd->z[k]=0.0f;
00098 
00099         //Fill LUTs:
00100         fillWaveLuts();
00101 }
00102 
00104 
00109 void wave_z_compute(datas *d)//, graphicDatas *gd)
00110 {
00111         float x,y, a,b;
00112         long i, j, iN, k;
00113         long N;
00114         float L;
00115 
00116         N=sd->N;
00117         L=sd->L;
00118         
00119         x = (float)d->x;
00120         y = (float)d->y;
00121 
00122         for (i=0; i<N; i++)
00123         {
00124                 iN=i*N;
00125                 a = x - (L/2.0f)+i*(L/((float)N-1.0f));
00126                 for (j=0; j<N; j++)
00127                 {
00128                         b = y - (L/2.0f)+j*(L/((float)N-1.0f));
00129                         k = iN+j;
00130 
00131                         //sd->z[k] = d->Wave_amp0*sin(2.0*pi*(a*cos(d->Wave_angle0)/d->Lambda0+b*sin(d->Wave_angle0)/d->Lambda0+d->V_wave0/d->Lambda0*d->t))+d->Wave_amp1*sin(2.0*pi*(a*cos(d->Wave_angle1)/d->Lambda1+b*sin(d->Wave_angle1)/d->Lambda1+d->V_wave1/d->Lambda1*d->t))+d->Wave_amp2*sin(2.0*pi*(a*cos(d->Wave_angle2)/d->Lambda2+b*sin(d->Wave_angle2)/d->Lambda2+d->V_wave2/d->Lambda2*d->t)); 
00132                         sd->z[k] = d->Wave_amp0*Wsin(2.0*pi*(a*Wcos(d->Wave_angle0)/d->Lambda0+b*Wsin(d->Wave_angle0)/d->Lambda0+d->V_wave0/d->Lambda0*d->t))+d->Wave_amp1*Wsin(2.0*pi*(a*Wcos(d->Wave_angle1)/d->Lambda1+b*Wsin(d->Wave_angle1)/d->Lambda1+d->V_wave1/d->Lambda1*d->t))+d->Wave_amp2*Wsin(2.0*pi*(a*Wcos(d->Wave_angle2)/d->Lambda2+b*Wsin(d->Wave_angle2)/d->Lambda2+d->V_wave2/d->Lambda2*d->t)); 
00133                         //sd->z[k] = 0.0f;
00134                 }
00135         }
00136 
00137 }
00138 
00139 
00141 
00144 void free_wave_variables()//graphicDatas *gd)
00145 {
00146 
00147         delete [] sd->z;
00148         delete [] sd->R;
00149         delete [] sd->G;
00150         delete [] sd->B;
00151 }
00152 
00153 
00155 
00159 void Draw_sea()//graphicDatas *gd)
00160 {
00161         float x,y, a,b, dl;
00162         long i, j, iN, k;
00163         long N;
00164         float L;
00165 
00166         N=sd->N;
00167         L=sd->L;
00168 
00169 
00170         dl = L/((float)N-1);
00171 
00172         glEnable(GL_BLEND) ;
00173         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ; 
00174         
00175         glBegin(GL_QUADS);
00176         for (i=0; i<(N-1); i++)
00177         {
00178                 iN=i*N;
00179                 a = 0.0 - (L/2.0f)+i*(L/((float)N-1.0f));//a = x - (L/2.0f)+i*(L/((float)N-1.0f));
00180                 for (j=0; j<(N-1); j++)
00181                 {
00182                         b = 0.0 - (L/2.0f)+j*(L/((float)N-1.0f));//b = y - (L/2.0f)+j*(L/((float)N-1.0f));
00183                         k = iN+j;
00184                         glNormal3f(0.0,0.0,1.0);
00185                         glColor4f(0.0f,0.1f*(sd->z[k]+2.0f),0.5f*(sd->z[k]+2.0f),0.7f);
00186                                 glVertex3f(a,    b ,   sd->z[k]);       
00187                         glColor4f(0.0f,0.1f*(sd->z[k+N]+2.0f),0.5f*(sd->z[k+N]+2.0f),0.7f);
00188                                 glVertex3f(a+dl, b ,   sd->z[k+N]);     
00189                         glColor4f(0.0f,0.1f*(sd->z[k+N+1]+2.0f),0.5f*(sd->z[k+N+1]+2.0f),0.7f);
00190                                 glVertex3f(a+dl, b+dl, sd->z[k+N+1]);
00191                         glColor4f(0.0f,0.1f*(sd->z[k+1]+2.0f),0.5f*(sd->z[k+1]+2.0f),0.7f);
00192                                 glVertex3f(a,    b+dl, sd->z[k+1]);
00193                 }
00194         }
00195         glEnd();
00196 }

Generated on Wed Sep 20 14:30:04 2006 for hydroSDL by  doxygen 1.4.7