orientationoverlay.cpp 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 117 118 119 120 121 122 123
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
// 
// ================================================================================================

#include "orientationoverlay.h"
#include "cgt/logmanager.h"
#include "cgt/shadermanager.h"
#include "cgt/textureunit.h"
#include "cgt/texturereadertga.h"

#include "core/datastructures/cameradata.h"
#include "core/datastructures/imagedata.h"
#include "core/datastructures/meshgeometry.h"
#include "core/datastructures/renderdata.h"

#include "core/tools/quadrenderer.h"

namespace campvis {
    
    const std::string OrientationOverlay::loggerCat_ = "CAMPVis.modules.vis.OrientationOverlay";

    OrientationOverlay::OrientationOverlay(IVec2Property* viewportSizeProp)
        : VisualizationProcessor(viewportSizeProp)
        , p_camera("Camera", "Camera", "camera", DataNameProperty::READ)
        , p_sourceImageId("SourceImageId", "Source Image ID", "", DataNameProperty::READ)
        , p_targetImageId("TargetImageId", "Output Image", "OrientationOverlay", DataNameProperty::WRITE)
        , p_passThroughImageId("PassThroughImageId", "Passthrough Image ID (optional)", "", DataNameProperty::READ)
        , p_cubeSize("CubeSize", "Cube Size", .1f, .01f, .5f, .1f, 2)
        , p_cubePosition("CubePosition", "Cube Position", cgt::vec2(.85f, .15f), cgt::vec2(0.f), cgt::vec2(1.f), cgt::vec2(.1f), cgt::ivec2(2))
        , p_enableTexturing("EnableTexturing", "Enable Texturing", true)
        , p_flipMatrix("FlipMatrix", "Flip Matrix for Alignment (optional)", cgt::mat4::identity)
        , _cubeGeometry(nullptr)
        , _shader(nullptr)
        , _textures(nullptr)
    {
        addProperty(p_camera);
        addProperty(p_sourceImageId);
        addProperty(p_targetImageId);
        addProperty(p_passThroughImageId);

        addProperty(p_cubeSize, INVALID_RESULT | INVALID_PROPERTIES);
        addProperty(p_cubePosition);
        addProperty(p_enableTexturing);
        addProperty(p_flipMatrix);

        p_flipMatrix.setVisible(false);
    }

    OrientationOverlay::~OrientationOverlay() {

    }

    void OrientationOverlay::init() {
        VisualizationProcessor::init();
        _shader = ShdrMgr.load("core/glsl/passthrough.vert", "modules/vis/glsl/orientationoverlay.frag", "");
        _passthroughShader = ShdrMgr.load("core/glsl/passthrough.vert", "modules/vis/glsl/quadview.frag", "");

        createCube();

        std::vector<std::string> textureFileNames;
        textureFileNames.push_back(ShdrMgr.completePath("/modules/vis/textures/front.tga"));
        textureFileNames.push_back(ShdrMgr.completePath("/modules/vis/textures/back.tga"));
        textureFileNames.push_back(ShdrMgr.completePath("/modules/vis/textures/top.tga"));
        textureFileNames.push_back(ShdrMgr.completePath("/modules/vis/textures/bottom.tga"));
        textureFileNames.push_back(ShdrMgr.completePath("/modules/vis/textures/left.tga"));
        textureFileNames.push_back(ShdrMgr.completePath("/modules/vis/textures/right.tga"));

        cgt::TextureReaderTga tgaReader;
        _textures = tgaReader.loadTextureArray(textureFileNames, cgt::Texture::LINEAR);
    }

    void OrientationOverlay::deinit() {
        VisualizationProcessor::deinit();

        delete _cubeGeometry;
        ShdrMgr.dispose(_shader);
        ShdrMgr.dispose(_passthroughShader);

        delete _textures;
    }

