eepgenerator.cpp 11 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
39
#include "core/datastructures/imagedata.h"
#include "core/datastructures/imagerepresentationgl.h"
#include "core/datastructures/imagerepresentationrendertarget.h"
40
#include "core/datastructures/meshgeometry.h"
41
#include "core/pipeline/processordecoratormasking.h"
42

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

    EEPGenerator::EEPGenerator(GenericProperty<tgt::ivec2>& canvasSize)
        : VisualizationProcessor(canvasSize)
48
49
50
        , 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)
51
        , p_geometryImageId("GeometryImageId", "Rendered Geometry to Integrate (optional)", "", DataNameProperty::READ)
52
53
54
55
        , 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)
56
57
        , _shader(0)
    {
58
59
        addDecorator(new ProcessorDecoratorMasking());

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

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

    EEPGenerator::~EEPGenerator() {

    }

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

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

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

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

102
                ImageRepresentationRenderTarget::ScopedRepresentation geometryImage(data, p_geometryImageId.getValue());
103

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

107
108
                // 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
109
                float nearPlaneDistToOrigin = tgt::dot(p_camera.getValue().getPosition(), -p_camera.getValue().getLook()) - p_camera.getValue().getNearDist() - .002f;
110
                MeshGeometry clipped = proxyGeometry->clipAgainstPlane(nearPlaneDistToOrigin, -p_camera.getValue().getLook(), true, 0.02f);
111

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

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

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

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

145
146
                _shader->setIgnoreUniformLocationError(true);
                _shader->setUniform("_viewportSizeRCP", 1.f / tgt::vec2(_renderTargetSize.getValue()));
147
                _shader->setUniform("_modelMatrix", mirrorMatrix);
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
                _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);

166
                    _shader->setUniform("_volumeWorldToTexture", img->getParent()->getMappingInformation().getWorldToTextureMatrix());
167
168
169
170
171
                }
                else {
                    _shader->setUniform("_integrateGeometry", false);
                }

172
                _shader->setIgnoreUniformLocationError(false);
173

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

                // create entry points texture
178
179
                std::pair<ImageData*, ImageRepresentationRenderTarget*> entrypoints = ImageRepresentationRenderTarget::createWithImageData(_renderTargetSize.getValue(), GL_RGBA16);
                entrypoints.second->activate();
180
                _shader->setUniform("_isEntrypoint", true);
181

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

188
                entrypoints.second->deactivate();
189
190

                // create exit points texture
191
192
                std::pair<ImageData*, ImageRepresentationRenderTarget*> exitpoints = ImageRepresentationRenderTarget::createWithImageData(_renderTargetSize.getValue(), GL_RGBA16);
                exitpoints.second->activate();
193
194
195
                _shader->setUniform("_isEntrypoint", false);

                if (geometryImage != 0) {
196
                    entrypoints.second->bind(_shader, 0, &entryDepthUnit, "", "_entryDepthTexture");
197
                }
198

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

205
                exitpoints.second->deactivate();
206

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

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

225
        applyInvalidationLevel(InvalidationLevel::VALID);
226
227
    }

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

232
}