- Timestamp:
- 08/21/08 16:31:03 (16 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
r2857 r2859 316 316 </File> 317 317 <File 318 RelativePath=".\src\SsaoShader.cpp" 319 > 320 </File> 321 <File 322 RelativePath=".\src\SsaoShader.h" 323 > 324 </File> 325 <File 318 326 RelativePath=".\src\Triangle3.cpp" 319 327 > -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp
r2858 r2859 1 1 #include "DeferredShader.h" 2 #include "glInterface.h" 2 #include "FrameBufferObject.h" 3 #include "RenderState.h" 3 4 4 5 … … 9 10 { 10 11 12 13 static void PrintGLerror(char *msg) 14 { 15 GLenum errCode; 16 const GLubyte *errStr; 17 18 if ((errCode = glGetError()) != GL_NO_ERROR) 19 { 20 errStr = gluErrorString(errCode); 21 fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg); 22 } 23 } 24 25 26 static CGprogram sCgDeferredProgram; 27 static CGparameter sColorsTexParam; 28 static CGparameter sPositionsTexParam; 29 static CGparameter sNormalsTexParam; 30 31 32 DeferredShader::DeferredShader(int w, int h): 33 mWidth(w), mHeight(h) 34 {} 35 36 37 void DeferredShader::Init(CGcontext context) 38 { 39 40 sCgDeferredProgram = 41 cgCreateProgramFromFile(context, 42 CG_SOURCE, 43 "src/shaders/deferred.cg", 44 RenderState::sCgFragmentProfile, 45 "main", 46 NULL); 47 48 if (sCgDeferredProgram != NULL) 49 { 50 cgGLLoadProgram(sCgDeferredProgram); 51 52 // we need size of texture for scaling 53 sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions"); 54 sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors"); 55 sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals"); 56 } 57 else 58 cerr << "deferred program failed to load" << endl; 59 60 61 PrintGLerror("init"); 62 } 63 64 11 65 void DeferredShader::Render(FrameBufferObject *fbo) 12 66 { 67 GLuint positionsTex = fbo->GetColorBuffer(0)->GetTexture(); 68 GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 69 GLuint normalsTex = fbo->GetColorBuffer(0)->GetTexture(); 70 13 71 glPushAttrib(GL_VIEWPORT_BIT); 14 glViewport(0, 0, fbo->GetWidth(), fbo->GetHeight());72 glViewport(0, 0, mWidth, mHeight); 15 73 16 74 glDisable(GL_ALPHA_TEST); … … 35 93 cgGLBindProgram(sCgDeferredProgram); 36 94 95 cgGLSetTextureParameter(sColorsTexParam, colorsTex); 96 cgGLEnableTextureParameter(sColorsTexParam); 97 37 98 cgGLSetTextureParameter(sPositionsTexParam, positionsTex); 38 99 cgGLEnableTextureParameter(sPositionsTexParam); 39 40 cgGLSetTextureParameter(sColorsTexParam, colorsTex);41 cgGLEnableTextureParameter(sColorsTexParam);42 100 43 101 cgGLSetTextureParameter(sNormalsTexParam, normalsTex); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.h
r2858 r2859 3 3 4 4 #include "common.h" 5 #include "glInterface.h" 6 #include <Cg/cg.h> 7 #include <Cg/cgGL.h> 8 5 9 6 10 namespace CHCDemoEngine … … 9 13 class FrameBufferObject; 10 14 11 /** This class implements a deferred shading algorithm. 15 16 /** This class implements a deferred shading algorithm that takes 17 a frame buffer object as input and outputs an image in the given size 12 18 */ 13 19 class DeferredShader 14 20 { 15 21 public: 16 /** constructor requesting an opengl occlusion query.22 /** constructor for a deferred shader taking the requested output image size 17 23 */ 18 DeferredShader( );24 DeferredShader(int w, int h); 19 25 20 26 /** The algorithm renders the scene given an fbo. … … 22 28 */ 23 29 void Render(FrameBufferObject *fbo); 30 31 /** Initialises the deferred shader and loads the required shaders: 32 This function has to be called only once. 33 */ 34 static void Init(CGcontext context); 35 36 37 protected: 38 39 int mWidth; 40 int mHeight; 24 41 }; 25 42 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp
r2857 r2859 132 132 133 133 134 ColorBufferObject *FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,135 136 137 138 134 int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col, 135 ColorBufferObject::WRAP_TYPE wrapType, 136 ColorBufferObject::FILTER_TYPE filterType, 137 bool useMipMap, 138 bool useMultiSampling) 139 139 { 140 140 ColorBufferObject *colorBuf = … … 142 142 mColorBuffers.push_back(colorBuf); 143 143 144 return colorBuf;144 return (int)mColorBuffers.size() - 1; 145 145 } 146 146 147 147 148 } // namespace -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.h
r2857 r2859 27 27 bool useMultiSampling); 28 28 29 unsigned int GetTexture() const {return mTexId;} 30 29 31 protected: 30 32 … … 41 43 42 44 enum DEPTH_FORMAT { DEPTH_16, DEPTH_24, DEPTH_32 }; 45 43 46 /** constructor requesting an opengl occlusion query. 44 47 */ 45 48 FrameBufferObject(int w, int h, bool useDepth, DEPTH_FORMAT d); 46 49 /** Creates and adds a color buffer to the current frame buffer object. 47 Returns a pointer to the buffer object50 Returns the index that allows to retrieve the color buffer object. 48 51 */ 49 ColorBufferObject *AddColorBuffer(ColorBufferObject::FORMAT col, 50 ColorBufferObject::WRAP_TYPE wrapType, 51 ColorBufferObject::FILTER_TYPE filterType, 52 bool useMipMap, 53 bool useMultiSampling); 52 int AddColorBuffer(ColorBufferObject::FORMAT col, 53 ColorBufferObject::WRAP_TYPE wrapType, 54 ColorBufferObject::FILTER_TYPE filterType, 55 bool useMipMap, 56 bool useMultiSampling); 57 58 /** Returns the color buffer object on position i. 59 */ 60 ColorBufferObject *GetColorBuffer(int i) const { return mColorBuffers[i]; } 61 54 62 55 63 protected: -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp
r2858 r2859 1 #include "DeferredShader.h" 1 #include "SsaoShader.h" 2 #include "FrameBufferObject.h" 3 #include "RenderState.h" 4 #include "SampleGenerator.h" 5 2 6 3 7 using namespace std; 4 8 9 GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT}; 5 10 6 11 namespace CHCDemoEngine 7 12 { 8 13 14 static CGprogram sCgSsaoProgram = NULL; 15 16 static CGparameter sColorsTexParam; 17 static CGparameter sPositionsTexParam; 18 static CGparameter sNormalsTexParam; 19 static CGparameter sOldModelViewProjMatrixParam; 20 static CGparameter sMaxDepthParam; 21 static CGparameter sSamplesParam; 22 static CGparameter sOldTexParam; 23 static CGparameter sNoiseTexParam; 24 static CGparameter sNoiseMultiplierParam; 25 static CGparameter sExpFactorParam; 26 27 28 #define NUM_SAMPLES 16 29 30 // ssao random spherical samples 31 static Sample2 samples[NUM_SAMPLES]; 32 33 34 static void PrintGLerror(char *msg) 35 { 36 GLenum errCode; 37 const GLubyte *errStr; 38 39 if ((errCode = glGetError()) != GL_NO_ERROR) 40 { 41 errStr = gluErrorString(errCode); 42 fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg); 43 } 44 } 45 46 47 /** Generate poisson disc distributed sample points on the unit disc 48 */ 49 static void GenerateSamples() 50 { 51 static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f); 52 poisson.Generate((Sample2 *)samples); 53 } 54 55 56 SsaoShader::SsaoShader(int w, int h, float expFactor, float scaleFactor): 57 mWidth(w), mHeight(h), mExpFactor(expFactor), mScaleFactor(scaleFactor) 58 {} 59 60 61 void SsaoShader::Init(CGcontext context) 62 { 63 64 /////////////// 65 66 sCgSsaoProgram = 67 cgCreateProgramFromFile(context, 68 CG_SOURCE, 69 "src/shaders/deferred.cg", 70 RenderState::sCgFragmentProfile, 71 "main_ssao", 72 NULL); 73 74 if (sCgSsaoProgram != NULL) 75 { 76 cgGLLoadProgram(sCgSsaoProgram); 77 78 // we need size of texture for scaling 79 sPositionsTexParam = cgGetNamedParameter(sCgSsaoProgram, "positions"); 80 sColorsTexParam = cgGetNamedParameter(sCgSsaoProgram, "colors"); 81 sNormalsTexParam = cgGetNamedParameter(sCgSsaoProgram, "normals"); 82 sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture"); 83 sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier"); 84 sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj"); 85 sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth"); 86 sExpFactorParam = cgGetNamedParameter(sCgSsaoProgram, "expFactor"); 87 88 sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples"); 89 sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex"); 90 91 // generate samples for ssao kernel 92 GenerateSamples(); 93 cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples); 94 } 95 else 96 cerr << "ssao program failed to load" << endl; 97 98 PrintGLerror("init"); 99 } 100 101 102 void SsaoShader::Render(FrameBufferObject *fbo, FrameBufferObject *fbo2) 103 { 104 FirstPass(fbo, fbo2); 105 SecondPass(fbo, fbo2); 106 } 107 108 109 void SsaoShader::FirstPass(FrameBufferObject *fbo, FrameBufferObject *fbo2) 110 { 111 GLuint positionsTex = fbo->GetColorBuffer(0)->GetTexture(); 112 GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 113 GLuint normalsTex = fbo->GetColorBuffer(0)->GetTexture(); 114 115 glPushAttrib(GL_VIEWPORT_BIT); 116 glViewport(0, 0, mWidth, mHeight); 117 118 glDrawBuffers(1, mrt); 119 120 glDisable(GL_ALPHA_TEST); 121 glDisable(GL_TEXTURE_2D); 122 glDisable(GL_LIGHTING); 123 124 glMatrixMode(GL_PROJECTION); 125 glPushMatrix(); 126 glLoadIdentity(); 127 128 glMatrixMode(GL_MODELVIEW); 129 glPushMatrix(); 130 glLoadIdentity(); 131 132 const float offs = 0.5f; 133 134 glOrtho(-offs, offs, -offs, offs, 0, 1); 135 136 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 137 138 cgGLEnableProfile(RenderState::sCgFragmentProfile); 139 140 cgGLBindProgram(sCgSsaoProgram); 141 142 cgGLSetTextureParameter(sPositionsTexParam, positionsTex); 143 cgGLEnableTextureParameter(sPositionsTexParam); 144 145 cgGLSetTextureParameter(sColorsTexParam, colorsTex); 146 cgGLEnableTextureParameter(sColorsTexParam); 147 148 cgGLSetTextureParameter(sNormalsTexParam, normalsTex); 149 cgGLEnableTextureParameter(sNormalsTexParam); 150 151 cgGLSetTextureParameter(sNoiseTexParam, noiseTex); 152 cgGLEnableTextureParameter(sNoiseTexParam); 153 154 cgGLSetTextureParameter(sOldTexParam, oldTex); 155 cgGLEnableTextureParameter(sOldTexParam); 156 157 cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f)); 158 159 cgGLSetParameter1f(sMaxDepthParam, mScaleFactor); 160 cgGLSetParameter1f(sExpFactorParam, mExpFactor); 161 162 163 GenerateSamples(); 164 cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples); 165 166 Vector3 tl, tr, bl, br; 167 ComputeViewVectors(tl, tr, bl, br); 168 169 glColor3f(1.0f, 1.0f, 1.0f); 170 171 glBegin(GL_QUADS); 172 173 // note: slightly larger texture hides ambient occlusion error on border but costs resolution 174 //float offs2 = 0.55f; 175 float offs2 = 0.5f; 176 177 glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f); 178 glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f); 179 glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f); 180 glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f); 181 182 glEnd(); 183 184 cgGLDisableTextureParameter(sColorsTexParamSsao); 185 cgGLDisableTextureParameter(sPositionsTexParamSsao); 186 cgGLDisableTextureParameter(sNormalsTexParamSsao); 187 cgGLDisableTextureParameter(sNoiseTexParamSsao); 188 cgGLDisableTextureParameter(sOldTexParamSsao); 189 190 cgGLDisableProfile(RenderState::sCgFragmentProfile); 191 192 glEnable(GL_LIGHTING); 193 glDisable(GL_TEXTURE_2D); 194 195 glMatrixMode(GL_PROJECTION); 196 glPopMatrix(); 197 198 glMatrixMode(GL_MODELVIEW); 199 glPopMatrix(); 200 201 glPopAttrib(); 202 203 PrintGLerror("displaytexture"); 204 205 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 206 207 208 PrintGLerror("ssao first pass"); 209 } 210 211 212 void SsaoShader::SecondPass(FrameBufferObject *fbo, FrameBufferObject *fbo2) 213 { 214 glEnable(GL_TEXTURE_2D); 215 216 glBindTexture(GL_TEXTURE_2D, fbo2->GetColorBuffer(0)->GetTexture()); 217 218 glDisable(GL_LIGHTING); 219 220 glMatrixMode(GL_PROJECTION); 221 glPushMatrix(); 222 glLoadIdentity(); 223 224 glMatrixMode(GL_MODELVIEW); 225 glPushMatrix(); 226 glLoadIdentity(); 227 228 const float offs = 0.5f; 229 glOrtho(-offs, offs, -offs, offs, 0, 1); 230 231 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 232 233 glColor4f(1.0f, 1.0f, 1.0f, 1.0f); 234 glBegin(GL_QUADS); 235 236 glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f); 237 glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f); 238 glTexCoord2f(1, 1); glVertex3f( offs, offs, -0.5f); 239 glTexCoord2f(0, 1); glVertex3f(-offs, offs, -0.5f); 240 241 glEnd(); 242 243 glEnable(GL_LIGHTING); 244 glDisable(GL_TEXTURE_2D); 245 246 glMatrixMode(GL_PROJECTION); 247 glPopMatrix(); 248 249 glMatrixMode(GL_MODELVIEW); 250 glPopMatrix(); 251 252 PrintGLerror("ssao second pass"); 253 } 254 255 256 9 257 } // namespace -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h
r2858 r2859 1 #ifndef _ DeferredShader_H__2 #define _ DeferredShader_H__1 #ifndef _SsaoShader_H__ 2 #define _SsaoShader_H__ 3 3 4 4 #include "common.h" 5 #include "glInterface.h" 6 #include <Cg/cg.h> 7 #include <Cg/cgGL.h> 8 5 9 6 10 namespace CHCDemoEngine … … 9 13 class FrameBufferObject; 10 14 11 /** This class implements a deferred shading algorithm. 15 16 /** This class implements a deferred shading algorithm that takes 17 a frame buffer object as input and outputs an image in the given size 12 18 */ 13 class DeferredShader19 class SsaoShader 14 20 { 15 21 public: 16 /** constructor requesting an opengl occlusion query. 22 /** constructor for a deferred shader taking the requested output image size, 23 the exponential smoothing factor for temporal reprojection. 24 25 The last parameter is a just a scale factor 26 for the scene depth in order to get better floating point precision in the shader 27 This must be reciprocal value of the scale factor used in the mrt shader. 17 28 */ 18 DeferredShader(); 19 29 SsaoShader(int w, int h, float expFactor, float scaleFactor); 20 30 /** The algorithm renders the scene given an fbo. 21 31 The fbo must have color buffer, position buffer, normal buffer. 22 32 */ 23 void Render(FrameBufferObject *fbo); 33 void Render(FrameBufferObject *fbo, FrameBufferObject *fbo2); 34 35 /** Initialises the deferred shader and loads the required shaders: 36 This function has to be called only once. 37 */ 38 static void Init(CGcontext context); 39 40 41 protected: 42 43 void FirstPass(FrameBufferObject *fbo, FrameBufferObject *fbo2); 44 45 void SecondPass(FrameBufferObject *fbo, FrameBufferObject *fbo2); 46 47 48 //////////// 49 50 int mWidth; 51 int mHeight; 52 53 /// the temporal smoothing factor 54 float mExpFactor; 55 /// this is just a scale factor for the scene depth in order to get better float precision in the shader 56 float mScaleFactor; 24 57 }; 25 58 26 59 } // namespace 27 #endif // _ DeferredShader_H__60 #endif // _SsaoShader_H__ -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
r2857 r2859 671 671 672 672 673 ColorBufferObject *diffuseBuffer = 674 myfbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, 675 ColorBufferObject::WRAP_CLAMP_TO_EDGE, 676 ColorBufferObject::FILTER_LINEAR, 677 false, false); 678 679 ColorBufferObject *mypositionBuffer = 680 myfbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, 681 ColorBufferObject::WRAP_CLAMP_TO_EDGE, 682 ColorBufferObject::FILTER_NEAREST, 683 false, false); 684 685 686 ColorBufferObject *mynormalsBuffer = 687 myfbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, 688 ColorBufferObject::WRAP_CLAMP_TO_EDGE, 689 ColorBufferObject::FILTER_NEAREST, 690 false, false); 673 // the diffuse color buffer 674 myfbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, 675 ColorBufferObject::WRAP_CLAMP_TO_EDGE, 676 ColorBufferObject::FILTER_LINEAR, 677 false, false); 678 679 // the positions buffer 680 myfbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, 681 ColorBufferObject::WRAP_CLAMP_TO_EDGE, 682 ColorBufferObject::FILTER_NEAREST, 683 false, false); 684 685 // the normals buffer 686 myfbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, 687 ColorBufferObject::WRAP_CLAMP_TO_EDGE, 688 ColorBufferObject::FILTER_NEAREST, 689 false, false); 691 690 692 691
Note: See TracChangeset
for help on using the changeset viewer.