/
testwindow.cpp
244 lines (203 loc) · 7.39 KB
/
testwindow.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* The Doomsday Engine Project
*
* Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "testwindow.h"
#include <QMessageBox>
#include <de/GLState>
#include <de/Drawable>
#include <de/GLBuffer>
#include <de/GLShader>
#include <de/GLTexture>
#include <de/GLTarget>
#include <de/GuiApp>
#include <de/Clock>
using namespace de;
DENG2_PIMPL(TestWindow),
DENG2_OBSERVES(Canvas, GLInit),
DENG2_OBSERVES(Canvas, GLResize),
DENG2_OBSERVES(Clock, TimeChange)
{
Drawable ob;
Matrix4f modelMatrix;
Matrix4f projMatrix;
GLUniform uMvpMatrix;
GLUniform uColor;
GLUniform uTime;
GLUniform uTex;
GLTexture frameTex;
GLTexture testpic;
std::auto_ptr<GLTarget> frameTarget;
Time startedAt;
typedef GLBufferT<Vertex3TexRgba> VertexBuf;
Instance(Public *i)
: Base(i),
uMvpMatrix("uMvpMatrix", GLUniform::Mat4),
uColor ("uColor", GLUniform::Vec4),
uTime ("uTime", GLUniform::Float),
uTex ("uTex", GLUniform::Sampler2D)
{
// Use this as the main window.
setMain(i);
self.canvas().audienceForGLInit += this;
self.canvas().audienceForGLResize += this;
Clock::appClock().audienceForTimeChange += this;
uColor = Vector4f(.5f, .75f, .5f, 1);
}
void canvasGLInit(Canvas &cv)
{
try
{
LOG_DEBUG("GLInit");
glInit(cv);
}
catch(Error const &er)
{
QMessageBox::critical(thisPublic, "GL Init Error", er.asText());
exit(1);
}
}
void glInit(Canvas &cv)
{
// Set up the default state.
GLState &st = GLState::top();
st.setBlend(true);
st.setBlendFunc(gl::SrcAlpha, gl::OneMinusSrcAlpha);
//st.setCull(gl::Back);
st.setDepthTest(true);
// Textures.
testpic.setAutoGenMips(true);
testpic.setImage(QImage(":/images/testpic.png"));
testpic.setWrapT(gl::RepeatMirrored);
//testpic.generateMipmap();
testpic.setMinFilter(gl::Linear, gl::MipLinear);
uTex = testpic;
// Prepare the custom target.
frameTex.setUndefinedImage(Vector2ui(512, 256), Image::RGBA_8888);
frameTarget.reset(new GLTarget(frameTex));
VertexBuf *buf = new VertexBuf;
ob.addBuffer(1, buf);
VertexBuf::Type verts[8] = {
{ Vector3f(-1, -1, -1), Vector2f(0, 0), Vector4f(1, 1, 1, 1) },
{ Vector3f( 1, -1, -1), Vector2f(1, 0), Vector4f(1, 1, 0, 1) },
{ Vector3f( 1, 1, -1), Vector2f(1, 1), Vector4f(1, 0, 0, 1) },
{ Vector3f(-1, 1, -1), Vector2f(0, 1), Vector4f(0, 0, 1, 1) },
{ Vector3f(-1, -1, 1), Vector2f(1, 1), Vector4f(1, 1, 1, 1) },
{ Vector3f( 1, -1, 1), Vector2f(0, 1), Vector4f(1, 1, 0, 1) },
{ Vector3f( 1, 1, 1), Vector2f(0, 0), Vector4f(1, 0, 0, 1) },
{ Vector3f(-1, 1, 1), Vector2f(1, 0), Vector4f(0, 0, 1, 1) }
};
buf->setVertices(verts, 8, gl::Static);
GLBuffer::Indices idx;
idx << 0 << 4 << 3 << 7 << 2 << 6 << 1 << 5 << 0 << 4
<< 4 << 0
<< 0 << 3 << 1 << 2
<< 2 << 7
<< 7 << 4 << 6 << 5;
buf->setIndices(gl::TriangleStrip, idx, gl::Static);
Block vertShader =
"uniform highp mat4 uMvpMatrix;\n"
"uniform highp vec4 uColor;\n"
"uniform highp float uTime;\n"
"attribute highp vec4 aVertex;\n"
"attribute highp vec2 aUV;\n"
"attribute highp vec4 aColor;\n"
"varying highp vec2 vUV;\n"
"varying highp vec4 vColor;\n"
"void main(void) {\n"
" gl_Position = uMvpMatrix * aVertex;\n"
" vUV = aUV + vec2(uTime/5.0, 0.0);\n"
" vColor = aColor + vec4(sin(uTime), cos(uTime), sin(uTime), cos(uTime)*0.5) * uColor;\n"
"}\n";
Block fragShader =
"uniform sampler2D uTex;\n"
"varying highp vec2 vUV;\n"
"varying highp vec4 vColor;\n"
"void main(void) {\n"
" highp vec4 color = texture2D(uTex, vUV);\n"
" if(color.a < 0.05) discard;\n"
" gl_FragColor = color * vColor;\n"
"}";
ob.program().build(vertShader, fragShader)
<< uMvpMatrix
<< uColor << uTime
<< uTex;
cv.renderTarget().setClearColor(Vector4f(.2f, .2f, .2f, 0));
}
void canvasGLResized(Canvas &cv)
{
LOG_DEBUG("GLResized: %i x %i") << cv.width() << cv.height();
GLState &st = GLState::top();
//st.setViewport(Rectangleui::fromSize(cv.size()));
st.setViewport(Rectangleui(0, 0, cv.width(), cv.height()));
/*uMvpMatrix = Matrix4f::ortho(-cv.width()/2, cv.width()/2,
-cv.height()/2, cv.height()/2)
* Matrix4f::scale(cv.height()/450.f)
* Matrix4f::translate(Vector2f(-200, -200));*/
projMatrix = Matrix4f::perspective(40, float(cv.width())/float(cv.height())) *
Matrix4f::lookAt(Vector3f(), Vector3f(0, 0, -5), Vector3f(0, -1, 0));
}
void draw(Canvas &)
{
// First render the frame to the texture.
GLState &frameState = GLState::push();
frameState.setTarget(*frameTarget.get());
frameState.setViewport(Rectangleui::fromSize(frameTex.size()));
drawFrame();
GLState::pop();
// Render normally.
drawFrame();
}
void drawFrame()
{
GLState::top().target().clear(GLTarget::Color | GLTarget::Depth);
// The left cube.
uTex = testpic;
uMvpMatrix = projMatrix *
Matrix4f::translate(Vector3f(-1.5f, 0, 0)) *
modelMatrix;
ob.draw();
// The right cube.
uTex = frameTex;
uMvpMatrix = projMatrix *
Matrix4f::translate(Vector3f(1.5f, 0, 0)) *
modelMatrix;
ob.draw();
}
void timeChanged(Clock const &clock)
{
if(!startedAt.isValid())
{
startedAt = clock.time();
}
uTime = startedAt.since();
modelMatrix = Matrix4f::rotate(std::cos(uTime.toFloat()/2) * 45, Vector3f(1, 0, 0)) *
Matrix4f::rotate(std::sin(uTime.toFloat()/3) * 60, Vector3f(0, 1, 0));
self.update();
}
};
TestWindow::TestWindow() : d(new Instance(this))
{
setWindowTitle("libgui GL Sandbox");
setMinimumSize(640, 480);
}
void TestWindow::canvasGLDraw(Canvas &canvas)
{
d->draw(canvas);
canvas.swapBuffers();
CanvasWindow::canvasGLDraw(canvas);
}