-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
336 lines (253 loc) · 11.2 KB
/
main.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <iostream>
#include <sstream>
#include <glbinding/gl/gl.h>
#include <globjects/Buffer.h>
#include <globjects/Error.h>
#include <globjects/Program.h>
#include <globjects/Shader.h>
#include <globjects/Texture.h>
#include <globjects/VertexArray.h>
#include <globjects/VertexAttributeBinding.h>
#include <globjects/base/File.h>
#include <globjects/base/StaticStringSource.h>
#include <globjects/globjects.h>
#include <glm/ext/matrix_clip_space.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <glm/mat4x4.hpp>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/Window.hpp>
#ifdef WIN32
using namespace gl;
#endif
int main()
{
sf::ContextSettings settings;
settings.depthBits = 24;
settings.stencilBits = 8;
settings.antialiasingLevel = 4;
settings.majorVersion = 3;
settings.minorVersion = 2;
settings.attributeFlags = sf::ContextSettings::Attribute::Core;
#ifdef SYSTEM_DARWIN
auto videoMode = sf::VideoMode(2048, 1536);
#else
auto videoMode = sf::VideoMode(1024, 768);
#endif
sf::Window window(videoMode, "Hello Lights!", sf::Style::Default, settings);
globjects::init([](const char* name) {
return sf::Context::getFunction(name);
});
globjects::DebugMessage::enable(); // enable automatic messages if KHR_debug is available
globjects::DebugMessage::setCallback([](const globjects::DebugMessage& message) {
std::cout << "[DEBUG] " << message.message() << std::endl;
});
std::cout << "[INFO] Initializing..." << std::endl;
std::cout << "[INFO] Creating shaders..." << std::endl;
std::cout << "[INFO] Compiling vertex shader...";
auto vertexShaderSource = globjects::Shader::sourceFromFile("media/vertex.glsl");
auto vertexShaderTemplate = globjects::Shader::applyGlobalReplacements(vertexShaderSource.get());
auto vertexShader = globjects::Shader::create(static_cast<gl::GLenum>(GL_VERTEX_SHADER), vertexShaderTemplate.get());
if (!vertexShader->compile())
{
std::cerr << "[ERROR] Can not compile vertex shader" << std::endl;
return 1;
}
std::cout << "done" << std::endl;
std::cout << "[INFO] Compiling fragment shader...";
auto fragmentShaderSource = globjects::Shader::sourceFromFile("media/fragment.glsl");
auto fragmentShaderTemplate = globjects::Shader::applyGlobalReplacements(fragmentShaderSource.get());
auto fragmentShader = globjects::Shader::create(static_cast<gl::GLenum>(GL_FRAGMENT_SHADER), fragmentShaderTemplate.get());
if (!fragmentShader->compile())
{
std::cerr << "[ERROR] Can not compile fragment shader" << std::endl;
return 1;
}
std::cout << "done" << std::endl;
std::cout << "[INFO] Linking shader programs...";
auto renderProgram = globjects::Program::create();
renderProgram->attach(vertexShader.get(), fragmentShader.get());
std::cout << "done" << std::endl;
std::cout << "[INFO] Creating buffer objects...";
auto meshVertexBuffer = globjects::Buffer::create();
meshVertexBuffer->setData(
std::array<glm::vec3, 8> {
{
glm::vec3(0, 0, 0),
glm::vec3(0, 1, 0),
glm::vec3(1, 1, 0),
glm::vec3(1, 0, 0),
glm::vec3(0, 0, 1),
glm::vec3(0, 1, 1),
glm::vec3(1, 1, 1),
glm::vec3(1, 0, 1),
} },
static_cast<gl::GLenum>(GL_STATIC_DRAW));
auto meshVertexUVsBuffer = globjects::Buffer::create();
meshVertexUVsBuffer->setData(
std::array<glm::vec2, 8> {
{
glm::vec2(0, 0),
glm::vec2(0, 1),
glm::vec2(1, 1),
glm::vec2(1, 0),
glm::vec2(0, 0),
glm::vec2(0, 1),
glm::vec2(1, 1),
glm::vec2(1, 0),
} },
static_cast<gl::GLenum>(GL_STATIC_DRAW));
auto meshVertexNormalsBuffer = globjects::Buffer::create();
meshVertexNormalsBuffer->setData(
std::array<glm::vec3, 8> {
{
glm::vec3(-0.5, -0.5, -0.5),
glm::vec3(-0.5, 0.5, -0.5),
glm::vec3(0.5, 0.5, -0.5),
glm::vec3(0.5, -0.5, -0.5),
glm::vec3(-0.5, -0.5, 0.5),
glm::vec3(-0.5, 0.5, 0.5),
glm::vec3(0.5, 0.5, 0.5),
glm::vec3(0.5, -0.5, 0.5),
} },
static_cast<gl::GLenum>(GL_STATIC_DRAW));
auto meshIndexBuffer = globjects::Buffer::create();
const auto numFaces = 12;
meshIndexBuffer->setData(
std::array<std::array<GLuint, 3>, numFaces> {
{
{ 0, 1, 2 }, // back
{ 2, 3, 0 },
{ 4, 5, 6 }, // front
{ 6, 7, 4 },
{ 0, 1, 5 }, // left
{ 5, 4, 0 },
{ 3, 2, 6 }, // right
{ 6, 7, 3 },
{ 1, 5, 6 }, // top
{ 6, 2, 1 },
{ 0, 3, 7 }, // bottom
{ 7, 4, 0 },
} },
static_cast<gl::GLenum>(GL_STATIC_DRAW));
auto vao = globjects::VertexArray::create();
vao->bindElementBuffer(meshIndexBuffer.get());
vao->binding(0)->setAttribute(0);
vao->binding(0)->setBuffer(meshVertexBuffer.get(), 0, sizeof(glm::vec3)); // number of elements in buffer, stride, size of buffer element
vao->binding(0)->setFormat(3, static_cast<gl::GLenum>(GL_FLOAT)); // number of data elements per buffer element (vertex), type of data
vao->enable(0);
vao->binding(1)->setAttribute(1);
vao->binding(1)->setBuffer(meshVertexUVsBuffer.get(), 0, sizeof(glm::vec2)); // number of elements in buffer, stride, size of buffer element
vao->binding(1)->setFormat(2, static_cast<gl::GLenum>(GL_FLOAT)); // number of data elements per buffer element (vertex), type of data
vao->enable(1);
vao->binding(2)->setAttribute(2);
vao->binding(2)->setBuffer(meshVertexNormalsBuffer.get(), 0, sizeof(glm::vec3)); // number of elements in buffer, stride, size of buffer element
vao->binding(2)->setFormat(3, static_cast<gl::GLenum>(GL_FLOAT)); // number of data elements per buffer element (vertex), type of data
vao->enable(2);
std::cout << "done" << std::endl;
std::cout << "[INFO] Loading texture...";
sf::Image textureImage;
if (!textureImage.loadFromFile("media/texture.jpg"))
{
std::cerr << "[ERROR] Can not load texture" << std::endl;
return 1;
}
textureImage.flipVertically();
auto texture = globjects::Texture::create(static_cast<gl::GLenum>(GL_TEXTURE_2D));
texture->setParameter(static_cast<gl::GLenum>(GL_TEXTURE_MIN_FILTER), static_cast<GLint>(GL_LINEAR));
texture->setParameter(static_cast<gl::GLenum>(GL_TEXTURE_MAG_FILTER), static_cast<GLint>(GL_LINEAR));
texture->image2D(
0,
static_cast<gl::GLenum>(GL_RGBA8),
glm::vec2(textureImage.getSize().x, textureImage.getSize().y),
0,
static_cast<gl::GLenum>(GL_RGBA),
static_cast<gl::GLenum>(GL_UNSIGNED_BYTE),
reinterpret_cast<const gl::GLvoid*>(textureImage.getPixelsPtr()));
renderProgram->setUniform("textureSampler", 0);
std::cout << "done" << std::endl;
std::cout << "[INFO] Done initializing" << std::endl;
const float fov = 45.0f;
const float cameraMoveSpeed = 1.0f;
const float cameraRotateSpeed = 10.0f;
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 cameraRight = glm::vec3(1.0f, 0.0f, 0.0f);
glm::vec3 cameraForward = glm::normalize(glm::cross(cameraUp, cameraRight));
sf::Clock clock;
glEnable(static_cast<gl::GLenum>(GL_DEPTH_TEST));
while (window.isOpen())
{
#ifdef WIN32
if (!window.hasFocus())
{
continue;
}
#endif
sf::Event event {};
// measure time since last frame, in seconds
float deltaTime = static_cast<float>(clock.restart().asSeconds());
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
break;
}
}
glm::vec2 currentMousePos = glm::vec2(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y);
glm::vec2 mouseDelta = currentMousePos - glm::vec2((window.getSize().x / 2), (window.getSize().y / 2));
sf::Mouse::setPosition(sf::Vector2<int>(window.getSize().x / 2, window.getSize().y / 2), window);
float horizontalAngle = (mouseDelta.x / static_cast<float>(window.getSize().x)) * -1 * deltaTime * cameraRotateSpeed * fov;
float verticalAngle = (mouseDelta.y / static_cast<float>(window.getSize().y)) * -1 * deltaTime * cameraRotateSpeed * fov;
cameraForward = glm::rotate(cameraForward, horizontalAngle, cameraUp);
cameraForward = glm::rotate(cameraForward, verticalAngle, cameraRight);
cameraRight = glm::normalize(glm::rotate(cameraRight, horizontalAngle, cameraUp));
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
cameraPos += cameraForward * cameraMoveSpeed * deltaTime;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
cameraPos -= cameraForward * cameraMoveSpeed * deltaTime;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
cameraPos -= glm::normalize(glm::cross(cameraForward, cameraUp)) * cameraMoveSpeed * deltaTime;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
cameraPos += glm::normalize(glm::cross(cameraForward, cameraUp)) * cameraMoveSpeed * deltaTime;
}
glm::mat4 projection = glm::perspective(glm::radians(fov), (float) window.getSize().x / (float) window.getSize().y, 0.1f, 100.0f);
glm::mat4 view = glm::lookAt(
cameraPos,
cameraPos + cameraForward,
cameraUp);
glm::mat4 model = glm::mat4(1.0f); // identity
renderProgram->setUniform("model", model);
renderProgram->setUniform("view", view);
renderProgram->setUniform("projection", projection);
renderProgram->setUniform("lightPosition", glm::vec3(-2, 2, 2));
renderProgram->setUniform("lightColor", glm::vec3(1, 0.5, 0.5));
renderProgram->setUniform("ambientColor", glm::vec3(0, 0, 0));
renderProgram->setUniform("materialSpecular", 12.0f);
renderProgram->setUniform("cameraPosition", cameraPos);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
::glViewport(0, 0, static_cast<GLsizei>(window.getSize().x), static_cast<GLsizei>(window.getSize().y));
vao->bind();
texture->bind();
renderProgram->use();
// number of values passed = number of elements * number of vertices per element
// in this case: 2 triangles, 3 vertex indexes per triangle
vao->drawElements(static_cast<gl::GLenum>(GL_TRIANGLES), numFaces * 3, static_cast<gl::GLenum>(GL_UNSIGNED_INT), nullptr);
renderProgram->release();
vao->unbind();
texture->unbind();
window.display();
}
return 0;
}