-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline.cpp
296 lines (239 loc) · 8.12 KB
/
line.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
#include <GL/glew.h>
#include <cmath>
#include <glfwpp/glfwpp.h>
#include <iostream>
#include <string>
#include <vector>
#include <chrono>
const int lineNum = 3300;
const int lineSize = 2000;
std::vector<float> vertices(lineNum *lineSize * 2);
std::vector<char> colors(lineNum *lineSize * 3);
std::chrono::high_resolution_clock timer;
std::chrono::nanoseconds elapsed(0);
int fps = 0;
void initVertices(std::vector<float> &vertices)
{
for (int i = 0; i < lineNum; i++)
{
for (int j = 0; j < lineSize; j++)
{
const float x = 2 * (float)j / (float)lineSize - 1.0f;
vertices[(i * lineSize + j) * 2] = x;
vertices[(i * lineSize + j) * 2 + 1] = 0.0;
}
}
}
void initColors(std::vector<char> &colors)
{
for (int i = 0; i < lineNum; i++)
{
const float r = std::rand() / (float)RAND_MAX;
const float g = std::rand() / (float)RAND_MAX;
const float b = std::rand() / (float)RAND_MAX;
for (int j = 0; j < lineSize; j++)
{
colors[(i * lineSize + j) * 3] = r * 255.0f;
colors[(i * lineSize + j) * 3 + 1] = g * 255.0f;
colors[(i * lineSize + j) * 3 + 2] = b * 255.0f;
}
}
}
void updateVertices(std::vector<float> &vertices, float phase = 0.0f)
{
for (int i = 0; i < lineNum; i++)
{
const float y0 = (float)i / (float)lineNum + phase * 0.1f;
for (int j = 0; j < lineSize; j++)
{
const float y = y0 + j * 0.1 / (float)lineSize;
const float yy = y - std::lroundf(y);
vertices[(i * lineSize + j) * 2 + 1] = 2 * yy;
}
}
}
void printVertices(const std::vector<float> &vertices)
{
for (std::vector<float>::size_type i = 0; i < vertices.size(); i += 2)
{
std::cout << vertices[i] << ", " << vertices[i + 1] << std::endl;
}
}
void printColors(const std::vector<float> &colors)
{
for (std::vector<float>::size_type i = 0; i < colors.size(); i += 3)
{
std::cout << colors[i] << ", " << colors[i + 1] << ", " << colors[i + 2] << std::endl;
}
}
void onResize([[maybe_unused]] GLFWwindow *window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main()
{
std::cout << "Hello, GLFWPP!" << std::endl;
glfw::GlfwLibrary library = glfw::init();
glfw::WindowHints hints;
hints.clientApi = glfw::ClientApi::OpenGl;
hints.contextVersionMajor = 4;
hints.contextVersionMinor = 6;
hints.apply();
// Or with C++20:
// glfw::WindowHints{
// .clientApi = glfw::ClientApi::OpenGl,
// .contextVersionMajor = 4,
// .contextVersionMinor = 6}
// .apply();
glfw::Window wnd(1200, 800, "Line Example");
glEnable(GL_DEBUG_OUTPUT);
GLenum error = glGetError();
if (wnd == nullptr)
{
std::cout << "Failed to create GLFW window" << std::endl;
// glfw::terminate();
return -1;
}
glfw::makeContextCurrent(wnd);
glfw::swapInterval(1);
std::cout << "GLFW version: " << glfw::getVersionString() << std::endl;
std::cout << "GLFW version: " << glGetString(GL_VERSION) << std::endl;
if (glewInit() != GLEW_OK)
{
throw std::runtime_error("Could not initialize GLEW");
}
const std::string vertexShaderSource = R"(
#version 330 core
layout (location = 1) in vec2 aPos;
layout (location = 2) in vec3 aColor;
out vec3 vColor;
void main()
{
vColor = aColor;
gl_Position = vec4(aPos.x, aPos.y, 0, 1.0);
vColor = aColor/ vec3(255.0, 255.0, 255.0);
}
)";
const std::string fragmentShaderSource = R"(
#version 330 core
in vec3 vColor;
out vec4 FragColor;
void main()
{
FragColor = vec4(vColor, 0.7);
}
)";
const auto vertexShader = glCreateShader(GL_VERTEX_SHADER);
const GLchar *vertexShaderSourcePtr = vertexShaderSource.c_str();
glShaderSource(vertexShader, 1, &vertexShaderSourcePtr, nullptr);
glCompileShader(vertexShader);
GLint success;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar infoLog[512];
glGetShaderInfoLog(vertexShader, sizeof(infoLog), nullptr, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n"
<< infoLog << std::endl;
}
const auto fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar *fragmentShaderSourcePtr = fragmentShaderSource.c_str();
glShaderSource(fragmentShader, 1, &fragmentShaderSourcePtr, nullptr);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar infoLog[512];
glGetShaderInfoLog(fragmentShader, sizeof(infoLog), nullptr, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n"
<< infoLog << std::endl;
}
const auto shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// Check for link errors
int success2;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success2);
if (!success2)
{
char infoLog[512];
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::PROGRAM::LINKING_FAILED\n"
<< infoLog << std::endl;
}
[[maybe_unused]] const auto VAO = []()
{
GLuint VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
return VAO;
}();
const auto VBO = []()
{
GLuint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
return VBO;
}();
const auto CBO = []()
{
GLuint CBO;
glGenBuffers(1, &CBO);
glBindBuffer(GL_ARRAY_BUFFER, CBO);
return CBO;
}();
// printVertices(vertices);
initColors(colors);
// printColors(colors);
glBindBuffer(GL_ARRAY_BUFFER, CBO);
glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(char), colors.data(), GL_STATIC_DRAW);
auto colorAttribute = glGetAttribLocation(shaderProgram, "aColor");
glEnableVertexAttribArray(colorAttribute);
glVertexAttribPointer(colorAttribute, 3, GL_UNSIGNED_BYTE, GL_FALSE, 0, (void *)0);
// Position
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
auto positionAttribute = glGetAttribLocation(shaderProgram, "aPos");
glEnableVertexAttribArray(positionAttribute);
glVertexAttribPointer(positionAttribute, 2, GL_FLOAT, GL_FALSE, 0, (void *)0);
// setup
glUseProgram(shaderProgram);
const auto bufferSize = vertices.size() * sizeof(float);
std::cout << "Buffer size: " << bufferSize << std::endl;
std::cout << "Here!" << std::endl;
error = glGetError();
while (error != GL_NO_ERROR)
{
const GLubyte *errorMessage = gluErrorString(error);
std::cerr << "OpenGL error: " << error << " (" << errorMessage << ")" << std::endl;
error = glGetError();
}
glfwSetWindowSizeCallback(wnd, onResize);
initVertices(vertices);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
while (!wnd.shouldClose())
{
auto start = timer.now();
double time = glfw::getTime();
glClear(GL_COLOR_BUFFER_BIT);
// glClearColor((sin(time) + 1.0) / 2.0, (cos(time) + 1.0) / 2.0, (-sin(time) + 1.0) / 2.0, 0.0);
updateVertices(vertices, time);
glBufferData(GL_ARRAY_BUFFER, bufferSize, vertices.data(), GL_DYNAMIC_DRAW);
for (int i = 0; i < lineNum; i++)
{
glDrawArrays(GL_LINE_STRIP, i * lineSize, lineSize);
}
glfw::pollEvents();
wnd.swapBuffers();
auto end = timer.now();
elapsed += std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
fps++;
if (elapsed.count() > 1e9)
{
std::cout << "FPS: " << fps << std::endl;
elapsed = std::chrono::nanoseconds(0);
fps = 0;
}
}
}