-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderer.cpp
203 lines (159 loc) · 6 KB
/
Renderer.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
#include "Renderer.h"
#include <QApplication>
#include <QFile>
#include <QTimer>
// float clamp(float value, float lower, float upper) {
// return std::min(std::max(value, lower), upper);
// }
Renderer::Renderer(QWidget *parent) : QOpenGLWidget(parent)
{
// similar to GLFW's window hints, we select a profile and set
// properties on the default surface that opengl will draw to
QSurfaceFormat format = QSurfaceFormat::defaultFormat();
// format.setVersion(4, 1);
format.setSamples(4);
format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setSwapInterval(0);
setFormat(format);
// QTimer *timer = new QTimer(this);
// connect(timer, SIGNAL(timeout()), this, SLOT(update()));
// timer->start(1000.f / 60.f);
// setAcceptDrops(true);
// connect(this, SIGNAL(frameSwapped()), this, SLOT(update()));
// initShaders();
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000.f / 60.f);
_elapsedTimer.start();
}
Renderer::~Renderer()
{
}
void Renderer::recompileFragShader(QString slot)
{
QFile shaderFile("frag.shader");
shaderFile.open(QFile::ReadOnly);
QString shaderBasePlusSlot = QString(shaderFile.readAll()).arg(slot);
// qDebug() << shaderBasePlusSlot;
// m_program->release();
m_program->removeShader(frag);
// delete frag;
frag = new QOpenGLShader(QOpenGLShader::Fragment);
frag->compileSourceCode(shaderBasePlusSlot);
m_program->addShader(frag);
// m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, shaderBasePlusSlot);
// m_program->link();
// m_program->bind();
}
void Renderer::update()
{
// Schedule a redraw
QOpenGLWidget::update();
}
void Renderer::initializeGL()
{
initializeOpenGLFunctions(); // obvious
const float quadVertices[] = { // vertex attributes for a quad that fills the entire screen in Normalized Device Coordinates.
// positions // texCoords
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
static const char *vshader =
"#version 330 core\n"
"layout(location=0) in vec2 posAttr;\n"
"layout(location=1) in vec2 texCoord;\n"
"out vec2 TexCoord;\n"
"void main() {\n"
"TexCoord = texCoord;\n"
"gl_Position = vec4(posAttr, 0.0, 1.0); }";
static const char *fshader =
"#version 330 core\n"
"out vec4 col;\n"
"uniform vec2 iResolution;\n"
"uniform float u_time;\n"
"in vec2 TexCoord;\n"
"void main() {\n"
"vec2 st = gl_FragCoord.xy / iResolution.xy;\n"
"vec3 color = vec3(0.);\n"
"color = vec3(st.x,st.y,abs(sin(u_time)));\n"
"col = vec4(color, 1.0); }";
// Create Shader (Do not release until VAO is created)
frag = new QOpenGLShader(QOpenGLShader::Fragment);
frag->compileSourceFile(":/default_frag.shader");
m_program = new QOpenGLShaderProgram();
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vshader);
// m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fshader);
// m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/frag.shader");
m_program->addShader(frag);
m_program->link();
m_program->bind();
iResolution = m_program->uniformLocation("iResolution");
u_time = m_program->uniformLocation("u_time");
vao = new QOpenGLVertexArrayObject;
vao->create();
vao->bind();
vertices = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
vertices->create();
vertices->setUsagePattern(QOpenGLBuffer::StaticDraw);
vertices->bind();
vertices->allocate(quadVertices, sizeof(quadVertices) * sizeof (GLfloat));
vao->release();
}
void Renderer::paintGL()
{
float timeElapsed = _elapsedTimer.nsecsElapsed() / (1000.f * 1000.f * 1000.f);
// restart this timer to get delta time!
// _elapsedTimer.restart();
// glClearColor(0.3, 0.5, 0.9, 1);
glClear(GL_COLOR_BUFFER_BIT);
// On 4k so need to scale to the monitor DPI
QVector2D paneSize(width() * devicePixelRatio(), height() * devicePixelRatio());
glViewport(0, 0, paneSize.x(), paneSize.y());
vao->bind();
m_program->bind();
m_program->setUniformValue(iResolution, paneSize);
m_program->setUniformValue(u_time, timeElapsed);
m_program->bindAttributeLocation("posAttr", 0);
m_program->enableAttributeArray(0);
m_program->setAttributeBuffer(0, GL_FLOAT, 0, 4);
m_program->bindAttributeLocation("texCoord", 1);
m_program->enableAttributeArray(1);
m_program->setAttributeBuffer(1, GL_UNSIGNED_BYTE, 2, 4);
glDrawArrays(GL_TRIANGLES, 0, 6);
m_program->release();
vao->release();
}
void Renderer::resizeGL(int w, int h)
{
QVector2D paneSize(width() * devicePixelRatio(), height() * devicePixelRatio());
glViewport(0, 0, paneSize.x(), paneSize.y());
update();
}
void Renderer::initShaders()
{
// m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vertex.shader");
// m_program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/frag.shader");
// m_program.link();
// m_program.bind();
// m_vertex.create();
// m_vertex.bind();
// m_vertex.setUsagePattern(QOpenGLBuffer::StaticDraw);
// m_vertex.allocate(quadVertices, sizeof(quadVertices));
// // Create Vertex Array Object
// m_object.create();
// m_object.bind();
// m_program.enableAttributeArray(0);
//// m_program.enableAttributeArray(1);
// m_program.setAttributeBuffer(0, GL_FLOAT, 0, 3, 3);
//// m_program.setAttributeBuffer(1, GL_FLOAT, Vertex::colorOffset(), Vertex::ColorTupleSize, Vertex::stride());
// // Release (unbind) all
// m_object.release();
// m_vertex.release();
// m_program.release();
}