- Timestamp:
- 08/22/08 13:20:06 (16 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
r2859 r2861 793 793 </Filter> 794 794 <File 795 RelativePath=".\default.env" 796 > 797 </File> 798 <File 795 799 RelativePath=".\ReadMe.txt" 796 800 > -
GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env
r2846 r2861 18 18 # shader stuff 19 19 20 # ssao exponential smoothing factor 20 21 expFactor=0.1f 21 22 #numSsaoSamples=8 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp
r2859 r2861 35 35 36 36 37 DeferredShader::~DeferredShader() 38 { 39 if (sCgDeferredProgram) 40 cgDestroyProgram(sCgDeferredProgram); 41 } 42 43 37 44 void DeferredShader::Init(CGcontext context) 38 45 { … … 65 72 void DeferredShader::Render(FrameBufferObject *fbo) 66 73 { 67 GLuint positionsTex = fbo->GetColorBuffer(0)->GetTexture(); 74 FrameBufferObject::Release(); 75 68 76 GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 69 GLuint normalsTex = fbo->GetColorBuffer(0)->GetTexture(); 77 GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 78 GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 70 79 71 80 glPushAttrib(GL_VIEWPORT_BIT); … … 103 112 104 113 glColor3f(1.0f, 1.0f, 1.0f); 105 114 106 115 glBegin(GL_QUADS); 107 116 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.h
r2859 r2861 24 24 DeferredShader(int w, int h); 25 25 26 ~DeferredShader(); 26 27 /** The algorithm renders the scene given an fbo. 27 28 The fbo must have color buffer, position buffer, normal buffer. -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp
r2859 r2861 9 9 { 10 10 11 GLenum color_attachment[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT}; 11 12 12 13 void PrintFBOStatus(GLenum status) … … 15 16 { 16 17 case GL_FRAMEBUFFER_COMPLETE_EXT: 17 cout << "frame buffer object c reated successfully" << endl;18 cout << "frame buffer object complete" << endl; 18 19 break; 19 20 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: … … 45 46 46 47 ColorBufferObject::ColorBufferObject(int w, int h, 47 FORMAT c,48 FORMAT format, 48 49 WRAP_TYPE wrapType, 49 50 FILTER_TYPE filterType, 50 51 bool useMipMap, 51 bool useMultiSampling) 52 bool useMultiSampling, 53 int attachment_idx) 52 54 { 55 GLuint glformat, internalFormat; 56 57 58 switch (format) 59 { 60 case BUFFER_UBYTE: 61 glformat = GL_UNSIGNED_BYTE; internalFormat = GL_RGBA8; break; 62 case BUFFER_FLOAT_16: 63 glformat = GL_FLOAT; internalFormat = GL_RGBA16F_ARB; break; 64 case BUFFER_FLOAT_32: 65 glformat = GL_FLOAT; internalFormat = GL_RGBA32F_ARB; break; 66 default: 67 glformat = GL_UNSIGNED_BYTE; internalFormat = GL_RGBA8; 68 cerr << "should not come here" << endl; 69 } 70 53 71 glGenRenderbuffersEXT(1, &mId); 54 72 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId); 55 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, w, h);73 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, internalFormat, w, h); 56 74 57 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_RENDERBUFFER_EXT, mId);75 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_RENDERBUFFER_EXT, mId); 58 76 59 77 glGenTextures(1, &mTexId); 60 78 glBindTexture(GL_TEXTURE_2D, mTexId); 61 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 62 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, mTexId, 0); 79 //glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RGBA, glformat, NULL); 80 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RGBA, glformat, NULL); 81 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_TEXTURE_2D, mTexId, 0); 63 82 64 83 GLuint filterParam; … … 72 91 case FILTER_MIPMAP_LINEAR: 73 92 filterParam = GL_LINEAR_MIPMAP_LINEAR; break; 93 default: 94 filterParam = GL_NEAREST; 95 cerr << "should not come here" << endl; 74 96 } 75 97 … … 96 118 97 119 98 FrameBufferObject::FrameBufferObject(int w, int h, bool useDepth,DEPTH_FORMAT d)120 FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d) 99 121 : mWidth(w), mHeight(h) 100 122 { … … 102 124 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId); 103 125 104 126 cout << "creating fbo" << endl; 127 105 128 /////////// 106 129 //-- create depth buffer 107 130 108 if ( useDepth)131 if (d != DEPTH_NONE) 109 132 { 110 133 glGenRenderbuffersEXT(1, &mDepthId); 111 134 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId); 135 136 cout << "adding depth buffer" << endl; 112 137 113 138 GLuint depthFormat; … … 121 146 case DEPTH_32: 122 147 depthFormat = GL_DEPTH_COMPONENT32; break; 148 default: 149 cerr << "should not come here" << endl; 123 150 } 124 151 … … 126 153 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId); 127 154 } 128 129 // print status130 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));131 155 } 132 156 … … 138 162 bool useMultiSampling) 139 163 { 164 cout << "adding color buffer" << endl; 165 166 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId); 167 168 int idx = (int)mColorBuffers.size(); 140 169 ColorBufferObject *colorBuf = 141 new ColorBufferObject(mWidth, mHeight, col, wrapType, filterType, useMipMap, useMultiSampling); 170 new ColorBufferObject(mWidth, mHeight, col, wrapType, filterType, useMipMap, useMultiSampling, idx); 171 142 172 mColorBuffers.push_back(colorBuf); 143 173 144 return (int)mColorBuffers.size() - 1; 174 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 175 176 return idx; 177 } 178 179 180 void FrameBufferObject::Bind() const 181 { 182 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId); 183 } 184 185 186 void FrameBufferObject::Release() 187 { 188 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 145 189 } 146 190 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.h
r2859 r2861 25 25 FILTER_TYPE filterType, 26 26 bool useMipMap, 27 bool useMultiSampling); 27 bool useMultiSampling, 28 int attachment_idx); 28 29 29 30 unsigned int GetTexture() const {return mTexId;} … … 42 43 public: 43 44 44 enum DEPTH_FORMAT { DEPTH_ 16, DEPTH_24, DEPTH_32 };45 enum DEPTH_FORMAT { DEPTH_NONE, DEPTH_16, DEPTH_24, DEPTH_32 }; 45 46 46 47 /** constructor requesting an opengl occlusion query. 47 48 */ 48 FrameBufferObject(int w, int h, bool useDepth,DEPTH_FORMAT d);49 FrameBufferObject(int w, int h, DEPTH_FORMAT d); 49 50 /** Creates and adds a color buffer to the current frame buffer object. 50 51 Returns the index that allows to retrieve the color buffer object. … … 59 60 */ 60 61 ColorBufferObject *GetColorBuffer(int i) const { return mColorBuffers[i]; } 61 62 /** Binds this frame buffer object. 63 */ 64 void Bind() const; 65 /** Releases any bound frame buffer object. 66 */ 67 static void Release(); 62 68 63 69 protected: -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp
r2851 r2861 3 3 #include "Material.h" 4 4 5 using namespace std; 5 6 6 7 namespace CHCDemoEngine -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp
r2853 r2861 69 69 str.read(reinterpret_cast<char *>(&dist), sizeof(float)); 70 70 71 if (i>=3) dist=2e20; 71 72 int numShapes; 72 73 str.read(reinterpret_cast<char *>(&numShapes), sizeof(int)); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp
r2860 r2861 61 61 SsaoShader::SsaoShader(int w, int h, 62 62 Camera *cam, 63 float expFactor,64 63 float scaleFactor 65 64 ): 66 65 mWidth(w), mHeight(h), 67 66 mCamera(cam), 68 mExpFactor(expFactor),69 67 mScaleFactor(scaleFactor) 70 {} 68 { 69 70 /////////// 71 //-- the flip-flop fbos 72 73 mNewFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 74 // the diffuse color buffer 75 mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false, false); 76 77 mOldFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 78 // the diffuse color buffer 79 mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false, false); 80 81 82 // create noise texture for ssao 83 CreateNoiseTex2D(); 84 } 85 86 87 SsaoShader::~SsaoShader() 88 { 89 if (sCgSsaoProgram) 90 cgDestroyProgram(sCgSsaoProgram); 91 92 DEL_PTR(mNewFbo); 93 DEL_PTR(mOldFbo); 94 95 glDeleteTextures(1, &noiseTex); 96 } 71 97 72 98 73 99 void SsaoShader::Init(CGcontext context) 74 { 75 100 { 76 101 /////////////// 77 102 … … 112 137 113 138 114 void SsaoShader::Render(FrameBufferObject *fbo, FrameBufferObject *fbo2) 115 { 116 FirstPass(fbo, fbo2); 117 SecondPass(fbo, fbo2); 118 } 119 120 121 void SsaoShader::FirstPass(FrameBufferObject *fbo, FrameBufferObject *fbo2) 122 { 123 GLuint positionsTex = fbo->GetColorBuffer(0)->GetTexture(); 139 void SsaoShader::Render(FrameBufferObject *fbo, 140 const Matrix4x4 &oldProjViewMatrix, 141 float expFactor) 142 { 143 cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x); 144 145 // switch roles of old and new fbo 146 // the algorihm uses two input fbos, where the one 147 // contais the color buffer from the last frame, 148 // the other one will be written 149 swap(mNewFbo, mOldFbo); 150 151 FirstPass(fbo, expFactor); 152 // the second pass just renders the combined solution 153 SecondPass(fbo); 154 } 155 156 157 void SsaoShader::FirstPass(FrameBufferObject *fbo, float expFactor) 158 { 124 159 GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 125 GLuint normalsTex = fbo->GetColorBuffer(0)->GetTexture(); 126 127 GLuint oldTex = fbo2->GetColorBuffer(0)->GetTexture(); 160 GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 161 GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 162 163 // read the second buffer, write to the first buffer 164 mNewFbo->Bind(); 165 GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture(); 128 166 129 167 glPushAttrib(GL_VIEWPORT_BIT); … … 172 210 173 211 cgGLSetParameter1f(sMaxDepthParam, mScaleFactor); 174 cgGLSetParameter1f(sExpFactorParam, mExpFactor);212 cgGLSetParameter1f(sExpFactorParam, expFactor); 175 213 176 214 … … 215 253 glPopAttrib(); 216 254 217 PrintGLerror("displaytexture"); 218 219 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 220 255 FrameBufferObject::Release(); 221 256 222 257 PrintGLerror("ssao first pass"); … … 224 259 225 260 226 void SsaoShader::SecondPass(FrameBufferObject *fbo , FrameBufferObject *fbo2)261 void SsaoShader::SecondPass(FrameBufferObject *fbo) 227 262 { 228 263 glEnable(GL_TEXTURE_2D); 229 264 230 glBindTexture(GL_TEXTURE_2D, fbo2->GetColorBuffer(0)->GetTexture());265 glBindTexture(GL_TEXTURE_2D, mNewFbo->GetColorBuffer(0)->GetTexture()); 231 266 232 267 glDisable(GL_LIGHTING); … … 274 309 mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr); 275 310 276 #if 1// matT: debug this!!311 #if 0 // matT: debug this!! 277 312 278 313 bl = Normalize(nbl - fbl); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h
r2860 r2861 14 14 class Vector3; 15 15 class Camera; 16 16 class Matrix4x4; 17 17 18 18 /** This class implements a deferred shading algorithm that takes … … 23 23 public: 24 24 /** constructor for a deferred shader taking the requested output image size, 25 the current camera, the exponential smoothing factor for temporal reprojection, 26 and a scaling factor. 25 the current camera, and a scaling factor. 27 26 28 27 The parameter scaleFactor must be reciprocal value of the … … 30 29 exact scene size that was scaled in order to improve floating point precision. 31 30 */ 32 SsaoShader(int w, int h, Camera *cam, float expFactor, float scaleFactor); 33 /** The algorithm renders the scene given an fbo. 34 The fbo must have color buffer, position buffer, normal buffer. 31 SsaoShader(int w, int h, Camera *cam, float scaleFactor); 32 /** 33 34 The algorithm renders the scene given an fbo consists of 1 color buffer, 35 1 position buffer, and 1 normal buffer. 36 We also need the projection view matrix of the last frame for reprojection, and 37 a smoothing factor for temporal coherence 35 38 */ 36 void Render(FrameBufferObject *fbo, FrameBufferObject *fbo2);39 void Render(FrameBufferObject *fbo, const Matrix4x4 &oldProjViewMatrix, float expFactor); 37 40 38 41 /** Initialises the deferred shader and loads the required shaders: … … 41 44 static void Init(CGcontext context); 42 45 46 ~SsaoShader(); 43 47 44 48 protected: 45 49 46 void FirstPass(FrameBufferObject *fbo, FrameBufferObject *fbo2);50 void FirstPass(FrameBufferObject *fbo, float expFactor); 47 51 48 void SecondPass(FrameBufferObject *fbo, FrameBufferObject *fbo2); 52 void SecondPass(FrameBufferObject *fbo); 53 54 void CreateNoiseTex2D(); 55 void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br); 49 56 50 57 … … 54 61 int mHeight; 55 62 56 /// the temporal smoothing factor57 float mExpFactor;58 63 /// this is just a scale factor for the scene depth in order to get better float precision in the shader 59 64 float mScaleFactor; … … 61 66 Camera *mCamera; 62 67 63 64 private: 65 66 void CreateNoiseTex2D(); 67 void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br); 68 68 FrameBufferObject *mOldFbo; 69 FrameBufferObject *mNewFbo; 69 70 }; 70 71 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
r2860 r2861 33 33 #include "SampleGenerator.h" 34 34 #include "FrameBufferObject.h" 35 #include "SsaoShader.h" 36 #include "DeferredShader.h" 35 37 36 38 … … 45 47 //-- fbos 46 48 47 GLuint fbo; 48 GLuint fbo1; 49 GLuint fbo2; 49 FrameBufferObject *fbo; 50 50 51 51 bool isFirstTexture = true; 52 53 /////////////54 //-- renderbuffers55 56 57 GLuint depthBuffer;58 59 GLuint positionsBuffer;60 GLuint colorsBuffer;61 GLuint colorsBuffer1;62 GLuint colorsBuffer2;63 GLuint normalsBuffer;64 65 66 /////////////67 //-- textures68 69 70 GLuint positionsTex;71 GLuint colorsTex;72 GLuint colorsTex1;73 GLuint colorsTex2;74 75 GLuint normalsTex;76 77 GLuint noiseTex;78 52 79 53 GLuint fontTex; … … 177 151 178 152 bool useSsao = false; 153 static float ssaoExpFactor = 0.1f; 179 154 180 155 bool showAlgorithmTime = false; … … 190 165 bool useFullScreen = false; 191 166 192 // exp factor for ssao193 float expFactor = 0.05f;194 195 // ssao number of samples196 //#define NUM_SAMPLES 8197 #define NUM_SAMPLES 16198 199 // ssao random spherical samples200 static float samples[NUM_SAMPLES * 2];201 202 167 static Matrix4x4 matProjectionView = IdentityMatrix(); 203 168 204 FrameBufferObject *myfbo = NULL;205 169 206 170 … … 241 205 242 206 void PlaceViewer(const Vector3 &oldPos); 243 void DisplayRenderTexture();244 207 245 208 … … 249 212 void InitFBO(); 250 213 251 void CreateNoiseTex2D(); 252 253 void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br); 254 255 void GenerateSamples(); 256 214 SsaoShader *ssaoShader = NULL; 215 DeferredShader *deferredShader = NULL; 257 216 258 217 GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT}; … … 264 223 static CGcontext sCgContext = NULL; 265 224 static CGprogram sCgMrtVertexProgram = NULL; 266 static CGprogram sCgSsaoProgram = NULL;267 static CGprogram sCgDeferredProgram = NULL;268 269 static CGparameter sColorsTexParam;270 static CGparameter sPositionsTexParam;271 static CGparameter sNormalsTexParam;272 273 static CGparameter sColorsTexParamSsao;274 static CGparameter sPositionsTexParamSsao;275 static CGparameter sNormalsTexParamSsao;276 static CGparameter sNoiseTexParamSsao;277 225 278 226 static CGparameter sModelViewProjMatrixParam; 279 static CGparameter sOldModelViewProjMatrixParam;280 227 static CGparameter sMaxDepthParam; 281 static CGparameter sMaxDepthParamSsao;282 228 static CGparameter sMaxDepthParamTex; 283 229 284 static CGparameter sSamplesParamSsao; 285 static CGparameter sOldTexParamSsao; 286 static CGparameter sNoiseMultiplierParam; 287 static CGparameter sExpFactorParamSsao; 230 static Matrix4x4 oldViewProjMatrix; 288 231 289 232 … … 296 239 printf("%s\n\n", cgGetErrorString(lastError)); 297 240 printf("%s\n", cgGetLastListing(sCgContext)); 241 298 242 printf("Cg error, exiting...\n"); 299 243 … … 343 287 344 288 env.GetBoolParam(string("useFullScreen"), useFullScreen); 345 env.GetFloatParam(string("expFactor"), expFactor);289 env.GetFloatParam(string("expFactor"), ssaoExpFactor); 346 290 env.GetVectorParam(string("camPosition"), camPos); 347 291 env.GetVectorParam(string("camDirection"), camDir); … … 360 304 cout << "useFullScreen: " << useFullScreen << endl; 361 305 cout << "camPosition: " << camPos << endl; 362 cout << "expFactor: " << expFactor << endl;306 cout << "expFactor: " << ssaoExpFactor << endl; 363 307 //cout << "model path: " << model_path << endl; 364 308 } … … 466 410 InitCg(); 467 411 468 // create noise texture for ssao 469 CreateNoiseTex2D(); 412 SsaoShader::Init(sCgContext); 413 DeferredShader::Init(sCgContext); 414 415 ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f); 416 deferredShader = new DeferredShader(texWidth, texHeight); 470 417 471 418 // initialize the render traverser … … 499 446 // get the best profile for this hardware 500 447 RenderState::sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT); 501 //assert(sCgFragmentProfile != CG_PROFILE_UNKNOWN);502 448 cgGLSetOptimalOptions(RenderState::sCgFragmentProfile); 503 449 … … 564 510 cerr << "fragment program failed to load" << endl; 565 511 566 PrintGLerror("test");567 568 569 ///////////////570 571 sCgSsaoProgram =572 cgCreateProgramFromFile(sCgContext,573 CG_SOURCE,574 "src/shaders/deferred.cg",575 RenderState::sCgFragmentProfile,576 "main_ssao",577 NULL);578 579 if (sCgSsaoProgram != NULL)580 {581 cgGLLoadProgram(sCgSsaoProgram);582 583 // we need size of texture for scaling584 sPositionsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "positions");585 sColorsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "colors");586 sNormalsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "normals");587 sNoiseTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");588 sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier");589 sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");590 sMaxDepthParamSsao = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");591 sExpFactorParamSsao = cgGetNamedParameter(sCgSsaoProgram, "expFactor");592 593 cgGLSetParameter1f(sMaxDepthParamSsao, myfar / 10.0f);594 cgGLSetParameter1f(sExpFactorParamSsao, expFactor);595 596 sSamplesParamSsao = cgGetNamedParameter(sCgSsaoProgram, "samples");597 sOldTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "oldTex");598 599 GenerateSamples(); cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples);600 }601 else602 cerr << "ssao program failed to load" << endl;603 604 605 sCgDeferredProgram =606 cgCreateProgramFromFile(sCgContext,607 CG_SOURCE,608 "src/shaders/deferred.cg",609 RenderState::sCgFragmentProfile,610 "main",611 NULL);612 613 if (sCgDeferredProgram != NULL)614 {615 cgGLLoadProgram(sCgDeferredProgram);616 617 // we need size of texture for scaling618 sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions");619 sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors");620 sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals");621 }622 else623 cerr << "deferred program failed to load" << endl;624 625 626 512 PrintGLerror("init"); 627 513 628 514 cout << "cg initialization successful" << endl; 629 }630 631 632 void PrintFBOStatus(GLenum status)633 {634 switch(status)635 {636 case GL_FRAMEBUFFER_COMPLETE_EXT:637 cout << "frame buffer object created successfully" << endl;638 break;639 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:640 cerr << "incomplete attachment" << endl;641 break;642 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:643 cerr << "missing attachment" << endl;644 break;645 case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:646 cerr << "incomplete dimensions" << endl;647 break;648 case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:649 cerr << "incomplete formats" << endl;650 break;651 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:652 cerr << "incomplete draw buffer" << endl;653 break;654 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:655 cerr << "incomplete read buffer" << endl;656 break;657 case GL_FRAMEBUFFER_UNSUPPORTED_EXT:658 cerr << "framebuffer unsupported" << endl;659 break;660 default:661 cerr << "unknown status code " << status << endl;662 }663 515 } 664 516 … … 668 520 // this fbo basicly stores the scene information we get from standard rendering of a frame 669 521 // we store colors, normals, positions (for the ssao) 670 fbo = new FrameBufferObject(texWidth, texHeight, true, FrameBufferObject::DEPTH_24); 671 522 fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_24); 672 523 673 524 // the diffuse color buffer 674 525 fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false, false); 675 676 526 // the positions buffer 677 527 fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false, false); 678 679 528 // the normals buffer 680 529 fbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false, false); 681 682 683 684 ////////////////////////685 686 glGenFramebuffersEXT(1, &fbo);687 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);688 689 690 ////////////691 //-- colors buffer692 693 glGenRenderbuffersEXT(1, &colorsBuffer);694 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer);695 696 #if 1697 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);698 #else699 int samples = 8;700 glEnable(GL_MULTISAMPLE_ARB);701 glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, samples, GL_RGBA8, texWidth, texHeight);702 #endif703 704 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer);705 706 glGenTextures(1, &colorsTex);707 glBindTexture(GL_TEXTURE_2D, colorsTex);708 709 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);710 //glGenerateMipmapEXT(GL_TEXTURE_2D);711 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex, 0);712 713 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);714 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);715 716 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);717 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);718 719 720 //////////721 //-- positions buffer722 723 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));724 glGenRenderbuffersEXT(1, &positionsBuffer);725 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, positionsBuffer);726 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);727 728 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_RENDERBUFFER_EXT, positionsBuffer);729 730 glGenTextures(1, &positionsTex);731 glBindTexture(GL_TEXTURE_2D, positionsTex);732 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);733 //glGenerateMipmapEXT(GL_TEXTURE_2D);734 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, positionsTex, 0);735 736 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);737 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);738 739 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);740 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);741 742 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));743 744 745 ////////746 //-- normals buffer747 748 glGenRenderbuffersEXT(1, &normalsBuffer);749 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, normalsBuffer);750 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, texWidth, texHeight);751 752 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_RENDERBUFFER_EXT, normalsBuffer);753 754 glGenTextures(1, &normalsTex);755 glBindTexture(GL_TEXTURE_2D, normalsTex);756 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);757 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, normalsTex, 0);758 759 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);760 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);761 762 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);763 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);764 765 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));766 767 768 ///////////769 //-- create depth buffer770 771 glGenRenderbuffersEXT(1, &depthBuffer);772 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBuffer);773 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, texWidth, texHeight);774 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthBuffer);775 776 777 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));778 779 780 781 //////////////////////////////////782 783 784 785 glGenFramebuffersEXT(1, &fbo1);786 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo1);787 788 789 ////////////790 //-- colors buffer791 792 glGenRenderbuffersEXT(1, &colorsBuffer1);793 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer1);794 795 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);796 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer1);797 798 glGenTextures(1, &colorsTex1);799 glBindTexture(GL_TEXTURE_2D, colorsTex1);800 801 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);802 //glGenerateMipmapEXT(GL_TEXTURE_2D);803 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex1, 0);804 805 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);806 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);807 808 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);809 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);810 811 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);812 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);813 814 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));815 816 817 818 /////////////////////////819 820 glGenFramebuffersEXT(1, &fbo2);821 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2);822 823 824 ////////////825 //-- colors buffer826 827 glGenRenderbuffersEXT(1, &colorsBuffer2);828 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer2);829 830 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);831 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer2);832 833 glGenTextures(1, &colorsTex2);834 glBindTexture(GL_TEXTURE_2D, colorsTex2);835 836 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);837 //glGenerateMipmapEXT(GL_TEXTURE_2D);838 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex2, 0);839 840 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);841 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);842 843 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);844 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);845 846 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);847 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);848 849 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);850 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);851 852 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));853 854 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);855 530 856 531 PrintGLerror("fbo"); … … 1073 748 Matrix4x4 matViewing, matProjection; 1074 749 1075 if (renderType == RenderState::DEFERRED) 1076 { 1077 cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)matProjectionView.x); 1078 } 750 // store matrix of last frame 751 oldViewProjMatrix = matProjectionView; 1079 752 1080 753 glMatrixMode(GL_PROJECTION); … … 1170 843 //glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB); 1171 844 1172 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);845 fbo->Release(); 1173 846 1174 847 state.SetRenderType(RenderState::FIXED); … … 1189 862 //glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB); 1190 863 1191 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);864 fbo->Release(); 1192 865 1193 866 cgGLDisableProfile(RenderState::sCgFragmentProfile); … … 1215 888 state.SetRenderType(RenderState::DEFERRED); 1216 889 1217 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);890 fbo->Bind(); 1218 891 1219 892 glPushAttrib(GL_VIEWPORT_BIT); … … 1226 899 cgGLBindProgram(sCgMrtVertexProgram); 1227 900 901 /// draw to 3 color buffers 1228 902 glDrawBuffers(3, mrt); 1229 903 … … 1240 914 1241 915 916 // reset lod levels for current frame 1242 917 LODLevel::InitFrame(camera->GetPosition()); 1243 918 … … 1279 954 if (renderType == RenderState::DEFERRED) 1280 955 { 1281 //glPopAttrib(); 1282 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 956 fbo->Release(); 1283 957 1284 958 cgGLDisableProfile(RenderState::sCgVertexProfile); … … 1287 961 glDrawBuffers(1, mrt); 1288 962 1289 DisplayRenderTexture(); 1290 } 1291 963 if (useSsao) 964 ssaoShader->Render(fbo, oldViewProjMatrix, ssaoExpFactor); 965 else 966 deferredShader->Render(fbo); 967 } 1292 968 1293 969 /////////// … … 1379 1055 break; 1380 1056 case '7': 1381 expFactor *= 0.5f;1057 ssaoExpFactor *= 0.5f; 1382 1058 break; 1383 1059 case '8': 1384 expFactor *= 2.0f;1385 if ( expFactor > 1.0f) expFactor = 1.0f;1060 ssaoExpFactor *= 2.0f; 1061 if (ssaoExpFactor > 1.0f) ssaoExpFactor = 1.0f; 1386 1062 break; 1387 1063 case 'o': … … 1819 1495 if (RenderState::sCgMrtFragmentTexProgram) 1820 1496 cgDestroyProgram(RenderState::sCgMrtFragmentTexProgram); 1821 if (sCgSsaoProgram) 1822 cgDestroyProgram(sCgSsaoProgram); 1823 if (sCgDeferredProgram) 1824 cgDestroyProgram(sCgDeferredProgram); 1497 1825 1498 if (sCgContext) 1826 1499 cgDestroyContext(sCgContext); 1827 1828 glDeleteFramebuffersEXT(1, &fbo);1829 glDeleteRenderbuffersEXT(1, &depthBuffer);1830 glDeleteRenderbuffersEXT(1, &colorsBuffer);1831 glDeleteRenderbuffersEXT(1, &normalsBuffer);1832 glDeleteRenderbuffersEXT(1, &positionsBuffer);1833 1834 glDeleteTextures(1, &colorsTex);1835 glDeleteTextures(1, &normalsTex);1836 glDeleteTextures(1, &positionsTex);1837 glDeleteTextures(1, &noiseTex);1838 glDeleteTextures(1, &fontTex);1839 1500 } 1840 1501 … … 2065 1726 } 2066 1727 } 2067 2068 2069 void DisplayRenderTexture3()2070 {2071 glEnable(GL_TEXTURE_2D);2072 2073 if (isFirstTexture)2074 glBindTexture(GL_TEXTURE_2D, colorsTex1);2075 else2076 glBindTexture(GL_TEXTURE_2D, colorsTex2);2077 2078 glDisable(GL_LIGHTING);2079 2080 glMatrixMode(GL_PROJECTION);2081 glPushMatrix();2082 glLoadIdentity();2083 2084 glMatrixMode(GL_MODELVIEW);2085 glPushMatrix();2086 glLoadIdentity();2087 2088 const float offs = 0.5f;2089 glOrtho(-offs, offs, -offs, offs, 0, 1);2090 2091 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);2092 2093 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);2094 glBegin(GL_QUADS);2095 2096 glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);2097 glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);2098 glTexCoord2f(1, 1); glVertex3f( offs, offs, -0.5f);2099 glTexCoord2f(0, 1); glVertex3f(-offs, offs, -0.5f);2100 2101 glEnd();2102 2103 glEnable(GL_LIGHTING);2104 glDisable(GL_TEXTURE_2D);2105 2106 glMatrixMode(GL_PROJECTION);2107 glPopMatrix();2108 2109 glMatrixMode(GL_MODELVIEW);2110 glPopMatrix();2111 2112 PrintGLerror("displaytexture3");2113 }2114 2115 2116 void DisplayRenderTexture()2117 {2118 GLuint oldTex;2119 2120 if (isFirstTexture)2121 {2122 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo1);2123 oldTex = colorsTex2;2124 }2125 else2126 {2127 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2);2128 oldTex = colorsTex1;2129 }2130 2131 glPushAttrib(GL_VIEWPORT_BIT);2132 glViewport(0, 0, texWidth, texHeight);2133 2134 glDrawBuffers(1, mrt);2135 2136 glDisable(GL_ALPHA_TEST);2137 glDisable(GL_TEXTURE_2D);2138 glDisable(GL_LIGHTING);2139 2140 glMatrixMode(GL_PROJECTION);2141 glPushMatrix();2142 glLoadIdentity();2143 2144 glMatrixMode(GL_MODELVIEW);2145 glPushMatrix();2146 glLoadIdentity();2147 2148 const float offs = 0.5f;2149 2150 glOrtho(-offs, offs, -offs, offs, 0, 1);2151 2152 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);2153 2154 cgGLEnableProfile(RenderState::sCgFragmentProfile);2155 2156 if (useSsao)2157 {2158 cgGLBindProgram(sCgSsaoProgram);2159 2160 cgGLSetTextureParameter(sPositionsTexParamSsao, positionsTex);2161 cgGLEnableTextureParameter(sPositionsTexParamSsao);2162 2163 cgGLSetTextureParameter(sColorsTexParamSsao, colorsTex);2164 cgGLEnableTextureParameter(sColorsTexParamSsao);2165 2166 cgGLSetTextureParameter(sNormalsTexParamSsao, normalsTex);2167 cgGLEnableTextureParameter(sNormalsTexParamSsao);2168 2169 cgGLSetTextureParameter(sNoiseTexParamSsao, noiseTex);2170 cgGLEnableTextureParameter(sNoiseTexParamSsao);2171 2172 cgGLSetTextureParameter(sOldTexParamSsao, oldTex);2173 cgGLEnableTextureParameter(sOldTexParamSsao);2174 2175 cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));2176 cgGLSetParameter1f(sExpFactorParamSsao, expFactor);2177 2178 GenerateSamples(); cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples);2179 }2180 else2181 {2182 cgGLBindProgram(sCgDeferredProgram);2183 2184 cgGLSetTextureParameter(sPositionsTexParam, positionsTex);2185 cgGLEnableTextureParameter(sPositionsTexParam);2186 2187 cgGLSetTextureParameter(sColorsTexParam, colorsTex);2188 cgGLEnableTextureParameter(sColorsTexParam);2189 2190 cgGLSetTextureParameter(sNormalsTexParam, normalsTex);2191 cgGLEnableTextureParameter(sNormalsTexParam);2192 }2193 2194 Vector3 tl, tr, bl, br;2195 ComputeViewVectors(tl, tr, bl, br);2196 2197 glColor3f(1.0f, 1.0f, 1.0f);2198 2199 glBegin(GL_QUADS);2200 2201 // note: slightly larger texture hides ambient occlusion error on border but costs resolution2202 //float offs2 = 0.55f;2203 float offs2 = 0.5f;2204 2205 glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);2206 glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);2207 glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);2208 glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);2209 2210 glEnd();2211 2212 2213 if (useSsao)2214 {2215 cgGLDisableTextureParameter(sColorsTexParamSsao);2216 cgGLDisableTextureParameter(sPositionsTexParamSsao);2217 cgGLDisableTextureParameter(sNormalsTexParamSsao);2218 cgGLDisableTextureParameter(sNoiseTexParamSsao);2219 cgGLDisableTextureParameter(sOldTexParamSsao);2220 }2221 else2222 {2223 cgGLDisableTextureParameter(sColorsTexParam);2224 cgGLDisableTextureParameter(sPositionsTexParam);2225 cgGLDisableTextureParameter(sNormalsTexParam);2226 }2227 2228 cgGLDisableProfile(RenderState::sCgFragmentProfile);2229 2230 glEnable(GL_LIGHTING);2231 glDisable(GL_TEXTURE_2D);2232 2233 glMatrixMode(GL_PROJECTION);2234 glPopMatrix();2235 2236 glMatrixMode(GL_MODELVIEW);2237 glPopMatrix();2238 2239 glPopAttrib();2240 2241 PrintGLerror("displaytexture");2242 2243 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);2244 2245 DisplayRenderTexture3();2246 }2247 2248 #if 02249 // kustls magic sample positions2250 void GenerateSamples()2251 {2252 static const float mysamples[] =2253 {2254 -0.326212f, -0.405805f,2255 -0.840144f, -0.07358f,2256 -0.695914f, 0.457137f,2257 -0.203345f, 0.620716,2258 0.96234f, -0.194983f,2259 0.473434f, -0.480026f,2260 0.519456, 0.767022f,2261 0.185461f, -0.893124f,2262 0.507431f, 0.064425f,2263 0.89642f, 0.412458f,2264 -0.32194f, -0.932615f,2265 -0.791559f, -0.597705f,2266 0.326212f, 0.405805f,2267 0.840144f, 0.07358f,2268 0.695914f, -0.457137f,2269 0.203345f, -0.620716,2270 -0.96234f, 0.194983f,2271 -0.473434f, 0.480026f,2272 -0.519456, -0.767022f,2273 -0.185461f, 0.893124f,2274 -0.507431f, -0.064425f,2275 -0.89642f, -0.412458f,2276 0.32194f, 0.932615f,2277 0.791559f, 0.597705f2278 };2279 2280 for (int i = 0; i < NUM_SAMPLES; ++ i)2281 {2282 samples[i] = mysamples[i];2283 }2284 }2285 2286 #else2287 2288 2289 void GenerateSamples()2290 {2291 /*static HaltonSequence halton;2292 2293 float r[2];2294 2295 // generates poisson distribution on disc2296 float minDist = 2.0f / sqrt((float)NUM_SAMPLES);2297 2298 //cout << "minDist before= " << minDist << endl;2299 2300 for (int i = 0; i < NUM_SAMPLES * 2; i += 2)2301 {2302 int tries = 0, totalTries = 0;2303 2304 // repeat until valid sample was found2305 while (1)2306 {2307 ++ tries;2308 ++ totalTries;2309 2310 halton.GetNext(2, r);2311 2312 //const float rx = RandomValue(-1, 1);2313 //const float ry = RandomValue(-1, 1);2314 2315 const float rx = r[0] * 2.0f - 1.0f;2316 const float ry = r[1] * 2.0f - 1.0f;2317 2318 // check if in disk, else exit early2319 if (rx * rx + ry * ry > 1)2320 continue;2321 2322 bool sampleValid = true;2323 2324 // check poisson property2325 for (int j = 0; ((j < i) && sampleValid); j += 2)2326 {2327 const float dist =2328 sqrt((samples[j] - rx) * (samples[j] - rx) +2329 (samples[j + 1] - ry) * (samples[j + 1] - ry));2330 2331 if (dist < minDist)2332 sampleValid = false;2333 }2334 2335 if (sampleValid)2336 {2337 samples[i] = rx;2338 samples[i + 1]= ry;2339 break;2340 }2341 2342 if (tries > 2000)2343 {2344 minDist *= 0.9f;2345 tries = 0;2346 }2347 }2348 }2349 */2350 2351 static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f);2352 poisson.Generate((Sample2 *)samples);2353 }2354 2355 2356 #endif2357 2358 2359 void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br)2360 {2361 float myfov = fov * M_PI / 180.0f;2362 2363 const float w_far = 2.0f * tan(myfov / 2.0f);2364 const float aspect = texWidth / texHeight;2365 2366 const float h_far = w_far / aspect;2367 2368 float t1 = h_far * 0.5f;2369 float t2 = w_far * 0.5f;2370 2371 #if 02372 bl = Normalize(-Vector3(-t1, -t2, 1.0f));2373 br = Normalize(-Vector3( t1, -t2, 1.0f));2374 tl = Normalize(-Vector3(-t1, t2, 1.0f));2375 tr = Normalize(-Vector3( t1, t2, 1.0f));2376 #else2377 bl = -Normalize(camera->GetDirection());2378 br = -Normalize(camera->GetDirection());2379 tl = -Normalize(camera->GetDirection());2380 tr = -Normalize(camera->GetDirection());2381 #endif2382 // normalize to 0 .. 12383 bl = bl * 0.5f + 0.5f;2384 br = br * 0.5f + 0.5f;2385 tl = tl * 0.5f + 0.5f;2386 tr = tr * 0.5f + 0.5f;2387 }2388 2389 2390 void CreateNoiseTex2D()2391 {2392 randomNormals = new GLubyte[texWidth * texHeight * 3];2393 2394 for (int i = 0; i < texWidth * texHeight * 3; i += 3)2395 {2396 // create random samples over sphere2397 const float rx = RandomValue(0, 1);2398 const float theta = 2.0f * acos(sqrt(1.0f - rx));2399 2400 randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f);2401 randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f);2402 randomNormals[i + 2] = 0;2403 }2404 2405 glEnable(GL_TEXTURE_2D);2406 glGenTextures(1, &noiseTex);2407 glBindTexture(GL_TEXTURE_2D, noiseTex);2408 2409 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);2410 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);2411 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);2412 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);2413 2414 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);2415 //gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, texWidth, texHeight, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);2416 2417 glBindTexture(GL_TEXTURE_2D, 0);2418 glDisable(GL_TEXTURE_2D);2419 2420 cout << "created noise texture" << endl;2421 PrintGLerror("noisetexture");2422 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
r2854 r2861 289 289 290 290 float4 col = shade(IN, colors, positions, normal.xyz, amb); 291 291 292 OUT.color = col; 292 293
Note: See TracChangeset
for help on using the changeset viewer.