campvispainter.cpp 6.13 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-2013, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8 9
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13 14 15 16
// 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
17
// 
18 19 20 21
// 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.
22 23 24
// 
// ================================================================================================

schultezub's avatar
schultezub committed
25
#include "campvispainter.h"
schultezub's avatar
schultezub committed
26

27
#include "tgt/assert.h"
schultezub's avatar
schultezub committed
28
#include "tgt/camera.h"
29 30 31
#include "tgt/quadric.h"
#include "tgt/shadermanager.h"
#include "tgt/textureunit.h"
32
#include "tgt/qt/qtthreadedcanvas.h"
33

schultezub's avatar
schultezub committed
34

35
#include "core/datastructures/imagedata.h"
36
#include "core/datastructures/renderdata.h"
37
#include "core/pipeline/abstractpipeline.h"
38 39
#include "core/tools/job.h"
#include "core/tools/opengljobprocessor.h"
40
#include "core/tools/quadrenderer.h"
schultezub's avatar
schultezub committed
41

schultezub's avatar
schultezub committed
42
namespace campvis {
43
    const std::string CampVisPainter::loggerCat_ = "CAMPVis.core.CampVisPainter";
schultezub's avatar
schultezub committed
44

45
    CampVisPainter::CampVisPainter(tgt::GLCanvas* canvas, AbstractPipeline* pipeline)
46
        : tgt::Painter(canvas)
schultezub's avatar
schultezub committed
47 48
        , _pipeline(0)
        , _copyShader(0)
49 50
    {
        tgtAssert(getCanvas() != 0, "The given canvas must not be 0!");
51
        _dirty = true;
schultezub's avatar
schultezub committed
52
        setPipeline(pipeline);
schultezub's avatar
schultezub committed
53 54
    }

55
    CampVisPainter::~CampVisPainter() {
schultezub's avatar
schultezub committed
56

57
    }
schultezub's avatar
schultezub committed
58

59
    void CampVisPainter::paint() {
60 61 62
        if (getCanvas() == 0)
            return;

63 64 65 66
        const tgt::ivec2& size = getCanvas()->getSize();
        glViewport(0, 0, size.x, size.y);

        // try get Data
67
        ScopedTypedData<RenderData> rd(_pipeline->getDataContainer(), _pipeline->getRenderTargetID());
68
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
69
        if (rd != 0) {
70 71 72 73 74 75 76 77 78
            // activate shader
            _copyShader->activate();
            _copyShader->setIgnoreUniformLocationError(true);
            _copyShader->setUniform("_viewportSize", size);
            _copyShader->setUniform("_viewportSizeRCP", 1.f / tgt::vec2(size));
            _copyShader->setIgnoreUniformLocationError(false);

            // bind input textures
            tgt::TextureUnit colorUnit, depthUnit;
79
            rd->bind(_copyShader, colorUnit, depthUnit);
80
            LGL_ERROR;
81 82

            // execute the shader
83 84 85
            _copyShader->setAttributeLocation(0, "in_Position");
            _copyShader->setAttributeLocation(1, "in_TexCoords");
            QuadRdr.renderQuad();
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
            _copyShader->deactivate();
            LGL_ERROR;
        }
        else {
            // TODO: render some nifty error texture
            //       so long, we do some dummy rendering
            tgt::Camera c(tgt::vec3(0.f,0.f,2.f)); 
            c.look();  
            glColor3f(1.f, 0.f, 0.f);  
            tgt::Sphere sphere(.5f, 64, 32);  
            sphere.render();

            /*
            // render error texture
            if (!errorTex_) {
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                return;
            }
            glClear(GL_DEPTH_BUFFER_BIT);

            glActiveTexture(GL_TEXTURE0);
            errorTex_->bind();
            errorTex_->enable();

            glColor3f(1.f, 1.f, 1.f);
            renderQuad();

            errorTex_->disable();*/
schultezub's avatar
schultezub committed
114
        }
115
        LGL_ERROR;
116 117

        getCanvas()->swap();
schultezub's avatar
schultezub committed
118 119
    }

120
    void CampVisPainter::sizeChanged(const tgt::ivec2& size) {
schultezub's avatar
schultezub committed
121
        _pipeline->setRenderTargetSize(size);
schultezub's avatar
schultezub committed
122 123
    }

124
    void CampVisPainter::init() {
schultezub's avatar
schultezub committed
125 126
        try {
            // TODO:    Remove hardcoded paths, and use ShdrMgr.addPath() at some central location
127
            _copyShader = ShdrMgr.load("core/glsl/passthrough.vert", "core/glsl/copyimage.frag", "");
schultezub's avatar
schultezub committed
128
        }
schultezub's avatar
schultezub committed
129 130
        catch (tgt::Exception& e) {
            LERRORC("main.cpp", "Encountered tgt::Exception: " << e.what());
schultezub's avatar
schultezub committed
131
        }
schultezub's avatar
schultezub committed
132
    }
schultezub's avatar
schultezub committed
133

134
    void CampVisPainter::deinit() {
135
        ShdrMgr.dispose(_copyShader);
schultezub's avatar
schultezub committed
136 137 138 139

        if (_pipeline != 0) {
            _pipeline->s_renderTargetChanged.disconnect(this);
            if (getCanvas()->getEventHandler() != 0)
140
                getCanvas()->getEventHandler()->removeEventListener(_pipeline);
schultezub's avatar
schultezub committed
141 142
            _pipeline = 0;
        }
143 144
    }

145
    void CampVisPainter::setPipeline(AbstractPipeline* pipeline) {
schultezub's avatar
schultezub committed
146 147 148 149
        tgtAssert(pipeline != 0, "The given pipeline must not be 0.");
        if (_pipeline != 0) {
            _pipeline->s_renderTargetChanged.disconnect(this);
            if (getCanvas()->getEventHandler() != 0)
150
                getCanvas()->getEventHandler()->removeEventListener(_pipeline);
schultezub's avatar
schultezub committed
151
        }
schultezub's avatar
schultezub committed
152

schultezub's avatar
schultezub committed
153
        _pipeline = pipeline;
154
        _pipeline->s_renderTargetChanged.connect(this, &CampVisPainter::onRenderTargetChanged);
schultezub's avatar
schultezub committed
155 156
        _pipeline->setRenderTargetSize(getCanvas()->getSize());
        if (getCanvas()->getEventHandler() != 0)
157
            getCanvas()->getEventHandler()->addEventListenerToFront(_pipeline);
schultezub's avatar
schultezub committed
158
    }
schultezub's avatar
schultezub committed
159

160 161
    void CampVisPainter::repaint() {
        GLJobProc.enqueueJob(getCanvas(), makeJobOnHeap(this, &CampVisPainter::paint), OpenGLJobProcessor::PaintJob);
schultezub's avatar
schultezub committed
162
    }
schultezub's avatar
schultezub committed
163

164
    void CampVisPainter::onRenderTargetChanged() {
165 166 167
        repaint();
    }

168
    void CampVisPainter::setCanvas(tgt::GLCanvas* canvas) {
169 170 171
        tgtAssert(dynamic_cast<tgt::QtThreadedCanvas*>(canvas) != 0, "Canvas must be of type QtThreadedCanvas!");
        Painter::setCanvas(canvas);
    }
172
}