1 | #include "glInterface.h"
|
---|
2 | #include "GlobalLinesRenderer.h"
|
---|
3 | #include "common.h"
|
---|
4 | #include "RenderTexture.h"
|
---|
5 | #include "Preprocessor.h"
|
---|
6 | #include "GlRenderer.h"
|
---|
7 |
|
---|
8 | #include <Cg/cg.h>
|
---|
9 | #include <Cg/cgGL.h>
|
---|
10 |
|
---|
11 | //#include <QtOpenGL>
|
---|
12 |
|
---|
13 | namespace GtpVisibilityPreprocessor {
|
---|
14 |
|
---|
15 |
|
---|
16 | static CGcontext sCgContext = NULL;
|
---|
17 | static CGprogram sCgDepthPeelingProgram = NULL;
|
---|
18 | static CGprogram sCgPassThroughProgram = NULL;
|
---|
19 |
|
---|
20 | static CGprofile sCgFragmentProfile;
|
---|
21 | static CGparameter sTextureParam;
|
---|
22 |
|
---|
23 | GlobalLinesRenderer *globalLinesRenderer = NULL;
|
---|
24 |
|
---|
25 | static int texWidth = 256;
|
---|
26 | static int texHeight = 256;
|
---|
27 |
|
---|
28 | static void cgErrorCallback()
|
---|
29 | {
|
---|
30 | CGerror lastError = cgGetError();
|
---|
31 |
|
---|
32 | if(lastError)
|
---|
33 | {
|
---|
34 | printf("%s\n\n", cgGetErrorString(lastError));
|
---|
35 | printf("%s\n", cgGetLastListing(sCgContext));
|
---|
36 | printf("Cg error, exiting...\n");
|
---|
37 |
|
---|
38 | exit(0);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | static void PrintGLerror(char *msg)
|
---|
43 | {
|
---|
44 | GLenum errCode;
|
---|
45 | const GLubyte *errStr;
|
---|
46 |
|
---|
47 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
48 | {
|
---|
49 | errStr = gluErrorString(errCode);
|
---|
50 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | void Reshape(int w, int h)
|
---|
56 | {
|
---|
57 | if (h == 0) h = 1;
|
---|
58 |
|
---|
59 | glViewport(0, 0, w, h);
|
---|
60 |
|
---|
61 | glMatrixMode(GL_PROJECTION);
|
---|
62 | glLoadIdentity();
|
---|
63 |
|
---|
64 | //gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 3, 5000.0);
|
---|
65 | //gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 0.5, 10.0);
|
---|
66 | glOrtho(-1000, 1000, -1000, 1000, 1, 1000);
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | void Display()
|
---|
71 | {
|
---|
72 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
73 | glMatrixMode(GL_MODELVIEW);
|
---|
74 | glPushMatrix();
|
---|
75 |
|
---|
76 | //globalLinesRenderer->DrawGeometry();
|
---|
77 |
|
---|
78 | globalLinesRenderer->CastGlobalLines(Beam(), 0);
|
---|
79 |
|
---|
80 | PrintGLerror("display");
|
---|
81 |
|
---|
82 | glPopMatrix();
|
---|
83 | glutSwapBuffers();
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | void Idle()
|
---|
88 | {
|
---|
89 | glutPostRedisplay();
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | void Keyboard(unsigned char key, int x, int y)
|
---|
94 | {
|
---|
95 | switch(key)
|
---|
96 | {
|
---|
97 | case '2':
|
---|
98 | ++ globalLinesRenderer->mMaxDepth;
|
---|
99 | cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
|
---|
100 | return;
|
---|
101 | case '1':
|
---|
102 | -- globalLinesRenderer->mMaxDepth;
|
---|
103 | cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
|
---|
104 | return;
|
---|
105 | default:
|
---|
106 | return;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /*GlobalLinesRenderer::GlobalLinesRenderer(RenderTexture *buffer1,
|
---|
111 | RenderTexture *buffer2,
|
---|
112 | Preprocessor *preprocessor,
|
---|
113 | GlRenderer *renderer)
|
---|
114 | : mNewBuffer(buffer1), mOldBuffer(buffer2), mPreprocessor(preprocessor), mMaxDepth(100),
|
---|
115 | mRenderer(renderer)
|
---|
116 | {
|
---|
117 | }
|
---|
118 | */
|
---|
119 | GlobalLinesRenderer::GlobalLinesRenderer(Preprocessor *preprocessor,
|
---|
120 | GlRenderer *renderer):
|
---|
121 | mNewBuffer(NULL),
|
---|
122 | mOldBuffer(NULL),
|
---|
123 | mMaxDepth(0),
|
---|
124 | mRenderer(renderer),
|
---|
125 | mPreprocessor(preprocessor)
|
---|
126 | {
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | GlobalLinesRenderer::~GlobalLinesRenderer()
|
---|
131 | {
|
---|
132 | if (sCgDepthPeelingProgram)
|
---|
133 | cgDestroyProgram(sCgDepthPeelingProgram);
|
---|
134 | if (sCgContext)
|
---|
135 | cgDestroyContext(sCgContext);
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | void GlobalLinesRenderer::CastGlobalLines(Beam &beam, const int samples)
|
---|
140 | {
|
---|
141 | // bind pixel shader implementing the front depth buffer functionality
|
---|
142 | ApplyDepthPeeling(beam, samples);
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | void GlobalLinesRenderer::RenderObject(Intersectable *obj)
|
---|
147 | {
|
---|
148 | mRenderer->RenderIntersectable(obj);
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | void GlobalLinesRenderer::DrawGeometry()
|
---|
153 | {
|
---|
154 | //mRenderer->mUseFalseColors = true;
|
---|
155 | ObjectContainer::const_iterator oit, oit_end = mPreprocessor->mObjects.end();
|
---|
156 |
|
---|
157 | Intersectable::NewMail();
|
---|
158 |
|
---|
159 | for (oit = mPreprocessor->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
160 | {
|
---|
161 | RenderObject(*oit);
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | void GlobalLinesRenderer::SwitchRenderTextures()
|
---|
167 | {
|
---|
168 | RenderTexture *buffer = mOldBuffer;
|
---|
169 | mOldBuffer = mNewBuffer;
|
---|
170 | mNewBuffer = buffer;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | void GlobalLinesRenderer::InitGl()
|
---|
175 | {
|
---|
176 | glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
|
---|
177 | glutInitWindowPosition(50, 50);
|
---|
178 | glutInitWindowSize(512, 512);
|
---|
179 | glutCreateWindow("TestRenderDepthTexture");
|
---|
180 |
|
---|
181 | int err = glewInit();
|
---|
182 | if (GLEW_OK != err)
|
---|
183 | {
|
---|
184 | // problem: glewInit failed, something is seriously wrong
|
---|
185 | fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
|
---|
186 | exit(-1);
|
---|
187 | }
|
---|
188 |
|
---|
189 | glutKeyboardFunc(Keyboard);
|
---|
190 | glutDisplayFunc(Display);
|
---|
191 | glutIdleFunc(Idle);
|
---|
192 | glutReshapeFunc(Reshape);
|
---|
193 |
|
---|
194 | Reshape(512, 512);
|
---|
195 | glMatrixMode(GL_MODELVIEW);
|
---|
196 | glLoadIdentity();
|
---|
197 |
|
---|
198 | AxisAlignedBox3 bbox = globalLinesRenderer->mPreprocessor->mKdTree->GetBox();
|
---|
199 | const Vector3 midPoint = bbox.Center();
|
---|
200 | //const Vector3 viewPoint = midPoint + Vector3(-800, 0, 0);
|
---|
201 | const Vector3 viewPoint = midPoint + Vector3(2, 0, 0);
|
---|
202 |
|
---|
203 | cout << "midPoint: " << midPoint << endl;
|
---|
204 | cout << "viewPoint: " << viewPoint << endl;
|
---|
205 | cout << "scene: " << bbox << endl;
|
---|
206 |
|
---|
207 | /*gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
|
---|
208 | midPoint.x, midPoint.y, midPoint.z,
|
---|
209 | 0, 1, 0);
|
---|
210 | */
|
---|
211 | gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
|
---|
212 |
|
---|
213 | //glDisable(GL_CULL_FACE);
|
---|
214 | glEnable(GL_CULL_FACE);
|
---|
215 | glDisable(GL_LIGHTING);
|
---|
216 | //glEnable(GL_COLOR_MATERIAL);
|
---|
217 | glDisable(GL_COLOR_MATERIAL);
|
---|
218 | glEnable(GL_DEPTH_TEST);
|
---|
219 | glClearColor(0.1, 0.2, 0.3, 1);
|
---|
220 |
|
---|
221 | // A square, mipmapped, anisotropically filtered 8-bit RGBA texture with
|
---|
222 | // depth and stencil.
|
---|
223 | // Note that RT_COPY_TO_TEXTURE is required for depth textures on ATI hardware
|
---|
224 |
|
---|
225 | mNewBuffer = new RenderTexture(texWidth, texHeight, true, true);
|
---|
226 | //mNewBuffer->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
227 | mNewBuffer->Initialize(true, true, false, true, true, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
228 |
|
---|
229 | mOldBuffer = new RenderTexture(texWidth, texHeight, true, true);
|
---|
230 | //mOldBuffer ->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
231 | mOldBuffer ->Initialize(true, true, false, true, true, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
232 |
|
---|
233 | // Setup Cg
|
---|
234 | cgSetErrorCallback(cgErrorCallback);
|
---|
235 |
|
---|
236 | // Create cgContext.
|
---|
237 | sCgContext = cgCreateContext();
|
---|
238 |
|
---|
239 | // get the best profile for this hardware
|
---|
240 | sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
|
---|
241 |
|
---|
242 | //assert(sCgFragmentProfile != CG_PROFILE_UNKNOWN);
|
---|
243 | cgGLSetOptimalOptions(sCgFragmentProfile);
|
---|
244 |
|
---|
245 | sCgDepthPeelingProgram =
|
---|
246 | cgCreateProgramFromFile(sCgContext,
|
---|
247 | CG_SOURCE,
|
---|
248 | "../src/depth_peeling.cg",
|
---|
249 | GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
|
---|
250 | NULL,
|
---|
251 | NULL);
|
---|
252 |
|
---|
253 | if(sCgDepthPeelingProgram != NULL)
|
---|
254 | {
|
---|
255 | cgGLLoadProgram(sCgDepthPeelingProgram);
|
---|
256 | sTextureParam = cgGetNamedParameter(sCgDepthPeelingProgram, "depthTex");
|
---|
257 | }
|
---|
258 |
|
---|
259 | sCgPassThroughProgram =
|
---|
260 | cgCreateProgramFromFile(sCgContext,
|
---|
261 | CG_SOURCE,
|
---|
262 | "../src/passthrough.cg",
|
---|
263 | GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
|
---|
264 | NULL,
|
---|
265 | NULL);
|
---|
266 |
|
---|
267 | if(sCgPassThroughProgram != NULL)
|
---|
268 | {
|
---|
269 | cgGLLoadProgram(sCgPassThroughProgram);
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | // setup the rendering context for the RenderTexture
|
---|
274 | mNewBuffer->BeginCapture();
|
---|
275 | {
|
---|
276 | Reshape(texWidth, texHeight);
|
---|
277 | glClearColor(0.1, 0.7, 0.2, 1);
|
---|
278 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
279 |
|
---|
280 | glFrontFace(GL_CCW);
|
---|
281 | glCullFace(GL_BACK);
|
---|
282 | glEnable(GL_CULL_FACE);
|
---|
283 | glShadeModel(GL_FLAT);
|
---|
284 | glEnable(GL_DEPTH_TEST);
|
---|
285 |
|
---|
286 | glMatrixMode(GL_MODELVIEW);
|
---|
287 | glLoadIdentity();
|
---|
288 | gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
|
---|
289 | midPoint.x, midPoint.y, midPoint.z,
|
---|
290 | 0, 1, 0);
|
---|
291 |
|
---|
292 | }
|
---|
293 | mNewBuffer->EndCapture();
|
---|
294 |
|
---|
295 | // setup the rendering context for the RenderTexture
|
---|
296 | mOldBuffer->BeginCapture();
|
---|
297 | {
|
---|
298 | Reshape(texWidth, texHeight);
|
---|
299 | glMatrixMode(GL_MODELVIEW);
|
---|
300 | glLoadIdentity();
|
---|
301 |
|
---|
302 | glFrontFace(GL_CCW);
|
---|
303 | glCullFace(GL_BACK);
|
---|
304 | glEnable(GL_CULL_FACE);
|
---|
305 | glShadeModel(GL_FLAT);
|
---|
306 | glEnable(GL_DEPTH_TEST);
|
---|
307 |
|
---|
308 | gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
|
---|
309 | midPoint.x, midPoint.y, midPoint.z,
|
---|
310 | 0, 1, 0);
|
---|
311 |
|
---|
312 | glClearColor(0.6, 0.2, 0.2, 1);
|
---|
313 | }
|
---|
314 | mOldBuffer->EndCapture();
|
---|
315 |
|
---|
316 | PrintGLerror("init");
|
---|
317 | }
|
---|
318 |
|
---|
319 |
|
---|
320 | void GlobalLinesRenderer::ProcessDepthBuffer()
|
---|
321 | {
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | void GlobalLinesRenderer::Run()
|
---|
326 | {
|
---|
327 | glutMainLoop();
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 | void GlobalLinesRenderer::ApplyDepthPeeling(Beam &beam, const int samples)
|
---|
332 | {
|
---|
333 | //if (mNewBuffer->IsDoubleBuffered())
|
---|
334 | // glDrawBuffer(GL_BACK);
|
---|
335 |
|
---|
336 | mNewBuffer->BeginCapture();
|
---|
337 | {
|
---|
338 | //cgGLBindProgram(sCgPassThroughProgram);
|
---|
339 | //cgGLEnableProfile(sCgFragmentProfile);
|
---|
340 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
341 | glMatrixMode(GL_MODELVIEW);
|
---|
342 | glPushMatrix();
|
---|
343 | {
|
---|
344 | //glutSolidTorus(0.25, 1, 32, 64);
|
---|
345 | DrawGeometry();
|
---|
346 | }
|
---|
347 | glPopMatrix();
|
---|
348 | }
|
---|
349 | mNewBuffer->EndCapture();
|
---|
350 |
|
---|
351 | //mNewBuffer->BindDepth();
|
---|
352 | float *data = new float[texWidth * texHeight];
|
---|
353 | //const int texFormat = WGL_TEXTURE_DEPTH_COMPONENT_NV;
|
---|
354 | const int texFormat = GL_LUMINANCE;
|
---|
355 | glGetTexImage(mNewBuffer->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
|
---|
356 |
|
---|
357 | delete [] data;
|
---|
358 |
|
---|
359 | PrintGLerror("get data");
|
---|
360 |
|
---|
361 | if (mNewBuffer->IsRectangleTexture()) cout << "rect" << endl;
|
---|
362 | //else cout << "haga"<<endl;
|
---|
363 |
|
---|
364 | for(int i = 0; i < mMaxDepth; ++ i)
|
---|
365 | {
|
---|
366 | // Peel another layer
|
---|
367 | SwitchRenderTextures(); // switch pointer between rendertextures
|
---|
368 |
|
---|
369 | mNewBuffer->BeginCapture();
|
---|
370 | {
|
---|
371 | //if (mNewBuffer->IsDoubleBuffered())
|
---|
372 | // glDrawBuffer(GL_BACK);
|
---|
373 |
|
---|
374 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
375 |
|
---|
376 | cgGLBindProgram(sCgDepthPeelingProgram);
|
---|
377 | // cgGLBindProgram(sCgPassThroughProgram);
|
---|
378 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
379 | cgGLSetTextureParameter(sTextureParam, mOldBuffer->GetDepthTextureID());
|
---|
380 | cgGLEnableTextureParameter(sTextureParam);
|
---|
381 |
|
---|
382 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
383 | glMatrixMode(GL_MODELVIEW);
|
---|
384 | glPushMatrix();
|
---|
385 | {
|
---|
386 | //glutSolidTorus(0.25, 1, 32, 64);
|
---|
387 | DrawGeometry();
|
---|
388 | }
|
---|
389 | glPopMatrix();
|
---|
390 |
|
---|
391 | cgGLDisableTextureParameter(sTextureParam);
|
---|
392 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
393 | }
|
---|
394 | mNewBuffer->EndCapture();
|
---|
395 | }
|
---|
396 |
|
---|
397 | PrintGLerror("display 1");
|
---|
398 |
|
---|
399 | //mNewBuffer->Bind();
|
---|
400 | mNewBuffer->BindDepth();
|
---|
401 | mNewBuffer->EnableTextureTarget();
|
---|
402 |
|
---|
403 |
|
---|
404 | PrintGLerror("display 2");
|
---|
405 | glColor3f(1,1,1);
|
---|
406 | glBegin(GL_QUADS);
|
---|
407 | glTexCoord2f(0, 0); glVertex3f(-1000, -1000, -0.5f);
|
---|
408 | glTexCoord2f(1, 0); glVertex3f( 1000, -1000, -0.5f);
|
---|
409 | glTexCoord2f(1, 1); glVertex3f( 1000, 1000, -0.5f);
|
---|
410 | glTexCoord2f(0, 1); glVertex3f(-1000, 1000, -0.5f);
|
---|
411 | glEnd();
|
---|
412 |
|
---|
413 | if (mNewBuffer->IsFloatTexture())
|
---|
414 | {
|
---|
415 | // cgGLDisableTextureParameter(textureParam);
|
---|
416 | // cgGLDisableProfile(theProfile);
|
---|
417 | }
|
---|
418 | else
|
---|
419 | {
|
---|
420 | mNewBuffer->DisableTextureTarget();
|
---|
421 | }
|
---|
422 | }
|
---|
423 |
|
---|
424 |
|
---|
425 | }
|
---|