eepgenerator.frag 5.14 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.
// 
// ================================================================================================

schultezub's avatar
schultezub committed
30 31 32
in vec3 ex_TexCoord;            ///< incoming texture coordinate
out vec4 out_Color;             ///< outgoing fragment color

33
#include "tools/texture2d.frag"
34
#include "tools/masking.frag"
35

schultezub's avatar
schultezub committed
36
uniform vec2 _viewportSizeRCP;
schultezub's avatar
schultezub committed
37

38 39
uniform bool _integrateGeometry;            ///< flag whether to integrate geometry into the EEP
uniform bool _isEntrypoint;                 ///< true if current run is for entrypoints, false if current run is for exitpoints
40 41 42 43 44 45

uniform sampler2D _entryDepthTexture;       ///< depth texture of the entrypoints (only used in exitpoints run)
uniform TextureParameters2D _entryDepthTexParams;

uniform sampler2D _geometryDepthTexture;    ///< depth texture of rendered geometry
uniform TextureParameters2D _geometryDepthTexParams;
46 47 48 49 50 51 52

uniform float _near;
uniform float _far;
uniform mat4 _inverseViewMatrix;            ///< inverse camera view matrix
uniform mat4 _inverseProjectionMatrix;      ///< inverse camera projection matrix
uniform mat4 _volumeWorldToTexture;         ///< world-to-texture matrix of volume

53
void main() {
54 55 56 57 58 59
    vec2 fragCoordNormalized = gl_FragCoord.xy * _viewportSizeRCP;
    MASKING_PROLOG(fragCoordNormalized);

    float fragDepth = gl_FragCoord.z;

    if (_integrateGeometry) {
60
        float geometryDepth = texture(_geometryDepthTexture, fragCoordNormalized).r;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

        if (_isEntrypoint) {
            // integrating geometry into Entrypoints
            float entryDepth = gl_FragCoord.z;

            if (geometryDepth <= entryDepth) {
                // geometry before Entrypoint
                out_Color = vec4(0.0);
                fragDepth = geometryDepth;
            }
            else {
                // geometry behind Entrypoint
                out_Color = vec4(ex_TexCoord, 1.0);
                fragDepth = entryDepth;
            }
        }
        else {
            // integrating geometry into Exitpoints
79
            float entryDepth = texture(_entryDepthTexture, fragCoordNormalized).r;
80 81 82 83 84
            float exitDepth = gl_FragCoord.z;

            if (geometryDepth <= entryDepth) {
                // geometry before Entrypoint
                out_Color = vec4(0.0);
85
                fragDepth = geometryDepth;
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            }
            else if (geometryDepth <= exitDepth) {
                // geometry between entrypoint and exitpoint

                // transform viewport coordinates to [-1, 1] NDC
                vec4 result = vec4(fragCoordNormalized, geometryDepth, 1.0);
                result = 2.0 * result - 1.0;
            
                // reverse perspective division by w (which is equal to the camera-space z)
                float origZG = (2.0 * _far * _near) / ((_far + _near) - result.z * (_far - _near));
                result *= origZG;

                // unproject and reverse camera-transform
                result = vec4((_volumeWorldToTexture * (_inverseViewMatrix * (_inverseProjectionMatrix * result))).xyz, 1.0);

                out_Color = vec4(result.xyz, 1.0);
                fragDepth = geometryDepth;
            }
            else {
                // geometry behind exitpoint
                out_Color = vec4(ex_TexCoord, 1.0);
                fragDepth = exitDepth;
            }
        }
    }
    else {
        out_Color = vec4(ex_TexCoord, 1.0);
    }

    gl_FragDepth = fragDepth;

117
    MASKING_EPILOG;
118
}