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

Revision 2897, 4.1 KB checked in by mattausch, 16 years ago (diff)

improved shadow mapping

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->Yaw(.5 * M_PI);
140        orthoCam->SetDirection(Vector3(0, 0, -1));
141
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        glDisable(GL_CULL_FACE);
159
160        glShadeModel(GL_FLAT);
161        glEnable(GL_DEPTH_TEST);
162
163        glMatrixMode(GL_MODELVIEW);
164        glPushMatrix();
165        glLoadIdentity();
166
167        glMatrixMode(GL_PROJECTION);
168        glPushMatrix();
169        glLoadIdentity();
170
171        glOrtho(+xlen, -xlen, ylen, -ylen, 0.0f, mSceneBox.Size().z);
172
173        glMatrixMode(GL_MODELVIEW);
174
175        orthoCam->SetupCameraView();
176
177        mDepth = new float[texHeight * texWidth];
178
179        Camera *oldCam = renderer->GetCamera();
180        renderer->SetCamera(orthoCam);
181
182        renderer->RenderScene();
183
184        renderer->SetCamera(oldCam);
185       
186
187        glEnable(GL_CULL_FACE);
188
189        glMatrixMode(GL_MODELVIEW);
190        glPopMatrix();
191       
192        glMatrixMode(GL_PROJECTION);
193        glPopMatrix();
194
195        glPopAttrib();
196       
197        FrameBufferObject::Release();
198
199        GrabDepthBuffer(mDepth, fbo->GetDepthTex());
200        //ExportDepthBuffer(mDepth); PrintGLerror("grab");
201
202        DEL_PTR(fbo);
203        DEL_PTR(orthoCam);
204}
205
206
207
208
209}
Note: See TracBrowser for help on using the repository browser.