source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp @ 2899

Revision 2899, 4.0 KB checked in by mattausch, 16 years ago (diff)
Line 
1#include "SceneQuery.h"
2#include "glInterface.h"
3#include "Vector3.h"
4#include "Camera.h"
5#include "SceneQuery.h"
6#include "RenderTraverser.h"
7#include "FrameBufferObject.h"
8
9#include <IL/il.h>
10#include <assert.h>
11
12
13
14
15using namespace std;
16
17const static int texWidth = 2048;
18const static int texHeight = 2048;
19
20
21
22
23static void PrintGLerror(char *msg)
24{
25        GLenum errCode;
26        const GLubyte *errStr;
27       
28        if ((errCode = glGetError()) != GL_NO_ERROR)
29        {
30                errStr = gluErrorString(errCode);
31                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
32        }
33}
34
35
36static void startil()
37{
38        ilInit();
39        assert(ilGetError() == IL_NO_ERROR);
40}
41
42
43static void stopil()
44{
45        ilShutDown();
46        assert(ilGetError() == IL_NO_ERROR);
47}
48
49
50namespace CHCDemoEngine
51{
52
53
54static void GrabDepthBuffer(float *data, GLuint depthTexture)
55{
56        glEnable(GL_TEXTURE_2D);
57        glBindTexture(GL_TEXTURE_2D, depthTexture);
58
59        glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT, data);
60
61        glBindTexture(GL_TEXTURE_2D, 0);
62        glDisable(GL_TEXTURE_2D);
63}
64
65
66static void ExportDepthBuffer(float *data)
67{
68        startil();
69
70        ILstring filename = ILstring("depth.tga");
71        ilRegisterType(IL_FLOAT);
72
73        const int depth = 1;
74        const int bpp = 1;
75
76        if (!ilTexImage(texWidth, texHeight, depth, bpp, IL_LUMINANCE, IL_FLOAT, data))
77        {
78                cerr << "IL error " << ilGetError() << endl;
79                stopil();
80                return;
81        }
82
83        if (!ilSaveImage(filename))
84        {
85                cerr << "TGA write error " << ilGetError() << endl;
86        }
87
88        stopil();
89
90        cout << "exported depth buffer" << endl;
91}
92
93
94SceneQuery::SceneQuery(const AxisAlignedBox3 &sceneBox,
95                                           RenderTraverser *renderer):
96mSceneBox(sceneBox), mDepth(NULL)
97{
98        Prepare(renderer);
99}
100
101
102bool SceneQuery::CalcIntersection(Vector3 &pt)
103{
104        const int px = (mSceneBox.Max(0) - pt.x) * (texWidth - 1) / mSceneBox.Size(0);
105        const int py = (mSceneBox.Max(1) - pt.y) * (texHeight - 1) / mSceneBox.Size(1);
106
107        float d = mDepth[px + py * texHeight];
108
109        static float depth = d;
110
111        if (d > 0)
112        {
113                // temporal smoothing of depth values
114                const float x = .5f;
115                depth = d * x + depth * (1.0f - x);
116
117                const float offs = mSceneBox.Size(2) * 5e-2f;
118                pt.z =  mSceneBox.Max().z - mSceneBox.Size().z * depth + offs;
119
120                return true;
121        }
122
123        return false;
124}
125
126
127void SceneQuery::Prepare(RenderTraverser *renderer)
128{
129        cout << "Preparing scene queries" << endl;
130
131        const float xlen = mSceneBox.Size().x * 0.5f;
132        const float ylen = mSceneBox.Size().y * 0.5f;
133       
134        Camera *orthoCam = new Camera(xlen, ylen);
135        orthoCam->SetOrtho(true);
136
137        orthoCam->SetNear(0.0f);
138        orthoCam->Yaw(.5 * M_PI);
139        orthoCam->SetDirection(Vector3(0, 0, -1));
140
141        Vector3 pos = Vector3(mSceneBox.Center().x, mSceneBox.Center().y, mSceneBox.Max().z);
142        orthoCam->SetPosition(pos);
143
144        FrameBufferObject *fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_32, true);
145        fbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
146
147
148        fbo->Bind();
149
150        glDrawBuffers(1, mrt);
151
152        glPushAttrib(GL_VIEWPORT_BIT);
153        glViewport(0, 0, texWidth, texHeight);
154
155        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
156
157        glDisable(GL_CULL_FACE);
158
159        glShadeModel(GL_FLAT);
160        glEnable(GL_DEPTH_TEST);
161
162        glMatrixMode(GL_MODELVIEW);
163        glPushMatrix();
164        glLoadIdentity();
165
166        glMatrixMode(GL_PROJECTION);
167        glPushMatrix();
168        glLoadIdentity();
169
170        glOrtho(+xlen, -xlen, ylen, -ylen, 0.0f, mSceneBox.Size().z);
171
172        glMatrixMode(GL_MODELVIEW);
173
174        orthoCam->SetupCameraView();
175
176        mDepth = new float[texHeight * texWidth];
177
178        Camera *oldCam = renderer->GetCamera();
179        renderer->SetCamera(orthoCam);
180
181        renderer->RenderScene();
182
183        renderer->SetCamera(oldCam);
184       
185
186        glEnable(GL_CULL_FACE);
187
188        glMatrixMode(GL_MODELVIEW);
189        glPopMatrix();
190       
191        glMatrixMode(GL_PROJECTION);
192        glPopMatrix();
193
194        glPopAttrib();
195       
196        FrameBufferObject::Release();
197
198        GrabDepthBuffer(mDepth, fbo->GetDepthTex());
199        //ExportDepthBuffer(mDepth); PrintGLerror("grab");
200
201        DEL_PTR(fbo);
202        DEL_PTR(orthoCam);
203}
204
205
206
207
208}
Note: See TracBrowser for help on using the repository browser.