/
FramebufferExample.cpp
107 lines (85 loc) · 3.61 KB
/
FramebufferExample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
96
97
98
99
100
101
102
103
104
105
106
107
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include "PluginManager/PluginManager.h"
#include "Scene.h"
#include "Trade/AbstractImporter.h"
#include "AbstractExample.h"
#include "Billboard.h"
#include "ColorCorrectionCamera.h"
#include "configure.h"
using namespace Corrade::Utility;
using namespace Corrade::PluginManager;
namespace Magnum { namespace Examples {
class FramebufferExample: public AbstractExample {
public:
FramebufferExample(int& argc, char** argv): AbstractExample(argc, argv, "Framebuffer example"), colorCorrectionBuffer(Buffer::Target::Texture) {
if(argc != 2) {
Debug() << "Usage:" << argv[0] << "image.tga";
exit(0);
}
camera = new ColorCorrectionCamera(&scene);
/* Load TGA importer plugin */
PluginManager<Trade::AbstractImporter> manager(PLUGIN_IMPORTER_DIR);
Trade::AbstractImporter* importer;
if(manager.load("TgaImporter") != AbstractPluginManager::LoadOk || !(importer = manager.instance("TgaImporter"))) {
Error() << "Cannot load TgaImporter plugin from" << PLUGIN_IMPORTER_DIR;
exit(1);
}
/* Load the image */
if(!importer->open(argv[1]) || !importer->image2DCount()) {
Error() << "Cannot open image" << argv[1];
exit(2);
}
/* Create color correction texture */
GLfloat texture[1024];
for(size_t i = 0; i != 1024; ++i) {
GLfloat x = i*2/1023.0-1;
texture[i] = (sin(x*PI)/3.7+x+1)/2;
}
colorCorrectionBuffer.setData(sizeof(texture), texture, Buffer::Usage::StaticDraw);
/* Add billboard to the scene */
billboard = new Billboard(importer->image2D(0), &colorCorrectionBuffer, &scene);
delete importer;
}
protected:
inline void viewportEvent(const Math::Vector2<GLsizei>& size) {
camera->setViewport(size);
}
inline void drawEvent() {
camera->draw();
swapBuffers();
}
void mouseEvent(MouseButton button, MouseState state, const Math::Vector2<int>& position) {
if(button == MouseButton::WheelUp)
billboard->scale(Vector3(5.0f/4));
else if(button == MouseButton::WheelDown)
billboard->scale(Vector3(4.0f/5));
previous = position;
redraw();
}
void mouseMoveEvent(const Math::Vector2<int>& position) {
Math::Vector2<int> delta = position-previous;
billboard->translate({static_cast<GLfloat>(delta.x())/camera->viewport().x(),
-static_cast<GLfloat>(delta.y())/camera->viewport().y(),
0});
previous = position;
redraw();
}
private:
Math::Vector2<int> previous;
Scene scene;
Camera* camera;
Billboard* billboard;
Buffer colorCorrectionBuffer;
};
}}
MAGNUM_EXAMPLE_MAIN(Magnum::Examples::FramebufferExample)