-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
332 lines (281 loc) · 10.6 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
/*Standard Includes*/
#include<iostream>
#include <iterator>
#include <vector>
#include <array>
#include <cstdlib>
#include <utility>
/*Stats*/
#include <time.h>
#include <chrono>
#include <ctime>
/*Point generation*/
#include"thinks/poisson_disk_sampling/poisson_disk_sampling.h"
/*Delaunay trianglulation*/
#include<delaunator-cpp/delaunator.hpp>
/*My includes*/
#include "map.h"
/*Gui Includes for openGL3*/
#include"imgui.h"
#include"imgui_impl_glfw.h"
#include"imgui_impl_opengl3.h"
/*OpenGL Includes*/
#include<glad.h>
#include<GLFW/glfw3.h>
#include "Main.h"
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec3 aColor;\n"
"out vec3 ourColor; // output a color to the fragment shader;\n"
"uniform float size;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(size * aPos.x, size * aPos.y, size * aPos.z, 1.0);\n"
" ourColor = aColor;\n"
"}\0";
//Fragment Shader source code
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec3 ourColor;\n"
//"uniform vec4 color;\n"
"void main()\n"
"{\n"
" FragColor = vec4(ourColor,1);\n"
"}\n\0";
//auto construct_voronoi_polygons() {
//
// function edgesAroundPoint(delaunay, start) {
// const result = [];
// let incoming = start;
// do {
// result.push(incoming);
// const outgoing = nextHalfedge(incoming);
// incoming = delaunay.halfedges[outgoing];
// } while (incoming != = -1 && incoming != = start);
// return result;
// }
//}
//vertices coordinates
void Print_Verts(std::vector<float>& vertices) {
unsigned int index = 0;
std::cout << "Beep Boop, Printing....." << '\n';
for (auto i :vertices)
{
index++;
if ((index % 3) != 0) {
std::cout << i << ',';
}
else
{
std::cout << i << ',' << '\n';
};
}
}
/*generates a Barycentric Dual Mesh (A kind of Centroidal Voronoi Tesselation)*/
//wip
int main()
{
auto start = std::chrono::system_clock::now();
// Initialize GLFW
glfwInit();
Map map;
Map newmap();
//Generate Poisson Points
//std::cout << del_triangles.triangles.size() << std::endl;
//std::cout << del_triangles.triangles.size()<< std::endl;
std::cout << map.vertices.data() << '\n';
// Tell GLFW what version of OpenGL we are using
// In this case we are using OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Tell GLFW we are using the CORE profile
// So that means we only have the modern functions
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Create a GLFWwindow object of 1200 by 1000 pixels, naming it "ImGui + GLFW"
GLFWwindow* window = glfwCreateWindow(1200, 1000, "ImGui + GLFW", NULL, NULL);
// Error check if the window fails to create
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Introduce the window into the current context
glfwMakeContextCurrent(window);
//Load GLAD so it configures OpenGL
gladLoadGL();
// Specify the viewport of OpenGL in the Window
// In this case the viewport goes from x = 0, y = 0, to x = 1000, y = 1000
glViewport(0, 0, 1000, 1000);
// Create Vertex Shader Object and get its reference
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
// Attach Vertex Shader source to the Vertex Shader Object
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
// Compile the Vertex Shader into machine code
glCompileShader(vertexShader);
// Create Fragment Shader Object and get its reference
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
// Attach Fragment Shader source to the Fragment Shader Object
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
// Compile the Vertex Shader into machine code
glCompileShader(fragmentShader);
// Create Shader Program Object and get its reference
GLuint shaderProgram = glCreateProgram();
// Attach the Vertex and Fragment Shaders to the Shader Program
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
// Wrap-up/Link all the shaders together into the Shader Program
glLinkProgram(shaderProgram);
// Delete the now useless Vertex and Fragment Shader objects
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
//Print_Verts(vertices);
// Create reference containers for the Vertex Array Object and the Vertex Buffer Object
GLuint VAO,VBO;
//Generate VAO
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*map.points.size(), map.points.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0);
// Initialize ImGUI
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
ImGui::SetNextWindowPos(ImVec2(0, 0));
// Variables to be changed in the ImGUI window
static int generation_stage = 0;
bool wireframe = false;
float size = 1.0f;
float color[4] = { 0.8f, 0.3f, 0.02f, 1.0f };
int test = 0;
// Exporting variables to shaders
glUseProgram(shaderProgram);
glUniform1f(glGetUniformLocation(shaderProgram, "size"), size);
glUniform4f(glGetUniformLocation(shaderProgram, "color"), color[0], color[1], color[2], color[3]);
// Main while loop
while (!glfwWindowShouldClose(window))
{
/*if (test%20 != 0)
float color[4] = { static_cast <float> (rand()) / static_cast <float> (RAND_MAX),static_cast <float> (rand()) / static_cast <float> (RAND_MAX), static_cast <float> (rand()) / static_cast <float> (RAND_MAX) };*/
// Specify the color of the background
glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
// Clean the back buffer and assign the new color to it
glClear(GL_COLOR_BUFFER_BIT);
// Tell OpenGL a new frame is about to begin
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// Only draw the dots if the ImGUI checkbox is ticked
if (wireframe) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
else
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if (generation_stage == 1 || generation_stage == 2)
{
// Draw Triangles
glDrawArrays(GL_TRIANGLES, 0, map.vertices.size() / 6);
if (test == 0)
{
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout <<"Drawing "<< map.vertices.size() / 18 << " triangles from " << map.vertices.size()/12 << " total vertices generated from "<< map.points.size()<< " points and rendered to view in " << elapsed_seconds.count() << '/n' << " at a rate of " << (map.vertices.size() /12) / elapsed_seconds.count() << " vertices rendered per second.";
test++;
}
test++;
};
if (generation_stage == 0)
{
// Draw points
glDrawArrays(GL_POINTS, 0, map.vertices.size() / 2);
glPointSize(1);
};
// ImGUI window creation
ImGui::Begin("My name is window, ImGUI window");
// Text that appears in the window
//Button to regenerate points
ImGui::Checkbox("Wireframe", &wireframe);
// Checkbox that appears in the window
ImGui::RadioButton("Points", &generation_stage, 0); ImGui::SameLine();
ImGui::RadioButton("Triangles", &generation_stage, 1); ImGui::SameLine();
ImGui::RadioButton("Polygons", &generation_stage, 2);
// Slider that appears in the window
ImGui::SliderFloat("Size", &size, 0.0001f, 20.0f);
// Fancy color editor that appears in the window
ImGui::ColorEdit4("Color", color);
// Ends the window
ImGui::End();
// Export variables to shader
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glUniform1f(glGetUniformLocation(shaderProgram, "size"), size);
glUniform4f(glGetUniformLocation(shaderProgram, "color"), color[0], color[1], color[2], color[3]);
// Renders the ImGUI elements
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// Swap the back buffer with the front buffer
glfwSwapBuffers(window);
// Take care of all GLFW events
glfwPollEvents();
}
// Deletes all ImGUI instances
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
// Delete all the objects we've created
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
// Delete window before ending the program
glfwDestroyWindow(window);
// Terminate GLFW before ending the program
glfwTerminate();
return 0;
}
void Init_Vertices(const delaunator::Delaunator& del_triangles, std::vector<float>& vertices)
{
//note : replacing del_triangles.triangles.size() with del_triangles.coords.size() makes it a circle, but it looks kinda cool
for (std::size_t i = 0; i < del_triangles.triangles.size(); i += 3) {
//Verts 0
vertices.push_back((float)del_triangles.coords[2 * del_triangles.triangles[i]]);//tx0
vertices.push_back((float)del_triangles.coords[2 * del_triangles.triangles[i] + 1]);//ty0
vertices.push_back(0.0); //z0
//Colours 0
vertices.push_back((float)(sin(rand() % 100) + 0.2));
vertices.push_back(0.f);
vertices.push_back(0.f);
//Verts 1
vertices.push_back((float)del_triangles.coords[2 * del_triangles.triangles[i + 1]]); //tx1
vertices.push_back((float)del_triangles.coords[2 * del_triangles.triangles[i + 1] + 1]);//ty1
vertices.push_back(0.0); //z 1
//Colours 1
vertices.push_back(0.f);
vertices.push_back((float)(sin(rand() % 100) + 0.2));
vertices.push_back(0.f);
//Verts 2
vertices.push_back((float)del_triangles.coords[2 * del_triangles.triangles[i + 2]]); //tx2
vertices.push_back((float)del_triangles.coords[2 * del_triangles.triangles[i + 2] + 1]); //ty2
vertices.push_back(0.0); //z 2
//Colours 2
vertices.push_back(0.f);
vertices.push_back(0.f);
vertices.push_back((float)(sin(rand() % 100) + 0.2));
}
}