[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 |
|
---|
| 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 |
|
---|
[2858] | 65 | void DeferredShader::Render(FrameBufferObject *fbo)
|
---|
| 66 | {
|
---|
[2859] | 67 | GLuint positionsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
| 68 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
| 69 | GLuint normalsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
| 70 |
|
---|
[2858] | 71 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
[2859] | 72 | glViewport(0, 0, mWidth, mHeight);
|
---|
[2858] | 73 |
|
---|
| 74 | glDisable(GL_ALPHA_TEST);
|
---|
| 75 | glDisable(GL_TEXTURE_2D);
|
---|
| 76 | glDisable(GL_LIGHTING);
|
---|
| 77 |
|
---|
| 78 | glMatrixMode(GL_PROJECTION);
|
---|
| 79 | glPushMatrix();
|
---|
| 80 | glLoadIdentity();
|
---|
| 81 |
|
---|
| 82 | glMatrixMode(GL_MODELVIEW);
|
---|
| 83 | glPushMatrix();
|
---|
| 84 | glLoadIdentity();
|
---|
| 85 |
|
---|
| 86 | const float offs = 0.5f;
|
---|
| 87 |
|
---|
| 88 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
| 89 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 90 |
|
---|
| 91 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
| 92 |
|
---|
| 93 | cgGLBindProgram(sCgDeferredProgram);
|
---|
| 94 |
|
---|
[2859] | 95 | cgGLSetTextureParameter(sColorsTexParam, colorsTex);
|
---|
| 96 | cgGLEnableTextureParameter(sColorsTexParam);
|
---|
| 97 |
|
---|
[2858] | 98 | cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
|
---|
| 99 | cgGLEnableTextureParameter(sPositionsTexParam);
|
---|
| 100 |
|
---|
| 101 | cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
|
---|
| 102 | cgGLEnableTextureParameter(sNormalsTexParam);
|
---|
| 103 |
|
---|
| 104 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 105 |
|
---|
| 106 | glBegin(GL_QUADS);
|
---|
| 107 |
|
---|
| 108 | float offs2 = 0.5f;
|
---|
| 109 |
|
---|
| 110 | glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
| 111 | glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
| 112 | glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
| 113 | glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
| 114 |
|
---|
| 115 | glEnd();
|
---|
| 116 |
|
---|
| 117 | cgGLDisableTextureParameter(sColorsTexParam);
|
---|
| 118 | cgGLDisableTextureParameter(sPositionsTexParam);
|
---|
| 119 | cgGLDisableTextureParameter(sNormalsTexParam);
|
---|
| 120 |
|
---|
| 121 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 122 |
|
---|
| 123 | glEnable(GL_LIGHTING);
|
---|
| 124 | glDisable(GL_TEXTURE_2D);
|
---|
| 125 |
|
---|
| 126 | glMatrixMode(GL_PROJECTION);
|
---|
| 127 | glPopMatrix();
|
---|
| 128 |
|
---|
| 129 | glMatrixMode(GL_MODELVIEW);
|
---|
| 130 | glPopMatrix();
|
---|
| 131 |
|
---|
| 132 | glPopAttrib();
|
---|
| 133 |
|
---|
| 134 | PrintGLerror("deferred shading");
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 |
|
---|
| 138 | } // namespace
|
---|