Nonfunctional new rendering

This commit is contained in:
karl 2020-01-07 15:35:12 +01:00
parent d99ca4c8d7
commit 02b0f6a67a
6 changed files with 67 additions and 69 deletions

View File

@ -10,14 +10,14 @@
#include <vector> #include <vector>
struct Mesh { struct Mesh {
explicit Mesh(std::vector<float> vertices_vector) : vertices(&vertices_vector[0]), vertex_count(vertices_vector.size()) { explicit Mesh(std::vector<float> vertices) : vertices(vertices), vertex_count(vertices.size() / 5) {
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
glBindVertexArray(VAO); glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(&vertices), &vertices, GL_STATIC_DRAW);
// position attribute // position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
@ -39,7 +39,7 @@ private:
unsigned int vertex_count; unsigned int vertex_count;
/// Vertices in the format x, y, z, u, v /// Vertices in the format x, y, z, u, v
float *vertices; std::vector<float> vertices;
}; };
#endif //ECSGAME_MESH_H #endif //ECSGAME_MESH_H

View File

@ -16,9 +16,14 @@ using namespace ECS;
class RenderSystem : public EntitySystem { class RenderSystem : public EntitySystem {
public: public:
void render(World *pWorld, Shader shader) { void render(World *pWorld, Shader shader) {
pWorld->each<Camera, Transform>([&](Entity *ent, ComponentHandle<Camera> camera, ComponentHandle<Transform> camera_transform) { pWorld->each<Camera, Transform>([&](Entity *ent, ComponentHandle<Camera> camera, ComponentHandle<Transform> cameraTransform) {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader.use();
shader.setMat4("projection", camera->projection); shader.setMat4("projection", camera->projection);
shader.setMat4("view", camera_transform->matrix); shader.setMat4("view", cameraTransform->matrix);
pWorld->each<Mesh, Transform>([&](Entity *ent, ComponentHandle<Mesh> mesh, ComponentHandle<Transform> transform) { pWorld->each<Mesh, Transform>([&](Entity *ent, ComponentHandle<Mesh> mesh, ComponentHandle<Transform> transform) {
shader.setMat4("model", transform->matrix); shader.setMat4("model", transform->matrix);

View File

@ -89,7 +89,7 @@ void Shader::setFloat(const std::string &name, float value) const {
} }
void Shader::setMat4(const std::string &name, glm::mat4 mat) const { void Shader::setMat4(const std::string &name, glm::mat4 mat) const {
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 10, GL_FALSE, glm::value_ptr(mat)); glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, glm::value_ptr(mat));
} }
void Shader::checkCompileErrors(unsigned int shader, const std::string &type) { void Shader::checkCompileErrors(unsigned int shader, const std::string &type) {

View File

@ -1,8 +1,7 @@
#version 320 es #version 320 es
out mediump vec4 FragColor; out mediump vec4 FragColor;
in mediump vec3 COLOR;
void main() void main()
{ {
FragColor = vec4(COLOR, 1.0); FragColor = vec4(1.0, 1.0, 1.0, 1.0);
} }

View File

@ -1,11 +1,12 @@
#version 320 es #version 320 es
layout (location = 0) in vec3 aPos; // the position variable has attribute position 0 layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor; // the color variable has attribute position 1 layout (location = 1) in vec3 uvPos;
out vec3 COLOR; // output a color to the fragment shader uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() void main()
{ {
gl_Position = vec4(aPos, 1.0); gl_Position = projection * view * model * vec4(aPos, 1.0);
COLOR = aColor;
} }

105
main.cpp
View File

@ -51,11 +51,15 @@ int main() {
return -1; return -1;
} }
glEnable(GL_DEPTH_TEST);
// TODO: Could be automated by getting all classes within 'ECS/Systems' // TODO: Could be automated by getting all classes within 'ECS/Systems'
// world->registerSystem(new GravitySystem(-9.8f)); // world->registerSystem(new GravitySystem(-9.8f));
world->registerSystem(new PositionDebugOutputSystem()); world->registerSystem(new PositionDebugOutputSystem());
world->registerSystem(new KeyboardMovementSystem()); world->registerSystem(new KeyboardMovementSystem());
world->registerSystem(new RenderSystem());
RenderSystem* renderSystem = new RenderSystem();
world->registerSystem(renderSystem);
Entity *player = world->create(); Entity *player = world->create();
player->assign<Transform>(); player->assign<Transform>();
@ -64,53 +68,53 @@ int main() {
Entity *box = world->create(); Entity *box = world->create();
box->assign<Transform>(); box->assign<Transform>();
box->assign<Mesh>(std::vector<float>{
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
});
box->get<Transform>()->translate(glm::vec3(0.0f, 0.0f, -10.0f)); box->get<Transform>()->translate(glm::vec3(0.0f, 0.0f, -10.0f));
Shader defaultShader("Shaders/default-vertex.vs", "Shaders/default-fragment.fs"); Shader defaultShader("Shaders/default-vertex.vs", "Shaders/default-fragment.fs");
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
// positions // colors
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 2 // first Triangle
};
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3* sizeof(float)));
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound.
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0);
double timeInLastFrame = glfwGetTime(); double timeInLastFrame = glfwGetTime();
double elapsed_time = 0.0; double elapsed_time = 0.0;
@ -124,18 +128,7 @@ int main() {
std::cout << "Elapsed time: " << elapsed_time << std::endl; std::cout << "Elapsed time: " << elapsed_time << std::endl;
world->tick(delta); world->tick(delta);
renderSystem->render(world, defaultShader);
/* Render here */
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
defaultShader.use();
glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
glDrawArrays(GL_TRIANGLES, 0, 3);
// glBindVertexArray(0); // no need to unbind it every time
/* Swap front and back buffers */ /* Swap front and back buffers */
glfwSwapBuffers(window); glfwSwapBuffers(window);