Compare commits
3 Commits
fc880b397a
...
af019d3318
Author | SHA1 | Date | |
---|---|---|---|
af019d3318 | |||
7cbaaf84f7 | |||
418b344dbe |
44
ECS/Components/Lines.h
Normal file
44
ECS/Components/Lines.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef ECSGAME_LINES_H
|
||||
#define ECSGAME_LINES_H
|
||||
|
||||
#include "../../Util/geometry.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glad/glad.h>
|
||||
#include <glm/fwd.hpp>
|
||||
#include <vector>
|
||||
|
||||
struct Lines {
|
||||
explicit Lines(std::vector<Vector> lines) : vertex_count(lines.size()) {
|
||||
GLfloat linearray[vertex_count * 3];
|
||||
int pos = 0;
|
||||
|
||||
for (const Vector &vec : lines) {
|
||||
linearray[pos++] = vec[0];
|
||||
linearray[pos++] = vec[1];
|
||||
linearray[pos++] = vec[2];
|
||||
}
|
||||
|
||||
glGenVertexArrays(1, &lineVAO);
|
||||
glGenBuffers(1, &lineVBO);
|
||||
glBindVertexArray(lineVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, lineVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(linearray), &linearray, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void *)0);
|
||||
}
|
||||
|
||||
void render() const {
|
||||
// glEnable(GL_LINE_SMOOTH);
|
||||
// glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
|
||||
glBindVertexArray(lineVAO);
|
||||
glLineWidth(3.3f);
|
||||
glDrawArrays(GL_LINES, 0, vertex_count);
|
||||
glLineWidth(1.0f);
|
||||
}
|
||||
|
||||
private:
|
||||
GLuint lineVAO, lineVBO;
|
||||
int vertex_count;
|
||||
};
|
||||
|
||||
#endif // ECSGAME_LINES_H
|
@ -10,6 +10,7 @@
|
||||
#include "../Components/Camera.h"
|
||||
#include "../Components/DirectionalLight.h"
|
||||
#include "../Components/LODObjMesh.h"
|
||||
#include "../Components/Lines.h"
|
||||
#include "../Components/Mesh.h"
|
||||
#include "../Components/ObjMesh.h"
|
||||
#include "../Components/Texture.h"
|
||||
@ -226,7 +227,8 @@ class RenderSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
void render(World *pWorld, Shader normalShader, Shader shadowShader, Shader debugShader) {
|
||||
void render(World *pWorld, Shader normalShader, Shader shadowShader, Shader debugShader,
|
||||
Shader lineShader) {
|
||||
pWorld->each<Camera, Transform>([&](Entity *ent, ComponentHandle<Camera> camera,
|
||||
ComponentHandle<Transform> cameraTransform) {
|
||||
// Get render objects
|
||||
@ -306,6 +308,16 @@ class RenderSystem : public EntitySystem, public EventSubscriber<InputEvent> {
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, depthMap);
|
||||
// renderQuad();
|
||||
|
||||
// Draw lines
|
||||
lineShader.use();
|
||||
lineShader.setMat4("projection", camera->projection);
|
||||
lineShader.setMat4("view", glm::inverse(view));
|
||||
lineShader.setMat4("model",
|
||||
glm::mat4(1.0f)); // TODO: integrate cameraTransform->get_origin()
|
||||
|
||||
pWorld->each<Lines>(
|
||||
[&](Entity *ent, ComponentHandle<Lines> lines) { lines->render(); });
|
||||
});
|
||||
}
|
||||
|
||||
|
7
Shaders/line-fragment.fs
Normal file
7
Shaders/line-fragment.fs
Normal file
@ -0,0 +1,7 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(1.0); // white
|
||||
}
|
11
Shaders/line-vertex.vs
Normal file
11
Shaders/line-vertex.vs
Normal file
@ -0,0 +1,11 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
// Forward declarations
|
||||
@ -24,7 +26,7 @@ struct Vector {
|
||||
}
|
||||
|
||||
Vector cross(const Vector &other) {
|
||||
return Vector(c[1] * other[2] - c[2] - other[1], c[2] * other[0] - c[0] * other[2],
|
||||
return Vector(c[1] * other[2] - c[2] * other[1], c[2] * other[0] - c[0] * other[2],
|
||||
c[0] * other[1] - c[1] * other[0]);
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ class KDTree {
|
||||
|
||||
~KDTree() = default; // TODO: Delete all allocated Nodes
|
||||
|
||||
Triangle *intersect_ray(Ray ray) { return intersect_ray_recurse(ray, root, 1000.0); }
|
||||
Triangle *intersect_ray(Ray ray) { return intersect_ray_recurse(ray, root, 1000.0, 0); }
|
||||
|
||||
std::string to_string() {
|
||||
std::string str = "";
|
||||
@ -19,6 +19,16 @@ class KDTree {
|
||||
return str;
|
||||
}
|
||||
|
||||
std::vector<Vector> get_lines() {
|
||||
std::vector<Vector> lines;
|
||||
float mins[3] = {-1000, -1000, -1000};
|
||||
float maxs[3] = {1000, 1000, 1000};
|
||||
|
||||
get_lines_recursive(lines, root, mins, maxs);
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
private:
|
||||
Node *root;
|
||||
|
||||
@ -75,7 +85,7 @@ class KDTree {
|
||||
build(right_of_median, depth + 1));
|
||||
}
|
||||
|
||||
Triangle *intersect_ray_recurse(Ray ray, Node *node, float max_distance) {
|
||||
Triangle *intersect_ray_recurse(Ray ray, Node *node, float max_distance, int depth) {
|
||||
// Exit condition: There was no collision
|
||||
if (node == nullptr) { return nullptr; }
|
||||
|
||||
@ -84,9 +94,6 @@ class KDTree {
|
||||
ray.origin[node->axis] > node->point->pos[node->axis] ? node->right : node->left;
|
||||
Node *far = near == node->right ? node->left : node->right;
|
||||
|
||||
std::cout << "Checking " << node->point->pos[0] << ", " << node->point->pos[1] << ", "
|
||||
<< node->point->pos[2] << std::endl;
|
||||
|
||||
// Check for collisions in this order (stopping if an intersection is found):
|
||||
// 1. In the nearer section
|
||||
// 2. With the point in this current node
|
||||
@ -98,7 +105,7 @@ class KDTree {
|
||||
? (node->point->pos[node->axis] - ray.origin[node->axis]) /
|
||||
ray.direction[node->axis]
|
||||
: max_distance;
|
||||
Triangle *near_result = intersect_ray_recurse(ray, near, t);
|
||||
Triangle *near_result = intersect_ray_recurse(ray, near, t, depth + 1);
|
||||
|
||||
// If the nearer segment had a collision, we're done! We're only interested in the closest
|
||||
// collision.
|
||||
@ -117,7 +124,7 @@ class KDTree {
|
||||
// For this, calculate a new ray origin and continue towards that direction, but with
|
||||
// the new origin (we can leave behind what we already checked)
|
||||
return intersect_ray_recurse(Ray(ray.origin + ray.direction * t, ray.direction), far,
|
||||
max_distance - t);
|
||||
max_distance - t, depth + 1);
|
||||
}
|
||||
|
||||
// If nothing worked, return a nullptr
|
||||
@ -136,4 +143,61 @@ class KDTree {
|
||||
to_string_recurse(str, node->left, depth + 1);
|
||||
to_string_recurse(str, node->right, depth + 1);
|
||||
}
|
||||
|
||||
void get_lines_recursive(std::vector<Vector> &lines, Node *node, float mins[3], float maxs[3]) {
|
||||
if (node == nullptr) return;
|
||||
|
||||
int axis = node->axis;
|
||||
|
||||
// Add lines for this node
|
||||
// We add a whole bounding box, so we need 8 points -> 12 lines
|
||||
lines.emplace_back(Vector(mins[0], mins[1], mins[2]));
|
||||
lines.emplace_back(Vector(maxs[0], mins[1], mins[2]));
|
||||
|
||||
lines.emplace_back(Vector(maxs[0], mins[1], mins[2]));
|
||||
lines.emplace_back(Vector(maxs[0], maxs[1], mins[2]));
|
||||
|
||||
lines.emplace_back(Vector(maxs[0], maxs[1], mins[2]));
|
||||
lines.emplace_back(Vector(mins[0], maxs[1], mins[2]));
|
||||
|
||||
lines.emplace_back(Vector(mins[0], maxs[1], mins[2]));
|
||||
lines.emplace_back(Vector(mins[0], mins[1], mins[2]));
|
||||
|
||||
lines.emplace_back(Vector(mins[0], mins[1], maxs[2]));
|
||||
lines.emplace_back(Vector(maxs[0], mins[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(maxs[0], mins[1], maxs[2]));
|
||||
lines.emplace_back(Vector(maxs[0], maxs[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(maxs[0], maxs[1], maxs[2]));
|
||||
lines.emplace_back(Vector(mins[0], maxs[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(mins[0], maxs[1], maxs[2]));
|
||||
lines.emplace_back(Vector(mins[0], mins[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(mins[0], mins[1], mins[2]));
|
||||
lines.emplace_back(Vector(mins[0], mins[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(maxs[0], mins[1], mins[2]));
|
||||
lines.emplace_back(Vector(maxs[0], mins[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(mins[0], maxs[1], mins[2]));
|
||||
lines.emplace_back(Vector(mins[0], maxs[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(maxs[0], maxs[1], mins[2]));
|
||||
lines.emplace_back(Vector(maxs[0], maxs[1], maxs[2]));
|
||||
|
||||
// TODO: We don't actually have to add all as some are always going to overlap with the
|
||||
// previous lines...
|
||||
|
||||
// Recurse on the left
|
||||
float new_maxs[3] = {maxs[0], maxs[1], maxs[2]}; // Copy initially
|
||||
new_maxs[axis] = node->point->pos[axis];
|
||||
get_lines_recursive(lines, node->left, mins, new_maxs);
|
||||
|
||||
// Recurse on the right
|
||||
float new_mins[3] = {mins[0], mins[1], mins[2]}; // Copy initially
|
||||
new_mins[axis] = node->point->pos[axis];
|
||||
get_lines_recursive(lines, node->right, new_mins, maxs);
|
||||
}
|
||||
};
|
||||
|
8
main.cpp
8
main.cpp
@ -3,6 +3,7 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "ECS/Components/DirectionalLight.h"
|
||||
#include "ECS/Components/Lines.h"
|
||||
#include "ECS/Components/ObjMesh.h"
|
||||
#include "ECS/Components/PathMove.h"
|
||||
#include "ECS/Components/SineAnimation.h"
|
||||
@ -196,12 +197,17 @@ int main(int argc, char **argv) {
|
||||
Entity *sun = world->create();
|
||||
sun->assign<DirectionalLight>(glm::normalize(glm::vec3(1.0, 1.0, 1.0)));
|
||||
|
||||
Entity *kdtree_vis = world->create();
|
||||
|
||||
// We're done loading geometry -> build the collision structure
|
||||
collision_system->build();
|
||||
|
||||
kdtree_vis->assign<Lines>(collision_system->kdtree->get_lines());
|
||||
|
||||
Shader defaultShader("Shaders/default-vertex.vs", "Shaders/default-fragment.fs");
|
||||
Shader shadowShader("Shaders/shadow-vertex.vs", "Shaders/shadow-fragment.fs");
|
||||
Shader debugShader("Shaders/debug-vertex.vs", "Shaders/debug-fragment.fs");
|
||||
Shader lineShader("Shaders/line-vertex.vs", "Shaders/line-fragment.fs");
|
||||
|
||||
double timeInLastFrame = glfwGetTime();
|
||||
double elapsed_time = 0.0;
|
||||
@ -214,7 +220,7 @@ int main(int argc, char **argv) {
|
||||
elapsed_time += delta;
|
||||
|
||||
world->tick(delta);
|
||||
renderSystem->render(world, defaultShader, shadowShader, debugShader);
|
||||
renderSystem->render(world, defaultShader, shadowShader, debugShader, lineShader);
|
||||
|
||||
ring->get<Transform>()->rotate(delta * 100.0, glm::vec3(0.0, 1.0, 0.0));
|
||||
sun->get<DirectionalLight>()->direction = glm::normalize(glm::vec3(
|
||||
|
Loading…
x
Reference in New Issue
Block a user