Skip to content

Commit

Permalink
opengl mesh node pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
amasson committed Nov 13, 2024
1 parent f83376e commit 77d8916
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 47 deletions.
68 changes: 68 additions & 0 deletions Engine/Render/src/Render/OpenGL/Renderable/MeshNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2024 Stone-Engine

#include "MeshNode.hpp"

#include "../OpenGLResources.hpp"
#include "Material.hpp"
#include "Mesh.hpp"
#include "Render/OpenGL/OpenGLRenderer.hpp"
#include "Scene/Renderable/Mesh.hpp"

#include <GL/glew.h>

namespace Stone::Render::OpenGL {

MeshNode::MeshNode(Scene::MeshNode &meshNode, const std::shared_ptr<OpenGLRenderer> &renderer)
: _meshNode(meshNode), _material(nullptr), _shaderCollection(nullptr) {

if (_meshNode.getMaterial() != nullptr) {
assert(_meshNode.getMaterial()->isDirty() == false);
_material = _meshNode.getMaterial()->getRendererObject<Material>().get();
} else if (_meshNode.getMesh() != nullptr && _meshNode.getMesh()->getDefaultMaterial() != nullptr) {
assert(_meshNode.getMesh()->getDefaultMaterial()->isDirty() == false);
_material = _meshNode.getMesh()->getDefaultMaterial()->getRendererObject<Material>().get();
}
assert(_material != nullptr);

if (_material != nullptr) {
_shaderCollection = _material->getShaderCollection().get();
} else {
_shaderCollection = renderer->getOpenGLResources()->getDefaultShaderCollection().get();
}
assert(_shaderCollection != nullptr);

_shaderCollection->makeMeshProgram();
}

void MeshNode::render(Scene::RenderContext &context) {
auto mesh = _meshNode.getMesh();
if (mesh == nullptr)
return;

auto rendererMesh = mesh->getRendererObject<RendererMesh>();
if (rendererMesh == nullptr) {
return;
}
const VRAMMesh &vramMesh = rendererMesh->getVRAMMesh();

GlShaderProgram *program = _shaderCollection->getMeshProgram().get();
program->use();

if (_material != nullptr) {
_material->setUniforms(Scene::MeshType::Standard);
}

program->setUniform("model", context.mvp.modelMatrix);
program->setUniform("view", context.mvp.viewMatrix);
program->setUniform("projection", context.mvp.projMatrix);

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

glBindVertexArray(vramMesh.elementsBuffer);
glDrawElements(GL_TRIANGLES, vramMesh.numIndices, GL_UNSIGNED_INT, nullptr);
}

} // namespace Stone::Render::OpenGL
55 changes: 8 additions & 47 deletions Engine/Render/src/Render/OpenGL/Renderable/MeshNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,26 @@

#pragma once

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

#include <GL/glew.h>

namespace Stone::Render::OpenGL {

class Material;
class OpenGLRenderer;
class ShaderCollection;

class MeshNode : public Scene::IRendererObject {
public:
MeshNode(Scene::MeshNode &meshNode, const std::shared_ptr<OpenGLRenderer> &renderer)
: _meshNode(meshNode), _renderer(renderer) {

std::shared_ptr<Scene::Material> usedMaterial =
meshNode.getMaterial() ? meshNode.getMaterial()
: meshNode.getMesh() != nullptr && meshNode.getMesh()->getDefaultMaterial() != nullptr
? meshNode.getMesh()->getDefaultMaterial()
: nullptr;
}
MeshNode(Scene::MeshNode &meshNode, const std::shared_ptr<OpenGLRenderer> &renderer);

~MeshNode() override = default;

void render(Scene::RenderContext &context) override {
auto mesh = _meshNode.getMesh();
if (mesh == nullptr)
return;

auto rendererMesh = mesh->getRendererObject<RendererMesh>();
if (rendererMesh == nullptr) {
return;
}
const VRAMMesh &vramMesh = rendererMesh->getVRAMMesh();

std::shared_ptr<Scene::Material> sceneMaterial = _meshNode.getMaterial() ? _meshNode.getMaterial()
: mesh->getDefaultMaterial() ? mesh->getDefaultMaterial()
: nullptr;

assert(sceneMaterial != nullptr);
assert(sceneMaterial->isDirty() == false);

std::shared_ptr<Material> material = sceneMaterial->getRendererObject<Material>();

material->render(context);

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

glBindVertexArray(vramMesh.elementsBuffer);
glDrawElements(GL_TRIANGLES, vramMesh.numIndices, GL_UNSIGNED_INT, 0);
}
void render(Scene::RenderContext &context) override;

private:
Scene::MeshNode &_meshNode;
std::weak_ptr<OpenGLRenderer> _renderer;
Material *_material;
ShaderCollection *_shaderCollection;
};

} // namespace Stone::Render::OpenGL

0 comments on commit 77d8916

Please sign in to comment.