#include ".\preillumsystem.h" #include PreIllumSystem::~PreIllumSystem(void) { } float PreIllumSystem::Phase(Vector diri,Vector dirj,float symmetry) { float anglecos=diri*dirj; float g=symmetry; float g2=g*g; float phase=3*(1-g2)*(1+anglecos*anglecos)/2/(2+g2)/pow((1+g2-2*anglecos*g),1.5f); return phase/(4*M_PI); //return 1.0/(4*M_PI); } void PreIllumSystem::Calculate_Up_Right_Vector(Vector viewdir,Vector& UpVector,Vector& RightVector) { if(viewdir.y==0) { UpVector.x=0;UpVector.y=1;UpVector.z=0; } else { Vector projected; projected.x=viewdir.x;projected.z=viewdir.z; projected.y=0; projected.Normalize(); UpVector=viewdir-projected; UpVector.Normalize(); } RightVector=UpVector%viewdir; RightVector.Normalize(); } PreIllumSystem::GetNearestDirection(Vector LightPosition) { Vector dir=LightPosition; dir.Normalize(); float angle1,angle2; //transform to polar coordinates angle2=asin(dir.y); // -pi/2 -- pi/2 dir.y=0; dir.Normalize(); float sign; if(dir.z>0)sign=1; if(dir.z<0)sign=-1; if(dir.z==0)sign=0; angle1=acos(dir.x)*sign; // -pi -- pi Vector dirre(cos(angle1)*cos(angle2), sin(angle2), sin(angle1)*cos(angle2)); angle1=angle1/6.28+0.5; // 0 -- 1 angle2=angle2/3.14+0.5; // 0 -- 1 int coord1=angle1*512; int coord2=angle2*512; int index=coord2*512*4+coord1*4; if(index+3>512*512*4) exit(1); m_NearestDir=m_DirectionData[index]; m_NearestDir2=m_DirectionData[index+1]; m_Weight1=m_DirectionData[index+2]; m_Weight2=m_DirectionData[index+3]; } void PreIllumSystem::CreateGivenDirections() { /* m_Directions=new Vector[4]; m_Directions[0]=Vector(1,0,0); m_Directions[1]=Vector(0,0,1); m_Directions[2]=Vector(0,0,-1); m_Directions[3]=Vector(-1,0,0); */ m_Directions=new Vector[m_DirectionCount]; for(int i=0;i0) { //if(z distlist; std::list ::iterator Iter; for(int j=0;jCreateVisibilityTexture(); unsigned int thistime=timeGetTime(); float elapsed=(float)(thistime-lasttime)/1000.0f; lasttime=thistime; fprintf(stderr, "OK %fs\n",elapsed); fprintf(stderr, "Building Nearest Directions Map: .."); this->CreateNearestDirectionTexture(); thistime=timeGetTime(); elapsed=(float)(thistime-lasttime)/1000.0f; lasttime=thistime; fprintf(stderr, "OK %fs\n",elapsed); fprintf(stderr, "Building Phase Function Map: .."); CreatePhaseTexture(); thistime=timeGetTime(); elapsed=(float)(thistime-lasttime)/1000.0f; lasttime=thistime; fprintf(stderr, "OK %fs\n",elapsed); fprintf(stderr, "Building Tau Map: .."); CreateTauTexture(); thistime=timeGetTime(); elapsed=(float)(thistime-lasttime)/1000.0f; lasttime=thistime; fprintf(stderr, "OK %fs\n",elapsed); /* fprintf(stderr, "Building Light Visibility Map: .."); CreateLVisMap(); thistime=timeGetTime(); elapsed=(float)(thistime-lasttime)/1000.0f; lasttime=thistime; fprintf(stderr, "OK %fs\n",elapsed); */ m_IllumColorTex=&m_IllumTexture2; m_IllumRenderTex=&m_IllumTexture; m_IllumTexture2.EnablewithColorRelease(); glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT); m_IllumTexture2.DisablewithColorBind(); } char* PreIllumSystem::DisplayTexture(int tex) { char* displaytext; int dimX,dimY; unsigned int TexID; float MaxValue; switch(tex) { case 1: //LightView /* dimX=m_ParticleCount; dimY=m_DirectionCount; TexID=m_VisibilityTexID; MaxValue=m_ParticleCount;*/ dimX=m_LightWindowSize; dimY=m_LightWindowSize; TexID=m_Target.getColorTextureID(); MaxValue=m_ParticleCount; displaytext="LightView"; break; case 2: //LightVisibility dimX=m_ParticleCount; dimY=2; TexID=m_RenderedVisID; MaxValue=1; displaytext="LightVisibility"; break; case 3: //DirectIllumTexture dimX=m_ParticleCount; dimY=m_DirectionCount; TexID=m_DirectIlumTexture.getColorTextureID(); MaxValue=1; displaytext="DirectIllumTexture"; break; case 4: //IllumTexture1 dimX=m_ParticleCount; dimY=m_DirectionCount; TexID=m_IllumTexture.getColorTextureID(); MaxValue=1; displaytext="IllumTexture1"; break; case 5: //IllumTexture2 dimX=m_ParticleCount; dimY=m_DirectionCount; TexID=m_IllumTexture2.getColorTextureID(); MaxValue=1; displaytext="IllumTexture2"; break; case 6: //EyeRadTexture dimX=m_ParticleCount; dimY=1; TexID=m_EyeRadTexture.getColorTextureID(); MaxValue=1; displaytext="EyeRadTexture"; break; } m_TexRectPrograms.Enable(); m_TexRectPrograms.SetFragmentTexParam("Texture",TexID); m_TexRectPrograms.SetFragmentParam1f("invmaxvalue",1.0/MaxValue); m_ScreenQuad.DisplayScreenQuad(dimX,dimY); m_TexRectPrograms.Disable(); return displaytext; } void PreIllumSystem::Refresh(Vector lightpos,Vector lightpos2,Vector lightcolor,Vector lightcolor2) { /* m_IllumColorTex->EnablewithColorRelease(); glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT); m_IllumColorTex->DisablewithColorBind(); */ static bool first=true; ////Light1 FindVisiblesWithRendering(lightpos,0); GetNearestDirection(lightpos); int lastNearest=m_NearestDir; m_NearestDir=lastNearest; m_LightPosition=lightpos; m_LightColor=lightcolor; if(first) { ////Light2 FindVisiblesWithRendering(lightpos2,1); GetNearestDirection(lightpos2); int thisNearest=m_NearestDir; m_NearestDir2=thisNearest; m_LightPosition2=lightpos2; first=false; } m_LightColor2=lightcolor2; RefreshDirectIllumTexture(); for(int i=0;igetPosition()*-1); CreateEyeRadTexture(); } void PreIllumSystem::RefreshDirectIllumTexture() { m_LightIllumPrograms.SetFragmentTexParam("LVisMap",m_RenderedVisID); //m_LightIllumPrograms.SetFragmentTexParam("LVisMap",m_LVisMapID); m_LightIllumPrograms.SetFragmentTexParam("Phase",m_PhaseTextureID); m_LightIllumPrograms.SetFragmentParam3f("LightRad",m_LightColor.x,m_LightColor.y,m_LightColor.z); m_LightIllumPrograms.SetFragmentParam3f("LightRad2",m_LightColor2.x,m_LightColor2.y,m_LightColor2.z); m_LightIllumPrograms.SetFragmentParam4f("LightDirsWeights",m_NearestDir,m_NearestDir2,m_Weight1,m_Weight2); m_LightIllumPrograms.SetFragmentParam2f("Alb_Op",m_Albedo,m_Opacity); m_DirectIlumTexture.EnablewithColorRelease(); //glClearColor(0,0,0,0); //glClear(GL_COLOR_BUFFER_BIT); glBlendColorEXT(1,1,1,0.08); //glEnable(GL_BLEND); //glDisable(GL_BLEND); glBlendFunc(GL_CONSTANT_ALPHA_EXT,GL_ONE_MINUS_CONSTANT_ALPHA_EXT); m_LightIllumPrograms.Enable(); m_ScreenQuad.DisplayScreenQuad(m_ParticleCount,m_DirectionCount); m_LightIllumPrograms.DisableFragmentTexParam("LVisMap"); m_LightIllumPrograms.DisableFragmentTexParam("Phase"); m_LightIllumPrograms.Disable(); m_DirectIlumTexture.DisablewithColorBind(); } void PreIllumSystem::Iterate() { m_IllumIteratePrograms.SetFragmentTexParam("IllumTexture",m_IllumColorTex->getColorTextureID()); m_IllumIteratePrograms.SetFragmentTexParam("VisMap",m_VisibilityTexID); m_IllumIteratePrograms.SetFragmentTexParam("TauTexture",m_TauTextureID); m_IllumIteratePrograms.SetFragmentTexParam("DirectIllum",m_DirectIlumTexture.getColorTextureID()); m_IllumIteratePrograms.SetFragmentParam3f("Alb_Op",m_Albedo,m_Opacity,m_Albedo*(4*M_PI)/m_DirectionCount); m_IllumIteratePrograms.SetFragmentParam3f("SkyColor",m_SkyColor.x,m_SkyColor.y,m_SkyColor.z); m_IllumIteratePrograms.SetFragmentTexParam("Phase",m_PhaseTextureID); m_IllumRenderTex->EnablewithColorRelease(); m_IllumIteratePrograms.Enable(); //glClearColor(0,0,0,0); //glClear(GL_COLOR_BUFFER_BIT); //glDisable(GL_BLEND); m_ScreenQuad.DisplayScreenQuad(m_ParticleCount,m_DirectionCount); m_IllumIteratePrograms.DisableFragmentTexParam("IllumTexture"); m_IllumIteratePrograms.DisableFragmentTexParam("VisMap"); m_IllumIteratePrograms.DisableFragmentTexParam("DirectIllum"); m_IllumIteratePrograms.DisableFragmentTexParam("Phase"); m_IllumIteratePrograms.Disable(); m_IllumRenderTex->DisablewithColorBind(); RenderTexture* nextcolor=m_IllumRenderTex; m_IllumRenderTex=m_IllumColorTex; m_IllumColorTex=nextcolor; } void PreIllumSystem::CreateEyeRadTexture() { m_EyeRadPrograms.SetFragmentTexParam("DirectIllumTexture",m_DirectIlumTexture.getColorTextureID()); m_EyeRadPrograms.SetFragmentTexParam("IllumTexture",m_IllumColorTex->getColorTextureID()); m_EyeRadPrograms.SetFragmentTexParam("Phase",m_PhaseTextureID); m_EyeRadPrograms.SetFragmentTexParam("TauTexture",m_TauTextureID); m_EyeRadPrograms.SetFragmentParam4f("EyeDirsWeights",m_NearestDir,m_NearestDir2,m_Weight1,m_Weight2); float ize=m_Albedo*(4*M_PI)/(float)m_DirectionCount; m_EyeRadPrograms.SetFragmentParam3f("Alb_Op",m_Albedo,m_Opacity,ize); m_EyeRadTexture.EnablewithColorRelease(); glBlendColorEXT(1,1,1,0.06); glEnable(GL_BLEND); glBlendFunc(GL_CONSTANT_ALPHA_EXT,GL_ONE_MINUS_CONSTANT_ALPHA_EXT); m_EyeRadPrograms.Enable(); m_ScreenQuad.DisplayScreenQuad(m_ParticleCount,1); m_EyeRadPrograms.DisableFragmentTexParam("DirectIllumTexture"); m_EyeRadPrograms.DisableFragmentTexParam("IllumTexture"); m_EyeRadPrograms.DisableFragmentTexParam("Phase"); m_EyeRadPrograms.Disable(); m_EyeRadTexture.DisablewithColorBind(); } void PreIllumSystem::RenderToImpostor() { m_ImpostorTexture.EnablewithColorRelease(); glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT); m_CameraImpostor.ApplyCameraTransform(); m_FinalRenderPrograms.Enable(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glPointSize(1000*m_System.m_Emitter.getSize()); m_FinalRenderPrograms.SetFragmentTexParam("BbTexture",m_BillboardTexture.getTextureHandler()); m_FinalRenderPrograms.SetFragmentTexParam("IllumTex",m_EyeRadTexture.getColorTextureID()); //sort particles m_System.SortFromCamera(m_EyeCamera,true,true); glDepthMask(GL_FALSE); //glDisable(GL_BLEND); m_System.RenderAsPoints(true); glDepthMask(GL_TRUE); glDisable(GL_BLEND); m_FinalRenderPrograms.DisableFragmentTexParam("BbTexture"); m_FinalRenderPrograms.DisableFragmentTexParam("IllumTex"); m_FinalRenderPrograms.Disable(); m_ImpostorTexture.DisablewithColorBind(); } void PreIllumSystem::Display() { m_FinalRenderPrograms.Enable(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glPointSize(1000); m_FinalRenderPrograms.SetFragmentTexParam("VisMap",m_VisibilityTexID); m_FinalRenderPrograms.SetFragmentTexParam("BbTexture",m_BillboardTexture.getTextureHandler()); m_FinalRenderPrograms.SetFragmentTexParam("IllumTex",m_EyeRadTexture.getColorTextureID()); m_FinalRenderPrograms.SetVertexParam1f("Opacity",m_Opacity); //sort particles m_System.SortFromCamera(m_EyeCamera,true,true); glDepthMask(GL_FALSE); //glDisable(GL_BLEND); m_System.RenderAsPoints(true); glDepthMask(GL_TRUE); glDisable(GL_BLEND); m_FinalRenderPrograms.DisableFragmentTexParam("BbTexture"); m_FinalRenderPrograms.DisableFragmentTexParam("IllumTex"); m_FinalRenderPrograms.Disable(); } /* void PreIllumSystem::Display() { m_FinalRenderPrograms.Enable(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glPointSize(1000*m_System.m_Emitter.getSize()); m_FinalRenderPrograms.SetFragmentTexParam("BbTexture",m_BillboardTexture.getTextureHandler()); m_FinalRenderPrograms.SetFragmentTexParam("LVisMap",m_LVisMapID); m_FinalRenderPrograms.SetFragmentParam1f("Ldir",(float)m_NearestDir); //sort particles m_System.SortFromCamera(m_EyeCamera,true,true); glDepthMask(GL_FALSE); //glDisable(GL_BLEND); m_System.RenderAsPoints(true); glDepthMask(GL_TRUE); glDisable(GL_BLEND); m_FinalRenderPrograms.DisableFragmentTexParam("BbTexture"); m_FinalRenderPrograms.DisableFragmentTexParam("LVisMap"); m_FinalRenderPrograms.Disable(); }*/