1 | #include "DeferredShader.h"
|
---|
2 | #include "FrameBufferObject.h"
|
---|
3 | #include "RenderState.h"
|
---|
4 |
|
---|
5 |
|
---|
6 | using namespace std;
|
---|
7 |
|
---|
8 |
|
---|
9 | namespace CHCDemoEngine
|
---|
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 | DeferredShader::~DeferredShader()
|
---|
38 | {
|
---|
39 | if (sCgDeferredProgram)
|
---|
40 | cgDestroyProgram(sCgDeferredProgram);
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | void DeferredShader::Init(CGcontext context)
|
---|
45 | {
|
---|
46 |
|
---|
47 | sCgDeferredProgram =
|
---|
48 | cgCreateProgramFromFile(context,
|
---|
49 | CG_SOURCE,
|
---|
50 | "src/shaders/deferred.cg",
|
---|
51 | RenderState::sCgFragmentProfile,
|
---|
52 | "main",
|
---|
53 | NULL);
|
---|
54 |
|
---|
55 | if (sCgDeferredProgram != NULL)
|
---|
56 | {
|
---|
57 | cgGLLoadProgram(sCgDeferredProgram);
|
---|
58 |
|
---|
59 | // we need size of texture for scaling
|
---|
60 | sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions");
|
---|
61 | sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors");
|
---|
62 | sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
|
---|
63 | }
|
---|
64 | else
|
---|
65 | cerr << "deferred program failed to load" << endl;
|
---|
66 |
|
---|
67 |
|
---|
68 | PrintGLerror("init");
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | void DeferredShader::Render(FrameBufferObject *fbo)
|
---|
73 | {
|
---|
74 | FrameBufferObject::Release();
|
---|
75 |
|
---|
76 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
77 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
78 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
79 |
|
---|
80 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
81 | glViewport(0, 0, mWidth, mHeight);
|
---|
82 |
|
---|
83 | glDisable(GL_ALPHA_TEST);
|
---|
84 | glDisable(GL_TEXTURE_2D);
|
---|
85 | glDisable(GL_LIGHTING);
|
---|
86 |
|
---|
87 | glMatrixMode(GL_PROJECTION);
|
---|
88 | glPushMatrix();
|
---|
89 | glLoadIdentity();
|
---|
90 |
|
---|
91 | glMatrixMode(GL_MODELVIEW);
|
---|
92 | glPushMatrix();
|
---|
93 | glLoadIdentity();
|
---|
94 |
|
---|
95 | const float offs = 0.5f;
|
---|
96 |
|
---|
97 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
98 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
99 |
|
---|
100 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
101 |
|
---|
102 | cgGLBindProgram(sCgDeferredProgram);
|
---|
103 |
|
---|
104 | cgGLSetTextureParameter(sColorsTexParam, colorsTex);
|
---|
105 | cgGLEnableTextureParameter(sColorsTexParam);
|
---|
106 |
|
---|
107 | cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
|
---|
108 | cgGLEnableTextureParameter(sPositionsTexParam);
|
---|
109 |
|
---|
110 | cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
|
---|
111 | cgGLEnableTextureParameter(sNormalsTexParam);
|
---|
112 |
|
---|
113 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
114 |
|
---|
115 | glBegin(GL_QUADS);
|
---|
116 |
|
---|
117 | float offs2 = 0.5f;
|
---|
118 |
|
---|
119 | glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
120 | glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
121 | glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
122 | glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
123 |
|
---|
124 | glEnd();
|
---|
125 |
|
---|
126 | cgGLDisableTextureParameter(sColorsTexParam);
|
---|
127 | cgGLDisableTextureParameter(sPositionsTexParam);
|
---|
128 | cgGLDisableTextureParameter(sNormalsTexParam);
|
---|
129 |
|
---|
130 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
131 |
|
---|
132 | glEnable(GL_LIGHTING);
|
---|
133 | glDisable(GL_TEXTURE_2D);
|
---|
134 |
|
---|
135 | glMatrixMode(GL_PROJECTION);
|
---|
136 | glPopMatrix();
|
---|
137 |
|
---|
138 | glMatrixMode(GL_MODELVIEW);
|
---|
139 | glPopMatrix();
|
---|
140 |
|
---|
141 | glPopAttrib();
|
---|
142 |
|
---|
143 | PrintGLerror("deferred shading");
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | } // namespace
|
---|