source: GTP/trunk/App/Demos/Illum/Rain/src/RainCloseDropsParticles.cpp @ 2221

Revision 2221, 32.2 KB checked in by plemenos, 17 years ago (diff)
  • Property svn:executable set to *
Line 
1
2///////////////////////////////////////////////////////////////////////////
3//
4//              Realistic rain real-time simulation program
5//
6//              Pierre Rousseau
7//
8///////////////////////////////////////////////////////////////////////////
9//
10//              Particle systems handling
11//
12///////////////////////////////////////////////////////////////////////////
13
14
15#include "windows.h"
16#include "RainCloseDropsParticles.h"
17#include <OgreHardwarePixelBuffer.h>
18using namespace std;
19
20
21extern float startPos[3];
22
23
24
25
26
27//////////////////////////////////////////////////////////////////////////////////////////////////////////
28
29
30CloseDropsParticles::CloseDropsParticles(SceneManager *sceneMgr, SceneNode *parentSyst, SceneNode *parentCam)
31{
32    mSceneMgr = sceneMgr;
33    mParentCam = parentCam;
34    mParentSyst = parentSyst;
35
36        // CHANGE THE FOLLOWING PARAMETERS TO MODIFY THE SIZES OF THE PARTICLES
37    mRainWidth = 0.2f;  // ranging from 0.1 to 0.9
38    mRainStreaksHeight = 1.0;
39    mActiveParticlesCount = BILLBOARD_POS_TEX_SIZE * BILLBOARD_POS_TEX_SIZE;
40
41    mParticlesNode = parentSyst->createChildSceneNode();
42
43    mRainLightMgr = new RainLightManager(mSceneMgr);
44    createLights();
45
46    std::cout << "creating rain system" << std::endl << std::endl;
47    initParticles();
48}
49
50
51
52
53
54CloseDropsParticles::~CloseDropsParticles()
55{
56    mSceneMgr->destroyStaticGeometry(STATIC_GEOMETRY_NAME);
57    cout << "Particles destructed." << endl;
58}
59
60
61
62
63void CloseDropsParticles::createLights()
64{
65    mSceneMgr->setAmbientLight(ColourValue(1.0, 1.0, 1.0));
66
67    mRainLightMgr->addStreetLight(ColourValue::Green, Vector3(startPos[0] + 100 , startPos[1] - HAUTEUR_OBSERVATEUR, startPos[2] + 20), 120); // green light
68    mRainLightMgr->addStreetLight(ColourValue::Red, Vector3(startPos[0] + 130 , startPos[1] - HAUTEUR_OBSERVATEUR, startPos[2] + 60), 120); // red light
69    mRainLightMgr->addStreetLight(ColourValue::Red + ColourValue::Green + ColourValue::Blue, Vector3(startPos[0] + 160 , startPos[1] - HAUTEUR_OBSERVATEUR, startPos[2] + 100), 120); // white light
70
71
72    mRainLightMgr->finalizeLights();
73}
74
75
76
77
78RainLightManager *CloseDropsParticles::getLightManagerPtr()
79{
80    return mRainLightMgr;
81}
82
83
84
85
86void CloseDropsParticles::updateLights()
87{
88    mRainLightMgr->updateLights();
89}
90
91
92
93void CloseDropsParticles::createBillboardsPositionRenderTexture(RenderTexture *PSPosTexture, Vector3 nodePos, BillboardSet *bbSet)
94{
95    //initialize the render to texture related stuff
96
97    String camName = "camera_for_" + PSPosTexture->getName();
98    Camera *PSPosCamera = new Ogre::Camera(camName, mSceneMgr);
99
100    SceneNode *mPSPosCamNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
101    mPSPosCamNode->setPosition(nodePos);
102    mPSPosCamNode->attachObject(PSPosCamera);
103
104    PSPosCamera->setPosition(Vector3(0, 0, 0));
105    PSPosCamera->setNearClipDistance( 10 );
106    PSPosCamera->setFarClipDistance( 12 );
107    PSPosCamera->setFOVy(Ogre::Radian(Ogre::Degree(90)));
108    PSPosCamera->setProjectionType(PT_ORTHOGRAPHIC );
109    PSPosCamera->setAspectRatio(1.0);
110
111    mPSPosCamNode->attachObject(bbSet);
112
113    Billboard *mBillboard = bbSet->createBillboard(Vector3(0, 0, -11));
114    mBillboard->setDimensions(20, 20);
115    mBillboard->setColour(ColourValue::White);
116
117    TextureFrameListener *textureLis = new TextureFrameListener(mSceneMgr);
118
119    PSPosTexture->addListener(textureLis);
120    PSPosTexture->addViewport(PSPosCamera);
121    PSPosTexture->getViewport (0)->setOverlaysEnabled (false);
122    PSPosTexture->getViewport (0)->setClearEveryFrame(true);
123
124    PSPosTexture->setAutoUpdated(false);
125}
126
127
128
129
130
131void CloseDropsParticles::createBillboardsPositionTextureMaterial(String inputTextureName)
132{
133    MaterialPtr posTextureMaterial = MaterialManager::getSingleton().create("rtt_material_using_" + inputTextureName, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
134
135    // alpha rejection mustn't be set, to avoid breaking the FBO
136
137    TextureUnitState *tPosSrcUnit = posTextureMaterial->getTechnique (0)->getPass (0)->createTextureUnitState ();
138    tPosSrcUnit->setTextureName (inputTextureName);
139    tPosSrcUnit->setTextureFiltering (FO_POINT, FO_POINT, FO_NONE );
140    tPosSrcUnit->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP); // mode clamp for this texture
141
142    posTextureMaterial->setDepthCheckEnabled (true);
143    posTextureMaterial->setDepthWriteEnabled (true);
144
145    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
146    // shader part
147    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
148
149    String progName;
150
151    // fragment program which will map the generated textures
152    progName = "billboardPosTexUpdater" + inputTextureName;
153    HighLevelGpuProgramPtr fp = HighLevelGpuProgramManager::getSingleton().createProgram(progName, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "cg", GPT_FRAGMENT_PROGRAM);
154    fp->setSourceFile("billboardPosTexUpdaterFP.cg");
155    fp->setParameter("profiles", "ps_2_x arbfp1");
156
157    fp->setParameter("entry_point", "main");
158
159    posTextureMaterial->getTechnique (0)->getPass (0)->setFragmentProgram (progName);
160
161    posTextureMaterial->compile();
162}
163
164
165
166void CloseDropsParticles::definePosTextureAndBackup()
167{
168    String tmp;
169
170    // first : define the "primary" and "secondary" RTT's
171    tmp = String("billboardset_for_") + PARTICLE_POSITION_TEXTURE_NAME;
172    bbSet1 = mSceneMgr->createBillboardSet(tmp);
173    particlePosTexture = Root::getSingleton().getRenderSystem()->createRenderTexture (PARTICLE_POSITION_TEXTURE_NAME, BILLBOARD_POS_TEX_SIZE, BILLBOARD_POS_TEX_SIZE, TEX_TYPE_2D, PF_FLOAT32_RGB);
174    createBillboardsPositionRenderTexture(particlePosTexture, Vector3(0, 10, 0), bbSet1);
175
176    tmp = String("billboardset_for_") + PARTICLE_POSITION_BACKUP_TEXTURE_NAME;
177    bbSet2 = mSceneMgr->createBillboardSet(tmp);
178
179    // particlePosTextureBackup was already created and initialized
180    particlePosTextureBackup = Root::getSingleton().getRenderSystem()->createRenderTexture (PARTICLE_POSITION_BACKUP_TEXTURE_NAME, BILLBOARD_POS_TEX_SIZE, BILLBOARD_POS_TEX_SIZE, TEX_TYPE_2D, PF_FLOAT32_RGB);
181
182    createBillboardsPositionRenderTexture(particlePosTextureBackup, Vector3(100, 10, 0), bbSet2);
183
184    // initialization
185    createBillboardsPositionTextureMaterial(PARTICLE_POSITION_INIT_TEXTURE_NAME);
186    tmp = String("rtt_material_using_") + PARTICLE_POSITION_INIT_TEXTURE_NAME;
187    bbSet2->setMaterialName(tmp);
188
189    particlePosTextureBackup->update();
190
191    // create and bind the materials used for the billboardSets
192    createBillboardsPositionTextureMaterial(PARTICLE_POSITION_BACKUP_TEXTURE_NAME);
193    tmp = String("rtt_material_using_") + PARTICLE_POSITION_BACKUP_TEXTURE_NAME;
194    bbSet1->setMaterialName(tmp);
195
196    // now it's initialized, set it to normal rendering
197    createBillboardsPositionTextureMaterial(PARTICLE_POSITION_TEXTURE_NAME);
198    tmp = String("rtt_material_using_") + PARTICLE_POSITION_TEXTURE_NAME;
199    bbSet2->setMaterialName(tmp);
200
201}
202
203
204
205
206void CloseDropsParticles::initializeBillboardsPositionTexture()
207{
208    // initialize the texture
209    TexturePtr InitializationBillBoardPositionTexture = TextureManager::getSingleton().createManual(
210                PARTICLE_POSITION_INIT_TEXTURE_NAME, // name
211                ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
212                TEX_TYPE_2D,      // type
213                BILLBOARD_POS_TEX_SIZE, BILLBOARD_POS_TEX_SIZE,         // width & height
214                0,                // number of mipmaps
215                PF_FLOAT32_RGB,     // pixel format
216                TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
217
218    // Get the pixel buffer
219    HardwarePixelBufferSharedPtr pixelBuffer = InitializationBillBoardPositionTexture->getBuffer();
220
221    // Lock the pixel buffer and get a pixel box
222    pixelBuffer->lock(HardwareBuffer::HBL_DISCARD); // for best performance use HBL_DISCARD!
223    const PixelBox& pixelBox = pixelBuffer->getCurrentLock();
224
225    float* pDest = static_cast<float*>(pixelBox.data);
226
227    /////////////////////////////////////////////////
228    //  (indPart%100) * 10 - 500, 10, (indPart/100) * 10 - 1000
229    //  ((i + j * BILLBOARD_POS_TEX_SIZE )% 100) * 10 - 500, 10, ((i + j * BILLBOARD_POS_TEX_SIZE) / 100) * 10 - 1000
230    /////////////////////////////////////////////////
231
232    // Fill in some pixel data.
233    for (size_t j = 0; j < BILLBOARD_POS_TEX_SIZE; j++)
234        for(size_t i = 0; i < BILLBOARD_POS_TEX_SIZE; i++)
235        {
236            if (i + j * BILLBOARD_POS_TEX_SIZE < mActiveParticlesCount)
237            {
238                // the drops are initially positionned on a regular horizontal grid, with random altitude.
239                *pDest++ = i / float(BILLBOARD_POS_TEX_SIZE);           // R -> X
240                *pDest++ = (rand() % 10000) / 10000.0f;                 // G -> Y
241                *pDest++ = j / float(BILLBOARD_POS_TEX_SIZE);           // B -> Z
242            }
243            else
244            {
245                *pDest++ = 0.0;         // R -> X
246                *pDest++ = 0.0;         // G -> Y
247                *pDest++ = 0.0;         // B -> Z
248            }
249        }
250
251    // Unlock the pixel buffer
252    pixelBuffer->unlock();
253
254    InitializationBillBoardPositionTexture->load();
255
256}
257
258
259
260
261void CloseDropsParticles::makeControlOverlay()
262{
263    MaterialPtr posTextureVerifMaterial1 = MaterialManager::getSingleton().create("overlayVerif1", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
264
265    posTextureVerifMaterial1->getTechnique (0)->getPass (0)->setAlphaRejectSettings(CMPF_NOT_EQUAL, 0);
266    TextureUnitState *tUnitVerif = posTextureVerifMaterial1->getTechnique (0)->getPass (0)->createTextureUnitState ();
267   
268        //tUnitVerif->setTextureName (REFRACT_RTT_NAME);
269        tUnitVerif->setTextureName (PARTICLE_POSITION_TEXTURE_NAME);
270
271    tUnitVerif->setTextureFiltering (TFO_NONE );
272    tUnitVerif->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP); // mode clamp for this texture
273
274    posTextureVerifMaterial1->compile();
275
276
277    OverlayElement *posTexPanel1 = OverlayManager::getSingleton ().createOverlayElement ("Panel", "PosTexPanel1", false);
278    posTexPanel1->setDimensions(0.15, 0.2);
279    posTexPanel1->setPosition(0.01, 0.01);
280    posTexPanel1->setMaterialName ("overlayVerif1");
281
282    MaterialPtr posTextureVerifMaterial2 = posTextureVerifMaterial1->clone("overlayVerif2");
283    TextureUnitState *tUnitVerif2 = posTextureVerifMaterial2->getTechnique (0)->getPass (0)->getTextureUnitState (0);
284
285    tUnitVerif2->setTextureName (LIGHT_INFO_TEXTURE_NAME);
286
287    tUnitVerif2->setTextureFiltering (TFO_NONE );
288    tUnitVerif2->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
289
290    posTextureVerifMaterial2->compile();
291
292
293    OverlayElement *posTexPanel2 = OverlayManager::getSingleton ().createOverlayElement ("Panel", "PosTexPanel2", false);
294    posTexPanel2->setDimensions(0.15, 0.01);
295    //posTexPanel2->setDimensions(0.15, 0.2);
296    posTexPanel2->setPosition(0.01, 0.22);
297    posTexPanel2->setMaterialName ("overlayVerif2");
298
299    Overlay *posTexOverlay = OverlayManager::getSingleton ().create("PositionTextureOverlay");
300    posTexOverlay->add2D ((OverlayContainer *)posTexPanel1);
301    posTexOverlay->add2D ((OverlayContainer *)posTexPanel2);
302    posTexOverlay->show ();
303}
304
305
306
307
308
309
310
311
312MeshPtr CloseDropsParticles::createHardwareBillboardMesh(String materialName, String meshName)
313{
314    MeshPtr msh = MeshManager::getSingleton().createManual(meshName,
315                  ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
316                  new ManualHardwareBillboardMeshLoader(materialName));
317
318    msh->load();
319
320    return msh;
321}
322
323
324
325
326
327
328void CloseDropsParticles::modifyClonedHardwareBillboardMesh(MeshPtr &msh, int partNumber)
329{
330    SubMesh* sm = msh->getSubMesh(0);
331
332    Ogre::VertexData* vertex_data = sm->vertexData;
333
334    const Ogre::VertexElement* texCoordsElem = vertex_data->vertexDeclaration->findElementBySemantic(Ogre::VES_TEXTURE_COORDINATES);
335    Ogre::HardwareVertexBufferSharedPtr vbuf = vertex_data->vertexBufferBinding->getBuffer(texCoordsElem->getSource());
336    unsigned char* vertex = static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_DISCARD))
337                            ;
338    float* pReal;
339
340    // vertex 0
341    texCoordsElem->baseVertexPointerToElement(vertex, &pReal);
342    // uv
343    *pReal++ = 1;
344    *pReal++ = 0;
345    // position texture coordinates
346    *pReal++ = float(partNumber % BILLBOARD_POS_TEX_SIZE) / float(BILLBOARD_POS_TEX_SIZE);
347    *pReal++ = float(partNumber / BILLBOARD_POS_TEX_SIZE) / float(BILLBOARD_POS_TEX_SIZE);
348
349    // vertex 1
350    vertex += vbuf->getVertexSize();
351    texCoordsElem->baseVertexPointerToElement(vertex, &pReal);
352    // uv
353    *pReal++ = 0;
354    *pReal++ = 0;
355    // position texture coordinates
356    *pReal++ = float(partNumber % BILLBOARD_POS_TEX_SIZE) / float(BILLBOARD_POS_TEX_SIZE);
357    *pReal++ = float(partNumber / BILLBOARD_POS_TEX_SIZE) / float(BILLBOARD_POS_TEX_SIZE);
358
359    // vertex 2
360    vertex += vbuf->getVertexSize();
361    texCoordsElem->baseVertexPointerToElement(vertex, &pReal);
362    // uv
363    *pReal++ = 0;
364    *pReal++ = 1;
365    // position texture coordinates
366    *pReal++ = float(partNumber % BILLBOARD_POS_TEX_SIZE) / float(BILLBOARD_POS_TEX_SIZE);
367    *pReal++ = float(partNumber / BILLBOARD_POS_TEX_SIZE) / float(BILLBOARD_POS_TEX_SIZE);
368
369    // vertex 3
370    vertex += vbuf->getVertexSize();
371    texCoordsElem->baseVertexPointerToElement(vertex, &pReal);
372    // uv
373    *pReal++ = 1;
374    *pReal++ = 1;
375    // position texture coordinates
376    *pReal++ = float(partNumber % BILLBOARD_POS_TEX_SIZE) / float(BILLBOARD_POS_TEX_SIZE);
377    *pReal++ = float(partNumber / BILLBOARD_POS_TEX_SIZE) / float(BILLBOARD_POS_TEX_SIZE);
378
379
380    vbuf->unlock();
381
382    msh->load();
383}
384
385
386
387
388
389void CloseDropsParticles::initParticles()
390{
391    StaticGeometry *s;
392
393    // we create the texture which will be used to update the particles positions.
394    initializeBillboardsPositionTexture();
395
396    createMaterial(RAIN_MATERIAL_NAME);
397
398    // we create the billboards as static geometry in a vertex buffer on the graphics card. The positions will only be modified in a shader
399    s = mSceneMgr->createStaticGeometry(STATIC_GEOMETRY_NAME);
400
401    // we must set our region parameters so that only one region includes all the geometry
402    s->setOrigin(Vector3(-5000, -500, -5000));
403    s->setRegionDimensions(Vector3(10000, 1000, 10000));
404    s->setRenderingDistance(100000);
405
406    int discreetSize = int(sqrt(BILLBOARD_POS_TEX_SIZE * BILLBOARD_POS_TEX_SIZE / 3.0));
407
408    cout << endl << endl << "Loading " << BILLBOARD_POS_TEX_SIZE * BILLBOARD_POS_TEX_SIZE << " particles" << endl;
409
410    // we should position the particles so that they cover the whole world (to avoid clipping)
411    for (int indPart = 0; indPart < BILLBOARD_POS_TEX_SIZE * BILLBOARD_POS_TEX_SIZE; indPart++)
412    {
413        String meshName = BILLBOARD_MESH_NAME + StringConverter::toString(indPart);
414        // define the billboard (quad) mesh to use
415        modifyClonedHardwareBillboardMesh(createHardwareBillboardMesh(RAIN_MATERIAL_NAME, meshName), indPart);
416
417        String entName = "simple_quad_for_hardware_bilboard_" + StringConverter::toString(indPart);
418        Entity *e = mSceneMgr->createEntity(entName, meshName);
419        e->setQueryFlags(0x00000002);
420
421        float indX = float( (indPart / 3) % discreetSize ) / float(discreetSize);
422        float indY = (indPart % 3) / 2.0;  // three stories of droplets
423        float indZ = float( (indPart / 3) / discreetSize ) / float(discreetSize);
424
425        Vector3 pos(PARTICLES_MIN_X + (PARTICLES_MAX_X - PARTICLES_MIN_X) * indX ,
426                    PARTICLES_MIN_Y + (PARTICLES_MAX_Y - PARTICLES_MIN_Y) * indY ,
427                    PARTICLES_MIN_Z + (PARTICLES_MAX_Z - PARTICLES_MIN_Z) * indZ); // we give each particle a different position, to avoid clipping
428
429                s->addEntity(e, pos);
430
431
432        // progress bar
433        printf("|");
434        int discret = 50;
435        int prog = int (discret * indPart / (BILLBOARD_POS_TEX_SIZE * BILLBOARD_POS_TEX_SIZE) );
436        for (int car=0 ; car <= prog ; car++)
437            printf("#");
438        for (int car=prog+1 ; car <= discret ; car++)
439            printf(" ");
440        printf("|\r");
441    }
442
443    s->build();
444
445    cout << endl << endl << "Particles loaded" << endl << endl;
446}
447
448
449
450
451void CloseDropsParticles::updatePositionTexture(bool primary)
452{
453    if (static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(RAIN_MATERIAL_NAME))->
454            getTechnique (0)->getPass (0)->getTextureUnitState (1)->isBlank())
455    {
456        std::cout << "merde !!!" << std::endl;
457    }
458
459    if (primary)
460    {
461        particlePosTexture->update();
462        static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(RAIN_MATERIAL_NAME))->getTechnique (0)->getPass (0)->getTextureUnitState (1)->setTextureName(PARTICLE_POSITION_TEXTURE_NAME);
463        static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(RAIN_MATERIAL_NAME))->compile();
464    }
465    else
466    {
467        particlePosTextureBackup->update();
468        static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(RAIN_MATERIAL_NAME))->getTechnique (0)->getPass (0)->getTextureUnitState (1)->setTextureName(PARTICLE_POSITION_BACKUP_TEXTURE_NAME);
469        static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(RAIN_MATERIAL_NAME))->compile();
470    }
471}
472
473
474
475
476
477
478
479
480
481
482
483
484
485void CloseDropsParticles::createMaterial(String name)
486{
487    dropsTexturesMaterial = MaterialManager::getSingleton().create(name, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
488
489#ifndef SNOW
490
491    mCamera = createDropsCamera();
492    mParentCam->attachObject(mCamera);
493
494    dropsTexture = Root::getSingleton().getRenderSystem()->createRenderTexture (REFRACT_RTT_NAME, RTTSIZE, RTTSIZE, TEX_TYPE_2D, PF_R8G8B8);
495    dropsTexture->setAutoUpdated(true); // the main RTT texture will be updated automatically
496    TextureFrameListener *textureLis = new TextureFrameListener(mSceneMgr); // to avoid seeing the drops in the rtt
497
498    dropsTexture->addListener(textureLis);
499    dropsTexture->addViewport(mCamera);
500    dropsTexture->getViewport (0)->setOverlaysEnabled (false);
501    dropsTexture->getViewport (0)->setClearEveryFrame(true);
502#endif
503
504    dropsTexturesMaterial->getTechnique (0)->getPass (0)->setAlphaRejectSettings(CMPF_NOT_EQUAL, 0);    // we discard the portions of the texture where alpha = 0
505
506    TextureUnitState *createMaterialTUnit1 = dropsTexturesMaterial->getTechnique (0)->getPass (0)->createTextureUnitState ();
507#ifdef SNOW
508
509    createMaterialTUnit1->setTextureName ("flocon.png");
510    dropsTexturesMaterial->getTechnique (0)->getPass (0)->setSceneBlending(SBT_TRANSPARENT_ALPHA);
511#else
512
513    createMaterialTUnit1->setTextureName (REFRACT_RTT_NAME);
514#endif
515
516    createMaterialTUnit1->setTextureFiltering (FO_POINT, FO_POINT, FO_NONE );
517    createMaterialTUnit1->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP); // mode wrap for this texture
518
519    TextureUnitState *createMaterialTUnit2 = dropsTexturesMaterial->getTechnique (0)->getPass (0)->createTextureUnitState ();
520    createMaterialTUnit2->setTextureName (PARTICLE_POSITION_INIT_TEXTURE_NAME); // real position texture isn't yet initialized
521       
522        // THE FOLLOWING LINE IS NECESSARY TO HANDLE VERTEX TEXTURE FETCH IN OGRE.
523        // IT NEEDS OGRE EIHORT
524        // THIS IS THE REASON WHY THE PROGRAM WILL NOT RUN PROPERLY IN DIRECTX
525        // -SHOULD- BE FINE AFTER THE PROGRAM GETS PORTED TO EIHORT (OGRE 1.4)
526
527        // createMaterialTUnit2->setBindingType(BT_VERTEX);
528   
529       
530       
531        createMaterialTUnit2->setTextureFiltering (FO_POINT, FO_POINT, FO_NONE );
532    createMaterialTUnit2->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP); // mode clamp for this texture
533
534#ifndef SNOW
535
536    String maskName = "masques/masque_" + StringConverter::toString(mRainWidth * 5.0f) + "_.tga";
537    TextureUnitState *createMaterialTUnit3 = dropsTexturesMaterial->getTechnique (0)->getPass (0)->createTextureUnitState ();
538    createMaterialTUnit3->setTextureName (maskName);  // because we start with billboard width = 0.5
539    createMaterialTUnit3->setTextureFiltering (FO_POINT, FO_POINT, FO_NONE );
540    createMaterialTUnit3->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP); // mode clamp for this texture
541#endif
542
543#ifdef STREAKS
544
545    TextureUnitState *createMaterialTUnit4 = dropsTexturesMaterial->getTechnique (0)->getPass (0)->createTextureUnitState ();
546    createMaterialTUnit4->setTextureName ("shape.tga");
547    createMaterialTUnit4->setTextureFiltering (FO_POINT, FO_POINT, FO_NONE );
548    createMaterialTUnit4->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP); // mode clamp for this texture
549    dropsTexturesMaterial->getTechnique (0)->getPass (0)->setSceneBlending(SBT_TRANSPARENT_ALPHA);
550#endif
551
552#ifdef LIGHT_EXTENSION
553
554    TextureUnitState *createMaterialTUnit5 = dropsTexturesMaterial->getTechnique (0)->getPass (0)->createTextureUnitState ();
555    createMaterialTUnit5->setTextureName (LIGHT_INFO_TEXTURE_NAME);
556    createMaterialTUnit5->setTextureFiltering (FO_POINT, FO_POINT, FO_NONE );
557    createMaterialTUnit5->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP); // mode clamp for this texture
558#endif
559
560    dropsTexturesMaterial->getTechnique (0)->setLodIndex(0);
561
562    dropsTexturesMaterial->setDepthCheckEnabled (true);   // we activate depth test
563    dropsTexturesMaterial->setDepthWriteEnabled (true);
564
565    GPUmaterial();
566
567    dropsTexturesMaterial->compile();
568}
569
570
571
572
573
574
575Camera* CloseDropsParticles::createDropsCamera()
576{
577    Camera *dropsCamera;
578    String camName;
579
580    dropsCamera = new Ogre::Camera("refract_drops_camera", mSceneMgr);
581
582    dropsCamera->setPosition(Vector3(0, 0, 0));
583    dropsCamera->setNearClipDistance( 1 );
584    dropsCamera->setFarClipDistance( 10000 );
585    dropsCamera->setFOVy(Ogre::Radian(FOVyRtt));  // large FOVy, 170
586    dropsCamera->setAspectRatio(4.0f/3.0f);
587    dropsCamera->setLodBias(0.01);
588
589    return dropsCamera;
590}
591
592
593void CloseDropsParticles::updateFragmentProgramParameter(const String &name, float value)
594{
595    GpuProgramParametersSharedPtr paramsFP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
596
597    paramsFP->setNamedConstant(name, value);
598}
599
600
601
602void CloseDropsParticles::updateFragmentProgramParameter(const String &name, Vector3 value)
603{
604    GpuProgramParametersSharedPtr paramsFP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
605
606    paramsFP->setNamedConstant(name, value);
607}
608
609
610
611void CloseDropsParticles::updateFragmentProgramParameter(const String &name, Vector4 value)
612{
613    GpuProgramParametersSharedPtr paramsFP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
614
615    paramsFP->setNamedConstant(name, value);
616}
617
618
619
620void CloseDropsParticles::updateTimeSinceLastFrame(float tslf)
621{
622    String tmpstr = String("rtt_material_using_") + PARTICLE_POSITION_TEXTURE_NAME;
623    GpuProgramParametersSharedPtr paramsPosFP = static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(tmpstr))->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
624    paramsPosFP->setNamedConstant("tslf", tslf);
625    // two distinct materials are used for position update, let's synchronize the second
626    tmpstr = String("rtt_material_using_") + PARTICLE_POSITION_BACKUP_TEXTURE_NAME;
627    paramsPosFP = static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(tmpstr))->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
628    paramsPosFP->setNamedConstant("tslf", tslf);
629}
630
631
632void CloseDropsParticles::updateRainBilboardSideLength(float rainBbSide)
633{
634    GpuProgramParametersSharedPtr paramsVP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getVertexProgramParameters ();
635    paramsVP->setNamedConstant("rainBbSide", rainBbSide);
636}
637
638void CloseDropsParticles::updateStreaksHeight(float streaksHeight)
639{
640#ifdef STREAKS
641    GpuProgramParametersSharedPtr paramsVP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getVertexProgramParameters ();
642    paramsVP->setNamedConstant("streaksHeight", streaksHeight);
643#endif
644
645}
646
647void CloseDropsParticles::updateLightCount(float nbLights)
648{
649#ifdef LIGHT_EXTENSION
650    GpuProgramParametersSharedPtr paramsFP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
651    paramsFP->setNamedConstant("nbLights", nbLights);
652#endif
653}
654
655void CloseDropsParticles::updateViewPos(Vector3 viewPos)
656{
657    GpuProgramParametersSharedPtr paramsVP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getVertexProgramParameters ();
658    paramsVP->setNamedConstant("viewPos", viewPos);
659}
660
661void CloseDropsParticles::updateViewDir(Vector3 viewDir)
662{
663    GpuProgramParametersSharedPtr paramsVP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getVertexProgramParameters ();
664    paramsVP->setNamedConstant("viewDir", viewDir);
665}
666
667void CloseDropsParticles::updateFallVector(Vector3 fallVect)
668{
669    String tmpstr = String("rtt_material_using_") + PARTICLE_POSITION_TEXTURE_NAME;
670    GpuProgramParametersSharedPtr paramsPosFP = static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(tmpstr))->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
671    paramsPosFP->setNamedConstant("fallVect", fallVect);
672    // two distinct materials are used for position update, let's synchronize the second
673    tmpstr = String("rtt_material_using_") + PARTICLE_POSITION_BACKUP_TEXTURE_NAME;
674    paramsPosFP = static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(tmpstr))->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
675    paramsPosFP->setNamedConstant("fallVect", fallVect);
676
677
678#ifdef STREAKS
679    // needed also in the vertex shader, to bend the streaks
680    GpuProgramParametersSharedPtr paramsVP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getVertexProgramParameters ();
681    paramsVP->setNamedConstant("fallVect", fallVect);
682#endif
683}
684
685void CloseDropsParticles::updateBoxMin(Vector3 boxMin)
686{
687    GpuProgramParametersSharedPtr paramsVP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getVertexProgramParameters ();
688    paramsVP->setNamedConstant("boxMin", boxMin);
689}
690
691void CloseDropsParticles::updateBoxMax(Vector3 boxMax)
692{
693    GpuProgramParametersSharedPtr paramsVP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getVertexProgramParameters ();
694    paramsVP->setNamedConstant("boxMax", boxMax);
695}
696
697void CloseDropsParticles::updateLastMove(Vector3 lastMove)
698{
699    String tmpstr = String("rtt_material_using_") + PARTICLE_POSITION_TEXTURE_NAME;
700    GpuProgramParametersSharedPtr paramsPosFP = static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(tmpstr))->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
701    paramsPosFP->setNamedConstant("lastMove", lastMove);
702    // two distinct materials are used for position update, let's synchronize the second
703    tmpstr = String("rtt_material_using_") + PARTICLE_POSITION_BACKUP_TEXTURE_NAME;
704    paramsPosFP = static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(tmpstr))->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
705    paramsPosFP->setNamedConstant("lastMove", lastMove);
706}
707
708void CloseDropsParticles::updateRandomMove(Vector3 randomMove)
709{
710    String tmpstr = String("rtt_material_using_") + PARTICLE_POSITION_TEXTURE_NAME;
711    GpuProgramParametersSharedPtr paramsPosFP = static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(tmpstr))->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
712    paramsPosFP->setNamedConstant("randomMove", randomMove);
713    // two distinct materials are used for position update, let's synchronize the second
714    tmpstr = String("rtt_material_using_") + PARTICLE_POSITION_BACKUP_TEXTURE_NAME;
715    paramsPosFP = static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(tmpstr))->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
716    paramsPosFP->setNamedConstant("randomMove", randomMove);
717}
718
719
720
721
722
723void CloseDropsParticles::updateShaderParameters(float tslf, float rainBbSide,
724        float streaksHeight, int nbLights,
725        Vector3 viewPos, Vector3 viewDir, Vector3 fallVect, Vector3 boxMin, Vector3 boxMax, Vector3 lastMove, Vector3 randomMove, int useShader)
726{
727    // data to update :
728
729    // floats :
730    // time since last frame (in seconds)
731    // particle count
732    // billboard position texture side length
733    // billboard side length
734    // streaks height
735    // number of lights
736
737    // vec3 :
738    // viewer position
739    // view direction
740    // fall vector (including wind)
741    // minimum box position
742    // maximum box position
743    // viewer movement to compensate (in box coordinates)
744    // random displacement when respawning the particle
745
746
747    // actualizing parameters for the position update shader
748
749    String tmpstr = String("rtt_material_using_") + PARTICLE_POSITION_TEXTURE_NAME;
750    GpuProgramParametersSharedPtr paramsPosFP = static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(tmpstr))->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
751    paramsPosFP->setNamedConstant("tslf", tslf);
752    paramsPosFP->setNamedConstant("randomMove", randomMove);
753    paramsPosFP->setNamedConstant("lastMove", lastMove);
754    paramsPosFP->setNamedConstant("fallVect", fallVect);
755    // two distinct materials are used for position update, let's synchronize the second
756    tmpstr = String("rtt_material_using_") + PARTICLE_POSITION_BACKUP_TEXTURE_NAME;
757    paramsPosFP = static_cast<MaterialPtr>(MaterialManager::getSingleton().getByName(tmpstr))->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
758    paramsPosFP->setNamedConstant("tslf", tslf);
759    paramsPosFP->setNamedConstant("randomMove", randomMove);
760    paramsPosFP->setNamedConstant("lastMove", lastMove);
761    paramsPosFP->setNamedConstant("fallVect", fallVect);
762
763
764    // actualizing vertex program parameters
765
766    GpuProgramParametersSharedPtr paramsVP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getVertexProgramParameters ();
767    paramsVP->setNamedConstant("viewPos", viewPos);
768    paramsVP->setNamedConstant("viewDir", viewDir);
769    paramsVP->setNamedConstant("boxMin", boxMin);
770    paramsVP->setNamedConstant("boxMax", boxMax);
771    paramsVP->setNamedConstant("rainBbSide", rainBbSide);
772#ifdef STREAKS
773
774    paramsVP->setNamedConstant("streaksHeight", streaksHeight);
775#endif
776
777
778    // actualizing fragment program parameters
779
780    GpuProgramParametersSharedPtr paramsFP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
781#ifdef LIGHT_EXTENSION
782
783    paramsFP->setNamedConstant("nbLights", nbLights);
784#endif
785}
786
787
788void CloseDropsParticles::GPUmaterial()
789{
790    // vertex program which will modify texture coordinates
791    HighLevelGpuProgramPtr vp = HighLevelGpuProgramManager::getSingleton().createProgram("gouttesTexCoordsVP", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "cg", GPT_VERTEX_PROGRAM);
792    vp->setSourceFile("gouttesTexCoordsVP.cg");
793    vp->setParameter("profiles", "vs_3_0 vp40");
794#ifdef STREAKS
795
796    vp->setParameter("entry_point", "mainStreaks");
797#else
798
799    vp->setParameter("entry_point", "mainNoStreaks");
800#endif
801
802    // vertex program binding
803    dropsTexturesMaterial->getTechnique (0)->getPass (0)->setVertexProgram ("gouttesTexCoordsVP");
804
805    // we give it some parameters
806    GpuProgramParametersSharedPtr paramsVP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getVertexProgramParameters ();
807    paramsVP->setNamedAutoConstant("worldviewproj", GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
808    paramsVP->setNamedAutoConstant("worldview", GpuProgramParameters::ACT_WORLDVIEW_MATRIX);
809
810
811    // fragment program which will map the generated textures
812    HighLevelGpuProgramPtr fp = HighLevelGpuProgramManager::getSingleton().createProgram("gouttesTexCoordsFP", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "cg", GPT_FRAGMENT_PROGRAM);
813    fp->setSourceFile("gouttesTexCoordsFP.cg");
814    fp->setParameter("profiles", "ps_3_0 fp40");
815
816
817#ifdef SNOW
818
819    fp->setParameter("entry_point", "main_Snow_noLight");
820
821#else
822
823#ifdef STREAKS
824
825#ifdef LIGHT_EXTENSION
826
827    fp->setParameter("entry_point", "main_Streaks_Light");
828#else  // LIGHT
829
830    fp->setParameter("entry_point", "main_Streaks_noLight");
831#endif  // LIGHT
832
833#else  // STREAKS
834
835#ifdef LIGHT_EXTENSION
836
837    fp->setParameter("entry_point", "main_noStreaks_Light");
838#else  // LIGHT
839
840    fp->setParameter("entry_point", "main_noStreaks_noLight");
841#endif  // LIGHT
842
843#endif  // STREAKS
844
845#endif  // SNOW
846
847
848
849    dropsTexturesMaterial->getTechnique (0)->getPass (0)->setFragmentProgram ("gouttesTexCoordsFP");
850
851
852#ifdef LIGHT_EXTENSION
853
854    GpuProgramParametersSharedPtr paramsFP = dropsTexturesMaterial->getTechnique (0)->getPass (0)->getFragmentProgramParameters ();
855    paramsFP->setNamedAutoConstant("worldview", GpuProgramParameters::ACT_WORLDVIEW_MATRIX);
856
857    // lighting
858    paramsFP->setNamedAutoConstant("colAmb", GpuProgramParameters::ACT_AMBIENT_LIGHT_COLOUR);
859
860#endif  //LIGHT
861}
Note: See TracBrowser for help on using the repository browser.