Add simple line drawing component + renderer

This commit is contained in:
karl 2021-01-11 12:35:52 +01:00
parent fc880b397a
commit 418b344dbe
5 changed files with 75 additions and 2 deletions

38
ECS/Components/Lines.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef ECSGAME_LINES_H
#define ECSGAME_LINES_H
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <glm/fwd.hpp>
#include <vector>
struct Lines {
explicit Lines() {
GLfloat lineSeg[] = {
0.0f, 0.0f, 0.0f, // first vertex
2.0f, 2.0f, 2.0f // second vertex
};
glGenVertexArrays(1, &lineVAO);
glGenBuffers(1, &lineVBO);
glBindVertexArray(lineVAO);
glBindBuffer(GL_ARRAY_BUFFER, lineVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(lineSeg), &lineSeg, 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, 2);
glLineWidth(1.0f);
}
private:
GLuint lineVAO, lineVBO;
};
#endif // ECSGAME_LINES_H

View File

@ -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
View 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
View 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);
}

View File

@ -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,16 @@ 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();
kdtree_vis->assign<Lines>();
// We're done loading geometry -> build the collision structure
collision_system->build();
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 +219,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(