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
| #include "Shader.h"
#include "Camera.h"
struct Ray {
glm::vec3 origin;
glm::vec3 direction;
};
struct ModelTransform
{
glm::vec3 pos;
glm::vec3 rotation;
glm::vec3 scale;
void setScale(float s)
{
scale.x = s;
scale.y = s;
scale.z = s;
}
};
class Object {
public:
glm::vec3 position;
float radius;
glm::vec3 color;
Object(const glm::vec3& pos, float rad, const glm::vec3& col) : position(pos), radius(rad), color(col) {}
bool intersect(const Ray& ray) const {
glm::vec3 oc = ray.origin - position;
float a = glm::dot(ray.direction, ray.direction);
float b = 2.0f * glm::dot(oc, ray.direction);
float c = glm::dot(oc, oc) - radius * radius;
float discriminant = b * b - 4 * a * c;
return (discriminant > 0);
}
};
class Cube : public Object {
public:
Cube(const glm::vec3& pos, float sideLength, const glm::vec3& col) : Object(pos, sideLength / 2.0f, col), sideLength(sideLength) {}
float dot(const glm::vec3& a, const glm::vec3& b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
bool intersect(const Ray& ray) const {
// Calculate ray origin in object space
glm::vec3 localOrigin = ray.origin - position;
// Calculate half extents of the cube
float halfLength = sideLength / 2.0f;
// Calculate intersection with each slab of the cube
float tNear = -FLT_MAX;
float tFar = FLT_MAX;
for (int i = 0; i < 3; ++i) {
if (abs(ray.direction[i]) < EPSILON) {
// Ray is parallel to slab, no intersection if origin is not within slab
if (localOrigin[i] < -halfLength || localOrigin[i] > halfLength)
return false;
}
else {
// Compute intersection t values of the two slab boundaries
float t1 = (-halfLength - localOrigin[i]) / ray.direction[i];
float t2 = (halfLength - localOrigin[i]) / ray.direction[i];
// Make t1 the intersection with the near plane, t2 with the far plane
if (t1 > t2)
std::swap(t1, t2);
// Update tNear and tFar
tNear = std::max(tNear, t1);
tFar = std::min(tFar, t2);
// If intersection is outside slab, no intersection
if (tNear > tFar)
return false;
// If slab intersection is behind ray, no intersection
if (tFar < 0)
return false;
}
}
return true;
}
private:
float sideLength;
const float EPSILON = 0.0001f;
};
Shader* shaderProgram;
std::vector<Object> objects;
std::vector<Cube> cubes;
Camera camera(glm::vec3(0.f, 0.f, -2.f));
void onScroll(GLFWwindow* win, double x, double y)
{
camera.ChangeFOV(y);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow* window, float dt) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
uint32_t dir = 0;
if (glfwGetKey(window, GLFW_KEY_PAGE_UP) == GLFW_PRESS)
dir |= CAM_UP;
if (glfwGetKey(window, GLFW_KEY_PAGE_DOWN) == GLFW_PRESS)
dir |= CAM_DOWN;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
dir |= CAM_FORWARD;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
dir |= CAM_BACKWARD;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
dir |= CAM_LEFT;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
dir |= CAM_RIGHT;
camera.Move(dir, dt);
}
void pickObject(const Ray& ray) {
for (const auto& obj : objects) {
if (obj.intersect(ray)) {
std::cout << "Object picked!" << std::endl;
// Do shit
}
}
}
Ray calculateRayFromMouse(double xpos, double ypos, GLFWwindow* window) {
int width, height;
glfwGetWindowSize(window, &width, &height);
// Convert screen coordinates to NDC (Normalized Device Coordinates)
float x = (2.0f * xpos) / width - 1.0f;
float y = 1.0f - (2.0f * ypos) / height;
// Convert NDC to clip coordinates
glm::vec4 clipCoords(x, y, -1.0f, 1.0f);
// Convert clip coordinates to eye coordinates
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
glm::mat4 view = glm::mat4(1.0f); // Assuming camera is at the origin and looking along the negative z-axis
glm::vec4 eyeCoords = glm::inverse(projection * view) * clipCoords;
eyeCoords.z = -1.0f; // Direction along the negative z-axis
// Convert eye coordinates to world coordinates
glm::vec4 worldCoords = glm::inverse(view) * eyeCoords;
Ray ray;
ray.origin = glm::vec3(0.0f); // Origin at camera position
ray.direction = glm::normalize(glm::vec3(worldCoords));
return ray;
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
Ray ray = calculateRayFromMouse(xpos, ypos, window);
pickObject(ray);
}
}
std::vector<glm::vec3> createSphereVertices(float radius, int segments, int stacks) {
std::vector<glm::vec3> vertices;
for (int j = 0; j <= segments; j++) {
float theta = j * glm::pi<float>() / segments;
float sinTheta = sin(theta);
float cosTheta = cos(theta);
for (int i = 0; i <= stacks; i++) {
float phi = i * 2 * glm::pi<float>() / stacks;
float sinPhi = sin(phi);
float cosPhi = cos(phi);
float x = cosPhi * sinTheta;
float z = cosTheta;
float y = sinPhi * sinTheta;
vertices.push_back(radius * glm::vec3(x, y, z));
}
}
return vertices;
}
int main() {
// Initialize GLFW
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return -1;
}
// Set GLFW window hints
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
int winw = 800, winh = 600;
// Create a GLFW window
GLFWwindow* window = glfwCreateWindow(winw, winh, "Pick Object using Ray Tracing", NULL, NULL);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLAD
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cerr << "Failed to initialize GLAD" << std::endl;
return -1;
}
// Set viewport size callback
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// Set mouse button callback
glfwSetMouseButtonCallback(window, mouse_button_callback);
glViewport(0, 0, winw, winh);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
shaderProgram = new Shader("Vertex.vert", "Fragment.frag");
ModelTransform polygonTrans = { glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 0.f, 0.f), glm::vec3(1.0f, 1.0f, 1.0f) };
Cube cube(glm::vec3(0.0f, 0.0f, -2.2f), 1.0f, glm::vec3(0.0f, 1.0f, 0.0f));
objects.push_back(cube);
cubes.push_back(cube);
// Создание вершинного буфера и массива вершин для куба
std::vector<glm::vec3> cubeVertices = {
// Первая грань
glm::vec3(-0.3f, -0.3f, 0.3f),
glm::vec3(0.3f, -0.3f, 0.3f),
glm::vec3(0.3f, 0.3f, 0.3f),
glm::vec3(0.3f, 0.3f, 0.3f),
glm::vec3(-0.3f, 0.3f, 0.3f),
glm::vec3(-0.3f, -0.3f, 0.3f),
// Вторая грань
glm::vec3(-0.3f, -0.3f, -0.3f),
glm::vec3(-0.3f, 0.3f, -0.3f),
glm::vec3(0.3f, 0.3f, -0.3f),
glm::vec3(0.3f, 0.3f, -0.3f),
glm::vec3(0.3f, -0.3f, -0.3f),
glm::vec3(-0.3f, -0.3f, -0.3f),
// Третья грань
glm::vec3(-0.3f, 0.3f, -0.3f),
glm::vec3(-0.3f, 0.3f, 0.3f),
glm::vec3(0.3f, 0.3f, 0.3f),
glm::vec3(0.3f, 0.3f, 0.3f),
glm::vec3(0.3f, 0.3f, -0.3f),
glm::vec3(-0.3f, 0.3f, -0.3f),
// Четвёртая грань
glm::vec3(-0.3f, -0.3f, -0.3f),
glm::vec3(0.3f, -0.3f, -0.3f),
glm::vec3(0.3f, -0.3f, 0.3f),
glm::vec3(0.3f, -0.3f, 0.3f),
glm::vec3(-0.3f, -0.3f, 0.3f),
glm::vec3(-0.3f, -0.3f, -0.3f),
// Пятая грань
glm::vec3(0.3f, -0.3f, -0.3f),
glm::vec3(0.3f, 0.3f, -0.3f),
glm::vec3(0.3f, 0.3f, 0.3f),
glm::vec3(0.3f, 0.3f, 0.3f),
glm::vec3(0.3f, -0.3f, 0.3f),
glm::vec3(0.3f, -0.3f, -0.3f),
// Шестая грань
glm::vec3(-0.3f, -0.3f, -0.3f),
glm::vec3(-0.3f, -0.3f, 0.3f),
glm::vec3(-0.3f, 0.3f, 0.3f),
glm::vec3(-0.3f, 0.3f, 0.3f),
glm::vec3(-0.3f, 0.3f, -0.3f),
glm::vec3(-0.3f, -0.3f, -0.3f)
};
//objects.emplace_back(glm::vec3(0.0f, 0.0f, -2.2f), 0.7f, glm::vec3(1.0f, 0.0f, 0.0f)); // Sphere at the center
// Create sphere vertices
//std::vector<glm::vec3> sphereVertices = createSphereVertices(objects[0].radius, 1000, 4);
// Create vertex buffer object
GLuint VBO, VAO;
glGenBuffers(1, &VBO);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, cubeVertices.size() * sizeof(glm::vec3), &cubeVertices[0], GL_STATIC_DRAW);
// Set vertex attributes
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); // Координаты вершин
glEnableVertexAttribArray(0);
double oldTime = glfwGetTime(), newTime, deltaTime;
// Main loop
while (!glfwWindowShouldClose(window)) {
newTime = glfwGetTime();
deltaTime = newTime - oldTime;
oldTime = newTime;
processInput(window, deltaTime);
polygonTrans.rotation.x = glfwGetTime() * 40.0;
// Render
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shaderProgram->use();
glm::mat4 pv = camera.GetProjectionMatrix() * camera.GetViewMatrix();
//1
glm::mat4 model = glm::mat4(1.0f);
float color[3] = { objects[0].color.x, objects[0].color.y, objects[0].color.z };
model = glm::translate(model, polygonTrans.pos);
model = glm::rotate(model, glm::radians(polygonTrans.rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
//model = glm::rotate(model, glm::radians(polygonTrans.rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 pvm = pv * model;
shaderProgram->SetMatrix4F("pvm", pvm);
shaderProgram->SetColor("aColor", color);
glBindVertexArray(VAO);
// Draw the sphere
glDrawArrays(GL_TRIANGLES, 0, cubeVertices.size());
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
delete shaderProgram;
glfwTerminate();
return 0;
} |