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

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