    void OrientationOverlay::updateResult(DataContainer& data) {
        ScopedTypedData<CameraData> camera(data, p_camera.getValue());
        ScopedTypedData<ImageData> sourceImage(data, p_sourceImageId.getValue());
        ScopedTypedData<RenderData> passthroughImage(data, p_passThroughImageId.getValue());

        if (camera && sourceImage) {
            FramebufferActivationGuard fag(this);
            createAndAttachColorTexture();
            createAndAttachDepthTexture();

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            if (passthroughImage) {
                _passthroughShader->activate();

                cgt::TextureUnit colorUnit, depthUnit;
                passthroughImage->bind(_passthroughShader, colorUnit, depthUnit);
                QuadRdr.renderQuad11();

                _passthroughShader->deactivate();
            }

124
            glEnable(GL_DEPTH_TEST);
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
            _shader->activate();

            cgt::TextureUnit textureUnit;
            textureUnit.activate();
            _textures->bind();
            _shader->setUniform("_cubeTexture", textureUnit.getUnitNumber());
            _shader->setUniform("_enableTexturing", p_enableTexturing.getValue());

            _shader->setUniform("_projectionMatrix", cgt::mat4::createOrtho(-1,1,1,-1,-2,2));
            const cgt::mat4 viewMatrix = cgt::mat4::createTranslation(cgt::vec3(p_cubePosition.getValue().x * 2.f - 1.f, p_cubePosition.getValue().y * 2.f - 1.f, 0.f))
                                       * cgt::mat4::createScale(cgt::vec3(float(getEffectiveViewportSize().y) / float(getEffectiveViewportSize().x), 1.f, 1.f))
                                       * camera->getCamera().getViewMatrix().getRotationalPart();
            _shader->setUniform("_viewMatrix", viewMatrix);
            _shader->setUniform("_modelMatrix", p_flipMatrix.getValue());

            _cubeGeometry->render(GL_TRIANGLE_FAN);

            glDisable(GL_DEPTH_TEST);

            _shader->deactivate();
            cgt::TextureUnit::setZeroUnit();
            LGL_ERROR;

            data.addData(p_targetImageId.getValue(), new RenderData(_fbo));
        }
        else {
            LDEBUG("No suitable input images found.");
        }
    }

