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

Revision 2887, 3.9 KB checked in by mattausch, 16 years ago (diff)

made changes to view transformation but still not working

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 = (pt.x - mSceneBox.Min(0)) * (texWidth - 1) / mSceneBox.Size(0);
106        const int py = (pt.y - mSceneBox.Min(1)) * (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        cout << orthoCam->GetDirection() << endl;
142        Vector3 pos = Vector3(mSceneBox.Center().x, mSceneBox.Center().y, mSceneBox.Max().z);
143        orthoCam->SetPosition(pos);
144
145        FrameBufferObject *fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_32, true);
146        fbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
147
148
149        fbo->Bind();
150
151        glDrawBuffers(1, mrt);
152
153        glPushAttrib(GL_VIEWPORT_BIT);
154        glViewport(0, 0, texWidth, texHeight);
155
156        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
157
158        glFrontFace(GL_CCW);
159        glCullFace(GL_BACK);
160       
161        glEnable(GL_CULL_FACE);
162
163        glShadeModel(GL_FLAT);
164        glEnable(GL_DEPTH_TEST);
165
166        glMatrixMode(GL_PROJECTION);
167        glPushMatrix();
168
169
170        glOrtho(+xlen, -xlen, ylen, -ylen, 0.0f, mSceneBox.Size().z);
171
172        glMatrixMode(GL_MODELVIEW);
173        glPushMatrix();
174
175        orthoCam->SetupCameraView();
176
177        mDepth = new float[texHeight * texWidth];
178
179        renderer->RenderScene();
180       
181        glPopMatrix();
182        glMatrixMode(GL_PROJECTION);
183       
184        glPopMatrix();
185
186        glPopAttrib();
187       
188        FrameBufferObject::Release();
189
190        GrabDepthBuffer(mDepth, fbo->GetDepthTex());
191        ExportDepthBuffer(mDepth); PrintGLerror("grab");
192
193        DEL_PTR(fbo);
194        DEL_PTR(orthoCam);
195}
196
197
198
199
200}
Note: See TracBrowser for help on using the repository browser.