-
Notifications
You must be signed in to change notification settings - Fork 6
/
dialog.cpp
184 lines (137 loc) · 4.72 KB
/
dialog.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
#include "dialog.h"
#include "ui_dialog.h"
#include "dialogeventhandlers.h"
#include <array>
#include <QTimer>
#include <QOpenGLFunctions>
#include <QWidget>
#include <QQuaternion>
#include <QOpenGLTexture>
#include <QImage>
#include <QDebug>
#include <glm/glm.hpp>
constexpr float CAMERA_SPEED = 3.0f;
Dialog::Dialog(QWidget *parent)
: QOpenGLWidget(parent)
, ui(new Ui::Dialog)
, m_view()
, m_cubeIndices(QOpenGLBuffer::IndexBuffer)
, m_sphere(100, 100)
, m_eventHandler(new EventHandler::NoopEventHandler())
{
//make the window appear
ui->setupUi(this);
//setup timer
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(nextFrame()));
m_timer->start(1000 / 60);
}
Dialog::~Dialog()
{
delete ui;
delete m_timer;
}
// Use C++11 raw string literals for GLSL shader source code
static const char *vertexShaderSourceCore = R"(
#version 120
uniform mat4 projMatrix;
uniform mat4 mvMatrix;
attribute vec4 vertex;
attribute vec2 in_texcoord;
varying vec2 v_texcoord;
void main() {
gl_Position = projMatrix * mvMatrix * vertex;
v_texcoord = in_texcoord;
}
)";
static const char *fragmentShaderSourceCore = R"(
#version 120
uniform sampler2D texture;
varying vec2 v_texcoord;
void main() {
gl_FragColor = texture2D(texture, v_texcoord);
}
)";
bool Dialog::event(QEvent* event)
{
return m_eventHandler->handleEvent(event) || QOpenGLWidget::event(event);
}
void Dialog::initializeGL()
{
m_view.setToIdentity();
m_view.translate(0, 0, -3);
glClearColor(0, 0, 0, 1);
m_program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSourceCore);
m_program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSourceCore);
m_program.link();
m_program.bind();
m_vao.create();
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
const std::vector<VertexData>& vertices = m_sphere.getVertexData();
const std::vector<unsigned short>& vertexIndices = m_sphere.getIndices();
static_assert(sizeof(VertexData) == 5*sizeof(float), "VertexData layout not as expected");
// Setup our vertex buffer object.
m_cubeVertices.create();
m_cubeVertices.bind();
m_cubeVertices.allocate(vertices.data(), vertices.size() * sizeof(VertexData));
m_cubeIndices.create();
m_cubeIndices.bind();
m_cubeIndices.allocate(vertexIndices.data(), vertexIndices.size() * sizeof(GLushort));
// Tell OpenGL programmable pipeline how to locate vertex position data
m_program.enableAttributeArray("vertex");
m_program.setAttributeBuffer("vertex", GL_FLOAT, 0, 3, sizeof(VertexData));
m_program.enableAttributeArray("in_texcoord");
m_program.setAttributeBuffer("in_texcoord", GL_FLOAT, sizeof(glm::vec3), 2, sizeof(VertexData));
m_program.release();
// Load textures
m_texture = new QOpenGLTexture(QImage("./map1.png").mirrored(true, true));
m_texture->setMinificationFilter(QOpenGLTexture::Nearest);
m_texture->setMagnificationFilter(QOpenGLTexture::Linear);
// Add renderer event chain
m_eventHandler.reset(new KeyEventHandler(*this));
m_eventHandler->chain(std::make_shared<KeyEventPan>(*this))
->chain(std::make_shared<MouseWheelEventZoom>(*this));
}
void Dialog::paintGL()
{
m_proj.setToIdentity();
m_proj.perspective(45.0f, GLfloat(this->width()) / this->height(), 0.01f, 100.0f);
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
f->glEnable(GL_DEPTH_TEST);
f->glEnable(GL_CULL_FACE);
m_vao.bind();
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
m_program.bind();
m_program.setUniformValue("projMatrix", m_proj);
m_program.setUniformValue("mvMatrix", m_view * m_model);
m_texture->bind();
// Texture unit 0
m_program.setUniformValue("texture", 0);
// Draw cube geometry using indices from VBO 1
f->glDrawElements(GL_TRIANGLES, m_sphere.getIndices().size(), GL_UNSIGNED_SHORT, 0);
m_program.release();
}
void Dialog::nextFrame()
{
// Removing this condition causes the model to shrink
if (m_cameraVelocity.x() != 0.0f)
{
m_model.rotate(CAMERA_SPEED, 0.0f, m_cameraVelocity.x(), 0.0f);
}
if (m_cameraVelocity.y() != 0.0f)
{
QMatrix4x4 rot;
rot.rotate(CAMERA_SPEED, -m_cameraVelocity.y(), 0.0f, 0.0f);
m_model = rot * m_model;
}
// QVector3D axis(-m_cameraVelocity.y(), m_cameraVelocity.x(), 0.0f);
// if (!axis.isNull())
// {
// QMatrix4x4 rot;
// rot.rotate(CAMERA_SPEED, axis);
// m_model = rot * m_model;
// }
//update the window (this will trigger paintEvent)
update();
}