eepgenerator.cpp 11.2 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
5
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
8
9
//      Chair for Computer Aided Medical Procedures
//      Technische Universitt Mnchen
//      Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
schultezub's avatar
schultezub committed
10
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
// 
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// ================================================================================================

30
31
32
33
34
35
36
#include "eepgenerator.h"

#include "tgt/glmath.h"
#include "tgt/logmanager.h"
#include "tgt/shadermanager.h"
#include "tgt/textureunit.h"

37
38
#include "core/datastructures/imagedata.h"
#include "core/datastructures/imagerepresentationgl.h"
39
#include "core/datastructures/renderdata.h"
40
#include "core/datastructures/renderdata.h"
41
#include "core/datastructures/meshgeometry.h"
42
#include "core/pipeline/processordecoratormasking.h"
43

schultezub's avatar
schultezub committed
44
45
namespace campvis {
    const std::string EEPGenerator::loggerCat_ = "CAMPVis.modules.vis.EEPGenerator";
46

47
48
    EEPGenerator::EEPGenerator(IVec2Property* viewportSizeProp)
        : VisualizationProcessor(viewportSizeProp)
49
        , p_sourceImageID("sourceImageID", "Input Image", "", DataNameProperty::READ)
50
        , p_geometryID("geometryID", "Input Proxy Geometry ID", "proxygeometry", DataNameProperty::READ)
51
        , p_geometryImageId("GeometryImageId", "Rendered Geometry to Integrate (optional)", "", DataNameProperty::READ)
52
53
54
        , p_entryImageID("entryImageID", "Output Entry Points Image", "eep.entry", DataNameProperty::WRITE)
        , p_exitImageID("exitImageID", "Output Exit Points Image", "eep.exit", DataNameProperty::WRITE)
        , p_camera("camera", "Camera")
55
56
        , p_enableMirror("enableMirror", "Enable Virtual Mirror Feature", false, AbstractProcessor::INVALID_RESULT | AbstractProcessor::INVALID_PROPERTIES)
        , p_mirrorID("mirrorID", "Input Mirror ID", "", DataNameProperty::READ)
57
58
        , _shader(0)
    {
59
60
        addDecorator(new ProcessorDecoratorMasking());

61
62
        addProperty(&p_sourceImageID);
        addProperty(&p_geometryID);
63
        addProperty(&p_geometryImageId);
64
65
66
        addProperty(&p_entryImageID);
        addProperty(&p_exitImageID);
        addProperty(&p_camera);
67

68
        addProperty(&p_enableMirror);
69
70
        addProperty(&p_mirrorID);
        p_mirrorID.setVisible(false);
71
72

        decoratePropertyCollection(this);
73
74
75
76
77
78
79
80
    }

    EEPGenerator::~EEPGenerator() {

    }

    void EEPGenerator::init() {
        VisualizationProcessor::init();
81
        _shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", "modules/vis/glsl/eepgenerator.frag", generateHeader(), false);
82
83
84
85
        if (_shader != 0) {
            _shader->setAttributeLocation(0, "in_Position");
            _shader->setAttributeLocation(1, "in_TexCoord");
        }
86
87
88
89
    }

    void EEPGenerator::deinit() {
        ShdrMgr.dispose(_shader);
schultezub's avatar
schultezub committed
90
        _shader = 0;
91
92
93
94
        VisualizationProcessor::deinit();
    }

