Skip to content

Commit

Permalink
OpenGL mesh render wip
Browse files Browse the repository at this point in the history
  • Loading branch information
amasson42 committed Sep 4, 2024
1 parent 8dee2b2 commit dcd5ab5
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Engine/Render/include/Render/OpenGL/OpenGLRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class OpenGLRenderer : public Renderer {

private:
std::pair<uint32_t, uint32_t> _frameSize;

std::unique_ptr<class RendererInternals> _internals;
friend class RendererInternals;
};

} // namespace Stone::Render::OpenGL
5 changes: 4 additions & 1 deletion Engine/Render/src/Render/OpenGL/OpenGLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ static void initializeOpenGL() {
}
}

OpenGLRenderer::OpenGLRenderer(RendererSettings &settings) : Renderer(), _frameSize(settings.frame_size) {
OpenGLRenderer::OpenGLRenderer(RendererSettings &settings) : Renderer(), _frameSize(settings.frame_size),
_internals(nullptr) {

initializeOpenGL();

std::cout << "OpenGLRenderer created" << std::endl;
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;

_internals = std::make_unique<RendererInternals>();
}

OpenGLRenderer::~OpenGLRenderer() {
Expand Down
100 changes: 96 additions & 4 deletions Engine/Render/src/Render/OpenGL/Renderable/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,102 @@

namespace Stone::Render::OpenGL {

class DynamicMesh : public Scene::IRendererObject {
struct VRAMMesh {

VRAMMesh(const std::shared_ptr<Scene::DynamicMesh> &mesh, const std::shared_ptr<OpenGLRenderer> &renderer) {
(void)renderer;
if (mesh == nullptr) {
return;
}

glGenVertexArrays(1, &elementsBuffer);
if (elementsBuffer == 0) {
throw std::runtime_error("Failed to generate vertex array buffer");
}

glGenBuffers(1, &verticesBuffer);
if (verticesBuffer == 0) {
glDeleteVertexArrays(1, &elementsBuffer);
throw std::runtime_error("Failed to generate indices buffer");
}

glGenBuffers(1, &indicesBuffer);
if (indicesBuffer == 0) {
glDeleteBuffers(1, &verticesBuffer);
glDeleteVertexArrays(1, &elementsBuffer);
throw std::runtime_error("Failed to generate indices buffer");
}

glBindVertexArray(elementsBuffer);

glBindBuffer(GL_ARRAY_BUFFER, verticesBuffer);
glBufferData(GL_ARRAY_BUFFER, mesh->getVertices().size() * sizeof(Scene::Vertex), mesh->getVertices().data(),
GL_STATIC_DRAW);

numIndices = mesh->getIndices().size();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(uint32_t), mesh->getIndices().data(), GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Scene::Vertex),
(void *)offsetof(Scene::Vertex, position));

glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Scene::Vertex), (void *)offsetof(Scene::Vertex, normal));

glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Scene::Vertex),
(void *)offsetof(Scene::Vertex, tangent));

glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Scene::Vertex),
(void *)offsetof(Scene::Vertex, bitangent));

glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(Scene::Vertex), (void *)offsetof(Scene::Vertex, uv));
}

~VRAMMesh() {
if (verticesBuffer != 0) {
glDeleteBuffers(1, &verticesBuffer);
}
if (indicesBuffer != 0) {
glDeleteBuffers(1, &indicesBuffer);
}
if (elementsBuffer != 0) {
glDeleteVertexArrays(1, &elementsBuffer);
}
}

GLuint verticesBuffer = 0;
GLuint indicesBuffer = 0;
GLuint elementsBuffer = 0;
GLsizei numIndices = 0;
};

class RendererMesh : public Scene::IRendererObject {

public:
RendererMesh(const std::shared_ptr<Scene::DynamicMesh> &mesh, const std::shared_ptr<OpenGLRenderer> &renderer)
: _vramMesh(mesh, renderer) {
}

~RendererMesh() override {
}

const VRAMMesh &getVRAMMesh() const {
return _vramMesh;
}

private:
VRAMMesh _vramMesh;
};