    void OrientationOverlay::createCube() {
        const float& cs = p_cubeSize.getValue();
        std::vector<cgt::vec3> vertices, texCoords;
        std::vector<FaceGeometry> faces;

        // bottom
        texCoords.push_back(cgt::vec3(0.f, 0.f, 3.f));
        vertices.push_back(cgt::vec3(-cs, -cs,  cs));
        texCoords.push_back(cgt::vec3(1.f, 0.f, 3.f));
        vertices.push_back(cgt::vec3( cs, -cs,  cs));
        texCoords.push_back(cgt::vec3(1.f, 1.f, 3.f));
        vertices.push_back(cgt::vec3( cs,  cs,  cs));
        texCoords.push_back(cgt::vec3(0.f, 1.f, 3.f));
        vertices.push_back(cgt::vec3(-cs,  cs,  cs));
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<cgt::vec4>(4, cgt::vec4(0.f, 0.f, 1.f, 1.f)), std::vector<cgt::vec3>(4, cgt::vec3(0.f, 0.f, 1.f))));
        vertices.clear();
        texCoords.clear();

        // top
        texCoords.push_back(cgt::vec3(0.f, 0.f, 2.f));
        vertices.push_back(cgt::vec3( cs, -cs, -cs));
        texCoords.push_back(cgt::vec3(1.f, 0.f, 2.f));
        vertices.push_back(cgt::vec3(-cs, -cs, -cs));
        texCoords.push_back(cgt::vec3(1.f, 1.f, 2.f));
        vertices.push_back(cgt::vec3(-cs,  cs, -cs));
        texCoords.push_back(cgt::vec3(0.f, 1.f, 2.f));
        vertices.push_back(cgt::vec3( cs,  cs, -cs));
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<cgt::vec4>(4, cgt::vec4(0.f, 0.f, 1.f, 1.f)), std::vector<cgt::vec3>(4, cgt::vec3(0.f, 0.f, 1.f))));
        vertices.clear();
        texCoords.clear();

        // front
        texCoords.push_back(cgt::vec3(0.f, 0.f, 0.f));
        vertices.push_back(cgt::vec3(-cs,  cs,  cs));
        texCoords.push_back(cgt::vec3(1.f, 0.f, 0.f));
        vertices.push_back(cgt::vec3( cs,  cs,  cs));
        texCoords.push_back(cgt::vec3(1.f, 1.f, 0.f));
        vertices.push_back(cgt::vec3( cs,  cs, -cs));
        texCoords.push_back(cgt::vec3(0.f, 1.f, 0.f));
        vertices.push_back(cgt::vec3(-cs,  cs, -cs));
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<cgt::vec4>(4, cgt::vec4(0.f, 1.f, 0.f, 1.f)), std::vector<cgt::vec3>(4, cgt::vec3(0.f, 0.f, 1.f))));
        vertices.clear();
        texCoords.clear();

        // back
        texCoords.push_back(cgt::vec3(0.f, 0.f, 1.f));
        vertices.push_back(cgt::vec3( cs, -cs,  cs));
        texCoords.push_back(cgt::vec3(1.f, 0.f, 1.f));
        vertices.push_back(cgt::vec3(-cs, -cs,  cs));
        texCoords.push_back(cgt::vec3(1.f, 1.f, 1.f));
        vertices.push_back(cgt::vec3(-cs, -cs, -cs));
        texCoords.push_back(cgt::vec3(0.f, 1.f, 1.f));
        vertices.push_back(cgt::vec3( cs, -cs, -cs));
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<cgt::vec4>(4, cgt::vec4(0.f, 1.f, 0.f, 1.f)), std::vector<cgt::vec3>(4, cgt::vec3(0.f, 0.f, 1.f))));
        vertices.clear();
        texCoords.clear();

        // right
        texCoords.push_back(cgt::vec3(0.f, 0.f, 5.f));
        vertices.push_back(cgt::vec3(-cs, -cs,  cs));
        texCoords.push_back(cgt::vec3(1.f, 0.f, 5.f));
        vertices.push_back(cgt::vec3(-cs,  cs,  cs));
        texCoords.push_back(cgt::vec3(1.f, 1.f, 5.f));
        vertices.push_back(cgt::vec3(-cs,  cs, -cs));
        texCoords.push_back(cgt::vec3(0.f, 1.f, 5.f));
        vertices.push_back(cgt::vec3(-cs, -cs, -cs));
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<cgt::vec4>(4, cgt::vec4(1.f, 0.f, 0.f, 1.f)), std::vector<cgt::vec3>(4, cgt::vec3(0.f, 0.f, 1.f))));
        vertices.clear();
        texCoords.clear();

        // left
        texCoords.push_back(cgt::vec3(0.f, 0.f, 4.f));
        vertices.push_back(cgt::vec3( cs,  cs,  cs));
        texCoords.push_back(cgt::vec3(1.f, 0.f, 4.f));
        vertices.push_back(cgt::vec3( cs, -cs,  cs));
        texCoords.push_back(cgt::vec3(1.f, 1.f, 4.f));
        vertices.push_back(cgt::vec3( cs, -cs, -cs));
        texCoords.push_back(cgt::vec3(0.f, 1.f, 4.f));
        vertices.push_back(cgt::vec3( cs,  cs, -cs));
        faces.push_back(FaceGeometry(vertices, texCoords, std::vector<cgt::vec4>(4, cgt::vec4(1.f, 0.f, 0.f, 1.f)), std::vector<cgt::vec3>(4, cgt::vec3(0.f, 0.f, 1.f))));
        vertices.clear();
        texCoords.clear();

        delete _cubeGeometry;
        _cubeGeometry = new MeshGeometry(faces);
    }

    void OrientationOverlay::updateProperties(DataContainer& dataContainer) {
        createCube();
    }

}