visualizationpipeline.cpp 4.78 KB
Newer Older
1
#include "visualizationpipeline.h"
schultezub's avatar
schultezub committed
2
#include "tgt/camera.h"
schultezub's avatar
schultezub committed
3
#include "tgt/quadrenderer.h"
schultezub's avatar
schultezub committed
4
5
#include "tgt/quadric.h"
#include "core/datastructures/imagedatarendertarget.h"
6
7
8
9
10

namespace TUMVis {
    const std::string VisualizationPipeline::loggerCat_ = "TUMVis.core.datastructures.VisualizationPipeline";

    VisualizationPipeline::VisualizationPipeline(tgt::GLCanvas* canvas /*= 0*/) 
schultezub's avatar
schultezub committed
11
        : AbstractPipeline()
schultezub's avatar
schultezub committed
12
        , _canvas(0)
schultezub's avatar
schultezub committed
13
14
        , _canvasSize("canvasSize", "Canvas Size", tgt::ivec2(128, 128))
        , _renderTargetID("renderTargetID", "Render Target ID", "VisualizationPipeline.renderTarget")
15
    {
schultezub's avatar
schultezub committed
16
        setCanvas(canvas);
17
18
19
20
21
22
23
24
25
26
27
28
    }

    VisualizationPipeline::~VisualizationPipeline() {
    }

    void VisualizationPipeline::onEvent(tgt::Event* e) {
        // cycle through event handlers, ask each one if it handles the event and if so, execute it.
        for (std::vector<AbstractEventHandler*>::iterator it = _eventHandlers.begin(); it != _eventHandlers.end() && !e->isAccepted(); ++it) {
            if ((*it)->accept(e)) {
                (*it)->execute(e);
            }
        }
schultezub's avatar
schultezub committed
29
30
31
32

        if (e->isAccepted()) {
            EventListener::onEvent(e);
        }
33
34
35
36
37
38
39
    }

    void VisualizationPipeline::setCanvas(tgt::GLCanvas* canvas) {
        if (_canvas != 0 && _canvas->getEventHandler() != 0) {
            _canvas->getEventHandler()->removeListener(this);
        }
        _canvas = canvas;
schultezub's avatar
schultezub committed
40
        _canvasSize.setValue(_canvas->getSize());
41
42
43
44
45
46
47
48
49
50
51
        if (_canvas->getEventHandler() != 0) {
            _canvas->getEventHandler()->addListenerToFront(this);
        }
    }

    PropertyCollection& VisualizationPipeline::getPropertyCollection() {
        return _properties;
    }

    void VisualizationPipeline::init() {
        AbstractPipeline::init();
schultezub's avatar
schultezub committed
52

schultezub's avatar
schultezub committed
53
54
55
56
57
58
59
        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);
        }
        catch (tgt::Exception& e) {
            LERRORC("main.cpp", "Encountered tgt::Exception: " << e.what());
        }
60
61
62
    }

    void VisualizationPipeline::paint() {
schultezub's avatar
schultezub committed
63
64
        execute();

schultezub's avatar
schultezub committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        if (_canvas == 0)
            return;

        // get OpenGL focus and setup context
        _canvas->getGLFocus();
        glViewport(0, 0, _canvas->getSize().x, _canvas->getSize().y);

        // try get Data
        const ImageDataRenderTarget* image = _data.getTypedData<ImageDataRenderTarget>(_renderTargetID.getValue());
        if (image != 0) {
            
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            // activate shader
            _copyShader->activate();
80
            _copyShader->setIgnoreUniformLocationError(true);
schultezub's avatar
schultezub committed
81
82
            _copyShader->setUniform("_viewportSize", tgt::vec2(_canvasSize.getValue()));
            _copyShader->setUniform("_viewportSizeRCP", 1.f / tgt::vec2(_canvasSize.getValue()));
83
            _copyShader->setIgnoreUniformLocationError(false);
schultezub's avatar
schultezub committed
84
85

            // bind input textures
schultezub's avatar
schultezub committed
86
87
            tgt::TextureUnit colorUnit, depthUnit;
            image->bind(_copyShader, colorUnit, depthUnit);
schultezub's avatar
schultezub committed
88
89
90
            LGL_ERROR;

            // execute the shader
schultezub's avatar
schultezub committed
91
            tgt::QuadRenderer::renderQuad();
schultezub's avatar
schultezub committed
92
93
94
95
96
97
98
            _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);  
99
            getCamera()->setPosition(tgt::vec3(0.f,0.f,2.f)); 
schultezub's avatar
schultezub committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
            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;
123
124
125
126
    }

    void VisualizationPipeline::sizeChanged(const tgt::ivec2& size) {
        // nothing to change if nothing changes
schultezub's avatar
schultezub committed
127
128
129
130
        if (size != _canvasSize.getValue()) {
            _canvasSize.setValue(size);
            // this should be enough here: Changing the property issues a notification of the 
            // owner (i.e. this pipeline), who is then up to decide how to proceed.
131
132
133
        }
    }

schultezub's avatar
schultezub committed
134
135
136
137
    const tgt::ivec2& VisualizationPipeline::getCanvasSize() const {
        return _canvasSize.getValue();
    }

138
}