tumvispainter.cpp 4.5 KB
Newer Older
schultezub's avatar
schultezub committed
1
#include "tumvispainter.h"
schultezub's avatar
schultezub committed
2

3
#include "tgt/assert.h"
schultezub's avatar
schultezub committed
4
#include "tgt/camera.h"
schultezub's avatar
schultezub committed
5
#include "tgt/quadrenderer.h"
schultezub's avatar
schultezub committed
6
7
#include "tgt/quadric.h"

schultezub's avatar
schultezub committed
8
9
#include "core/datastructures/imagedatarendertarget.h"

schultezub's avatar
schultezub committed
10
11
12
namespace TUMVis {
    const std::string TumVisPainter::loggerCat_ = "TUMVis.core.TumVisPainter";

schultezub's avatar
schultezub committed
13
    TumVisPainter::TumVisPainter(tgt::GLCanvas* canvas, VisualizationPipeline* pipeline)
14
        : tgt::Painter(canvas)
schultezub's avatar
schultezub committed
15
16
17
18
        , _pipeline(0)
        , _dirty(0)
        , _currentlyRendering(false)
        , _copyShader(0)
19
20
    {
        tgtAssert(getCanvas() != 0, "The given canvas must not be 0!");
schultezub's avatar
schultezub committed
21
        setPipeline(pipeline);
schultezub's avatar
schultezub committed
22
23
24
    }

    void TumVisPainter::paint() {
schultezub's avatar
schultezub committed
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
        if (getCanvas() == 0)
            return;
        {
            tbb::mutex::scoped_lock lock(_localMutex);
            _currentlyRendering = true;
            _dirty = false;
        }

        // get OpenGL focus and setup context
        getCanvas()->getGLFocus();
        const tgt::ivec2 size = getCanvas()->getSize();
        glViewport(0, 0, size.x, size.y);

        // try get Data
        const ImageDataRenderTarget* image = _pipeline->getRenderTarget();
        if (image != 0) {
            
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            // 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;
            image->bind(_copyShader, colorUnit, depthUnit);
            LGL_ERROR;

            // execute the shader
            tgt::QuadRenderer::renderQuad();
            _copyShader->deactivate();
            LGL_ERROR;
        }
        else {
            // TODO: render some nifty error texture
            //       so long, we do some dummy rendering
            glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);  
            getCamera()->setPosition(tgt::vec3(0.f,0.f,2.f)); 
            getCamera()->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();*/
        }
        LGL_ERROR;

        {
            tbb::mutex::scoped_lock lock(_localMutex);
            _currentlyRendering = false;
        }
        if (_dirty)
            getCanvas()->repaint();
schultezub's avatar
schultezub committed
96
97
98
99
100
101
    }

    void TumVisPainter::sizeChanged(const tgt::ivec2& size) {
        if (getCanvas()) {
            getCanvas()->getGLFocus();
        }
schultezub's avatar
schultezub committed
102
        _pipeline->setRenderTargetSize(size);
schultezub's avatar
schultezub committed
103
104
105
    }

    void TumVisPainter::init() {
schultezub's avatar
schultezub committed
106
107
108
        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
109
        }
schultezub's avatar
schultezub committed
110
111
        catch (tgt::Exception& e) {
            LERRORC("main.cpp", "Encountered tgt::Exception: " << e.what());
schultezub's avatar
schultezub committed
112
        }
schultezub's avatar
schultezub committed
113
    }
schultezub's avatar
schultezub committed
114

schultezub's avatar
schultezub committed
115
116
117
118
119
120
121
    void TumVisPainter::setPipeline(VisualizationPipeline* pipeline) {
        tgtAssert(pipeline != 0, "The given pipeline must not be 0.");
        if (_pipeline != 0) {
            _pipeline->s_renderTargetChanged.disconnect(this);
            if (getCanvas()->getEventHandler() != 0)
                getCanvas()->getEventHandler()->removeListener(_pipeline);
        }
schultezub's avatar
schultezub committed
122

schultezub's avatar
schultezub committed
123
124
125
126
127
128
        _pipeline = pipeline;
        _pipeline->s_renderTargetChanged.connect(this, &TumVisPainter::onPipelineInvalidated);
        _pipeline->setRenderTargetSize(getCanvas()->getSize());
        if (getCanvas()->getEventHandler() != 0)
            getCanvas()->getEventHandler()->addListenerToFront(_pipeline);
    }
schultezub's avatar
schultezub committed
129

schultezub's avatar
schultezub committed
130
131
132
133
134
135
136
    void TumVisPainter::onPipelineInvalidated() {
        if (! _currentlyRendering)
            getCanvas()->repaint();
        else {
            tbb::mutex::scoped_lock lock(_localMutex);
            _dirty = true;
        }
schultezub's avatar
schultezub committed
137
    }
schultezub's avatar
schultezub committed
138

schultezub's avatar
schultezub committed
139
}