-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
148 lines (103 loc) · 3.37 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
#include <iostream>
#include <cstdlib>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "shader_util.h"
#include "pingpong.h"
GLuint createDisplayQuad(GLuint& vbo, GLuint& ebo);
void error_callback(GLint error, const GLchar* description);
int main()
{
constexpr GLuint WIDTH = 512, HEIGHT = 512;
glfwSetErrorCallback(error_callback);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Belousov Zhabotinksy",
nullptr, nullptr);
if(!window)
{
std::cerr << "GLFW error: can't create window.\n";
return 1;
}
glfwMakeContextCurrent(window);
GLenum status = glewInit();
if(status != GLEW_OK)
{
std::cerr << "GLEW error: " << glewGetErrorString(status) << "\n";
return 1;
}
PingPong pingPong(512, 512);
GLuint shaderProgram = linkShaderProgram("vertex_shader.glsl",
"fragment_shader.glsl");
GLuint vao, vbo, ebo;
vao = createDisplayQuad(vbo, ebo);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
GLfloat currentTime = 0.0f;
GLfloat oldTime = 0.0f;
GLfloat elapsedTime = 0.0f;
while(!glfwWindowShouldClose(window))
{
currentTime = (GLfloat)glfwGetTime();
elapsedTime += currentTime - oldTime;
oldTime = currentTime;
if(elapsedTime > 0.0f)
{
pingPong.update(vao);
elapsedTime = 0.0f;
}
glfwPollEvents();
GLuint front = pingPong.getFront();
glViewport(0, 0, WIDTH, HEIGHT);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, front);
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (GLvoid*)0);
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
GLuint createDisplayQuad(GLuint& vbo, GLuint& ebo)
{
GLfloat vertices[] =
{
-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, 1.0f,
};
GLuint elements[] =
{
0, 1, 2,
1, 3, 2,
};
GLuint vao;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat),
(GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat),
(GLvoid*)(2 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
return vao;
}
void error_callback(GLint error, const GLchar* description)
{
std::cerr << "GLFW error " << error << ": " << description << "\n";
exit(1);
}