Move some code into source files. Make stb impl.

This commit is contained in:
bailwillharr 2023-01-08 15:22:44 +00:00
parent a266a11615
commit 59cfd3c2f2
17 changed files with 160 additions and 96 deletions

View File

@ -12,10 +12,14 @@ set(SRC_FILES
"src/ecs_system.cpp"
"src/application.cpp"
"src/libs/stb_image.cpp"
"src/systems/transform.cpp"
"src/systems/render.cpp"
"src/resources/shader.cpp"
"src/resources/material.cpp"
"src/resources/mesh.cpp"
"src/resources/texture.cpp"
"src/scene.cpp"

View File

@ -1,16 +1,16 @@
#pragma once
#include "log.hpp"
#include "gfx.hpp"
#include "gfx_device.hpp"
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <string>
#include <vector>
namespace engine {
class GFXDevice;
struct Vertex {
glm::vec3 pos;
glm::vec3 norm;
@ -23,31 +23,19 @@ namespace engine::resources {
class Mesh {
public:
Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices)
: m_gfx(gfx)
{
std::vector<uint32_t> indices(vertices.size());
for (uint32_t i = 0; i < indices.size(); i++) {
indices[i] = i;
}
initMesh(vertices, indices);
}
Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices);
Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices)
: m_gfx(gfx)
{
initMesh(vertices, indices);
}
~Mesh()
{
m_gfx->destroyBuffer(m_ib);
m_gfx->destroyBuffer(m_vb);
}
~Mesh();
Mesh(const Mesh&) = delete;
Mesh& operator=(const Mesh&) = delete;
auto getVB() { return m_vb; }
auto getIB() { return m_ib; }
auto getCount() { return m_count; }
const gfx::Buffer* getVB();
const gfx::Buffer* getIB();
uint32_t getCount();
private:
GFXDevice* const m_gfx;
@ -56,13 +44,7 @@ private:
const gfx::Buffer* m_ib;
uint32_t m_count;
void initMesh(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices)
{
m_vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, vertices.size() * sizeof(Vertex), vertices.data());
m_ib = m_gfx->createBuffer(gfx::BufferType::INDEX, indices.size() * sizeof(uint32_t), indices.data());
m_count = indices.size();
INFO("Loaded mesh, vertices: {}, indices: {}", vertices.size(), indices.size());
}
void initMesh(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices);
};

View File

@ -1,13 +1,10 @@
#pragma once
#include "log.hpp"
#include "gfx.hpp"
#include "gfx_device.hpp"
#include <glm/mat4x4.hpp>
#include <string>
namespace engine {
class GFXDevice;
}
namespace engine::resources {
@ -26,58 +23,12 @@ public:
bool hasColor;
};
Shader(GFXDevice* gfx, const char* vertPath, const char* fragPath, const VertexParams& vertexParams, bool alphaBlending, bool cullBackFace)
: m_gfx(gfx)
{
int index = 0;
uint32_t stride = 0;
gfx::VertexFormat vertFormat{};
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride);
stride += 3 * sizeof(float);
if (vertexParams.hasNormal) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride);
stride += 3 * sizeof(float);
}
if (vertexParams.hasUV0) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride);
stride += 2 * sizeof(float);
}
if (vertexParams.hasUV1) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride);
stride += 2 * sizeof(float);
}
if (vertexParams.hasUV2) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride);
stride += 2 * sizeof(float);
}
if (vertexParams.hasUV3) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride);
stride += 2 * sizeof(float);
}
if (vertexParams.hasTangent) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride);
stride += 3 * sizeof(float);
}
if (vertexParams.hasColor) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride);
stride += 3 * sizeof(float);
}
vertFormat.stride = stride;
m_pipeline = m_gfx->createPipeline(vertPath, fragPath, vertFormat, sizeof(glm::mat4), alphaBlending, cullBackFace);
INFO("Loaded shader: {}, vertex attribs: {}", vertPath, vertFormat.attributeDescriptions.size());
}
~Shader()
{
m_gfx->destroyPipeline(m_pipeline);
}
Shader(GFXDevice* gfx, const char* vertPath, const char* fragPath, const VertexParams& vertexParams, bool alphaBlending, bool cullBackFace);
~Shader();
Shader(const Shader&) = delete;
Shader& operator=(const Shader&) = delete;
const gfx::Pipeline* getPipeline() { return m_pipeline; }
const gfx::Pipeline* getPipeline();
private:
GFXDevice* const m_gfx;

View File

