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

Revision 2877, 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 <IL/il.h>
8#include <assert.h>
9#include "FrameBufferObject.h"
10
11
12
13using namespace std;
14
15const static int texWidth = 2048;
16const static int texHeight = 2048;
17
18
19static GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT};
20
21
22static void PrintGLerror(char *msg)
23{
24        GLenum errCode;
25        const GLubyte *errStr;
26       
27        if ((errCode = glGetError()) != GL_NO_ERROR)
28        {
29                errStr = gluErrorString(errCode);
30                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
31        }
32}
33
34
35void startil()
36{
37        ilInit();
38        assert(ilGetError() == IL_NO_ERROR);
39}
40
41
42void stopil()
43{
44        ilShutDown();
45        assert(ilGetError() == IL_NO_ERROR);
46}
47
48
49namespace CHCDemoEngine
50{
51
52
53//void GrabDepthBuffer(float *data, RenderTexture *rt)
54void 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
66void 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, RenderTraverser *renderer):
95mSceneBox(sceneBox), mDepth(NULL)
96{
97        Prepare(renderer);
98}
99
100
101bool SceneQuery::CalcIntersection(Vector3 &pt)
102{
103        const int px = (pt.x - mSceneBox.Min(0)) * (texWidth - 1) / mSceneBox.Size(0);
104        const int py = (pt.y - mSceneBox.Min(1)) * (texHeight - 1) / mSceneBox.Size(1);
105
106        float d = mDepth[px + py * texHeight];
107
108        static float depth = d;
109
110        if (d > 0)
111        {
112                // temporal smoothing of depth values
113                const float x = .5f;
114                depth = d * x + depth * (1.0f - x);
115
116                const float offs = mSceneBox.Size(2) * 5e-2f;
117                pt.z =  mSceneBox.Max().z - mSceneBox.Size().z * depth + offs;
118
119                return true;
120        }
121
122        return false;
123}
124
125
126void SceneQuery::Prepare(RenderTraverser *renderer)
127{
128        cout << "Preparing scene queries" << endl;
129
130        const float xlen = mSceneBox.Size().x * 0.5f;
131        const float ylen = mSceneBox.Size().y * 0.5f;
132       
133        Camera *orthoCam = new Camera(xlen, ylen);
134        orthoCam->SetOrtho(true);
135
136        orthoCam->SetNear(0.0f);
137        orthoCam->Yaw(M_PI * 0.5f);
138
139        Vector3 pos = Vector3(mSceneBox.Center().x, mSceneBox.Center().y, mSceneBox.Max().z);
140        orthoCam->SetPosition(pos);
141
142        FrameBufferObject *fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_32, true);
143        fbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
144
145        PrintGLerror("here667");
146
147        fbo->Bind();
148
149        glDrawBuffers(1, mrt);
150       
151        glViewport(0, 0, texWidth, texHeight);
152        glPushAttrib(GL_VIEWPORT_BIT);
153
154        glClearColor(1, 1, 1, 1);
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
169        glOrtho(+xlen, -xlen, ylen, -ylen, 0.0f, mSceneBox.Size().z);
170
171        glMatrixMode(GL_MODELVIEW);
172        glPushMatrix();
173
174        orthoCam->SetupCameraView();
175
176        mDepth = new float[texHeight * texWidth];
177
178        renderer->RenderScene();
179       
180        glPopMatrix();
181        glMatrixMode(GL_PROJECTION);
182       
183        glPopMatrix();
184
185        glPopAttrib();
186       
187        FrameBufferObject::Release();
188
189        GrabDepthBuffer(mDepth, fbo->GetDepthTex());
190       
191        ExportDepthBuffer(mDepth);
192        PrintGLerror("grab");
193
194        DEL_PTR(fbo);
195        DEL_PTR(orthoCam);
196}
197
198
199
200
201}
Note: See TracBrowser for help on using the repository browser.