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

Revision 3103, 4.7 KB checked in by mattausch, 16 years ago (diff)

still some error with ssao on edges
bilateral filter slow

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
93        if (!ilSaveImage(filename))
94        {
95                cerr << "TGA write error " << ilGetError() << endl;
96        }
97
98        stopil();
99
100        cout << "exported depth buffer" << endl;
101}
102
103
[2887]104SceneQuery::SceneQuery(const AxisAlignedBox3 &sceneBox,
[3101]105                                           RenderTraverser *renderer, RenderState *state):
106mSceneBox(sceneBox), mDepth(NULL), mRenderState(state)
[2764]107{
[2778]108        Prepare(renderer);
[2764]109}
110
111
112bool SceneQuery::CalcIntersection(Vector3 &pt)
113{
[2888]114        const int px = (mSceneBox.Max(0) - pt.x) * (texWidth - 1) / mSceneBox.Size(0);
115        const int py = (mSceneBox.Max(1) - pt.y) * (texHeight - 1) / mSceneBox.Size(1);
[2764]116
[3101]117        const float d = mDepth[px + py * texHeight];
[2764]118
[2808]119        static float depth = d;
[2800]120
[3101]121        if (d > .0f)
[2764]122        {
[2808]123                // temporal smoothing of depth values
[2848]124                const float x = .5f;
[2808]125                depth = d * x + depth * (1.0f - x);
[2764]126
[2808]127                const float offs = mSceneBox.Size(2) * 5e-2f;
128                pt.z =  mSceneBox.Max().z - mSceneBox.Size().z * depth + offs;
129
[2764]130                return true;
131        }
132
133        return false;
134}
135
[2778]136
137void SceneQuery::Prepare(RenderTraverser *renderer)
[2764]138{
139        cout << "Preparing scene queries" << endl;
140
[3101]141        const float xlen = mSceneBox.Size().x * .5f;
142        const float ylen = mSceneBox.Size().y * .5f;
[2796]143       
144        Vector3 pos = Vector3(mSceneBox.Center().x, mSceneBox.Center().y, mSceneBox.Max().z);
[2764]145
[2877]146        FrameBufferObject *fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_32, true);
[3001]147        fbo->AddColorBuffer(ColorBufferObject::RGBA_UBYTE,
[2965]148                                ColorBufferObject::WRAP_CLAMP_TO_EDGE,
149                                                ColorBufferObject::FILTER_NEAREST,
150                                                ColorBufferObject::FILTER_NEAREST);
[2809]151
152
[2877]153        fbo->Bind();
[2809]154
[2877]155        glDrawBuffers(1, mrt);
[2887]156
157        glPushAttrib(GL_VIEWPORT_BIT);
[2877]158        glViewport(0, 0, texWidth, texHeight);
[2764]159
[2877]160        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[2764]161
[2897]162        glMatrixMode(GL_MODELVIEW);
163        glPushMatrix();
[3103]164        glLoadIdentity();
[2897]165
[2877]166        glMatrixMode(GL_PROJECTION);
167        glPushMatrix();
[3103]168        glLoadIdentity();
[2808]169
[3102]170        OrthoCamera *orthoCam = new OrthoCamera(xlen, -xlen, ylen, -ylen, .0f, mSceneBox.Size().z);
171       
172        orthoCam->SetDirection(Vector3(0, 0, -1));
173        orthoCam->SetPosition(pos);
[2764]174
[3102]175        orthoCam->SetupViewProjection();
[2764]176
[3102]177        //glOrtho(xlen, -xlen, ylen, -ylen, 0.0f, mSceneBox.Size().z);
[3103]178        glMatrixMode(GL_MODELVIEW);
[3102]179        //orthoCam->SetupCameraView();
[2778]180
[2877]181        mDepth = new float[texHeight * texWidth];
[2809]182
[2897]183        Camera *oldCam = renderer->GetCamera();
184        renderer->SetCamera(orthoCam);
185
[2953]186        glEnableClientState(GL_VERTEX_ARRAY);
187        glEnableClientState(GL_NORMAL_ARRAY);
188
[3102]189        // hack: use highest lod level for trees (billboards)
190        LODLevel::InitFrame(Vector3(1e6f));
191
[3101]192        // forward rendering
193        mRenderState->SetRenderTechnique(0);
[3102]194        // temporarilly switch off
195        renderer->SetRenderDynamicObjects(false);
[3101]196
[2877]197        renderer->RenderScene();
[2897]198
[2953]199        glDisableClientState(GL_VERTEX_ARRAY);
200        glDisableClientState(GL_NORMAL_ARRAY);
201
[2897]202        renderer->SetCamera(oldCam);
[3102]203        // temporarilly switch off
204        renderer->SetRenderDynamicObjects(true);
[2897]205
206        glMatrixMode(GL_MODELVIEW);
[2877]207        glPopMatrix();
[2897]208       
[2877]209        glMatrixMode(GL_PROJECTION);
210        glPopMatrix();
[2834]211
[2877]212        glPopAttrib();
213       
214        FrameBufferObject::Release();
[2834]215
[2877]216        GrabDepthBuffer(mDepth, fbo->GetDepthTex());
[3101]217        ExportDepthBuffer(mDepth); PrintGLerror("grab depth");
[2796]218
[2877]219        DEL_PTR(fbo);
[2796]220        DEL_PTR(orthoCam);
[2764]221}
222
[2808]223
224
225
[2764]226}
Note: See TracBrowser for help on using the repository browser.