@ -2,6 +2,8 @@
#include "gfx_device.hpp"
#include <string>
namespace engine::resources {
class Texture {

View File

@ -56,10 +56,7 @@ namespace engine {
uint32_t getEntity(const std::string& tag, uint32_t parent = 0);
size_t getComponentSignaturePosition(size_t hash)
{
return m_componentSignaturePositions.at(hash);
}
size_t getComponentSignaturePosition(size_t hash);
template <typename T>
void registerComponent()

View File

@ -16,7 +16,7 @@ namespace engine {
void onUpdate(float ts) override;
void setCameraEntity(uint32_t entity) { m_camera.camEntity = entity; }
void setCameraEntity(uint32_t entity);
private:
struct {

View File

@ -63,10 +63,7 @@ namespace engine {
// Returns true if the window was just resized during the previous frame
bool getWindowResized() const;
// Set the window resized flag (to recalculate aspect ratios and such)
inline void setResizedFlag()
{
m_justResized = true;
}
void setResizedFlag();
// keyboard events

2
src/libs/stb_image.cpp Normal file
View File

@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

47
src/resources/mesh.cpp Normal file
View File

@ -0,0 +1,47 @@
#include "resources/mesh.hpp"
#include "log.hpp"
#include "gfx_device.hpp"
namespace engine::resources {
Mesh::Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices)
: m_gfx(gfx)
{
std::vector<uint32_t> indices(vertices.size());
for (uint32_t i = 0; i < indices.size(); i++) {
indices[i] = i;
}
initMesh(vertices, indices);
}
Mesh::~Mesh()
{
m_gfx->destroyBuffer(m_ib);
m_gfx->destroyBuffer(m_vb);
}
const gfx::Buffer* Mesh::getVB()
{
return m_vb;
}
const gfx::Buffer* Mesh::getIB()
{
return m_ib;
}
uint32_t Mesh::getCount()
{
return m_count;
}
void Mesh::initMesh(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices)
{
m_vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, vertices.size() * sizeof(Vertex), vertices.data());
m_ib = m_gfx->createBuffer(gfx::BufferType::INDEX, indices.size() * sizeof(uint32_t), indices.data());
m_count = (uint32_t)indices.size();
INFO("Loaded mesh, vertices: {}, indices: {}", vertices.size(), indices.size());
}
}

66
src/resources/shader.cpp Normal file
View File

@ -0,0 +1,66 @@
#include "resources/shader.hpp"
#include "gfx_device.hpp"
#include "log.hpp"
#include <glm/mat4x4.hpp>
namespace engine::resources {
Shader::Shader(GFXDevice* gfx, const char* vertPath, const char* fragPath, const VertexParams& vertexParams, bool alphaBlending, bool cullBackFace)
: m_gfx(gfx)
{
int index = 0;
uint32_t stride = 0;
gfx::VertexFormat vertFormat{};
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride);
stride += 3 * sizeof(float);
if (vertexParams.hasNormal) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride);
stride += 3 * sizeof(float);
}
if (vertexParams.hasUV0) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride);
stride += 2 * sizeof(float);
}
if (vertexParams.hasUV1) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride);
stride += 2 * sizeof(float);
}
if (vertexParams.hasUV2) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride);
stride += 2 * sizeof(float);
}
if (vertexParams.hasUV3) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride);
stride += 2 * sizeof(float);
}
if (vertexParams.hasTangent) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride);
stride += 3 * sizeof(float);
}
if (vertexParams.hasColor) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride);
stride += 3 * sizeof(float);
}
vertFormat.stride = stride;
m_pipeline = m_gfx->createPipeline(vertPath, fragPath, vertFormat, sizeof(glm::mat4), alphaBlending, cullBackFace);
INFO("Loaded shader: {}, vertex attribs: {}", vertPath, vertFormat.attributeDescriptions.size());
}
Shader::~Shader()
{
m_gfx->destroyPipeline(m_pipeline);
}
const gfx::Pipeline* Shader::getPipeline()
{
return m_pipeline;
}
}

View File

@ -41,6 +41,11 @@ namespace engine {
return getSystem<TransformSystem>()->getChildEntity(parent, tag);
}
size_t Scene::getComponentSignaturePosition(size_t hash)
{
return m_componentSignaturePositions.at(hash);
}
void Scene::update(float ts)
{

View File

@ -18,7 +18,7 @@ namespace engine {
{
auto scene = std::make_unique<Scene>(m_app);
m_scenes.emplace_back(std::move(scene));
m_activeSceneIndex = m_scenes.size() - 1;
m_activeSceneIndex = (int)m_scenes.size() - 1;
return m_scenes.back().get();
}

View File

@ -79,5 +79,10 @@ namespace engine {
}
void RenderSystem::setCameraEntity(uint32_t entity)
{
{ m_camera.camEntity = entity; }
}
}

View File

@ -1,6 +1,5 @@
#include "util/files.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <fstream>

View File

@ -181,8 +181,8 @@ namespace engine::util {
absPath = absPath.parent_path();
absPath /= texPath.C_Str();
try {
textures[i] = std::make_shared<resources::Texture>(parent->app()->gfx(), absPath);
} catch (const std::runtime_error& e) {
textures[i] = std::make_shared<resources::Texture>(parent->app()->gfx(), absPath.string());
} catch (const std::runtime_error&) {
textures[i] = parent->getResource<resources::Texture>("textures/white.png");
}
}

View File

@ -243,6 +243,11 @@ namespace engine {
return m_justResized;
}
void Window::setResizedFlag()
{
m_justResized = true;
}
void Window::show()
{
SDL_ShowWindow(m_handle);

View File

@ -2,6 +2,8 @@
#include "resources/mesh.hpp"
#include <memory>
// generates a UV sphere
std::unique_ptr<engine::resources::Mesh> genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside = false, bool flipNormals = false);
std::unique_ptr<engine::resources::Mesh> genCuboidMesh(engine::GFXDevice* gfx, float x, float y, float z);