-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizeglfw.cpp
More file actions
485 lines (391 loc) · 16 KB
/
visualizeglfw.cpp
File metadata and controls
485 lines (391 loc) · 16 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "visualizeglfw.hpp"
#include "geometry_loader.hpp"
#include "ray.hpp"
#include "shaders.hpp"
#include "optdev.hpp"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> // For transformations (e.g., glm::translate)
#include <glm/gtc/type_ptr.hpp> // For glm::value_ptr
#define GLAD_GL_IMPLEMENTATION
#include <glad/glad.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
std::vector<Vertex> createCylinderSideVertices(
const Vector& start_,
const Vector& end_,
float radius,
int segments,
const glm::vec3& color,
float opacity
)
{
glm::vec3 start{start_.x, start_.y, start_.z};
glm::vec3 end{end_.x, end_.y, end_.z};
std::vector<Vertex> vertices;
glm::vec3 axis = glm::normalize(end - start);
// float height = glm::length(end - start);
// Generate two rings of vertices
for (int i = 0; i <= segments; ++i) {
float angle = 2.0f * M_PI * i / segments;
glm::vec3 circleDir(cos(angle), sin(angle), 0.0f);
// glm::vec3 normal = glm::normalize(circleDir);
// glm::vec3 normal{1,0,0};
// Rotate circleDir to align with the cylinder axis
glm::vec3 tangent = glm::cross(glm::vec3(0.0f, 0.0f, 1.0f), axis);
float rotationAngle = acos(glm::dot(glm::vec3(0.0f, 0.0f, 1.0f), axis));
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), rotationAngle, tangent);
circleDir = glm::vec3(rotation * glm::vec4(circleDir, 0.0f));
// Bottom and top vertices
vertices.push_back({start + circleDir * radius, circleDir, color, opacity});
vertices.push_back({end + circleDir * radius, circleDir, color, opacity});
}
return vertices; // Combine with indices for rendering
}
std::vector<unsigned int> createCylinderSideIndices(int segments, unsigned int base) {
std::vector<unsigned int> indices;
for (int i = 0; i < segments; ++i) {
unsigned int k = base + i * 2;
indices.insert(indices.end(), {k, k + 1, k + 2});
indices.insert(indices.end(), {k + 1, k + 3, k + 2});
}
return indices;
}
glm::mat4 orientSphere(const glm::vec3& targetDirection) {
const glm::vec3 originalUp(0.0f, 0.0f, 1.0f); // Assuming Z-up
const glm::vec3 targetDir = glm::normalize(targetDirection);
// Handle case where target is parallel/anti-parallel to original up
if (glm::length(targetDir - originalUp) < 1e-6f) {
return glm::mat4(1.0f); // Identity (no rotation needed)
}
if (glm::length(targetDir + originalUp) < 1e-6f) {
return glm::rotate(glm::mat4(1.0f), glm::radians(180.0f), glm::vec3(1.0f, 0.0f, 0.0f));
}
// Compute rotation axis and angle
const glm::vec3 axis = glm::normalize(glm::cross(originalUp, targetDir));
const float angle = glm::acos(glm::dot(originalUp, targetDir));
return glm::rotate(glm::mat4(1.0f), angle, axis);
}
std::vector<Vertex> createSphereVertices(
const Vector& origin_,
const Vector& upDirection,
float radius,
float openingAngle,
int segments,
const glm::vec3& color,
float opacity
) {
std::vector<Vertex> vertices;
glm::vec3 origin{origin_.x, origin_.y, origin_.z};
const glm::mat4 rotation = orientSphere(upDirection);
int sectorCount = segments;
int stackCount = segments;
for (int i = 0; i < stackCount+1; ++i) {
float stackAngle = M_PI_2 - i * openingAngle / stackCount; // from pi/2 to pi/2 - openingAngle
float xy = radius * cos(stackAngle); // r * cos(u)
float z = radius * sin(stackAngle); // r * sin(u)
for (int j = 0; j < sectorCount; ++j) {
float sectorAngle = j * M_PI * 2 / sectorCount; // from 0 to 2pi
float x = xy * cos(sectorAngle);
float y = xy * sin(sectorAngle);
glm::vec3 pos = glm::vec3(x, y, z);
pos = glm::vec3(rotation * glm::vec4(pos, 1.0f));
glm::vec3 normal = glm::vec3(rotation * glm::vec4(glm::normalize(pos), 0.0f));
vertices.push_back({glm::vec3(pos+origin), normal, color, opacity});
}
}
return vertices;
}
std::vector<unsigned int> createSphereIndices(int segments, unsigned int base) {
std::vector<unsigned int> indices;
unsigned int stackCount = segments;
unsigned int sectorCount = segments;
for (unsigned int i = 0; i < stackCount; i++) {
unsigned int k1 = i * sectorCount + base; // beginning of current stack
unsigned int k2 = k1 + sectorCount; // beginning of next stack
for (unsigned int j = 0; j < sectorCount; j++, k1++, k2++) {
indices.insert(indices.end(), {k1, k2, k1+1});
if (j != (sectorCount-1)) {
indices.insert(indices.end(), {k1+1, k2, k2+1});
} else {
indices.insert(indices.end(), {k1-sectorCount+1, k1, k1+1});
}
}
}
return indices;
}
std::vector<Vertex> createParabolaVertices(
const Vector& origin_,
const Vector& height,
float curvature,
int segments,
const glm::vec3& color,
float opacity
) {
glm::vec3 origin{origin_.x, origin_.y, origin_.z};
const glm::mat4 rotation = orientSphere(height);
// create vertices in local coordinates, but push back after translation into world coordinates
std::vector<Vertex> vertices;
int sectorCount = segments;
int stackCount = segments;
for (int i = 0; i < stackCount+1; ++i) {
float z = float(i) / float(stackCount) * height.magnitude();
float radius = sqrt(z/curvature);
for (int j = 0; j < sectorCount; ++j) {
float sectorAngle = j * M_PI * 2 / sectorCount; // from 0 to 2pi
float x = radius * cos(sectorAngle);
float y = radius * sin(sectorAngle);
glm::vec3 pos = glm::vec3(x, y, z);
pos = glm::vec3(rotation * glm::vec4(pos, 1.0f));
glm::vec3 normal = glm::vec3(rotation * glm::vec4(glm::normalize(pos), 0.0f));
vertices.push_back({glm::vec3(pos+origin), normal, color, opacity});
}
}
return vertices;
}
std::vector<Vertex> createDiscVertices(
const Vector& origin_,
const Vector& surfaceNormal_,
const double radius_,
int segments,
const glm::vec3& color,
float opacity
) {
std::vector<Vertex> vertices;
glm::vec3 origin{origin_.x, origin_.y, origin_.z};
// make center vertex for construction of triangles in shape of cut pie
vertices.push_back({glm::vec3(origin), surfaceNormal_, color, 0.7f});
const glm::mat4 rotation = orientSphere(surfaceNormal_);
int sectorCount = segments;
for (int i = 0; i < sectorCount; i++) {
float sectorAngle = i * M_PI * 2 / sectorCount; // from 0 to 2pi
float x = radius_ * cos(sectorAngle);
float y = radius_ * sin(sectorAngle);
glm::vec3 pos = glm::vec3(x, y, 0);
pos = glm::vec3(rotation * glm::vec4(pos, 1.0f));
glm::vec3 normal = glm::vec3(rotation * glm::vec4(glm::normalize(pos), 0.0f));
vertices.push_back({glm::vec3(pos+origin), normal, color, opacity});
}
return vertices;
}
std::vector<unsigned int> createDiscIndices(int segments, unsigned int base) {
std::vector<unsigned int> indices;
unsigned int sectorCount = segments;
for (unsigned int i = 1; i < sectorCount; i++) {
indices.insert(indices.end(), {base, base+i, base+i+1});
}
indices.insert(indices.end(), {base, base+sectorCount, base+1});
return indices;
}
class Camera {
public:
glm::vec3 position;
glm::vec3 front;
glm::vec3 up;
glm::vec3 right;
glm::vec3 worldUp;
float yaw = 90.0f; // Rotation around Z axis (starts looking along -Y)
float pitch = 0.0f; // Rotation around X axis
float speed = 0.5f;
float sensitivity = 0.1f;
Camera(glm::vec3 pos = glm::vec3(0.0f, -3.0f, 0.0f),
glm::vec3 up = glm::vec3(0.0f, 0.0f, 1.0f))
: position(pos), worldUp(up) {
updateVectors();
}
glm::mat4 getViewMatrix() {
return glm::lookAt(position, position + front, up);
}
void updateVectors() {
glm::vec3 newFront;
newFront.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
newFront.z = sin(glm::radians(pitch));
newFront.y = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
front = glm::normalize(newFront);
right = glm::normalize(glm::cross(front, worldUp));
up = glm::normalize(glm::cross(right, front));
}
};
int width = 1000;
int height = 1000;
Camera camera;
bool firstMouse = true;
float lastX = width / 2.0f;
float lastY = height / 2.0f;
glm::mat4 perspectiveProjection = glm::perspective(
glm::radians(45.0f),
(float)width / (float)height,
0.01f,
1000.0f
);
glm::mat4 orthographicProjection = glm::ortho(-2.0f, +2.0f, -1.5f, +1.5f, 0.1f, 100.0f);
glm::mat4 projection = perspectiveProjection;
// Keyboard callback
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS || action == GLFW_REPEAT) {
switch (key) {
case GLFW_KEY_W: camera.position += camera.front * camera.speed; break;
case GLFW_KEY_S: camera.position -= camera.front * camera.speed; break;
case GLFW_KEY_A: camera.position -= camera.right * camera.speed; break;
case GLFW_KEY_D: camera.position += camera.right * camera.speed; break;
case GLFW_KEY_O: projection = orthographicProjection; break;
case GLFW_KEY_P: projection = perspectiveProjection; break;
}
}
}
// Mouse callback
void mouseCallback(GLFWwindow* window, double xpos, double ypos) {
if (firstMouse) {
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = ypos - lastY;
lastX = xpos;
lastY = ypos;
xoffset *= camera.sensitivity;
yoffset *= camera.sensitivity;
camera.yaw += xoffset;
camera.pitch += yoffset;
// Constrain pitch to avoid screen flipping
if (camera.pitch > 89.0f) camera.pitch = 89.0f;
if (camera.pitch < -89.0f) camera.pitch = -89.0f;
camera.updateVectors();
}
void addRays(const std::vector<Ray>& rays,
std::vector<Vertex>& vertices,
std::vector<unsigned int>& indices,
int segments
) {
unsigned int firstIndex = 0;
glm::vec3 color;
for (const Ray& ray: rays) {
if (ray.refractiveIndex == Config::VACUUM_REFRACTIVE_INDEX) {
color = glm::vec3(0,0,1);
} else {
color = glm::vec3(1,0.5,0);
}
if ((ray.wavelength > 380e-9) && (ray.wavelength < 780e-9)) {
color = wavelengthToRGB(ray.wavelength);
}
std::vector<Vertex> cylinderSideVerts = createCylinderSideVertices(ray.origin, ray.end, 0.005f*ray.energyDensity, segments, color);
vertices.insert(vertices.end(), cylinderSideVerts.begin(), cylinderSideVerts.end());
if (indices.size() > 0) {
firstIndex = *std::max_element(indices.begin(), indices.end()) + 1;
}
std::vector<unsigned int> csIndices = createCylinderSideIndices(segments, firstIndex);
indices.insert(indices.end(), csIndices.begin(), csIndices.end());
}
}
void visualizeWithGLFW(GeometryLoader& geometry) {
// glfwSetErrorCallback(error_callback);
if (!glfwInit())
{
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(width, height, "OpenGL OpticSim", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwGetWindowSize(window, &width, &height);
glfwSetKeyCallback(window, keyCallback);
glfwSetCursorPosCallback(window, mouseCallback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Hide and capture mouse
glfwMakeContextCurrent(window);
gladLoadGL();
glEnable(GL_DEPTH_TEST); // Add this after gladLoadGL()
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glfwSwapInterval(1);
std::vector<Vertex> vertices;
int segments = 16;
std::vector<unsigned int> indices;
addRays(geometry.rays, vertices, indices, segments);
// OpticalDevice* d = geometry.devices[0].get();
for (const auto& device : geometry.devices) {
device->createGraphicVertices(vertices, indices);
}
unsigned int VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
glCompileShader(vertex_shader);
const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
glCompileShader(fragment_shader);
const GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
glEnableVertexAttribArray(0);
// Normal attribute (for lighting)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glEnableVertexAttribArray(1);
// Color attribute
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, color));
glEnableVertexAttribArray(2);
// Opacity
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, alpha));
glEnableVertexAttribArray(3);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glUseProgram(program); // Compile/link shaders first
GLint success;
GLchar infoLog[512];
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(program, 512, NULL, infoLog);
std::cerr << "Shader Program linking failed:\n" << infoLog << std::endl;
}
glBindVertexArray(VAO);
// glDrawArrays(GL_LINES, 0, vertices.size()); // 2 vertices per ray
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
while (!glfwWindowShouldClose(window))
{
glfwGetWindowSize(window, &width, &height);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = camera.getViewMatrix();
// glm::mat4 mvp = projection * view * model;
glUseProgram(program);
// glUniformMatrix4fv(glGetUniformLocation(program, "MVP"), 1, GL_FALSE, glm::value_ptr(mvp));
glUniformMatrix4fv(glGetUniformLocation(program, "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(glGetUniformLocation(program, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(VAO);
// glDrawArrays(GL_LINES, 0, vertices.size());
// Before rendering:
glDepthMask(GL_FALSE); // Disable depth writing
glEnable(GL_BLEND);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
// glPointSize(10);
// glDrawArrays(GL_POINTS, 0, vertices.size());
glDepthMask(GL_TRUE); // Re-enable depth writing
glDisable(GL_BLEND);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(program);
glfwTerminate();
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}