source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/demos/Standalone/Hierarchical Systems Demo [OpenGL]/src/PreIllumSystem.cpp @ 3255

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