source: GTP/trunk/App/Demos/Illum/Illumination Networks Demo [OpenGL]/src/PreIllumSystem.cpp @ 845

Revision 845, 28.6 KB checked in by szirmay, 18 years ago (diff)
Line 
1#include ".\preillumsystem.h"
2
3#include <list>
4
5
6PreIllumSystem::~PreIllumSystem(void)
7{
8}
9
10float PreIllumSystem::Phase(Vector diri,Vector dirj,float symmetry)
11{
12        float anglecos=diri*dirj;
13        float g=symmetry;
14        float g2=g*g;
15        float phase=3*(1-g2)*(1+anglecos*anglecos)/2/(2+g2)/pow((1+g2-2*anglecos*g),1.5f);
16        return phase/(4*M_PI);
17        //return 1.0/(4*M_PI);
18}
19
20void PreIllumSystem::Calculate_Up_Right_Vector(Vector viewdir,Vector& UpVector,Vector& RightVector)
21{
22        if(viewdir.y==0)
23        {
24                UpVector.x=0;UpVector.y=1;UpVector.z=0;
25        }
26        else
27        {
28                Vector projected;
29                projected.x=viewdir.x;projected.z=viewdir.z;
30                projected.y=0;
31                projected.Normalize();
32       
33                UpVector=viewdir-projected;
34                UpVector.Normalize();
35        }
36        RightVector=UpVector%viewdir;
37        RightVector.Normalize();
38}
39
40PreIllumSystem::GetNearestDirection(Vector LightPosition)
41{       
42        Vector dir=LightPosition;
43        dir.Normalize();
44       
45        float angle1,angle2;
46        //transform to polar coordinates
47        angle2=asin(dir.y); // -pi/2 -- pi/2
48       
49        dir.y=0;
50        dir.Normalize();
51
52        float sign;
53        if(dir.z>0)sign=1;
54        if(dir.z<0)sign=-1;
55        if(dir.z==0)sign=0;
56
57        angle1=acos(dir.x)*sign; // -pi -- pi
58       
59        Vector dirre(cos(angle1)*cos(angle2),
60                                                        sin(angle2),
61                                                        sin(angle1)*cos(angle2));
62       
63        angle1=angle1/6.28+0.5; // 0 -- 1
64        angle2=angle2/3.14+0.5; // 0 -- 1
65
66        int coord1=angle1*512;
67        int coord2=angle2*512;
68        int index=coord2*512*4+coord1*4;
69
70        if(index+3>512*512*4) exit(1);
71        m_NearestDir=m_DirectionData[index];
72        m_NearestDir2=m_DirectionData[index+1];
73        m_Weight1=m_DirectionData[index+2];
74        m_Weight2=m_DirectionData[index+3];     
75}
76
77void PreIllumSystem::CreateGivenDirections()
78{       /*
79        m_Directions=new Vector[4];     
80
81        m_Directions[0]=Vector(1,0,0);
82        m_Directions[1]=Vector(0,0,1);
83        m_Directions[2]=Vector(0,0,-1);
84        m_Directions[3]=Vector(-1,0,0);
85        */
86        m_Directions=new Vector[m_DirectionCount];
87        for(int i=0;i<m_DirectionCount/2;i++)
88        {
89                float angle=(float)i*2.0*3.14/(float)m_DirectionCount;
90                Vector dir(cos(angle),0,sin(angle));
91                m_Directions[i]=dir;
92                m_Directions[m_DirectionCount-1-i]=dir*-1;
93        }
94}
95
96void PreIllumSystem::CreateRandomDirections(bool fillarray=false)
97{       
98        Halton Halton1(1,7);
99        Halton Halton2(1,13);
100        Halton Halton3(1,17);
101
102        if(fillarray)
103        m_DirectionArray=new float[3*m_DirectionCount];
104       
105        m_Directions=new Vector[m_DirectionCount];     
106       
107        for(int i=0;i<m_DirectionCount/2;i++)
108        {
109        /*      Vector randpoint(RangeRandom(-1,1),
110                                                        RangeRandom(-1,1),
111                                                        RangeRandom(-1,1));*/
112                Vector randpoint((float)Halton1.Random(),
113                                                        (float)Halton2.Random(),
114                                                        (float)Halton3.Random());
115
116                randpoint.Normalize();
117                if(fillarray)
118                {
119                        m_DirectionArray[m_DirectionCount-3*(i+1)]=randpoint.x;
120                        m_DirectionArray[m_DirectionCount-3*(i+1)+1]=randpoint.y;
121                        m_DirectionArray[m_DirectionCount-3*(i+1)+2]=randpoint.z;
122                }
123                m_Directions[m_DirectionCount-(i+1)]=randpoint;
124                randpoint=randpoint*-1;
125                if(fillarray)
126                {
127                        m_DirectionArray[3*i]=randpoint.x;
128                        m_DirectionArray[3*i+1]=randpoint.y;
129                        m_DirectionArray[3*i+2]=randpoint.z;
130                }
131                m_Directions[i]=randpoint;
132        }       
133}
134
135void PreIllumSystem::CreateVisibilityTexture()
136{               
137        float *data=new float[m_DirectionCount*m_ParticleCount*3];
138        for(int m=0;m<m_DirectionCount*m_ParticleCount;m++)
139        {
140                data[3*m]=m_ParticleCount;             
141                data[3*m+1]=0;         
142                data[3*m+2]=0;  //channel not used     
143        }
144        /*
145        //distances
146        float *distances=new float[m_DirectionCount*m_ParticleCount];
147        for(int m=0;m<m_DirectionCount*m_ParticleCount;m++)
148        {
149                distances[m]=-1;//initial value
150        }
151*/
152        CreateRandomDirections(true);
153        //CreateGivenDirections();
154
155        Vector RightVector;
156        Vector UpVector;
157        Vector ViewDir;
158
159        float particleradius=m_System.m_Emitter.getSize();
160        Vector temp;
161
162        for(int d=0;d<m_DirectionCount;d++)
163        {
164               
165                ViewDir=m_Directions[d];
166                Calculate_Up_Right_Vector(m_Directions[d],UpVector,RightVector);
167                       
168                for(int i=0;i<m_ParticleCount;i++)
169                {
170                       
171                        Vector particle0=m_ParticleArray[i].getPosition();
172                        //adott részecske transzformálása
173                       
174                        temp=particle0;
175                        particle0.x=temp.x*RightVector.x+temp.y*RightVector.y+temp.z*RightVector.z;
176                        particle0.y=temp.x*UpVector.x+temp.y*UpVector.y+temp.z*UpVector.z;
177                        particle0.z=temp.x*ViewDir.x+temp.y*ViewDir.y+temp.z*ViewDir.z;
178
179                        for(int j=0;j<m_ParticleCount;j++)
180                        {
181
182                                if(i!=j)
183                                {
184                                        Vector particle=m_ParticleArray[j].getPosition();
185                                        temp=particle;
186                                        //transform
187                                        particle.x=temp.x*RightVector.x+temp.y*RightVector.y+temp.z*RightVector.z;
188                                        particle.y=temp.x*UpVector.x+temp.y*UpVector.y+temp.z*UpVector.z;
189                                        particle.z=temp.x*ViewDir.x+temp.y*ViewDir.y+temp.z*ViewDir.z;
190
191                                        //visible?
192                                        if(abs(particle.x-particle0.x)<2*particleradius&&abs(particle.y-particle0.y)<2*particleradius)
193                                        {
194                                                float z=particle.z-particle0.z;
195                                               
196                                                if(z>0)
197                                                {
198                                                        //if(z<distances[d*m_ParticleCount+i]||distances[d*m_ParticleCount+i]==-1)
199                                                        if(z<data[3*d*m_ParticleCount+3*i+1]||data[3*d*m_ParticleCount+3*i+1]==0)
200                                                        {
201                                                                //distances[d*m_ParticleCount+i]=z;
202                                                                data[3*d*m_ParticleCount+3*i]=j;
203                                                                data[3*d*m_ParticleCount+3*i+1]=z;
204                                                        }
205                                                }                                               
206                                        }
207                                }
208                        }
209                       
210                       
211                }
212
213        }
214               
215        glGenTextures(1, &m_VisibilityTexID);
216        glBindTexture(GL_TEXTURE_RECTANGLE_NV, m_VisibilityTexID);
217       
218        /*glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
219        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);*/
220        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER,
221        GL_NEAREST);
222        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER,
223        GL_NEAREST);
224
225        glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGB16_NV, m_ParticleCount,
226                m_DirectionCount, 0,GL_RGB , GL_FLOAT,
227        data);
228       
229        delete[] data;         
230}
231
232void PreIllumSystem::CreateTauTexture()
233{
234        float* taus=new float[m_ParticleCount];
235
236        int samplecount=1;
237               
238        for(int i=0;i<m_ParticleCount;i++)
239        {
240                int count=0;
241
242                std::list <float> distlist;
243                std::list <float>::iterator Iter;
244
245                for(int j=0;j<m_ParticleCount;j++)
246                {
247                        if(i!=j)
248                        {
249                                float dist=(m_ParticleArray[j].getPosition()-m_ParticleArray[i].getPosition()).Length();
250                                distlist.push_back(dist);
251                        }
252                }
253               
254                distlist.sort();
255                float sum=0;
256                Iter=distlist.begin();
257                for(int h=0;h<samplecount;h++)
258                {
259                        sum+=*Iter;
260                        Iter++;
261                }
262                taus[i]=sum/(float)samplecount;
263                //fprintf(stderr, "avrdist: %f\n",taus[i]);
264        }
265       
266        glGenTextures(1, &m_TauTextureID);
267        glBindTexture(GL_TEXTURE_RECTANGLE_NV, m_TauTextureID);
268       
269        /*glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
270        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);*/
271        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER,
272        GL_NEAREST);
273        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER,
274        GL_NEAREST);
275
276        glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RG16_NV, m_ParticleCount,
277                1, 0,GL_RED , GL_FLOAT,
278        taus);
279       
280}
281
282void PreIllumSystem::CreateNearestDirectionTexture()
283{
284        m_DirectionData=new float[512*512*4];
285        for(int i=0;i<512*512*4;i++)
286        {
287                m_DirectionData[i]=0;
288        }
289
290        float distance=0;
291       
292        float nearestdist=-1;
293        float nearestdist2=-1;
294        int nearestdirid=-1;
295        int nearestdirid2=-1;
296
297
298        for(int i=0;i<512;i++)
299        {
300                for(int j=0;j<512;j++)
301                {
302                        float angle1=2*M_PI/512*i-M_PI;
303                        float angle2=M_PI/512*j-M_PI/2;
304
305                        distance=0;
306                        nearestdist=-1;
307                        nearestdist2=-1;
308                        nearestdirid=-1;
309                        nearestdirid2=-1;
310
311                        Vector dir(cos(angle1)*cos(angle2),
312                                                        sin(angle2),
313                                                        sin(angle1)*cos(angle2));
314                        dir.Normalize();
315
316                        angle1=angle1/6.28+0.5; // 0 -- 1
317                        angle2=angle2/3.14+0.5; // 0 -- 1
318
319                        int coord1=angle1*512;
320                        int coord2=angle2*512;
321                       
322                        for(int k=0;k<m_DirectionCount;k++)
323                        {
324                                distance=(dir-m_Directions[k]).Length();
325                                //nearest direction
326                                if(distance<nearestdist||nearestdist==-1)
327                                {
328                                        nearestdist=distance;
329                                        nearestdirid=k;
330                                }
331                                else
332                                {
333                                        //second nearest direction
334                                        if(distance<nearestdist2||nearestdist2==-1)
335                                        {
336                                                nearestdist2=distance;
337                                                nearestdirid2=k;
338                                        }
339                                }
340                        }
341                        //m_DirectionData[i*512+j]=UnitRandom();
342                        m_DirectionData[j*512*4+4*i]=nearestdirid;
343                        m_DirectionData[j*512*4+4*i+1]=nearestdirid2;
344                        //weights
345                        float weight1=1-(nearestdist/(nearestdist+nearestdist2));
346                        float weight2=1-(nearestdist2/(nearestdist+nearestdist2));
347                        m_DirectionData[j*512*4+4*i+2]=weight1;
348                        m_DirectionData[j*512*4+4*i+3]=weight2;
349                }
350        }
351
352       
353        glGenTextures(1, &m_DirectionsTexID);
354        glBindTexture(GL_TEXTURE_2D, m_DirectionsTexID);
355       
356        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
357        GL_NEAREST);
358        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
359        GL_NEAREST);
360
361        glTexImage2D(GL_TEXTURE_2D, 0, GL_FLOAT_RGBA16_NV, 512,
362        512, 0, GL_RGBA, GL_FLOAT,
363        m_DirectionData);
364
365        //delete[] m_DirectionData;     
366}
367
368void PreIllumSystem::CreatePhaseTexture()
369{
370        float* data=new float[m_DirectionCount*m_DirectionCount];
371
372        for(int i=0;i<m_DirectionCount;i++)
373        {
374                Vector diri=m_Directions[i]*-1;
375
376                for(int j=0;j<m_DirectionCount;j++)
377                {
378                        Vector dirj=m_Directions[j];
379                       
380                        data[j*m_DirectionCount+i]=Phase(diri,dirj,m_Symmetry);                 
381                }
382        }
383
384        glGenTextures(1, &m_PhaseTextureID);
385        glBindTexture(GL_TEXTURE_RECTANGLE_NV, m_PhaseTextureID);
386       
387        /*glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
388        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);*/
389       
390        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER,
391        GL_NEAREST);
392        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER,
393        GL_NEAREST);
394
395        glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_LUMINANCE_FLOAT16_ATI, m_DirectionCount,
396                m_DirectionCount, 0, GL_LUMINANCE, GL_FLOAT,    data);
397
398        delete[] data;
399}
400
401void PreIllumSystem::CreateLVisMap()
402{
403       
404        Texture Bbtex;
405        Bbtex.setFilename("kor.bmp");
406        Bbtex.LoadImage();
407
408        glBindTexture(GL_TEXTURE_2D,Bbtex.getTextureHandler());
409        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
410                GL_NEAREST);
411        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
412        GL_NEAREST);
413
414        m_LVisMap=new unsigned char[m_DirectionCount*m_ParticleCount]; 
415        for(int i=0;i<m_DirectionCount*m_ParticleCount;i++)
416        {
417                m_LVisMap[i]=0;
418        }
419       
420        m_LVisPrograms.SetFragmentTexParam("BbTexture",Bbtex.getTextureHandler());
421       
422        m_Target.EnablewithColorRelease();
423       
424        m_LVisPrograms.Enable();   
425       
426       
427        Vector RightVector;
428        Vector UpVector;
429       
430               
431        for(int i=0;i<m_DirectionCount;i++)
432        {
433                Vector CamPos=m_Directions[i]*-1;
434               
435                Calculate_Up_Right_Vector(CamPos,UpVector,RightVector);
436                       
437                CamPos=CamPos*-1*m_System.getBoundingRadius();         
438               
439                glClearColor(m_ParticleCount,1,1,1);
440                glClearDepth(1.0);
441                glEnable(GL_DEPTH_TEST);
442                glDepthFunc(GL_LESS);
443                glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
444
445                glMatrixMode(GL_PROJECTION);
446                glLoadIdentity();
447                glOrtho(-m_System.getBoundingRadius(),m_System.getBoundingRadius(),-m_System.getBoundingRadius(),m_System.getBoundingRadius(),0,2*m_System.getBoundingRadius());
448                glMatrixMode(GL_MODELVIEW);
449                glLoadIdentity();
450                gluLookAt(CamPos.x,CamPos.y,CamPos.z,0,0,0,UpVector.x,UpVector.y,UpVector.z);
451                //render particles
452                m_TempCamera.SetPosition(CamPos);
453               
454                m_System.SortFromCamera(&m_TempCamera,true,true);
455                glEnable(GL_BLEND);
456                glDisable(GL_DEPTH_TEST);
457               
458                glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
459                int pointsize=m_System.m_Emitter.getSize()*m_LightWindowSize/m_System.getBoundingRadius();
460                glPointSize(pointsize);
461               
462                m_System.RenderAsPoints(true);
463
464                glReadPixels(0,0,m_LightWindowSize,m_LightWindowSize,GL_RED,GL_FLOAT,m_Pixels);
465               
466                for(int j=0;j<m_LightWindowSize*m_LightWindowSize;j++)
467                {
468                        if(m_Pixels[j]<m_ParticleCount)
469                        {                               
470                                m_LVisMap[i*m_ParticleCount+(int)m_Pixels[j]]+=50;
471                                //m_LVisMap[i*m_ParticleCount+(int)m_Pixels[j]]=255;
472                        }
473                }
474        }
475
476        glDisable(GL_BLEND);
477        m_LVisPrograms.DisableFragmentTexParam("BbTexture");   
478        m_LVisPrograms.Disable();
479
480        m_Target.DisablewithColorBind();
481
482        glGenTextures(1, &m_LVisMapID);
483        glBindTexture(GL_TEXTURE_RECTANGLE_NV, m_LVisMapID);
484       
485        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER,
486                GL_NEAREST);
487        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER,
488        GL_NEAREST);
489       
490        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S,
491        GL_CLAMP);
492        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T,
493        GL_CLAMP);
494
495        glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_LUMINANCE,m_ParticleCount,
496                m_DirectionCount, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE,m_LVisMap);
497
498        delete[] m_LVisMap;
499}
500
501void PreIllumSystem::FindVisiblesWithRendering(Vector LightPosition,int row)
502{
503       
504       
505        for(int i=0;i<m_ParticleCount;i++)
506        {
507                m_LRendVisMap[row*m_ParticleCount+i]=0;
508        }       
509       
510        m_LVisPrograms.SetFragmentTexParam("BbTexture",m_Bbtex.getTextureHandler());
511       
512        m_Target.EnablewithColorRelease();
513
514        m_LVisPrograms.Enable();
515
516       
517        Vector RightVector;
518        Vector UpVector;
519
520                Vector CamPos=LightPosition;
521                CamPos.Normalize();
522                CamPos=CamPos*-1;
523
524                Calculate_Up_Right_Vector(CamPos,UpVector,RightVector);
525                       
526                CamPos=CamPos*-1*m_System.getBoundingRadius();         
527               
528                glClearColor(m_ParticleCount,1,1,1);
529                glClearDepth(1.0);
530                glEnable(GL_DEPTH_TEST);
531                glDepthFunc(GL_LESS);
532                glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
533
534                glMatrixMode(GL_PROJECTION);
535                glLoadIdentity();
536                glOrtho(-m_System.getBoundingRadius(),m_System.getBoundingRadius(),-m_System.getBoundingRadius(),m_System.getBoundingRadius(),0,2*m_System.getBoundingRadius());
537                glMatrixMode(GL_MODELVIEW);
538                glLoadIdentity();
539                gluLookAt(CamPos.x,CamPos.y,CamPos.z,0,0,0,UpVector.x,UpVector.y,UpVector.z);
540               
541                //render particles
542                m_TempCamera.SetPosition(CamPos);               
543                m_System.SortFromCamera(&m_TempCamera,true,true);
544                glEnable(GL_BLEND);
545                glDisable(GL_DEPTH_TEST);
546                glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
547               
548                float pointsize=m_LightWindowSize/m_System.getBoundingRadius();
549                glPointSize(pointsize);
550               
551                //glDisable(GL_BLEND);
552                //glEnable(GL_DEPTH_TEST);
553                m_System.RenderAsPoints(true);
554
555                glReadPixels(0,0,m_LightWindowSize,m_LightWindowSize,GL_RED,GL_FLOAT,m_Pixels);
556               
557                for(int j=0;j<m_LightWindowSize*m_LightWindowSize;j++)
558                {
559                        if(m_Pixels[j]<m_ParticleCount)
560                        {                               
561                                m_LRendVisMap[m_ParticleCount*row+(unsigned int)m_Pixels[j]]=255;
562                                //m_LRendVisMap[(unsigned int)m_Pixels[j]]+=100;
563                        }
564                }
565       
566        glDisable(GL_BLEND);
567               
568        m_LVisPrograms.DisableFragmentTexParam("BbTexture");
569        m_LVisPrograms.Disable();
570       
571        m_Target.DisablewithColorBind();
572
573        glBindTexture(GL_TEXTURE_RECTANGLE_NV,m_RenderedVisID);
574       
575        glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_LUMINANCE,m_ParticleCount,
576                2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE,m_LRendVisMap);
577
578}
579
580void PreIllumSystem::Init(int particlecount,int directioncount)
581{
582        m_ParticleCount=particlecount;
583        m_DirectionCount=directioncount;
584
585        m_TexRectPrograms.SetProgramFiles("SimplePrograms.cg","SimplePrograms.cg");
586        m_TexRectPrograms.SetProgramEntries("VertexProgram","FragmentProgram");
587        m_TexRectPrograms.InitPrograms();
588       
589        m_LightIllumPrograms.SetProgramFiles("LightIlluminatePrograms.cg","LightIlluminatePrograms.cg");
590        //m_LightIllumPrograms.SetProgramFiles("InternalLightIlluminatePrograms.cg","InternalLightIlluminatePrograms.cg");
591        m_LightIllumPrograms.SetProgramEntries("VertexProgram","FragmentProgram");
592        m_LightIllumPrograms.InitPrograms();
593       
594    m_IllumIteratePrograms.SetProgramFiles("IllumIteratePrograms.cg","IllumIteratePrograms.cg");
595        m_IllumIteratePrograms.SetProgramEntries("VertexProgram","FragmentProgram");
596        m_IllumIteratePrograms.InitPrograms();
597       
598        m_EyeRadPrograms.SetProgramFiles("FinalizeIllumMap.cg","FinalizeIllumMap.cg");
599        m_EyeRadPrograms.SetProgramEntries("VertexProgram","FragmentProgram");
600        m_EyeRadPrograms.InitPrograms();
601
602        m_LVisPrograms.SetProgramFiles("LVisDisplay.cg","LVisDisplay.cg");
603        m_LVisPrograms.SetProgramEntries("VertexProgram","FragmentProgram");
604        m_LVisPrograms.InitPrograms(); 
605       
606        m_FinalRenderPrograms.SetProgramFiles("FinalDisplay.cg","FinalDisplay.cg");
607        m_FinalRenderPrograms.SetProgramEntries("VertexProgram","FragmentProgram");
608        m_FinalRenderPrograms.InitPrograms();
609               
610        m_DirectIlumTexture.Initialize(m_ParticleCount,m_DirectionCount,"rgb float=16 textureRECT");
611        m_IllumTexture.Initialize(m_ParticleCount,m_DirectionCount,"rgb float=16 textureRECT");
612        m_IllumTexture2.Initialize(m_ParticleCount,m_DirectionCount,"rgb float=16 textureRECT");
613        m_EyeRadTexture.Initialize(m_ParticleCount,1,"rgb float=16 textureRECT");
614        m_Target.Initialize(m_LightWindowSize,m_LightWindowSize,"rgba float=16 depth=16 textureRECT");
615        m_ImpostorTexture.Initialize(512,512,"rgba texture2D",false);
616
617        m_Pixels=new float[m_LightWindowSize*m_LightWindowSize];
618        m_LRendVisMap=new unsigned char[m_ParticleCount*2];
619
620        InitSystem(particlecount,directioncount);
621
622        m_BillboardTexture.setFilename("proba.dds");
623        m_BillboardTexture.LoadImage();
624
625        m_Bbtex.setFilename("kor.bmp");
626        m_Bbtex.LoadImage();
627
628        glBindTexture(GL_TEXTURE_2D,m_Bbtex.getTextureHandler());
629        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
630                GL_NEAREST);
631        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
632        GL_NEAREST);
633
634        glGenTextures(1,&m_RenderedVisID);
635        glBindTexture(GL_TEXTURE_RECTANGLE_NV,m_RenderedVisID);
636        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER,
637                GL_NEAREST);
638        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER,
639        GL_NEAREST);   
640        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S,
641        GL_CLAMP);
642        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T,
643        GL_CLAMP);
644
645        m_DirectIlumTexture.EnablewithColorRelease();
646        glClearColor(0,0,0,0);//Emission
647        glClear(GL_COLOR_BUFFER_BIT);
648        m_DirectIlumTexture.DisablewithColorBind();             
649        m_EyeRadTexture.EnablewithColorRelease();
650        glClearColor(0,0,0,0);
651        glClear(GL_COLOR_BUFFER_BIT);
652        m_EyeRadTexture.DisablewithColorBind();
653
654}       
655
656void PreIllumSystem::InitSystem(int particlecount,int directioncount)
657{
658        m_System.m_Emitter.setPositionsFile("cloud.txt");
659        m_System.m_Emitter.setOffset(0.15);
660        m_System.m_Emitter.setPosition(Vector(-0.625,-6,-1.75));
661       
662        m_System.m_Emitter.setRadius(0.2);
663        //7,4,5
664       
665        m_System.m_Emitter.setSize(0.95);
666        m_System.m_Emitter.setSizevariation(0.9);
667        m_System.m_Emitter.setEmissionRate(0);
668        m_System.setQuota(m_ParticleCount);
669        m_System.m_Emitter.setTimeToLive(0);
670        m_System.m_Emitter.setTimeToLiveVariation(0);
671        m_System.m_Emitter.setVelocity(0);
672        m_System.m_Emitter.setVelocityVariation(0);
673
674        m_System.RefreshParticles(0,0);
675       
676        m_System.RefreshCamera(m_EyeCamera);
677        m_System.RefrBRadius_Dist();
678        m_System.RefreshBuffer(false);
679       
680        //m_System.SortFromCamera(m_EyeCamera,true,true);
681
682        m_ParticleArray=m_System.getParticleArray();
683        m_PositionArray=m_System.getVertexBuffer();
684
685        unsigned int lasttime=timeGetTime();
686        fprintf(stderr, "Building Visibility Map: ..");
687        this->CreateVisibilityTexture();
688        unsigned int thistime=timeGetTime();
689        float elapsed=(float)(thistime-lasttime)/1000.0f;
690        lasttime=thistime;
691        fprintf(stderr, "OK %fs\n",elapsed);
692       
693
694        fprintf(stderr, "Building Nearest Directions Map: ..");
695        this->CreateNearestDirectionTexture();
696        thistime=timeGetTime();
697        elapsed=(float)(thistime-lasttime)/1000.0f;
698        lasttime=thistime;
699        fprintf(stderr, "OK %fs\n",elapsed);
700
701        fprintf(stderr, "Building Phase Function Map: ..");
702        CreatePhaseTexture();
703        thistime=timeGetTime();
704        elapsed=(float)(thistime-lasttime)/1000.0f;
705        lasttime=thistime;
706        fprintf(stderr, "OK %fs\n",elapsed);
707
708        fprintf(stderr, "Building Tau Map: ..");
709        CreateTauTexture();
710        thistime=timeGetTime();
711        elapsed=(float)(thistime-lasttime)/1000.0f;
712        lasttime=thistime;
713        fprintf(stderr, "OK %fs\n",elapsed);
714/*
715        fprintf(stderr, "Building Light Visibility Map: ..");
716        CreateLVisMap();
717        thistime=timeGetTime();
718        elapsed=(float)(thistime-lasttime)/1000.0f;
719        lasttime=thistime;
720        fprintf(stderr, "OK %fs\n",elapsed);
721*/
722        m_IllumColorTex=&m_IllumTexture2;
723        m_IllumRenderTex=&m_IllumTexture;
724
725        m_IllumTexture2.EnablewithColorRelease();
726                glClearColor(0,0,0,0);
727                glClear(GL_COLOR_BUFFER_BIT);
728        m_IllumTexture2.DisablewithColorBind();
729       
730}
731
732char* PreIllumSystem::DisplayTexture(int tex)
733{
734        char* displaytext;
735        int dimX,dimY;
736        unsigned int TexID;
737        float MaxValue;
738
739        switch(tex)
740        {
741        case 1: //LightView
742        /*      dimX=m_ParticleCount;
743                dimY=m_DirectionCount;
744                TexID=m_VisibilityTexID;
745                MaxValue=m_ParticleCount;*/
746                dimX=m_LightWindowSize;
747                dimY=m_LightWindowSize;
748                TexID=m_Target.getColorTextureID();
749                MaxValue=m_ParticleCount;
750               
751                displaytext="LightView";
752                break; 
753
754        case 2: //LightVisibility
755                dimX=m_ParticleCount;
756                dimY=2;
757                TexID=m_RenderedVisID;
758                MaxValue=1;
759                displaytext="LightVisibility";
760                break; 
761
762        case 3: //DirectIllumTexture
763                dimX=m_ParticleCount;
764                dimY=m_DirectionCount;
765                TexID=m_DirectIlumTexture.getColorTextureID();
766                MaxValue=1;
767                displaytext="DirectIllumTexture";
768                break; 
769
770        case 4: //IllumTexture1
771                dimX=m_ParticleCount;
772                dimY=m_DirectionCount;
773                TexID=m_IllumTexture.getColorTextureID();
774                MaxValue=1;
775                displaytext="IllumTexture1";
776                break; 
777
778        case 5: //IllumTexture2
779                dimX=m_ParticleCount;
780                dimY=m_DirectionCount;
781                TexID=m_IllumTexture2.getColorTextureID();
782                MaxValue=1;
783                displaytext="IllumTexture2";
784                break;
785
786        case 6: //EyeRadTexture
787                dimX=m_ParticleCount;
788                dimY=1;
789                TexID=m_EyeRadTexture.getColorTextureID();
790                MaxValue=1;
791                displaytext="EyeRadTexture";
792                break; 
793        }
794
795        m_TexRectPrograms.Enable();
796
797        m_TexRectPrograms.SetFragmentTexParam("Texture",TexID);
798        m_TexRectPrograms.SetFragmentParam1f("invmaxvalue",1.0/MaxValue);
799
800        m_ScreenQuad.DisplayScreenQuad(dimX,dimY);
801
802        m_TexRectPrograms.Disable();
803
804        return displaytext;
805}
806
807void PreIllumSystem::Refresh(Vector lightpos,Vector lightpos2,Vector lightcolor,Vector lightcolor2)
808{       
809/*
810        m_IllumColorTex->EnablewithColorRelease();
811        glClearColor(0,0,0,0);
812        glClear(GL_COLOR_BUFFER_BIT);
813        m_IllumColorTex->DisablewithColorBind();
814*/
815        static bool first=true;
816       
817        ////Light1
818        FindVisiblesWithRendering(lightpos,0); 
819        GetNearestDirection(lightpos);
820        int lastNearest=m_NearestDir;   
821        m_NearestDir=lastNearest;       
822        m_LightPosition=lightpos;       
823        m_LightColor=lightcolor;
824
825        if(first)
826        {
827                ////Light2
828                FindVisiblesWithRendering(lightpos2,1);
829                GetNearestDirection(lightpos2);
830                int thisNearest=m_NearestDir;
831                m_NearestDir2=thisNearest;
832                m_LightPosition2=lightpos2;     
833               
834                first=false;
835        }
836        m_LightColor2=lightcolor2;
837
838        RefreshDirectIllumTexture();
839
840        for(int i=0;i<m_IterateCount;i++)
841        {
842                Iterate();
843        }
844       
845        GetNearestDirection(m_EyeCamera->getPosition()*-1);
846
847        CreateEyeRadTexture();
848       
849}
850
851void PreIllumSystem::RefreshDirectIllumTexture()
852{       
853        m_LightIllumPrograms.SetFragmentTexParam("LVisMap",m_RenderedVisID);
854        //m_LightIllumPrograms.SetFragmentTexParam("LVisMap",m_LVisMapID);
855        m_LightIllumPrograms.SetFragmentTexParam("Phase",m_PhaseTextureID);
856        m_LightIllumPrograms.SetFragmentParam3f("LightRad",m_LightColor.x,m_LightColor.y,m_LightColor.z);
857        m_LightIllumPrograms.SetFragmentParam3f("LightRad2",m_LightColor2.x,m_LightColor2.y,m_LightColor2.z);
858        m_LightIllumPrograms.SetFragmentParam4f("LightDirsWeights",m_NearestDir,m_NearestDir2,m_Weight1,m_Weight2);
859        m_LightIllumPrograms.SetFragmentParam2f("Alb_Op",m_Albedo,m_Opacity);
860       
861        m_DirectIlumTexture.EnablewithColorRelease();
862        //glClearColor(0,0,0,0);
863        //glClear(GL_COLOR_BUFFER_BIT);
864        glBlendColorEXT(1,1,1,0.08);
865        //glEnable(GL_BLEND);
866        //glDisable(GL_BLEND);
867        glBlendFunc(GL_CONSTANT_ALPHA_EXT,GL_ONE_MINUS_CONSTANT_ALPHA_EXT);
868       
869        m_LightIllumPrograms.Enable(); 
870
871        m_ScreenQuad.DisplayScreenQuad(m_ParticleCount,m_DirectionCount);
872
873        m_LightIllumPrograms.DisableFragmentTexParam("LVisMap");
874        m_LightIllumPrograms.DisableFragmentTexParam("Phase");
875        m_LightIllumPrograms.Disable();
876       
877        m_DirectIlumTexture.DisablewithColorBind();             
878}
879
880void PreIllumSystem::Iterate()
881{       
882        m_IllumIteratePrograms.SetFragmentTexParam("IllumTexture",m_IllumColorTex->getColorTextureID());
883        m_IllumIteratePrograms.SetFragmentTexParam("VisMap",m_VisibilityTexID);
884        m_IllumIteratePrograms.SetFragmentTexParam("TauTexture",m_TauTextureID);
885        m_IllumIteratePrograms.SetFragmentTexParam("DirectIllum",m_DirectIlumTexture.getColorTextureID());
886        m_IllumIteratePrograms.SetFragmentParam3f("Alb_Op",m_Albedo,m_Opacity,m_Albedo*(4*M_PI)/m_DirectionCount);
887        m_IllumIteratePrograms.SetFragmentParam3f("SkyColor",m_SkyColor.x,m_SkyColor.y,m_SkyColor.z);
888        m_IllumIteratePrograms.SetFragmentTexParam("Phase",m_PhaseTextureID);
889       
890        m_IllumRenderTex->EnablewithColorRelease();
891
892        m_IllumIteratePrograms.Enable();
893       
894        //glClearColor(0,0,0,0);
895        //glClear(GL_COLOR_BUFFER_BIT);
896        //glDisable(GL_BLEND);
897       
898        m_ScreenQuad.DisplayScreenQuad(m_ParticleCount,m_DirectionCount);
899       
900        m_IllumIteratePrograms.DisableFragmentTexParam("IllumTexture");
901        m_IllumIteratePrograms.DisableFragmentTexParam("VisMap");
902        m_IllumIteratePrograms.DisableFragmentTexParam("DirectIllum");
903        m_IllumIteratePrograms.DisableFragmentTexParam("Phase");
904        m_IllumIteratePrograms.Disable();       
905               
906        m_IllumRenderTex->DisablewithColorBind();
907
908
909        RenderTexture* nextcolor=m_IllumRenderTex;
910        m_IllumRenderTex=m_IllumColorTex;
911        m_IllumColorTex=nextcolor;
912       
913}
914
915void PreIllumSystem::CreateEyeRadTexture()
916{
917        m_EyeRadPrograms.SetFragmentTexParam("DirectIllumTexture",m_DirectIlumTexture.getColorTextureID());
918        m_EyeRadPrograms.SetFragmentTexParam("IllumTexture",m_IllumColorTex->getColorTextureID());
919        m_EyeRadPrograms.SetFragmentTexParam("Phase",m_PhaseTextureID);
920        m_EyeRadPrograms.SetFragmentTexParam("TauTexture",m_TauTextureID);     
921        m_EyeRadPrograms.SetFragmentParam4f("EyeDirsWeights",m_NearestDir,m_NearestDir2,m_Weight1,m_Weight2);
922        float ize=m_Albedo*(4*M_PI)/(float)m_DirectionCount;
923        m_EyeRadPrograms.SetFragmentParam3f("Alb_Op",m_Albedo,m_Opacity,ize);
924       
925        m_EyeRadTexture.EnablewithColorRelease();
926
927        glBlendColorEXT(1,1,1,0.06);
928        glEnable(GL_BLEND);
929        glBlendFunc(GL_CONSTANT_ALPHA_EXT,GL_ONE_MINUS_CONSTANT_ALPHA_EXT);
930       
931        m_EyeRadPrograms.Enable();
932       
933        m_ScreenQuad.DisplayScreenQuad(m_ParticleCount,1);
934        m_EyeRadPrograms.DisableFragmentTexParam("DirectIllumTexture");
935        m_EyeRadPrograms.DisableFragmentTexParam("IllumTexture");
936        m_EyeRadPrograms.DisableFragmentTexParam("Phase");
937        m_EyeRadPrograms.Disable();
938
939        m_EyeRadTexture.DisablewithColorBind();
940}
941
942void PreIllumSystem::RenderToImpostor()
943{
944        m_ImpostorTexture.EnablewithColorRelease();
945        glClearColor(0,0,0,0);
946        glClear(GL_COLOR_BUFFER_BIT);
947        m_CameraImpostor.ApplyCameraTransform();
948
949        m_FinalRenderPrograms.Enable();
950       
951        glEnable(GL_BLEND);
952        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
953        glPointSize(1000*m_System.m_Emitter.getSize());
954       
955        m_FinalRenderPrograms.SetFragmentTexParam("BbTexture",m_BillboardTexture.getTextureHandler());
956        m_FinalRenderPrograms.SetFragmentTexParam("IllumTex",m_EyeRadTexture.getColorTextureID());
957               
958        //sort particles       
959        m_System.SortFromCamera(m_EyeCamera,true,true);
960       
961        glDepthMask(GL_FALSE);
962        //glDisable(GL_BLEND);
963        m_System.RenderAsPoints(true);
964       
965        glDepthMask(GL_TRUE);
966        glDisable(GL_BLEND);
967       
968        m_FinalRenderPrograms.DisableFragmentTexParam("BbTexture");
969        m_FinalRenderPrograms.DisableFragmentTexParam("IllumTex");
970        m_FinalRenderPrograms.Disable();
971
972        m_ImpostorTexture.DisablewithColorBind();
973}
974
975void PreIllumSystem::Display()
976{
977        m_FinalRenderPrograms.Enable();
978       
979        glEnable(GL_BLEND);
980        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
981        glPointSize(1000);
982       
983        m_FinalRenderPrograms.SetFragmentTexParam("VisMap",m_VisibilityTexID);
984        m_FinalRenderPrograms.SetFragmentTexParam("BbTexture",m_BillboardTexture.getTextureHandler());
985        m_FinalRenderPrograms.SetFragmentTexParam("IllumTex",m_EyeRadTexture.getColorTextureID());
986        m_FinalRenderPrograms.SetVertexParam1f("Opacity",m_Opacity);
987               
988        //sort particles       
989        m_System.SortFromCamera(m_EyeCamera,true,true);
990       
991        glDepthMask(GL_FALSE);
992        //glDisable(GL_BLEND);
993        m_System.RenderAsPoints(true);
994       
995        glDepthMask(GL_TRUE);
996        glDisable(GL_BLEND);
997       
998        m_FinalRenderPrograms.DisableFragmentTexParam("BbTexture");
999        m_FinalRenderPrograms.DisableFragmentTexParam("IllumTex");
1000        m_FinalRenderPrograms.Disable();
1001}
1002/*
1003void PreIllumSystem::Display()
1004{
1005        m_FinalRenderPrograms.Enable();
1006       
1007        glEnable(GL_BLEND);
1008        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1009        glPointSize(1000*m_System.m_Emitter.getSize());
1010       
1011        m_FinalRenderPrograms.SetFragmentTexParam("BbTexture",m_BillboardTexture.getTextureHandler());
1012        m_FinalRenderPrograms.SetFragmentTexParam("LVisMap",m_LVisMapID);
1013        m_FinalRenderPrograms.SetFragmentParam1f("Ldir",(float)m_NearestDir);   
1014       
1015        //sort particles       
1016        m_System.SortFromCamera(m_EyeCamera,true,true);
1017       
1018        glDepthMask(GL_FALSE);
1019        //glDisable(GL_BLEND);
1020        m_System.RenderAsPoints(true);
1021       
1022        glDepthMask(GL_TRUE);
1023        glDisable(GL_BLEND);
1024       
1025        m_FinalRenderPrograms.DisableFragmentTexParam("BbTexture");
1026        m_FinalRenderPrograms.DisableFragmentTexParam("LVisMap");
1027        m_FinalRenderPrograms.Disable();
1028}*/
Note: See TracBrowser for help on using the repository browser.