[2858] | 1 | #include "DeferredShader.h"
|
---|
[2859] | 2 | #include "FrameBufferObject.h"
|
---|
| 3 | #include "RenderState.h"
|
---|
[2858] | 4 |
|
---|
| 5 |
|
---|
| 6 | using namespace std;
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | namespace CHCDemoEngine
|
---|
| 10 | {
|
---|
| 11 |
|
---|
[2859] | 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 |
|
---|
[2879] | 26 | static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
|
---|
[2865] | 27 |
|
---|
[2879] | 28 |
|
---|
[2859] | 29 | static CGprogram sCgDeferredProgram;
|
---|
[2865] | 30 | static CGprogram sCgAntiAliasingProgram;
|
---|
| 31 |
|
---|
| 32 |
|
---|
[2859] | 33 | static CGparameter sColorsTexParam;
|
---|
| 34 | static CGparameter sPositionsTexParam;
|
---|
| 35 | static CGparameter sNormalsTexParam;
|
---|
| 36 |
|
---|
[2865] | 37 | static CGparameter sColorsTexAntiAliasingParam;
|
---|
| 38 | static CGparameter sNormalsTexAntiAliasingParam;
|
---|
[2859] | 39 |
|
---|
[2865] | 40 |
|
---|
[2859] | 41 | DeferredShader::DeferredShader(int w, int h):
|
---|
| 42 | mWidth(w), mHeight(h)
|
---|
[2865] | 43 | {
|
---|
[2879] | 44 | //mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
|
---|
| 45 | //mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
[2865] | 46 | }
|
---|
[2859] | 47 |
|
---|
| 48 |
|
---|
[2861] | 49 | DeferredShader::~DeferredShader()
|
---|
| 50 | {
|
---|
| 51 | if (sCgDeferredProgram)
|
---|
| 52 | cgDestroyProgram(sCgDeferredProgram);
|
---|
[2879] | 53 |
|
---|
| 54 | //DEL_PTR(mFbo);
|
---|
[2861] | 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
[2859] | 58 | void DeferredShader::Init(CGcontext context)
|
---|
| 59 | {
|
---|
| 60 | sCgDeferredProgram =
|
---|
| 61 | cgCreateProgramFromFile(context,
|
---|
| 62 | CG_SOURCE,
|
---|
| 63 | "src/shaders/deferred.cg",
|
---|
| 64 | RenderState::sCgFragmentProfile,
|
---|
| 65 | "main",
|
---|
| 66 | NULL);
|
---|
| 67 |
|
---|
| 68 | if (sCgDeferredProgram != NULL)
|
---|
| 69 | {
|
---|
| 70 | cgGLLoadProgram(sCgDeferredProgram);
|
---|
| 71 |
|
---|
| 72 | // we need size of texture for scaling
|
---|
| 73 | sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions");
|
---|
| 74 | sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors");
|
---|
| 75 | sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
|
---|
| 76 | }
|
---|
| 77 | else
|
---|
| 78 | cerr << "deferred program failed to load" << endl;
|
---|
| 79 |
|
---|
[2865] | 80 | sCgAntiAliasingProgram =
|
---|
| 81 | cgCreateProgramFromFile(context,
|
---|
| 82 | CG_SOURCE,
|
---|
| 83 | "src/shaders/antialiasing.cg",
|
---|
| 84 | RenderState::sCgFragmentProfile,
|
---|
| 85 | "main",
|
---|
| 86 | NULL);
|
---|
[2859] | 87 |
|
---|
[2865] | 88 | if (sCgAntiAliasingProgram != NULL)
|
---|
| 89 | {
|
---|
| 90 | cgGLLoadProgram(sCgAntiAliasingProgram);
|
---|
| 91 |
|
---|
| 92 | sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors");
|
---|
| 93 | sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
|
---|
| 94 | }
|
---|
| 95 | else
|
---|
| 96 | cerr << "antialiasing program failed to load" << endl;
|
---|
| 97 |
|
---|
| 98 |
|
---|
[2859] | 99 | PrintGLerror("init");
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
[2858] | 103 | void DeferredShader::Render(FrameBufferObject *fbo)
|
---|
| 104 | {
|
---|
[2865] | 105 | FirstPass(fbo);
|
---|
| 106 | AntiAliasing(fbo);
|
---|
| 107 | }
|
---|
[2861] | 108 |
|
---|
[2865] | 109 |
|
---|
| 110 | void DeferredShader::FirstPass(FrameBufferObject *fbo)
|
---|
| 111 | {
|
---|
[2859] | 112 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
[2861] | 113 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
| 114 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
[2859] | 115 |
|
---|
[2879] | 116 | fbo->Bind();
|
---|
| 117 | glDrawBuffers(1, mymrt + 3);
|
---|
[2865] | 118 |
|
---|
[2879] | 119 |
|
---|
[2858] | 120 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
[2859] | 121 | glViewport(0, 0, mWidth, mHeight);
|
---|
[2858] | 122 |
|
---|
| 123 | glDisable(GL_ALPHA_TEST);
|
---|
| 124 | glDisable(GL_TEXTURE_2D);
|
---|
| 125 | glDisable(GL_LIGHTING);
|
---|
| 126 |
|
---|
| 127 | glMatrixMode(GL_PROJECTION);
|
---|
| 128 | glPushMatrix();
|
---|
| 129 | glLoadIdentity();
|
---|
| 130 |
|
---|
| 131 | glMatrixMode(GL_MODELVIEW);
|
---|
| 132 | glPushMatrix();
|
---|
| 133 | glLoadIdentity();
|
---|
| 134 |
|
---|
| 135 | const float offs = 0.5f;
|
---|
| 136 |
|
---|
| 137 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
| 138 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 139 |
|
---|
| 140 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
| 141 |
|
---|
| 142 | cgGLBindProgram(sCgDeferredProgram);
|
---|
| 143 |
|
---|
[2859] | 144 | cgGLSetTextureParameter(sColorsTexParam, colorsTex);
|
---|
| 145 | cgGLEnableTextureParameter(sColorsTexParam);
|
---|
| 146 |
|
---|
[2858] | 147 | cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
|
---|
| 148 | cgGLEnableTextureParameter(sPositionsTexParam);
|
---|
| 149 |
|
---|
| 150 | cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
|
---|
| 151 | cgGLEnableTextureParameter(sNormalsTexParam);
|
---|
| 152 |
|
---|
| 153 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
[2861] | 154 |
|
---|
[2858] | 155 | glBegin(GL_QUADS);
|
---|
| 156 |
|
---|
| 157 | float offs2 = 0.5f;
|
---|
| 158 |
|
---|
| 159 | glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
| 160 | glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
| 161 | glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
| 162 | glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
| 163 |
|
---|
| 164 | glEnd();
|
---|
| 165 |
|
---|
| 166 | cgGLDisableTextureParameter(sColorsTexParam);
|
---|
| 167 | cgGLDisableTextureParameter(sPositionsTexParam);
|
---|
| 168 | cgGLDisableTextureParameter(sNormalsTexParam);
|
---|
| 169 |
|
---|
| 170 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 171 |
|
---|
| 172 | glEnable(GL_LIGHTING);
|
---|
| 173 | glDisable(GL_TEXTURE_2D);
|
---|
| 174 |
|
---|
| 175 | glMatrixMode(GL_PROJECTION);
|
---|
| 176 | glPopMatrix();
|
---|
| 177 |
|
---|
| 178 | glMatrixMode(GL_MODELVIEW);
|
---|
| 179 | glPopMatrix();
|
---|
| 180 |
|
---|
| 181 | glPopAttrib();
|
---|
| 182 |
|
---|
[2865] | 183 | FrameBufferObject::Release();
|
---|
| 184 |
|
---|
[2858] | 185 | PrintGLerror("deferred shading");
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 |
|
---|
[2865] | 189 | static void SetVertex(float x, float y, float x_offs, float y_offs)
|
---|
| 190 | {
|
---|
| 191 | glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
|
---|
| 192 | glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
|
---|
| 193 | glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
|
---|
| 194 | glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
|
---|
| 195 | glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
|
---|
| 196 |
|
---|
| 197 | glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
|
---|
| 198 | glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
|
---|
| 199 |
|
---|
| 200 | glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 |
|
---|
| 204 | void DeferredShader::AntiAliasing(FrameBufferObject *fbo)
|
---|
| 205 | {
|
---|
[2879] | 206 | GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
|
---|
[2865] | 207 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
| 208 |
|
---|
| 209 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
| 210 | glViewport(0, 0, mWidth, mHeight);
|
---|
| 211 |
|
---|
| 212 | glDisable(GL_ALPHA_TEST);
|
---|
| 213 | glDisable(GL_TEXTURE_2D);
|
---|
| 214 | glDisable(GL_LIGHTING);
|
---|
| 215 |
|
---|
| 216 | glMatrixMode(GL_PROJECTION);
|
---|
| 217 | glPushMatrix();
|
---|
| 218 | glLoadIdentity();
|
---|
| 219 |
|
---|
| 220 | glMatrixMode(GL_MODELVIEW);
|
---|
| 221 | glPushMatrix();
|
---|
| 222 | glLoadIdentity();
|
---|
| 223 |
|
---|
| 224 | const float offs = 0.5f;
|
---|
| 225 |
|
---|
| 226 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
| 227 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 228 |
|
---|
| 229 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
| 230 |
|
---|
| 231 | cgGLBindProgram(sCgAntiAliasingProgram);
|
---|
| 232 |
|
---|
| 233 | cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
|
---|
| 234 | cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
|
---|
| 235 |
|
---|
| 236 | cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
|
---|
| 237 | cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
|
---|
| 238 |
|
---|
| 239 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 240 |
|
---|
| 241 | float offs2 = 0.5f;
|
---|
| 242 |
|
---|
| 243 | glBegin(GL_QUADS);
|
---|
| 244 |
|
---|
| 245 | // the neighbouring texels
|
---|
| 246 | float x_offs = 1.0f / mWidth;
|
---|
| 247 | float y_offs = 1.0f / mHeight;
|
---|
| 248 |
|
---|
| 249 | SetVertex(0, 0, x_offs, y_offs);
|
---|
| 250 | SetVertex(1, 0, x_offs, y_offs);
|
---|
| 251 | SetVertex(1, 1, x_offs, y_offs);
|
---|
| 252 | SetVertex(0, 1, x_offs, y_offs);
|
---|
| 253 |
|
---|
| 254 | glEnd();
|
---|
| 255 |
|
---|
| 256 | cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
|
---|
| 257 | cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
|
---|
| 258 |
|
---|
| 259 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 260 |
|
---|
| 261 | glEnable(GL_LIGHTING);
|
---|
| 262 | glDisable(GL_TEXTURE_2D);
|
---|
| 263 |
|
---|
| 264 | glMatrixMode(GL_PROJECTION);
|
---|
| 265 | glPopMatrix();
|
---|
| 266 |
|
---|
| 267 | glMatrixMode(GL_MODELVIEW);
|
---|
| 268 | glPopMatrix();
|
---|
| 269 |
|
---|
| 270 | glPopAttrib();
|
---|
| 271 |
|
---|
| 272 | PrintGLerror("deferred shading");
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 |
|
---|
[2858] | 276 | } // namespace
|
---|