Basic camera, mesh, rendering systems
Not used yet!
This commit is contained in:
parent
7675ddc3a4
commit
d99ca4c8d7
@ -7,7 +7,7 @@ find_package(OpenGL REQUIRED)
|
||||
find_package(glfw3 REQUIRED)
|
||||
find_package(glm REQUIRED)
|
||||
|
||||
add_executable(ecsgame Util/glad.c Util/OBJ_Loader.h Rendering/Shader.cpp Rendering/Shader.h main.cpp ECS/Components/Transform.h ECS/Components/Movement.h ECS/Events/InputEvent.h ECS/Systems/GravitySystem.h ECS/Systems/PositionDebugSystem.h ECS/Systems/KeyboardMovementSystem.h ECS/Components/Camera.h)
|
||||
add_executable(ecsgame Util/glad.c Util/OBJ_Loader.h Rendering/Shader.cpp Rendering/Shader.h main.cpp ECS/Components/Transform.h ECS/Components/Movement.h ECS/Events/InputEvent.h ECS/Systems/GravitySystem.h ECS/Systems/PositionDebugSystem.h ECS/Systems/KeyboardMovementSystem.h ECS/Components/Camera.h ECS/Systems/RenderSystem.h ECS/Components/Mesh.h)
|
||||
|
||||
include_directories(${OPENGL_INCLUDE_DIRS})
|
||||
|
||||
|
19
ECS/Components/Camera.h
Normal file
19
ECS/Components/Camera.h
Normal file
@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by karl on 07.01.20.
|
||||
//
|
||||
|
||||
#ifndef ECSGAME_CAMERA_H
|
||||
#define ECSGAME_CAMERA_H
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
struct Camera {
|
||||
/// Create a camera with a field of view (in degrees), width and height (in any unit) and near and far distances
|
||||
Camera(float fov, float width, float height, float near, float far) : projection(
|
||||
glm::perspective(glm::radians(fov), width / height, near, far)) {}
|
||||
|
||||
glm::mat4 projection;
|
||||
};
|
||||
|
||||
#endif //ECSGAME_CAMERA_H
|
45
ECS/Components/Mesh.h
Normal file
45
ECS/Components/Mesh.h
Normal file
@ -0,0 +1,45 @@
|
||||
//
|
||||
// Created by karl on 07.01.20.
|
||||
//
|
||||
|
||||
#ifndef ECSGAME_MESH_H
|
||||
#define ECSGAME_MESH_H
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <vector>
|
||||
|
||||
struct Mesh {
|
||||
explicit Mesh(std::vector<float> vertices_vector) : vertices(&vertices_vector[0]), vertex_count(vertices_vector.size()) {
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
// position attribute
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// texture coord attribute
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
}
|
||||
|
||||
void render() {
|
||||
glBindVertexArray(VAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, vertex_count);
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned int VBO;
|
||||
unsigned int VAO;
|
||||
unsigned int vertex_count;
|
||||
|
||||
/// Vertices in the format x, y, z, u, v
|
||||
float *vertices;
|
||||
};
|
||||
|
||||
#endif //ECSGAME_MESH_H
|
@ -26,6 +26,10 @@ struct Transform {
|
||||
matrix = glm::scale(matrix, factors);
|
||||
}
|
||||
|
||||
void rotate(float degrees, glm::vec3 axis) {
|
||||
matrix = glm::rotate(matrix, glm::radians(degrees), axis);
|
||||
}
|
||||
|
||||
glm::vec3 getPosition() {
|
||||
return matrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
}
|
||||
|
35
ECS/Systems/RenderSystem.h
Normal file
35
ECS/Systems/RenderSystem.h
Normal file
@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by karl on 07.01.20.
|
||||
//
|
||||
|
||||
#ifndef ECSGAME_RENDERSYSTEM_H
|
||||
#define ECSGAME_RENDERSYSTEM_H
|
||||
|
||||
#include "../ECS.h"
|
||||
#include "../Components/Transform.h"
|
||||
#include "../Components/Mesh.h"
|
||||
#include "../Components/Camera.h"
|
||||
#include "../../Rendering/Shader.h"
|
||||
|
||||
using namespace ECS;
|
||||
|
||||
class RenderSystem : public EntitySystem {
|
||||
public:
|
||||
void render(World *pWorld, Shader shader) {
|
||||
pWorld->each<Camera, Transform>([&](Entity *ent, ComponentHandle<Camera> camera, ComponentHandle<Transform> camera_transform) {
|
||||
shader.setMat4("projection", camera->projection);
|
||||
shader.setMat4("view", camera_transform->matrix);
|
||||
|
||||
pWorld->each<Mesh, Transform>([&](Entity *ent, ComponentHandle<Mesh> mesh, ComponentHandle<Transform> transform) {
|
||||
shader.setMat4("model", transform->matrix);
|
||||
|
||||
mesh->render();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
float gravityAmount;
|
||||
};
|
||||
|
||||
#endif //ECSGAME_RENDERSYSTEM_H
|
@ -5,3 +5,5 @@ C++ ECS OpenGL game demo
|
||||
Some code adapted from Joey de Vries' (https://twitter.com/JoeyDeVriez) tutorials at https://learnopengl.com/About licensed under https://creativecommons.org/licenses/by-nc/4.0/.
|
||||
|
||||
OBJ-Loader from https://github.com/Bly7/OBJ-Loader.
|
||||
|
||||
ECS library from https://github.com/redxdev/ECS.
|
@ -9,6 +9,7 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
Shader::Shader(const char *vertexPath, const char *fragmentPath) {
|
||||
// 1. retrieve the vertex/fragment source code from filePath
|
||||
@ -87,6 +88,10 @@ void Shader::setFloat(const std::string &name, float value) const {
|
||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
|
||||
void Shader::setMat4(const std::string &name, glm::mat4 mat) const {
|
||||
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 10, GL_FALSE, glm::value_ptr(mat));
|
||||
}
|
||||
|
||||
void Shader::checkCompileErrors(unsigned int shader, const std::string &type) {
|
||||
int success;
|
||||
char infoLog[1024];
|
||||
|
@ -6,6 +6,7 @@
|
||||
#define ECSGAME_SHADER_H
|
||||
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
@ -28,6 +29,9 @@ public:
|
||||
/// Set a uniform float in the shader
|
||||
void setFloat(const std::string &name, float value) const;
|
||||
|
||||
/// Set a uniform mat4 in the shader
|
||||
void setMat4(const std::string &name, glm::mat4 mat) const;
|
||||
|
||||
private:
|
||||
static void checkCompileErrors(unsigned int shader, const std::string &type);
|
||||
};
|
||||
|
27
main.cpp
27
main.cpp
@ -9,6 +9,7 @@
|
||||
#include "ECS/Systems/GravitySystem.h"
|
||||
#include "ECS/Systems/PositionDebugSystem.h"
|
||||
#include "ECS/Systems/KeyboardMovementSystem.h"
|
||||
#include "ECS/Systems/RenderSystem.h"
|
||||
|
||||
using namespace ECS;
|
||||
|
||||
@ -24,17 +25,6 @@ static void key_callback(GLFWwindow *window, int key, int scancode, int action,
|
||||
}
|
||||
|
||||
int main() {
|
||||
// TODO: Could be automated by getting all classes within 'ECS/Systems'
|
||||
world->registerSystem(new GravitySystem(-9.8f));
|
||||
world->registerSystem(new PositionDebugOutputSystem());
|
||||
world->registerSystem(new KeyboardMovementSystem());
|
||||
|
||||
Entity *ent = world->create();
|
||||
ent->assign<Transform>();
|
||||
ent->assign<Movement>(glm::vec3(1.f, 1.f, 1.f));
|
||||
|
||||
ComponentHandle<Transform> pos = ent->get<Transform>();
|
||||
|
||||
GLFWwindow *window;
|
||||
|
||||
/* Initialize the library */
|
||||
@ -61,6 +51,21 @@ int main() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// TODO: Could be automated by getting all classes within 'ECS/Systems'
|
||||
// world->registerSystem(new GravitySystem(-9.8f));
|
||||
world->registerSystem(new PositionDebugOutputSystem());
|
||||
world->registerSystem(new KeyboardMovementSystem());
|
||||
world->registerSystem(new RenderSystem());
|
||||
|
||||
Entity *player = world->create();
|
||||
player->assign<Transform>();
|
||||
player->assign<Movement>(glm::vec3(1.f, 1.f, 1.f));
|
||||
player->assign<Camera>(70.0f, 640, 480, 0.1f, 100.0f);
|
||||
|
||||
Entity *box = world->create();
|
||||
box->assign<Transform>();
|
||||
box->get<Transform>()->translate(glm::vec3(0.0f, 0.0f, -10.0f));
|
||||
|
||||
Shader defaultShader("Shaders/default-vertex.vs", "Shaders/default-fragment.fs");
|
||||
|
||||
// set up vertex data (and buffer(s)) and configure vertex attributes
|
||||
|
Loading…
x
Reference in New Issue
Block a user