eepgenerator.cpp 10.7 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
37
38
#include "eepgenerator.h"

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

#include "core/datastructures/imagedatagl.h"
#include "core/datastructures/imagedatarendertarget.h"
39
#include "core/datastructures/meshgeometry.h"
40
#include "core/pipeline/processordecoratormasking.h"
41

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

    EEPGenerator::EEPGenerator(GenericProperty<tgt::ivec2>& canvasSize)
        : VisualizationProcessor(canvasSize)
47
48
49
        , p_sourceImageID("sourceImageID", "Input Image", "", DataNameProperty::READ)
        , p_geometryID("geometryID", "Input Geometry ID", "proxygeometry", DataNameProperty::READ)
        , p_mirrorID("mirrorID", "Input Mirror ID", "mirror", DataNameProperty::READ)
50
        , p_geometryImageId("GeometryImageId", "Rendered Geometry to Integrate (optional)", "", DataNameProperty::READ)
51
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")
        , p_enableMirror("enableMirror", "Enable Virtual Mirror Feature", false)
55
56
        , _shader(0)
    {
57
58
        addDecorator(new ProcessorDecoratorMasking());

59
60
61
        addProperty(&p_sourceImageID);
        addProperty(&p_geometryID);
        addProperty(&p_mirrorID);
62
        addProperty(&p_geometryImageId);
63
64
65
66
        addProperty(&p_entryImageID);
        addProperty(&p_exitImageID);
        addProperty(&p_camera);
        addProperty(&p_enableMirror);
67
68

        decoratePropertyCollection(this);
69
70
71
72
73
74
75
76
    }

    EEPGenerator::~EEPGenerator() {

    }

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

    void EEPGenerator::deinit() {
        ShdrMgr.dispose(_shader);
schultezub's avatar
schultezub committed
86
        _shader = 0;
87
88
89
90
        VisualizationProcessor::deinit();
    }

    void EEPGenerator::process(DataContainer& data) {
91
92
        DataContainer::ScopedTypedData<ImageDataGL> img(data, p_sourceImageID.getValue());
        DataContainer::ScopedTypedData<MeshGeometry> proxyGeometry(data, p_geometryID.getValue());
93

94
        if (img != 0 && proxyGeometry != 0 && _shader != 0) {
95
            if (img->getDimensionality() == 3) {
schultezub's avatar
schultezub committed
96
97
98
99
100
                if (_invalidationLevel.isInvalidShader()) {
                    _shader->setHeaders(generateHeader());
                    _shader->rebuild();
                }

101
102
                DataContainer::ScopedTypedData<ImageDataRenderTarget> geometryImage(data, p_geometryImageId.getValue());

103
104
                tgt::Bounds volumeExtent = img->getWorldBounds();
                tgt::Bounds textureBounds(tgt::vec3(0.f), tgt::vec3(1.f));
105

106
107
                // clip proxy geometry against near-plane to support camera in volume
                // FIXME:   In some cases, the near plane is not rendered correctly...
108
109
                float nearPlaneDistToOrigin = tgt::dot(p_camera.getValue().getPosition(), -p_camera.getValue().getLook()) - p_camera.getValue().getNearDist() - .001f;
                MeshGeometry clipped = proxyGeometry->clipAgainstPlane(nearPlaneDistToOrigin, -p_camera.getValue().getLook(), true, 0.02f);
110

111
                // start render setup
112
113
                glPushAttrib(GL_ALL_ATTRIB_BITS);
                _shader->activate();
114

115
                // setup virtual mirror if necessary
schultezub's avatar
schultezub committed
116
                tgt::mat4 mirrorMatrix = tgt::mat4::identity;
117
118
                if (p_enableMirror.getValue()) {
                    DataContainer::ScopedTypedData<FaceGeometry> mirrorGeometry(data, p_mirrorID.getValue());
schultezub's avatar
schultezub committed
119
120
121
122
123
124
125
                    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 
126
                        mirrorMatrix = tgt::transpose(tgt::mat4(
schultezub's avatar
schultezub committed
127
128
129
                            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, 
130
                            2*n.x*k      , 2*n.y*k      , 2*n.z*k      , 1));
schultezub's avatar
schultezub committed
131

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

139
                decorateRenderProlog(data, _shader);
140
                
schultezub's avatar
schultezub committed
141
142
143
                const tgt::Camera& cam = p_camera.getValue();
                tgt::TextureUnit geometryDepthUnit, entryDepthUnit;

144
145
                _shader->setIgnoreUniformLocationError(true);
                _shader->setUniform("_viewportSizeRCP", 1.f / tgt::vec2(_renderTargetSize.getValue()));
146
                _shader->setUniform("_modelMatrix", mirrorMatrix);
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
                _shader->setUniform("_projectionMatrix", cam.getProjectionMatrix());
                _shader->setUniform("_viewMatrix", cam.getViewMatrix());

                if (geometryImage != 0) {
                     geometryImage->bind(_shader, 0, &geometryDepthUnit, "", "_geometryDepthTexture");

                    _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);

                    _shader->setUniform("_volumeWorldToTexture", img->getMappingInformation().getWorldToTextureMatrix());
                }
                else {
                    _shader->setUniform("_integrateGeometry", false);
                }

171
                _shader->setIgnoreUniformLocationError(false);
172

173
                glEnable(GL_CULL_FACE);
174
                glEnable(GL_DEPTH_TEST);
175
176

                // create entry points texture
177
                ImageDataRenderTarget* entrypoints = new ImageDataRenderTarget(tgt::svec3(_renderTargetSize.getValue(), 1), GL_RGBA16);
178
                entrypoints->activate();
179
                _shader->setUniform("_isEntrypoint", true);
180

181
182
                glDepthFunc(GL_LESS);
                glClearDepth(1.0f);
183
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
184
                glCullFace(p_enableMirror.getValue() ? GL_FRONT : GL_BACK);
185
                clipped.render();
186
187
188
189

                entrypoints->deactivate();

                // create exit points texture
190
                ImageDataRenderTarget* exitpoints = new ImageDataRenderTarget(tgt::svec3(_renderTargetSize.getValue(), 1), GL_RGBA16);
191
                exitpoints->activate();
192
193
194
                _shader->setUniform("_isEntrypoint", false);

                if (geometryImage != 0) {
schultezub's avatar
schultezub committed
195
                    entrypoints->bind(_shader, 0, &entryDepthUnit, "", "_entryDepthTexture");
196
                }
197

198
199
                glDepthFunc(GL_GREATER);
                glClearDepth(0.0f);
200
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
201
                glCullFace(p_enableMirror.getValue() ? GL_BACK : GL_FRONT);
202
                clipped.render();
203
204
205

                exitpoints->deactivate();

206
                decorateRenderEpilog(_shader);
207
208
                _shader->deactivate();
                glPopAttrib();
schultezub's avatar
schultezub committed
209
                LGL_ERROR;
210

211
212
213
214
                data.addData(p_entryImageID.getValue(), entrypoints);
                data.addData(p_exitImageID.getValue(), exitpoints);
                p_entryImageID.issueWrite();
                p_exitImageID.issueWrite();
215
216
217
218
219
220
            }
            else {
                LERROR("Input image must have dimensionality of 3.");
            }
        }
        else {
221
            LERROR("No suitable input image or proxy geometry found.");
222
223
224
225
226
        }

        _invalidationLevel.setValid();
    }

schultezub's avatar
schultezub committed
227
    std::string EEPGenerator::generateHeader() const {
228
        return getDecoratedHeader();
schultezub's avatar
schultezub committed
229
230
    }

231
}