    void EEPGenerator::process(DataContainer& data) {
95
        ImageRepresentationGL::ScopedRepresentation img(data, p_sourceImageID.getValue());
96
        ScopedTypedData<MeshGeometry> proxyGeometry(data, p_geometryID.getValue());
97

98
        if (img != 0 && proxyGeometry != 0 && _shader != 0) {
99
            if (img->getDimensionality() == 3) {
100
                if (hasInvalidShader()) {
schultezub's avatar
schultezub committed
101
102
                    _shader->setHeaders(generateHeader());
                    _shader->rebuild();
103
                    validate(INVALID_SHADER);
schultezub's avatar
schultezub committed
104
105
                }

106
                ScopedTypedData<RenderData> geometryImage(data, p_geometryImageId.getValue());
107

108
                tgt::Bounds volumeExtent = img->getParent()->getWorldBounds();
109
                tgt::Bounds textureBounds(tgt::vec3(0.f), tgt::vec3(1.f));
110

111
112
                // clip proxy geometry against near-plane to support camera in volume
                // FIXME:   In some cases, the near plane is not rendered correctly...
schultezub's avatar
schultezub committed
113
                float nearPlaneDistToOrigin = tgt::dot(p_camera.getValue().getPosition(), -p_camera.getValue().getLook()) - p_camera.getValue().getNearDist() - .002f;
114
                MeshGeometry clipped = proxyGeometry->clipAgainstPlane(nearPlaneDistToOrigin, -p_camera.getValue().getLook(), true, 0.02f);
115

116
                // start render setup
117
                _shader->activate();
118

119
                // setup virtual mirror if necessary
schultezub's avatar
schultezub committed
120
                tgt::mat4 mirrorMatrix = tgt::mat4::identity;
121
                if (p_enableMirror.getValue()) {
122
                    ScopedTypedData<FaceGeometry> mirrorGeometry(data, p_mirrorID.getValue());
schultezub's avatar
schultezub committed
123
124
125
126
127
128
129
                    if (mirrorGeometry && mirrorGeometry->size() > 0) {
                        const tgt::vec3& p = mirrorGeometry->getVertices()[0];
                        tgt::vec3 n = tgt::normalize(tgt::cross(mirrorGeometry->getVertices()[1] - mirrorGeometry->getVertices()[0], mirrorGeometry->getVertices()[2] - mirrorGeometry->getVertices()[0]));
                        float k = tgt::dot(p, n);

                        // mirror matrix sponsored by:
                        // Jiang 
130
                        mirrorMatrix = tgt::transpose(tgt::mat4(
schultezub's avatar
schultezub committed
131
132
133
                            1 - 2*n.x*n.x, -2*n.y*n.x   , -2*n.z*n.x   , 0, 
                            -2*n.x*n.y   , 1 - 2*n.y*n.y, -2*n.z*n.y   , 0, 
                            -2*n.x*n.z   , -2*n.y*n.z   , 1 - 2*n.z*n.z, 0, 
134
                            2*n.x*k      , 2*n.y*k      , 2*n.z*k      , 1));
schultezub's avatar
schultezub committed
135

136
                        // TODO: double check, whether matrix transpose is necessary
schultezub's avatar
schultezub committed
137
138
139
140
                    }
                    else {
                        LERROR("No suitable virtual mirror geometry found.");
                    }
141
                }
schultezub's avatar
schultezub committed
142

143
                FramebufferActivationGuard fag(this);
144
                decorateRenderProlog(data, _shader);
145
                
schultezub's avatar
schultezub committed
146
147
148
                const tgt::Camera& cam = p_camera.getValue();
                tgt::TextureUnit geometryDepthUnit, entryDepthUnit;

149
                _shader->setIgnoreUniformLocationError(true);
150
                _shader->setUniform("_viewportSizeRCP", 1.f / tgt::vec2(getEffectiveViewportSize()));
151
                _shader->setUniform("_modelMatrix", mirrorMatrix);
152
153
154
155
                _shader->setUniform("_projectionMatrix", cam.getProjectionMatrix());
                _shader->setUniform("_viewMatrix", cam.getViewMatrix());

                if (geometryImage != 0) {
156
                    geometryImage->bindDepthTexture(_shader, geometryDepthUnit, "_geometryDepthTexture", "_geometryDepthTexParams");
157
158
159
160
161
162
163
164
165
166
167
168
169

                    _shader->setUniform("_integrateGeometry", true);
                    _shader->setUniform("_near", cam.getNearDist());
                    _shader->setUniform("_far", cam.getFarDist());

                    tgt::mat4 inverseView = tgt::mat4::identity;
                    if (cam.getViewMatrix().invert(inverseView))
                        _shader->setUniform("_inverseViewMatrix", inverseView);

                    tgt::mat4 inverseProjection = tgt::mat4::identity;
                    if (cam.getProjectionMatrix().invert(inverseProjection))
                        _shader->setUniform("_inverseProjectionMatrix", inverseProjection);

170
                    _shader->setUniform("_volumeWorldToTexture", img->getParent()->getMappingInformation().getWorldToTextureMatrix());
171
172
173
174
175
                }
                else {
                    _shader->setUniform("_integrateGeometry", false);
                }

176
                _shader->setIgnoreUniformLocationError(false);
177

178
                glEnable(GL_CULL_FACE);
179
                glEnable(GL_DEPTH_TEST);
180
181

                // create entry points texture
182
183
                createAndAttachTexture(GL_RGBA16);
                createAndAttachDepthTexture();
184
                _shader->setUniform("_isEntrypoint", true);
185

186
187
                glDepthFunc(GL_LESS);
                glClearDepth(1.0f);
188
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
189
                glCullFace(p_enableMirror.getValue() ? GL_FRONT : GL_BACK);
190
                clipped.render(GL_POLYGON);
191

192
                RenderData* entrypoints = new RenderData(_fbo);
193
                _fbo->detachAll();
194
195

                // create exit points texture
196
197
                createAndAttachTexture(GL_RGBA16);
                createAndAttachDepthTexture();
198
199
200
                _shader->setUniform("_isEntrypoint", false);

                if (geometryImage != 0) {
201
                    entrypoints->bindDepthTexture(_shader, entryDepthUnit, "_entryDepthTexture", "_entryDepthTexParams");
202
                }
203

204
205
                glDepthFunc(GL_GREATER);
                glClearDepth(0.0f);
206
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
207
                glCullFace(p_enableMirror.getValue() ? GL_BACK : GL_FRONT);
208
                clipped.render(GL_POLYGON);
209

210
                RenderData* exitpoints = new RenderData(_fbo);
211
                decorateRenderEpilog(_shader);
212
                _shader->deactivate();
213
214
215
216
217
218
219

                glDepthFunc(GL_LESS);
                glClearDepth(1.0f);
                glCullFace(GL_BACK);
                glDisable(GL_CULL_FACE);
                glDisable(GL_DEPTH_TEST);

schultezub's avatar
schultezub committed
220
                LGL_ERROR;
221

222
223
                data.addData(p_entryImageID.getValue(), entrypoints);
                data.addData(p_exitImageID.getValue(), exitpoints);
224
225
226
227
228
229
            }
            else {
                LERROR("Input image must have dimensionality of 3.");
            }
        }
        else {
230
            LERROR("No suitable input image or proxy geometry found.");
231
232
        }

233
        validate(INVALID_RESULT);
234
235
    }

schultezub's avatar
schultezub committed
236
    std::string EEPGenerator::generateHeader() const {
237
        return getDecoratedHeader();
schultezub's avatar
schultezub committed
238
239
    }

240
241
    void EEPGenerator::updateProperties() {
        p_mirrorID.setVisible(p_enableMirror.getValue());
242
        validate(AbstractProcessor::INVALID_PROPERTIES);
243
244
    }

245
}