Morph-SynRJ

view efx_screen1.cxx @ 0:22de913c2d84

morph-synrj intro
author "Cedric Pinson <cedric.pinson@alcove.fr> <mornifle@plopbyte.net>"
date Tue Nov 27 15:23:52 2007 +0100 (2007-11-27)
parents
children
line source
1 #include "efx_screen1.h"
2 #include "sys_assert.h"
3 #include "mth_vector3.h"
4 #include "gl_header.h"
5 #include "cmn_intro.h"
6 #include "cmn_design.h"
9 // sync
10 extern fxSync * sync;
12 efxScreen1_t::efxScreen1_t()
13 {
14 texture=0;
15 equaResX=equaResY=0;
16 mapOfNoise=0;
17 equaCenter=0;
18 equaTransition=false;
19 equaTransitionFinished=false;
20 equaAlpha=0;
21 cameraRestoreAngle=-mth::PI/2;
22 }
25 void efxScreen1_t::Init()
26 {
28 internalTime=0;
30 // background init
31 int size=256;
32 float div=4.0/size;
33 if (mapOfNoise)
34 delete [] mapOfNoise;
35 mapOfNoise=new uchar[size*size*4];
36 texture=txtCreatePerlin2d(size,size,div,div,0.5,6,mapOfNoise);
39 InitEquaLand(9,9,mapOfNoise,size,size);
43 // camera init
44 const int CameraNbKeys=15;
45 const float CameraKeyTime=3.0;
46 cameraLookAt.Init(50,-15,0);
47 cameraPosition.Init(-50.2,-8,95);
49 cameraLookAt.Init(50,0,15);
50 cameraPosition.Init(-50.2,95,8);
51 cameraUp.Init(0,0,-1);
53 cameraPath= new mth::Hermite_t(CameraNbKeys*2);
54 cameraKeyData= new mth::HermiteKey_t[CameraNbKeys];
55 mth::Vector3_t cpos[CameraNbKeys]= {
56 cameraPosition,
57 mth::Vector3_t(110,140,-190),
58 mth::Vector3_t(130,130,-160),
59 mth::Vector3_t(-130,150,-130),
60 mth::Vector3_t(-160,120,-110),
61 mth::Vector3_t(-190,160,50),
62 mth::Vector3_t(-170,180,100),
63 mth::Vector3_t(-130,140,140),
64 /*
65 mth::Vector3_t(-140,150,170),
66 mth::Vector3_t(-130,180,190),
67 mth::Vector3_t(-120,160,150),
68 */
69 mth::Vector3_t(-110,100,120),
70 mth::Vector3_t(-100,110,-110),
71 mth::Vector3_t(-110,150,-150),
72 mth::Vector3_t(110,250,-190),
73 mth::Vector3_t(110,140,-190),
74 mth::Vector3_t(130,130,-160),
75 mth::Vector3_t(-160,120,-110),
76 };
77 for (int i=0;i<CameraNbKeys;i++) {
78 cameraKeyData[i].Init(CameraKeyTime*i,cpos[i]);
79 cameraPath->AddKey(&cameraKeyData[i]);
80 }
83 InitDesign();
84 }
87 void efxScreen1_t::InitDesign()
88 {
89 const char* text1="morph";
90 const int NbChars=strlen(text1);
91 const float StartPositionX=-1000;
92 const float SpaceElement=30;
93 const float StartYDesign=600-350;
94 const float QuadSizeX = (RX-SpaceElement*NbChars)/NbChars;///150;
95 const float QuadSizeY = 150;
96 const float PositionXElement=0;
97 const float TimeStartIntro=10;
98 const float AngleTexture=90; //45
100 nbDesignElements=24;
101 design.Init(nbDesignElements);
102 designElements=new cmnDesignElementTexturedQuad_t[nbDesignElements];
104 glColor4f(1,1,1,1);
106 font->SetScale(1);
107 for (int i=0;i<NbChars;i++) {
109 designElements[i].texture=txtCreateObliqueTexture(256,128,AngleTexture,0,text1[i]);
110 designElements[i].Init(TimeStartIntro+i*0.1);
111 designElements[i].position.Init(StartPositionX,StartYDesign,0);
112 designElements[i].target.Init(PositionXElement+SpaceElement/2+i*(QuadSizeX+SpaceElement),StartYDesign,0);
113 designElements[i].factor=20;
114 designElements[i].sizex=QuadSizeX;
115 designElements[i].sizey=QuadSizeY;
116 designElements[i].fadeAlpha=1.0;
118 design.AddElement(&designElements[i]);
119 }
120 }
123 void efxScreen1_t::DisplayDesign()
124 {
125 cmnPushMatrix();
127 cmnSetPlanarView();
129 glDisable(GL_CULL_FACE);
130 glDisable(GL_DEPTH_TEST);
131 glEnable(GL_BLEND);
133 // glBlendFunc(GL_ONE,GL_ONE);
134 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
136 design.Draw();
140 cmnPopMatrix();
141 glMatrixMode(GL_MODELVIEW);
143 }
149 void efxScreen1_t::InitEquaLand(int _sizeX,int _sizeY,uchar* _map,int _mapSizeX,int _mapSizeY)
150 {
151 SYS_ASSERT(_map);
153 equaResX=_sizeX;
154 equaResY=_sizeY;
156 const int EquaBoxRes=8;
157 const float Mapdu=_mapSizeX*1.0/(1.0*equaResX*EquaBoxRes);
158 const float Mapdv=_mapSizeY*1.0/(1.0*equaResY*EquaBoxRes);
159 const float ScaleDepth=0.5;
160 const float SizeOfEquaBoxX=(EQUA_SIZE_X+EQUA_SIZE_SPACE)*EquaBoxRes;
161 const float SizeOfEquaBoxY=(EQUA_SIZE_Y+EQUA_SIZE_SPACE)*EquaBoxRes;
162 const float TotalSizeX=SizeOfEquaBoxX*equaResX;
163 const float TotalSizeY=SizeOfEquaBoxY*equaResY;
165 // equa init
166 fxEqua=new efxEqua_t[equaResX*equaResY];
167 for (int j=0;j<equaResY;j++)
168 for (int i=0;i<equaResX;i++) {
170 float positionX=i*(SizeOfEquaBoxX)-TotalSizeX*0.5;
171 float positionY=j*(SizeOfEquaBoxY)-TotalSizeY*0.5;
173 fxEqua[i+j*equaResX].Init(EquaBoxRes);
174 fxEqua[i+j*equaResX].SetPosition(positionX,positionY,0);
175 for (int jj=0;jj<fxEqua[i+j*equaResX].ResY();jj++)
176 for( int ii = 0; ii < fxEqua[i+j*equaResX].ResX(); ii++ ) {
177 int u,v;
178 u=i*EquaBoxRes+ii;
179 v=j*EquaBoxRes+jj;
180 u=(int)(u*1.0*Mapdu);
181 v=(int)(v*1.0*Mapdv);
182 float texel=(float)(_map[(u+v*_mapSizeX)*4+0])+(float)(_map[(u+v*_mapSizeX)*4+1])+(float)(_map[(u+v*_mapSizeX)*4+2]);
183 texel/=3;
184 fxEqua[i+j*equaResX].GetBox(ii+jj*EquaBoxRes).SetBaseLevel(texel*1.0*ScaleDepth);
185 fxEqua[i+j*equaResX].GetBox(ii+jj*EquaBoxRes).SetDepthLevel(0);
186 }
187 }
189 equaLightIntensity=1.0;
192 equaCenter=equaResX/2+equaResX/2*equaResX;
194 #ifdef WAVE
195 waveSimulation[0]=new float[equaResX*equaResY*EquaBoxRes*EquaBoxRes*2];
196 waveSimulation[1]=&waveSimulation[0][equaResX*equaResY*EquaBoxRes*EquaBoxRes];
197 memset(waveSimulation[0],0,sizeof(float)*equaResX*equaResY*EquaBoxRes*EquaBoxRes*2);
198 currentWaveBuffer=0;
200 InitSimulation(0.02/*1.0/33*/,10.0,0.01,1);
201 #endif
204 }
207 #ifdef WAVE
208 void efxScreen1_t::InitSimulation(float _timeStep,float _speedOfWave,float _viscosity,float _sizeCell)
209 {
210 #define Sqr(a) (a*a)
212 speedOfWave=_speedOfWave;
213 viscosity=_viscosity;
214 timeStepSimulation=_timeStep;
216 f1=Sqr(speedOfWave)*Sqr(timeStepSimulation)/Sqr(_sizeCell);
217 f2=1.0/(viscosity*timeStepSimulation+2.0);
218 k1=(4.0-8.0*f1)*f2;
219 k2=(viscosity*timeStepSimulation-2)*f2;
220 k3= 2.0*f1*f2;
221 #undef Sqr
222 }
225 void efxScreen1_t::Simulate()
226 {
227 // we should use a threshold
228 if (internalTime-counterSimulation<timeStepSimulation)
229 return;
231 counterSimulation=internalTime;
234 currentWaveBuffer^=1;
235 float* c=waveSimulation[currentWaveBuffer];
236 float* o=waveSimulation[(currentWaveBuffer+1)%2];
238 int sizex=equaResX*fxEqua[0].ResX();
239 int jend=equaResY*fxEqua[0].ResY();
240 int iend=sizex;
241 int jindex;
242 int jindexM1;
243 int jindexA1;
244 int jindexi;
247 // o[j*nbCells+i][1]=
248 // k3* (c[(j-1)*nbCells+i][1] + c[(j+1)*nbCells+i][1] + c[j*nbCells+(i-1)][1] + c[j*nbCells+(i+1)][1]) +
249 // k1* c[j*nbCells+i][1] + k2*o[j*nbCells+i][1];
252 for (int j=1;j<jend-1;j++) {
253 jindex=j*sizex;
254 jindexM1=jindex-sizex;
255 jindexA1=jindex+sizex;
256 for (int i=1;i<iend-1;i++) {
257 jindexi=jindex+i;
258 o[jindexi]= k3* (c[jindexM1+i] + c[jindexA1+i] + c[jindex+(i-1)]+c[jindex+(i+1)]) + k1* c[jindexi] + k2*o[jindexi];
259 }
260 }
261 }
264 void efxScreen1_t::UpdateWave()
265 {
267 Simulate();
269 int i,j;
271 // now update the equalizer
272 float* o=waveSimulation[(currentWaveBuffer+1)%2];
273 int sizex=equaResX*fxEqua[0].ResX();
274 int sizey=equaResY*fxEqua[0].ResY();
275 int resBoxX=fxEqua[0].ResX();
276 int resBoxY=fxEqua[0].ResY();
278 for (j=0;j<equaResY;j++)
279 for (i=0;i<equaResX;i++) {
280 for (int jj=0;jj<resBoxY;jj++)
281 for( int ii = 0; ii < resBoxX; ii++ ) {
282 fxEqua[i+j*equaResX].GetBox(ii+jj*resBoxX).SetDepthLevel(o[ (j*resBoxY+jj)*sizex+(i*resBoxX+ii)]);
283 }
284 }
285 }
288 void efxScreen1_t::UpdateWaveBuffer()
289 {
290 // return ;
291 int centerx=mth::Floor(equaResX*1.0/2);
292 int centery=mth::Floor(equaResY*1.0/2);
294 int sizex=equaResX*fxEqua[0].ResX();
295 int sizey=equaResY*fxEqua[0].ResY();
296 int resBoxX=fxEqua[0].ResX();
297 int resBoxY=fxEqua[0].ResY();
299 int boxRes=fxEqua[0].ResX();
301 float maxValue=0;
302 float value=0;
303 for (int j=0;j<resBoxY;j++)
304 for (int i=0;i<resBoxX;i++) {
305 value+=fxEqua[equaCenter].GetBox(i+j*resBoxX).level;
306 maxValue=mth::Max(maxValue,fxEqua[equaCenter].GetBox(i+j*resBoxX).level);
307 }
308 value/=resBoxY*resBoxX;
309 // waveSimulation[currentWaveBuffer][(centery*resBoxY+j)*sizex+(centerx*resBoxX+i)]=fxEqua[equaCenter].GetBox(i+j*resBoxX).level;
311 // for (j=0;j<resBoxY;j++)
312 // for (int i=0;i<resBoxX;i++)
313 // waveSimulation[currentWaveBuffer][(centery*resBoxY+j)*sizex+(centerx*resBoxX+i)]=value;
314 waveSimulation[currentWaveBuffer][(centery*resBoxY+boxRes/2)*sizex+(centerx*resBoxX+boxRes/2)]=maxValue;
316 }
319 #endif
321 void efxScreen1_t::DrawEquaOnce(int _i/*,GLenum _mode*/)
322 {
323 cmnPushMatrix();
324 fxEqua[_i].Draw(/*_mode*/);
325 glTranslatef(1.5,1.5,1.5);
326 fxEqua[_i].Draw(/*_mode*/);
327 glTranslatef(-3,-3,-3);
328 fxEqua[_i].Draw(/*_mode*/);
329 cmnPopMatrix();
330 }
335 void efxScreen1_t::DrawEquaFullExceptCenter()
336 {
337 int add;
339 if (equaTransition && !equaTransitionFinished)
340 add=2;
341 else
342 add=1;
344 for (int j=0;j<equaResY;j+=add)
345 for (int i=0;i<equaResX;i+=add) {
346 int index=i+j*equaResX;
347 if (index!=equaCenter)
348 DrawEquaOnce(index);
349 }
350 }
354 void efxScreen1_t::DrawEquaCenter()
355 {
356 DrawEquaOnce(equaCenter);
357 }
364 void efxScreen1_t::Update()
365 {
367 const float EquaTransitionStart=6;
368 const float EquaTransitionEnd=24.99-12.312; // from start
369 const float EquaAlphaAdd=0.05;
370 const float CameraRestoreUp=0.5; // value in radian/second
371 const float TimeRefreshWave = 2.0;//8.0;
373 const float TimeStartDesign1 = 3.0;
375 // const mth::Vector3_t CameraInitialUp=mth::Vector3_t(0,0,-1);
377 internalTime+=Timer->GetFrameTime();
379 #ifdef WAVE
380 UpdateWave();
381 #endif
383 sync->SyncEqua();
385 #ifdef WAVE
386 // to test only
387 static float test=0;
388 test+=Timer->GetFrameTime();
389 if (internalTime>EquaTransitionEnd && test>TimeRefreshWave) {
390 test=0;
391 UpdateWaveBuffer();
392 }
393 #endif
395 //fxEqua->Update();
396 // UpdateSpherical();
398 //#if 0
400 if (internalTime>=EquaTransitionStart && internalTime<EquaTransitionEnd) {
401 equaTransition=true;
402 cameraRestoreAngle+=CameraRestoreUp*Timer->GetFrameTime();
403 } else if (internalTime>EquaTransitionEnd) {
404 equaTransition=false;
405 equaTransitionFinished=true;
406 }
408 if (internalTime>=EquaTransitionStart) {
409 equaAlpha+=EquaAlphaAdd*Timer->GetFrameTime();
410 equaAlpha=mth::Min(0.5,equaAlpha);
411 cameraPath->GetData(internalTime-EquaTransitionStart,cameraPosition);
412 }
416 cameraRestoreAngle=mth::Min(cameraRestoreAngle,0);
417 cameraUp[0]=0;
418 cameraUp[1]=mth::Cos(cameraRestoreAngle);
419 cameraUp[2]=mth::Sin(cameraRestoreAngle);
421 // if (internalTime>=TimeStartDesign1)
422 //UpdateDesign1();
423 design.Update();
424 //#endif
425 }
429 void efxScreen1_t::DrawEqua()
430 {
432 glEnable(GL_CULL_FACE);
433 glDisable(GL_BLEND);
435 glColor3f(1,1,1);
437 glEnable(GL_LIGHTING);
438 glEnable(GL_LIGHT0);
440 float light[4]={0.7,0,0.7,0};
441 glLightfv(GL_LIGHT0,GL_POSITION,light);
443 equaLightIntensity=1;
445 light[0]=219.0/255*equaLightIntensity;
446 light[1]=127.0/255*equaLightIntensity;
447 light[2]=13.0/255*equaLightIntensity;
448 light[3]=1.0;
449 glLightfv(GL_LIGHT0,GL_DIFFUSE,light) ;
451 glEnable(GL_BLEND);
452 glBlendFunc(GL_ONE,GL_ONE);
455 DrawEquaCenter();
457 if (equaTransition || equaTransitionFinished) {
458 if (equaTransition && !equaTransitionFinished) {
459 light[0]=1;
460 light[1]=1;
461 light[2]=1;
462 light[3]=equaAlpha;
463 glBlendFunc(GL_SRC_ALPHA,GL_ONE);
464 glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,light);
465 }
467 DrawEquaFullExceptCenter();
468 }
471 glDisable(GL_BLEND);
472 glDisable(GL_LIGHT0);
473 glDisable(GL_LIGHTING);
475 //cmnPopMatrix();
476 }
479 void efxScreen1_t::Draw()
480 {
481 cmnSetFrustrum(121);
482 cmnSetCamera( cameraPosition,cameraLookAt,cameraUp); // good camera on gf4
485 DisplayBackground();
487 cmnPushGLState();
488 glRotatef(-90,1,0,0); // to see the mesh as a human
489 DrawEqua();
490 cmnPopGLState();
492 DisplayDesign();
493 //design.Draw();
495 }
498 void efxScreen1_t::DisplayBackground()
499 {
501 glEnable(GL_BLEND);
502 glBlendFunc(GL_ONE,GL_ONE);
504 cmnDisplayBackground(texture);
505 }