-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
331 lines (261 loc) · 8.84 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
/*
* main.cpp
*
* Created on: 03.11.2017
* Author: kupperp1
*/
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <string>
#include <iostream>
#include <sstream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "shader.h"
//int WIDTH = 1920;
//int HEIGHT = 1080;
int WIDTH = 1024;
int HEIGHT = 720;
double upsGoal = 16.0f;
bool playing = true;
bool dragging = false;
float mousePosX;
float mousePosY;
float dragStartX;
float dragStartY;
float offsetX = 10.0f;
float offsetY = 10.0f;
float scale = 1.0f;
void glfwErrorCallback(int error, const char* description)
{
std::cerr << "Error: " << description << std::endl;
}
void glfwFramebufferSizeCallback(GLFWwindow* window, int width, int height)
{
WIDTH = width;
HEIGHT = height;
}
void checkGlError() {
GLenum err (glGetError());
while(err!=GL_NO_ERROR) {
std::string error;
switch(err) {
case GL_INVALID_OPERATION: error="INVALID_OPERATION"; break;
case GL_INVALID_ENUM: error="INVALID_ENUM"; break;
case GL_INVALID_VALUE: error="INVALID_VALUE"; break;
case GL_OUT_OF_MEMORY: error="OUT_OF_MEMORY"; break;
case GL_INVALID_FRAMEBUFFER_OPERATION: error="INVALID_FRAMEBUFFER_OPERATION"; break;
}
std::cerr << "OpenGL error: " << error << std::endl;
err=glGetError();
}
}
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods){
if (action == GLFW_PRESS){
switch(key){
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, true);
break;
case GLFW_KEY_PERIOD:
if(upsGoal < 65536.0)upsGoal *= 2.0;
break;
case GLFW_KEY_COMMA:
if(upsGoal > 1)upsGoal /= 2.0;
break;
case GLFW_KEY_SPACE:
playing = !playing;
break;
}
}else{
}
}
void scrollCallback(GLFWwindow* window, double xoffset, double yoffset){
float factor = pow(1.1f, yoffset);
scale *= factor;
float distX = mousePosX - offsetX;
float distY = mousePosY - offsetY;
offsetX -= factor * distX - distX;
offsetY -= factor * distY - distY;
}
void cursorPositionCallback(GLFWwindow* window, double xpos, double ypos){
mousePosX = xpos;
mousePosY = ypos;
}
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods){
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS){
dragging = true;
dragStartX = mousePosX;
dragStartY = mousePosY;
}
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE){
offsetX = offsetX + (mousePosX - dragStartX);
offsetY = offsetY + (mousePosY - dragStartY);
dragging = false;
}
}
int main(int argc, const char* argv[]){
std::string imagePath;
for(int i = 0; i < argc - 1; i++){
if(strcmp(argv[i], "-img") == 0 || strcmp(argv[i], "-i") == 0){
imagePath = std::string(argv[i+1]);
}
if(strcmp(argv[i], "-ups") == 0 || strcmp(argv[i], "-u") == 0){
upsGoal = std::stof(argv[i+1]);
}
}
if (!glfwInit())
{
std::cerr << "GLFW init failed!" << std::endl;
return 1;
}
glfwSetErrorCallback(glfwErrorCallback);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Conways Game of Life", nullptr, nullptr);
if (!window)
{
std::cerr << "Creating window failed!" << std::endl;
return 1;
}
glfwMakeContextCurrent(window);
GLenum error = glewInit();
if(error != GLEW_OK){
std::cerr << "Failed to init glew!" << std::endl;
return 1;
}
glfwSetFramebufferSizeCallback(window, glfwFramebufferSizeCallback);
glfwSetKeyCallback(window, keyCallback);
glfwSetCursorPosCallback(window, cursorPositionCallback);
glfwSetMouseButtonCallback(window, mouseButtonCallback);
glfwSetScrollCallback(window, scrollCallback);
glfwSwapInterval(1);
Shader screenShader("res/screen.vert", "res/screen.frag");
screenShader.bind();
screenShader.addUniform("tex");
screenShader.setUniform("tex", 0);
screenShader.addUniform("transform");
screenShader.addUniform("size");
screenShader.addUniform("scale");
screenShader.addUniform("mousePos");
Shader conwayShader("res/conway_step.vert", "res/conway_step.frag");
conwayShader.bind();
conwayShader.addUniform("previous");
conwayShader.setUniform("previous", 0);
conwayShader.addUniform("size");
int width, height, channels;
unsigned char* image = stbi_load(imagePath.c_str(), &width, &height, &channels, STBI_rgb_alpha);
GLuint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
GLuint buffers[2];
glGenTextures(2, buffers);
glBindTexture(GL_TEXTURE_2D, buffers[0]);
if(channels == 4){
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
}else if(channels == 3){
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
}else{
std::cerr << "unsupported image format" << std::endl;
return 1;
}
GLenum edgeMode;
//edgeMode = GL_CLAMP_TO_BORDER;
edgeMode = GL_REPEAT;
float border[] = {0.0f, 0.0f, 0.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, edgeMode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, edgeMode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
stbi_image_free(image);
glBindTexture(GL_TEXTURE_2D, buffers[1]);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, edgeMode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, edgeMode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
float screenQuad[] = {
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};
GLuint vao;
glGenVertexArrays(1, &vao);
GLuint vbo;
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(screenQuad), screenQuad, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (int*)0);
glBindVertexArray(0);
checkGlError();
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glDisable(GL_CULL_FACE);
//glCullFace(GL_BACK);
int drawBuffer = 1;
int frames = 0;
int updates = 0;
double lastUpdate = glfwGetTime();
double lastSecond = glfwGetTime();
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(vao);
double now = glfwGetTime();
conwayShader.bind();
conwayShader.setUniform("size", (float)width, (float)height);
glViewport(0, 0, width, height);
while(now - lastUpdate >= 1.0 / upsGoal){
lastUpdate += 1.0 / upsGoal;
if(playing){
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, buffers[1-drawBuffer]);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, buffers[drawBuffer], 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
drawBuffer = 1 - drawBuffer;
}
updates++;
}
if(now - lastSecond >= 1.0){
std::cout << "Conways Game of Life - " << frames << "FPS, " << updates << "UPS" << std::endl;
frames = 0;
updates = 0;
lastSecond += 1.0;
}
screenShader.bind();
screenShader.setUniform("size", (float)width, (float)height);
float scaledWidth = (float)width * scale;
float scaledHeight = (float)height * scale;
glm::vec2 offset = glm::vec2(dragging ? offsetX + (mousePosX - dragStartX): offsetX, dragging ? offsetY + (mousePosY - dragStartY) : offsetY);
glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(offset, 0.0f));
model = glm::scale(model, glm::vec3(scaledWidth, scaledHeight, 1.0f));
glm::mat4 projection = glm::ortho(0.0f, (float)WIDTH, (float)HEIGHT, 0.0f, -1.0f, 1.0f);
glm::mat4 transform = projection * model;
screenShader.setUniform("transform", transform);
screenShader.setUniform("scale", 0.5f / scale);
screenShader.setUniform("mousePos", (glm::vec2((float)mousePosX, (float)mousePosY) - offset) / scale);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, buffers[drawBuffer]);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, WIDTH, HEIGHT);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
frames++;
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}