eepgenerator.cpp 9.01 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
50
51
52
53
        , 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)
        , 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)
54
55
        , _shader(0)
    {
56
57
        addDecorator(new ProcessorDecoratorMasking());

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

        decoratePropertyCollection(this);
67
68
69
70
71
72
73
74
    }

    EEPGenerator::~EEPGenerator() {

    }

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

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

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

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

99
100
                tgt::Bounds volumeExtent = img->getWorldBounds();
                tgt::Bounds textureBounds(tgt::vec3(0.f), tgt::vec3(1.f));
101

102
103
                // clip proxy geometry against near-plane to support camera in volume
                // FIXME:   In some cases, the near plane is not rendered correctly...
104
105
                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);
106

107
                // start render setup
108
109
                glPushAttrib(GL_ALL_ATTRIB_BITS);
                _shader->activate();
110

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

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

135
                decorateRenderProlog(data, _shader);
136
                
137
138
                _shader->setIgnoreUniformLocationError(true);
                _shader->setUniform("_viewportSizeRCP", 1.f / tgt::vec2(_renderTargetSize.getValue()));
139
                _shader->setUniform("_modelMatrix", mirrorMatrix);
140
141
                _shader->setUniform("_projectionMatrix", p_camera.getValue().getProjectionMatrix());
                _shader->setUniform("_viewMatrix", p_camera.getValue().getViewMatrix());
142
                _shader->setIgnoreUniformLocationError(false);
143

144
                glEnable(GL_CULL_FACE);
145
                glEnable(GL_DEPTH_TEST);
146
147

                // create entry points texture
148
                ImageDataRenderTarget* entrypoints = new ImageDataRenderTarget(tgt::svec3(_renderTargetSize.getValue(), 1), GL_RGBA16);
149
150
                entrypoints->activate();

151
152
                glDepthFunc(GL_LESS);
                glClearDepth(1.0f);
153
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
154
                glCullFace(p_enableMirror.getValue() ? GL_FRONT : GL_BACK);
155
                clipped.render();
156
157
158
159

                entrypoints->deactivate();

                // create exit points texture
160
                ImageDataRenderTarget* exitpoints = new ImageDataRenderTarget(tgt::svec3(_renderTargetSize.getValue(), 1), GL_RGBA16);
161
162
                exitpoints->activate();

163
164
                glDepthFunc(GL_GREATER);
                glClearDepth(0.0f);
165
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
166
                glCullFace(p_enableMirror.getValue() ? GL_BACK : GL_FRONT);
167
                clipped.render();
168
169
170

                exitpoints->deactivate();

171
                decorateRenderEpilog(_shader);
172
173
                _shader->deactivate();
                glPopAttrib();
schultezub's avatar
schultezub committed
174
                LGL_ERROR;
175

176
177
178
179
                data.addData(p_entryImageID.getValue(), entrypoints);
                data.addData(p_exitImageID.getValue(), exitpoints);
                p_entryImageID.issueWrite();
                p_exitImageID.issueWrite();
180
181
182
183
184
185
            }
            else {
                LERROR("Input image must have dimensionality of 3.");
            }
        }
        else {
186
            LERROR("No suitable input image or proxy geometry found.");
187
188
189
190
191
        }

        _invalidationLevel.setValid();
    }

schultezub's avatar
schultezub committed
192
    std::string EEPGenerator::generateHeader() const {
193
        return getDecoratedHeader();
schultezub's avatar
schultezub committed
194
195
    }

196
}