ADD: Rendering cubes
This commit is contained in:
parent
2088dde6ca
commit
30c9683780
@ -95,6 +95,8 @@ add_library(Engine STATIC
|
|||||||
engine/src/utils/openglWrapper/openglObjects/Vao.h
|
engine/src/utils/openglWrapper/openglObjects/Vao.h
|
||||||
engine/src/utils/openglWrapper/shader/ShaderProgram.cpp
|
engine/src/utils/openglWrapper/shader/ShaderProgram.cpp
|
||||||
engine/src/utils/openglWrapper/shader/ShaderProgram.h
|
engine/src/utils/openglWrapper/shader/ShaderProgram.h
|
||||||
|
engine/src/renderer/primitives/CubeFactory.cpp
|
||||||
|
engine/src/renderer/primitives/CubeFactory.h
|
||||||
)
|
)
|
||||||
target_include_directories(Engine PUBLIC engine/src)
|
target_include_directories(Engine PUBLIC engine/src)
|
||||||
target_link_libraries(Engine PUBLIC OpenGL::GL glfw glad glm::glm imgui)
|
target_link_libraries(Engine PUBLIC OpenGL::GL glfw glad glm::glm imgui)
|
||||||
@ -104,6 +106,10 @@ add_executable(ColorRace
|
|||||||
game/src/main.cpp
|
game/src/main.cpp
|
||||||
game/src/ColorRaceApp.cpp
|
game/src/ColorRaceApp.cpp
|
||||||
game/src/ColorRaceApp.h
|
game/src/ColorRaceApp.h
|
||||||
|
engine/src/renderer/model/RawModel.cpp
|
||||||
|
engine/src/renderer/model/RawModel.h
|
||||||
|
engine/src/renderer/model/Entity.cpp
|
||||||
|
engine/src/renderer/model/Entity.h
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
21
engine/src/renderer/model/Entity.cpp
Normal file
21
engine/src/renderer/model/Entity.cpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 29.07.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Entity.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
engine::Entity::Entity(std::shared_ptr<engine::RawModel> model, glm::vec3 position, glm::vec3 rotation,
|
||||||
|
glm::vec3 scale) : m_model(std::move(model)), m_position(position), m_rotation(rotation), m_scale(scale) {
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::mat4 engine::Entity::getModelMatrix() const {
|
||||||
|
auto modelMatrix = glm::mat4(1.0f);
|
||||||
|
modelMatrix = glm::translate(modelMatrix, m_position);
|
||||||
|
modelMatrix = glm::rotate(modelMatrix, glm::radians(m_rotation.x), {1.0f, 0.0f, 0.0f});
|
||||||
|
modelMatrix = glm::rotate(modelMatrix, glm::radians(m_rotation.y), {0.0f, 1.0f, 0.0f});
|
||||||
|
modelMatrix = glm::rotate(modelMatrix, glm::radians(m_rotation.z), {0.0f, 0.0f, 1.0f});
|
||||||
|
modelMatrix = glm::scale(modelMatrix, m_scale);
|
||||||
|
return modelMatrix;
|
||||||
|
}
|
||||||
41
engine/src/renderer/model/Entity.h
Normal file
41
engine/src/renderer/model/Entity.h
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 29.07.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef COLORRACE_ENTITY_H
|
||||||
|
#define COLORRACE_ENTITY_H
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "RawModel.h"
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
|
namespace engine {
|
||||||
|
class Entity {
|
||||||
|
public:
|
||||||
|
explicit Entity(std::shared_ptr<engine::RawModel> model,
|
||||||
|
glm::vec3 position = {0.0f, 0.0f, 0.0f},
|
||||||
|
glm::vec3 rotation = {0.0f, 0.0f, 0.0f},
|
||||||
|
glm::vec3 scale = { 1.0f, 1.0f, 1.0f});
|
||||||
|
|
||||||
|
void setPosition(glm::vec3 position) { m_position = position; };
|
||||||
|
void setRotation(glm::vec3 rotation) { m_rotation = rotation; };
|
||||||
|
void setScale(glm::vec3 scale) {m_scale = scale; };
|
||||||
|
|
||||||
|
[[nodiscard]] const glm::vec3& getPosition() const { return m_position; };
|
||||||
|
[[nodiscard]] const glm::vec3& getRotation() const { return m_rotation; };
|
||||||
|
[[nodiscard]] const glm::vec3& getScale() const { return m_scale; };
|
||||||
|
|
||||||
|
glm::mat4 getModelMatrix() const;
|
||||||
|
RawModel& getModel() const { return *m_model; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<engine::RawModel> m_model;
|
||||||
|
glm::vec3 m_position;
|
||||||
|
glm::vec3 m_rotation;
|
||||||
|
glm::vec3 m_scale;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //COLORRACE_ENTITY_H
|
||||||
26
engine/src/renderer/model/RawModel.cpp
Normal file
26
engine/src/renderer/model/RawModel.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 29.07.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "RawModel.h"
|
||||||
|
using namespace engine;
|
||||||
|
|
||||||
|
RawModel::RawModel(std::unique_ptr<Vao> vao, int vertexCount, bool isIndexed) : m_vao(std::move(vao)), m_vertexCount(vertexCount), m_isIndexed(isIndexed) {}
|
||||||
|
|
||||||
|
RawModel::RawModel(std::unique_ptr<Vao> vao, int vertexCount) : RawModel(std::move(vao), vertexCount, false) {}
|
||||||
|
|
||||||
|
void RawModel::bind() const {
|
||||||
|
m_vao->bind();
|
||||||
|
}
|
||||||
|
|
||||||
|
void RawModel::unbind() const {
|
||||||
|
m_vao->unbind();
|
||||||
|
}
|
||||||
|
|
||||||
|
void RawModel::draw() const {
|
||||||
|
if (m_isIndexed) {
|
||||||
|
glDrawElements(GL_TRIANGLES, m_vertexCount, GL_UNSIGNED_INT, nullptr);
|
||||||
|
} else {
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, m_vertexCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
32
engine/src/renderer/model/RawModel.h
Normal file
32
engine/src/renderer/model/RawModel.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 29.07.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef COLORRACE_RAWMODEL_H
|
||||||
|
#define COLORRACE_RAWMODEL_H
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "utils/openglWrapper/openglObjects/Vao.h"
|
||||||
|
|
||||||
|
namespace engine {
|
||||||
|
class RawModel {
|
||||||
|
public:
|
||||||
|
RawModel(std::unique_ptr<Vao> vao, int vertexCount, bool isIndexed);
|
||||||
|
RawModel(std::unique_ptr<Vao> vao, int vertexCount);
|
||||||
|
|
||||||
|
void bind() const;
|
||||||
|
void unbind() const;
|
||||||
|
void draw() const;
|
||||||
|
|
||||||
|
int getVertexCount() const { return m_vertexCount; }
|
||||||
|
bool isIndexed() const { return m_isIndexed; }
|
||||||
|
private:
|
||||||
|
std::unique_ptr<Vao> m_vao;
|
||||||
|
int m_vertexCount;
|
||||||
|
bool m_isIndexed;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //COLORRACE_RAWMODEL_H
|
||||||
38
engine/src/renderer/primitives/CubeFactory.cpp
Normal file
38
engine/src/renderer/primitives/CubeFactory.cpp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 29.07.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "CubeFactory.h"
|
||||||
|
|
||||||
|
std::shared_ptr<engine::RawModel> engine::CubeFactory::createCube() {
|
||||||
|
float vertices[] = {
|
||||||
|
-0.5f, -0.5f, 0.5f, // 0
|
||||||
|
0.5f, -0.5f, 0.5f, // 1
|
||||||
|
0.5f, 0.5f, 0.5f, // 2
|
||||||
|
-0.5f, 0.5f, 0.5f, // 3
|
||||||
|
-0.5f, -0.5f, -0.5f, // 4
|
||||||
|
0.5f, -0.5f, -0.5f, // 5
|
||||||
|
0.5f, 0.5f, -0.5f, // 6
|
||||||
|
-0.5f, 0.5f, -0.5f, // 7
|
||||||
|
};
|
||||||
|
|
||||||
|
unsigned int indices[] = {
|
||||||
|
0, 1, 2, 2, 3, 0, // front
|
||||||
|
1, 5, 6, 6, 2, 1, // right
|
||||||
|
5, 4, 7, 7, 6, 5, // back
|
||||||
|
4, 0, 3, 3, 7, 4, // left
|
||||||
|
3, 2, 6, 6, 7, 3, // top
|
||||||
|
4, 5, 1, 1, 0, 4, // bottom
|
||||||
|
};
|
||||||
|
|
||||||
|
auto vao = Vao::create();
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<Attribute>> attribs;
|
||||||
|
attribs.push_back(std::make_unique<Vec3Attribute>(0));
|
||||||
|
vao->initDataFeed(vertices, sizeof(vertices), GL_STATIC_DRAW, std::move(attribs));
|
||||||
|
|
||||||
|
vao->createIndexBuffer(indices, 36);
|
||||||
|
vao->unbind();
|
||||||
|
|
||||||
|
return std::make_shared<RawModel>(std::move(vao), 36, true);
|
||||||
|
}
|
||||||
16
engine/src/renderer/primitives/CubeFactory.h
Normal file
16
engine/src/renderer/primitives/CubeFactory.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 29.07.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef COLORRACE_CUBEFACTORY_H
|
||||||
|
#define COLORRACE_CUBEFACTORY_H
|
||||||
|
#include "renderer/model/RawModel.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace engine::CubeFactory {
|
||||||
|
std::shared_ptr<RawModel> createCube();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif //COLORRACE_CUBEFACTORY_H
|
||||||
@ -7,20 +7,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
void ColorRaceApp::initTriangle() {
|
|
||||||
float vertices[] = {
|
|
||||||
-0.5f, -0.5f, 0.0f,
|
|
||||||
0.5f, -0.5f, 0.0f,
|
|
||||||
0.0f, 0.5f, 0.0f,
|
|
||||||
};
|
|
||||||
|
|
||||||
m_triangleVao = engine::Vao::create();
|
|
||||||
|
|
||||||
std::vector<std::unique_ptr<engine::Attribute>> attributes;
|
|
||||||
attributes.push_back(std::make_unique<engine::Vec3Attribute>(0));
|
|
||||||
m_triangleVao->initDataFeed(vertices, sizeof(vertices), GL_STATIC_DRAW, std::move(attributes));
|
|
||||||
m_triangleVao->unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ColorRaceApp::onUpdate(float deltaTime) {
|
void ColorRaceApp::onUpdate(float deltaTime) {
|
||||||
|
|
||||||
@ -28,8 +15,10 @@ void ColorRaceApp::onUpdate(float deltaTime) {
|
|||||||
|
|
||||||
void ColorRaceApp::onRender() {
|
void ColorRaceApp::onRender() {
|
||||||
m_renderState.apply();
|
m_renderState.apply();
|
||||||
m_triangleVao->bind();
|
|
||||||
m_shader->start();
|
m_shader->start();
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
m_cubeEntity->getModel().bind();
|
||||||
|
m_cubeEntity->getModel().draw();
|
||||||
|
m_cubeEntity->getModel().unbind();
|
||||||
m_shader->stop();
|
m_shader->stop();
|
||||||
}
|
}
|
||||||
@ -5,6 +5,8 @@
|
|||||||
#ifndef COLORRACE_COLORRACEAPP_H
|
#ifndef COLORRACE_COLORRACEAPP_H
|
||||||
#define COLORRACE_COLORRACEAPP_H
|
#define COLORRACE_COLORRACEAPP_H
|
||||||
#include "core/Application.h"
|
#include "core/Application.h"
|
||||||
|
#include "renderer/model/Entity.h"
|
||||||
|
#include "renderer/primitives/CubeFactory.h"
|
||||||
#include "utils/openglWrapper/GLRenderState.h"
|
#include "utils/openglWrapper/GLRenderState.h"
|
||||||
#include "utils/openglWrapper/openglObjects/Vao.h"
|
#include "utils/openglWrapper/openglObjects/Vao.h"
|
||||||
#include "utils/openglWrapper/shader/ShaderProgram.h"
|
#include "utils/openglWrapper/shader/ShaderProgram.h"
|
||||||
@ -28,12 +30,14 @@ public:
|
|||||||
m_renderState.backfaceCulling = false;
|
m_renderState.backfaceCulling = false;
|
||||||
|
|
||||||
m_shader = std::make_unique<engine::ShaderProgram>("assets/shaders/basic.vert", "assets/shaders/basic.frag");
|
m_shader = std::make_unique<engine::ShaderProgram>("assets/shaders/basic.vert", "assets/shaders/basic.frag");
|
||||||
initTriangle();
|
|
||||||
|
auto cubeModel = engine::CubeFactory::createCube();
|
||||||
|
m_cubeEntity = std::make_unique<engine::Entity>(cubeModel);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
engine::GLRenderState m_renderState;
|
engine::GLRenderState m_renderState;
|
||||||
std::unique_ptr<engine::ShaderProgram> m_shader;
|
std::unique_ptr<engine::ShaderProgram> m_shader;
|
||||||
std::unique_ptr<engine::Vao> m_triangleVao;
|
std::unique_ptr<engine::Entity> m_cubeEntity;
|
||||||
|
|
||||||
void initTriangle();
|
void initTriangle();
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user