class DynamicMesh : public RendererMesh {
public:
DynamicMesh(const std::shared_ptr<Scene::DynamicMesh> &mesh, const std::shared_ptr<OpenGLRenderer> &renderer)
: _mesh(mesh), _renderer(renderer) {
: RendererMesh(mesh, renderer), _mesh(mesh), _renderer(renderer) {
}

~DynamicMesh() override {
Expand All @@ -27,10 +119,10 @@ class DynamicMesh : public Scene::IRendererObject {
std::weak_ptr<OpenGLRenderer> _renderer;
};

class StaticMesh : public Scene::IRendererObject {
class StaticMesh : public RendererMesh {
public:
StaticMesh(const std::shared_ptr<Scene::StaticMesh> &mesh, const std::shared_ptr<OpenGLRenderer> &renderer)
: _mesh(mesh), _renderer(renderer) {
: RendererMesh(mesh->getSourceMesh(), renderer), _mesh(mesh), _renderer(renderer) {
}

~StaticMesh() override {
Expand Down
38 changes: 37 additions & 1 deletion Engine/Render/src/Render/OpenGL/Renderable/MeshNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

#pragma once

#include "Mesh.hpp"
#include "Render/OpenGL/OpenGLRenderer.hpp"
#include "Scene/Node/MeshNode.hpp"
#include "Scene/Renderable/Mesh.hpp"

#include <GL/glew.h>

template <typename T, typename... Args>
const std::shared_ptr<T> &first_non_null(const std::shared_ptr<T> &ptr, const Args &...args) {
if (ptr != nullptr) {
return ptr;
}
return first_non_null(args...);
}

namespace Stone::Render::OpenGL {

class MeshNode : public Scene::IRendererObject {
Expand All @@ -19,7 +29,33 @@ class MeshNode : public Scene::IRendererObject {
}

void render(Scene::RenderContext &context) override {
(void)context;
assert(_meshNode.expired() == false);

auto meshNode = _meshNode.lock();
auto mesh = meshNode->getMesh();
if (mesh == nullptr) {
return;
}

auto rendererMesh = mesh->getRendererObject<RendererMesh>();
if (rendererMesh == nullptr) {
return;
}

auto material = first_non_null(meshNode->getMaterial(), mesh->getDefaultMaterial(), );

const VRAMMesh &vramMesh = rendererMesh->getVRAMMesh();

// TODO: Retrieve the corect shader program to use

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

// TODO: Send uniforms from context to shader

// Send material uniforms to program
}

private:
Expand Down
24 changes: 24 additions & 0 deletions Engine/Render/src/Render/OpenGL/RendererInternals.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2024 StoneEngine

#pragma once

#include "Scene/RenderContext.hpp"

#include "Renderable/Material.hpp"

namespace Stone::Render::OpenGL {

class RendererInternals {
public:
RendererInternals() = default;
RendererInternals(const RendererInternals &other) = delete;

~RendererInternals() = default;

static RendererInternals& getFrom(OpenGLRenderer &renderer) {
return *(renderer._internals);
}

};

} // namespace Stone::Render::OpenGL
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ void MeshNode::_createDescriptorPool(const std::shared_ptr<SwapChain> &swapChain
if (shader) {
material->forEachTextures(
[&](const std::pair<const std::string, std::shared_ptr<Scene::Texture>> &texture) {
(void)texture;
VkDescriptorPoolSize poolSize = {};
poolSize.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
poolSize.descriptorCount = swapChain->getImageCount();
Expand Down
8 changes: 8 additions & 0 deletions Engine/Scene/src/Scene/RendererObjectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,26 @@ void RendererObjectManager::updateMaterial(const std::shared_ptr<Material> &mate
}

void RendererObjectManager::updateDynamicMesh(const std::shared_ptr<DynamicMesh> &mesh) {
if (mesh->getDefaultMaterial())
updateMaterial(mesh->getDefaultMaterial());
mesh->markUndirty();
}

void RendererObjectManager::updateStaticMesh(const std::shared_ptr<StaticMesh> &mesh) {
if (mesh->getDefaultMaterial())
updateMaterial(mesh->getDefaultMaterial());
mesh->markUndirty();
}

void RendererObjectManager::updateDynamicSkinMesh(const std::shared_ptr<DynamicSkinMesh> &skinmesh) {
if (skinmesh->getDefaultMaterial())
updateMaterial(skinmesh->getDefaultMaterial());
skinmesh->markUndirty();
}

void RendererObjectManager::updateStaticSkinMesh(const std::shared_ptr<StaticSkinMesh> &skinmesh) {
if (skinmesh->getDefaultMaterial())
updateMaterial(skinmesh->getDefaultMaterial());
skinmesh->markUndirty();
}

Expand Down

0 comments on commit dcd5ab5

Please sign in to comment.