2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

campvispainter.cpp 6.58 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-2014, 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 Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, 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
#include "tgt/quadric.h"
#include "tgt/shadermanager.h"
31
#include "tgt/texture.h"
32
#include "tgt/textureunit.h"
33
#include "tgt/qt/qtthreadedcanvas.h"
34

schultezub's avatar
schultezub committed
35

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

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

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

58
    CampVisPainter::~CampVisPainter() {
schultezub's avatar
schultezub committed
59

60
    }
schultezub's avatar
schultezub committed
61

62
    void CampVisPainter::paint() {
63
64
65
        if (getCanvas() == 0)
            return;

66
67
68
69
70
        if (_copyShader == nullptr) {
            LERROR("Shader not initialized!");
            return;
        }

71
72
73
74
        const tgt::ivec2& size = getCanvas()->getSize();
        glViewport(0, 0, size.x, size.y);

        // try get Data
75
        ScopedTypedData<RenderData> rd(_pipeline->getDataContainer(), _pipeline->getRenderTargetID());
76
        ImageRepresentationGL::ScopedRepresentation repGL(_pipeline->getDataContainer(), _pipeline->getRenderTargetID());
77
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
78
79
80
81
82
83

        // activate the shader
        _copyShader->activate();
        tgt::Shader::IgnoreUniformLocationErrorGuard guard(_copyShader);

        // render whatever there is to render
84
        if (rd != 0 || (repGL != 0 && repGL->getDimensionality() == 2)) {
85
            _copyShader->setUniform("_viewMatrix", tgt::mat4::identity);
86
87

            // bind input textures
88
89
90
91
92
            tgt::TextureUnit colorUnit;
            if (rd)
                rd->bindColorTexture(_copyShader, colorUnit);
            else if (repGL)
                repGL->bind(_copyShader, colorUnit);
93
94

            // execute the shader
95
            QuadRdr.renderQuad();
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        }
        // if there is nothing to render, render the error texture
        else if (_errorTexture != nullptr) {
            float ratioRatio = static_cast<float>(size.y) / size.x;
            tgt::mat4 viewMatrix = (ratioRatio > 1) ? tgt::mat4::createScale(tgt::vec3(1.f, 1.f / ratioRatio, 1.f)) : tgt::mat4::createScale(tgt::vec3(ratioRatio, 1.f, 1.f));
            _copyShader->setUniform("_viewMatrix", viewMatrix);

            // bind input textures
            tgt::TextureUnit colorUnit;
            colorUnit.activate();
            _errorTexture->bind();
            _copyShader->setUniform("_colorTexture", colorUnit.getUnitNumber());

            // execute the shader
            QuadRdr.renderQuad();
111
112
        }
        else {
113
            LERROR("Nothing to render but could not load error texture either.");
schultezub's avatar
schultezub committed
114
        }
115
116

        _copyShader->deactivate();
117
        LGL_ERROR;
118
119

        getCanvas()->swap();
schultezub's avatar
schultezub committed
120
121
    }

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

126
    void CampVisPainter::init() {
schultezub's avatar
schultezub committed
127
        try {
128
            _copyShader = ShdrMgr.load("core/glsl/passthrough.vert", "core/glsl/copyimage.frag", "");
129
130
            _copyShader->setAttributeLocation(0, "in_Position");
            _copyShader->setAttributeLocation(1, "in_TexCoords");
schultezub's avatar
schultezub committed
131
        }
schultezub's avatar
schultezub committed
132
        catch (tgt::Exception& e) {
133
            LFATAL("Encountered tgt::Exception: " << e.what());
schultezub's avatar
schultezub committed
134
        }
schultezub's avatar
schultezub committed
135
    }
schultezub's avatar
schultezub committed
136

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

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

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

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

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

167
    void CampVisPainter::onRenderTargetChanged() {
168
169
170
        repaint();
    }

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

    void CampVisPainter::setErrorTexture(tgt::Texture* texture) {
        _errorTexture = texture;
    }

180
}