campvispainter.cpp 6.74 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
#include "campvispainter.h"
schultezub's avatar
schultezub committed
31

32
#include "tgt/assert.h"
schultezub's avatar
schultezub committed
33
#include "tgt/camera.h"
34 35 36
#include "tgt/quadric.h"
#include "tgt/shadermanager.h"
#include "tgt/textureunit.h"
37
#include "tgt/qt/qtthreadedcanvas.h"
38

schultezub's avatar
schultezub committed
39

40
#include "core/datastructures/imagedata.h"
41
#include "core/datastructures/renderdata.h"
42
#include "core/pipeline/abstractpipeline.h"
43 44
#include "core/tools/job.h"
#include "core/tools/opengljobprocessor.h"
45
#include "core/tools/quadrenderer.h"
schultezub's avatar
schultezub committed
46

schultezub's avatar
schultezub committed
47
namespace campvis {
48
    const std::string CampVisPainter::loggerCat_ = "CAMPVis.core.CampVisPainter";
schultezub's avatar
schultezub committed
49

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

60
    CampVisPainter::~CampVisPainter() {
schultezub's avatar
schultezub committed
61

62
    }
schultezub's avatar
schultezub committed
63

64
    void CampVisPainter::paint() {
65 66 67
        if (getCanvas() == 0)
            return;

68 69 70 71
        const tgt::ivec2& size = getCanvas()->getSize();
        glViewport(0, 0, size.x, size.y);

        // try get Data
72
        ScopedTypedData<RenderData> rd(_pipeline->getDataContainer(), _pipeline->getRenderTargetID());
73
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
74
        if (rd != 0) {
75 76 77 78 79 80 81 82 83
            // 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;
84
            rd->bind(_copyShader, colorUnit, depthUnit);
85
            LGL_ERROR;
86 87

            // execute the shader
88 89 90
            _copyShader->setAttributeLocation(0, "in_Position");
            _copyShader->setAttributeLocation(1, "in_TexCoords");
            QuadRdr.renderQuad();
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
            _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
119
        }
120
        LGL_ERROR;
121 122

        getCanvas()->swap();
schultezub's avatar
schultezub committed
123 124
    }

125
    void CampVisPainter::sizeChanged(const tgt::ivec2& size) {
schultezub's avatar
schultezub committed
126
        _pipeline->setRenderTargetSize(size);
schultezub's avatar
schultezub committed
127 128
    }

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

139
    void CampVisPainter::deinit() {
140
        ShdrMgr.dispose(_copyShader);
schultezub's avatar
schultezub committed
141 142 143 144

        if (_pipeline != 0) {
            _pipeline->s_renderTargetChanged.disconnect(this);
            if (getCanvas()->getEventHandler() != 0)
145
                getCanvas()->getEventHandler()->removeEventListener(_pipeline);
schultezub's avatar
schultezub committed
146 147
            _pipeline = 0;
        }
148 149
    }

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

schultezub's avatar
schultezub committed
158
        _pipeline = pipeline;
159
        _pipeline->s_renderTargetChanged.connect(this, &CampVisPainter::onRenderTargetChanged);
schultezub's avatar
schultezub committed
160 161
        _pipeline->setRenderTargetSize(getCanvas()->getSize());
        if (getCanvas()->getEventHandler() != 0)
162
            getCanvas()->getEventHandler()->addEventListenerToFront(_pipeline);
schultezub's avatar
schultezub committed
163
    }
schultezub's avatar
schultezub committed
164

165 166
    void CampVisPainter::repaint() {
        GLJobProc.enqueueJob(getCanvas(), makeJobOnHeap(this, &CampVisPainter::paint), OpenGLJobProcessor::PaintJob);
schultezub's avatar
schultezub committed
167
    }
schultezub's avatar
schultezub committed
168

169
    void CampVisPainter::onRenderTargetChanged() {
170 171 172
        repaint();
    }

173
    void CampVisPainter::setCanvas(tgt::GLCanvas* canvas) {
174 175 176
        tgtAssert(dynamic_cast<tgt::QtThreadedCanvas*>(canvas) != 0, "Canvas must be of type QtThreadedCanvas!");
        Painter::setCanvas(canvas);
    }
177
}