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

Revision 2892, 3.9 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
21static GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT};
22
23
24static void PrintGLerror(char *msg)
25{
26        GLenum errCode;
27        const GLubyte *errStr;
28       
29        if ((errCode = glGetError()) != GL_NO_ERROR)
30        {
31                errStr = gluErrorString(errCode);
32                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
33        }
34}
35
36
37static void startil()
38{
39        ilInit();
40        assert(ilGetError() == IL_NO_ERROR);
41}
42
43
44static void stopil()
45{
46        ilShutDown();
47        assert(ilGetError() == IL_NO_ERROR);
48}
49
50
51namespace CHCDemoEngine
52{
53
54
55static void GrabDepthBuffer(float *data, GLuint depthTexture)
56{
57        glEnable(GL_TEXTURE_2D);
58        glBindTexture(GL_TEXTURE_2D, depthTexture);
59
60        glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT, data);
61
62        glBindTexture(GL_TEXTURE_2D, 0);
63        glDisable(GL_TEXTURE_2D);
64}
65
66
67static void ExportDepthBuffer(float *data)
68{
69        startil();
70
71        ILstring filename = ILstring("depth.tga");
72        ilRegisterType(IL_FLOAT);
73
74        const int depth = 1;
75        const int bpp = 1;
76
77        if (!ilTexImage(texWidth, texHeight, depth, bpp, IL_LUMINANCE, IL_FLOAT, data))
78        {
79                cerr << "IL error " << ilGetError() << endl;
80                stopil();
81                return;
82        }
83
84        if (!ilSaveImage(filename))
85        {
86                cerr << "TGA write error " << ilGetError() << endl;
87        }
88
89        stopil();
90
91        cout << "exported depth buffer" << endl;
92}
93
94
95SceneQuery::SceneQuery(const AxisAlignedBox3 &sceneBox,
96                                           RenderTraverser *renderer):
97mSceneBox(sceneBox), mDepth(NULL)
98{
99        Prepare(renderer);
100}
101
102
103bool SceneQuery::CalcIntersection(Vector3 &pt)
104{
105        const int px = (mSceneBox.Max(0) - pt.x) * (texWidth - 1) / mSceneBox.Size(0);
106        const int py = (mSceneBox.Max(1) - pt.y) * (texHeight - 1) / mSceneBox.Size(1);
107
108        float d = mDepth[px + py * texHeight];
109
110        static float depth = d;
111
112        if (d > 0)
113        {
114                // temporal smoothing of depth values
115                const float x = .5f;
116                depth = d * x + depth * (1.0f - x);
117
118                const float offs = mSceneBox.Size(2) * 5e-2f;
119                pt.z =  mSceneBox.Max().z - mSceneBox.Size().z * depth + offs;
120
121                return true;
122        }
123
124        return false;
125}
126
127
128void SceneQuery::Prepare(RenderTraverser *renderer)
129{
130        cout << "Preparing scene queries" << endl;
131
132        const float xlen = mSceneBox.Size().x * 0.5f;
133        const float ylen = mSceneBox.Size().y * 0.5f;
134       
135        Camera *orthoCam = new Camera(xlen, ylen);
136        orthoCam->SetOrtho(true);
137
138        orthoCam->SetNear(0.0f);
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        glFrontFace(GL_CCW);
158        glCullFace(GL_BACK);
159       
160        glEnable(GL_CULL_FACE);
161
162        glShadeModel(GL_FLAT);
163        glEnable(GL_DEPTH_TEST);
164
165        glMatrixMode(GL_PROJECTION);
166        glPushMatrix();
167
168        glOrtho(+xlen, -xlen, ylen, -ylen, 0.0f, mSceneBox.Size().z);
169
170        glMatrixMode(GL_MODELVIEW);
171        glPushMatrix();
172
173        orthoCam->SetupCameraView();
174
175        mDepth = new float[texHeight * texWidth];
176
177        renderer->RenderScene();
178       
179        glPopMatrix();
180        glMatrixMode(GL_PROJECTION);
181       
182        glPopMatrix();
183
184        glPopAttrib();
185       
186        FrameBufferObject::Release();
187
188        GrabDepthBuffer(mDepth, fbo->GetDepthTex());
189        //ExportDepthBuffer(mDepth); PrintGLerror("grab");
190
191        DEL_PTR(fbo);
192        DEL_PTR(orthoCam);
193}
194
195
196
197
198}
Note: See TracBrowser for help on using the repository browser.