Skip to content
Snippets Groups Projects
Commit 02f30c6c authored by Joseph Walton-Rivers's avatar Joseph Walton-Rivers
Browse files

cleanup static model rendering code

parent 5ded7174
No related branches found
No related tags found
No related merge requests found
......@@ -30,6 +30,44 @@
namespace fggl::gfx::ogl4 {
static std::shared_ptr<ogl::ArrayBuffer> setupArrayBuffer(std::shared_ptr<ogl::VertexArray>& vao,
std::vector<data::Vertex>& data) {
auto buff = std::make_shared<ogl::ArrayBuffer>();
buff->write(data.size() * sizeof(data::Vertex), data.data(), ogl::BufUsage::STATIC_DRAW);
// set up the vertex attributes
auto posAttr = ogl::attribute<data::Vertex, math::vec3>(offsetof(data::Vertex, posititon));
auto normalAttr = ogl::attribute<data::Vertex, math::vec3>(offsetof(data::Vertex, normal));
auto colAttr = ogl::attribute<data::Vertex, math::vec3>(offsetof(data::Vertex, colour));
vao->setAttribute( *buff.get(), 0, posAttr );
vao->setAttribute( *buff.get(), 1, normalAttr );
vao->setAttribute( *buff.get(), 3, colAttr );
return buff;
}
static std::shared_ptr<ogl::ElementBuffer> setupIndexBuffer(std::shared_ptr<ogl::VertexArray>& vao,
std::vector<uint32_t>& data) {
auto elementBuffer = std::make_shared<ogl::ElementBuffer>();
elementBuffer->write(data.size() * sizeof(uint32_t),
data.data(), ogl::BufUsage::STATIC_DRAW);
return elementBuffer;
}
static void setupComponent(StaticModel* modelComp, std::shared_ptr<ogl::Shader>& shader, data::Mesh& mesh) {
auto vao = std::make_shared< ogl::VertexArray >();
auto meshBuffer = setupArrayBuffer(vao, mesh.vertexList());
auto elementBuffer = setupIndexBuffer(vao, mesh.indexList());
// set up the element attributes
modelComp->vao = vao;
modelComp->vertexData = meshBuffer;
modelComp->elements = elementBuffer;
modelComp->pipeline = shader;
modelComp->elementCount = mesh.indexCount();
modelComp->drawType = ogl::Primative::TRIANGLE;
}
void StaticModelRenderer::resolveModels(ecs3::World &world) {
// FIXME: this needs something reactive or performance will suck.
auto renderables = world.findMatching<gfx::StaticMesh>();
......@@ -40,38 +78,8 @@ namespace fggl::gfx::ogl4 {
}
auto* meshComp = world.get<gfx::StaticMesh>(renderable);
// create a vao to store the mesh
auto vao = std::make_shared< ogl::VertexArray >();
vao->bind();
// setup the mesh buffer stuff
auto meshToUpload = meshComp->mesh;
auto meshBuffer = std::make_shared<ogl::ArrayBuffer>();
spdlog::info("mesh data to upload: {}", meshToUpload.vertexCount());
meshBuffer->write(meshToUpload.vertexCount() * sizeof(data::Vertex), meshToUpload.vertexList().data(), ogl::BufUsage::STATIC_DRAW);
// set up the vertex attributes
auto posAttr = ogl::attribute<data::Vertex, math::vec3>(offsetof(data::Vertex, posititon));
auto normalAttr = ogl::attribute<data::Vertex, math::vec3>(offsetof(data::Vertex, normal));
auto colAttr = ogl::attribute<data::Vertex, math::vec3>(offsetof(data::Vertex, colour));
vao->setAttribute( *meshBuffer.get(), 0, posAttr );
vao->setAttribute( *meshBuffer.get(), 1, normalAttr );
vao->setAttribute( *meshBuffer.get(), 3, colAttr );
// set up the element attributes
auto elementBuffer = std::make_shared<ogl::ElementBuffer>();
elementBuffer->write(meshToUpload.indexCount() * sizeof(uint32_t), meshToUpload.indexList().data(), ogl::BufUsage::STATIC_DRAW);
auto* modelComp = world.add<StaticModel>(renderable);
modelComp->vao = vao;
modelComp->vertexData = meshBuffer;
modelComp->elements = elementBuffer;
modelComp->pipeline = m_phong;
modelComp->elementCount = meshToUpload.indexCount();
modelComp->drawType = ogl::Primative::TRIANGLE;
setupComponent(modelComp, m_phong, meshComp->mesh);
// no active model, we need to resolve/load one.
spdlog::info("looks like {} needs a static mesh", renderable);
......@@ -86,40 +94,16 @@ namespace fggl::gfx::ogl4 {
}
auto* heightmap = world.get<data::HeightMap>(renderable);
// create a vao to store the mesh
auto vao = std::make_shared< ogl::VertexArray >();
vao->bind();
// setup the mesh buffer stuff
data::Mesh meshToUpload{};
data::generateHeightMesh(heightmap, meshToUpload);
auto meshBuffer = std::make_shared<ogl::ArrayBuffer>();
spdlog::info("mesh data to upload: {}", meshToUpload.vertexCount());
meshBuffer->write(meshToUpload.vertexCount() * sizeof(data::Vertex), meshToUpload.vertexList().data(), ogl::BufUsage::STATIC_DRAW);
// set up the vertex attributes
auto posAttr = ogl::attribute<data::Vertex, math::vec3>(offsetof(data::Vertex, posititon));
auto normalAttr = ogl::attribute<data::Vertex, math::vec3>(offsetof(data::Vertex, normal));
auto colAttr = ogl::attribute<data::Vertex, math::vec3>(offsetof(data::Vertex, colour));
vao->setAttribute( *meshBuffer.get(), 0, posAttr );
vao->setAttribute( *meshBuffer.get(), 1, normalAttr );
vao->setAttribute( *meshBuffer.get(), 3, colAttr );
// set up the element attributes
auto elementBuffer = std::make_shared<ogl::ElementBuffer>();
elementBuffer->write(meshToUpload.indexCount() * sizeof(uint32_t), meshToUpload.indexList().data(), ogl::BufUsage::STATIC_DRAW);
data::Mesh heightMapMesh{};
data::generateHeightMesh(heightmap, heightMapMesh);
auto* modelComp = world.add<StaticModel>(renderable);
modelComp->vao = vao;
modelComp->vertexData = meshBuffer;
modelComp->elements = elementBuffer;
modelComp->pipeline = m_phong;
modelComp->elementCount = meshToUpload.indexCount();
setupComponent(modelComp, m_phong, heightMapMesh);
// we know this is a triangle strip with a restart vertex...
// FIXME the model should be telling us this...
modelComp->drawType = ogl::Primative::TRIANGLE_STRIP;
modelComp->restartIndex = meshToUpload.restartVertex;
modelComp->restartIndex = heightMapMesh.restartVertex;
// no active model, we need to resolve/load one.
spdlog::info("looks like heightmap, {} needs a static mesh", renderable);
......@@ -137,8 +121,6 @@ namespace fggl::gfx::ogl4 {
return;
}
float deltaTime = 0.0f;
// perform a rendering pass for each camera (will usually only be one...)
for ( auto& cameraEnt : cameras ){
//TODO should be cliping this to only visible objects
......@@ -154,8 +136,8 @@ namespace fggl::gfx::ogl4 {
auto* const camTransform = world.get<math::Transform>(cameraEnt);
auto* const camComp = world.get<gfx::Camera>(cameraEnt);
math::mat4 projectionMatrix = glm::perspective(camComp->fov, camComp->aspectRatio, camComp->nearPlane, camComp->farPlane);
math::mat4 viewMatrix = glm::lookAt( camTransform->origin(), camComp->target, camTransform->up() );
const math::mat4 projectionMatrix = glm::perspective(camComp->fov, camComp->aspectRatio, camComp->nearPlane, camComp->farPlane);
const math::mat4 viewMatrix = glm::lookAt( camTransform->origin(), camComp->target, camTransform->up() );
// TODO lighting needs to not be this...
math::vec3 lightPos{20.0f, 20.0f, 15.0f};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment