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

add ability to render normals for testing purposes

parent 334fec28
No related branches found
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ uniform mat4 model; ...@@ -12,7 +12,7 @@ uniform mat4 model;
void main() void main()
{ {
gl_Position = view * model * vec4(aPos, 1.0); gl_Position = view * model * vec4(aPos, 1.0);
mat3 normalMatrix = mat3(transpose(inverse(view * model))); mat3 normalMatrix = mat3(transpose(inverse(view * model)));
vs_out.normal = normalize(vec3(vec4(normalMatrix * aNormal, 0.0))); vs_out.normal = normalize(vec3(vec4(normalMatrix * aNormal, 0.0)));
} }
...@@ -178,6 +178,7 @@ namespace fggl::gfx { ...@@ -178,6 +178,7 @@ namespace fggl::gfx {
m_cache->load(ShaderConfig::named("phong")); m_cache->load(ShaderConfig::named("phong"));
m_cache->load(ShaderConfig::named("redbook/lighting")); m_cache->load(ShaderConfig::named("redbook/lighting"));
m_cache->load(ShaderConfig::named("redbook/debug")); m_cache->load(ShaderConfig::named("redbook/debug"));
m_cache->load(ShaderConfig::named("normals", true));
// rendering helpers // rendering helpers
m_canvasRenderer = std::make_unique<ogl4::CanvasRenderer>(fonts); m_canvasRenderer = std::make_unique<ogl4::CanvasRenderer>(fonts);
......
...@@ -282,6 +282,58 @@ namespace fggl::gfx::ogl4 { ...@@ -282,6 +282,58 @@ namespace fggl::gfx::ogl4 {
} }
} }
static void forward_normal_pass(const entity::EntityID& camera, const fggl::entity::EntityManager& world, std::shared_ptr<ogl::Shader> shader) {
// enable required OpenGL state
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
// enable depth testing
glEnable(GL_DEPTH_TEST);
// set-up camera matrices
const auto &camTransform = world.get<fggl::math::Transform>(camera);
const auto &camComp = world.get<fggl::gfx::Camera>(camera);
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());
ogl::Location modelUniform = shader->uniform("model");
ogl::Location viewUniform = shader->uniform("view");
ogl::Location projUniform = shader->uniform("projection");
shader->use();
shader->setUniformMtx(projUniform, projectionMatrix);
shader->setUniformMtx(viewUniform, viewMatrix);
auto renderables = world.find<StaticModel>();
for (const auto &entity : renderables) {
// ensure that the model pipeline actually exists...
const auto &model = world.get<StaticModel>(entity);
// set model transform
const auto &transform = world.get<math::Transform>(entity);
shader->setUniformMtx(modelUniform, transform.model());
// render model
auto vao = model.vao;
vao->bind();
model.vertexData->bind();
if (model.restartIndex != NO_RESTART_IDX) {
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(model.restartIndex);
}
auto *elements = model.elements.get();
vao->drawElements(*elements, model.drawType, model.elementCount);
if (model.restartIndex != NO_RESTART_IDX) {
glDisable(GL_PRIMITIVE_RESTART);
}
}
}
void StaticModelRenderer::renderModelsForward(const entity::EntityManager &world) { void StaticModelRenderer::renderModelsForward(const entity::EntityManager &world) {
// fetch cameras we will need to render with // fetch cameras we will need to render with
...@@ -297,6 +349,11 @@ namespace fggl::gfx::ogl4 { ...@@ -297,6 +349,11 @@ namespace fggl::gfx::ogl4 {
for (const auto &cameraEnt : cameras) { for (const auto &cameraEnt : cameras) {
//TODO should be clipping this to only visible objects //TODO should be clipping this to only visible objects
forward_camera_pass(cameraEnt, world); forward_camera_pass(cameraEnt, world);
// enable rendering normals
if ( m_renderNormals ) {
forward_normal_pass(cameraEnt, world, m_shaders->get("normals"));
}
} }
} }
......
...@@ -91,6 +91,8 @@ namespace fggl::gfx::ogl4 { ...@@ -91,6 +91,8 @@ namespace fggl::gfx::ogl4 {
renderModelsForward(world); renderModelsForward(world);
} }
bool m_renderNormals = true;
private: private:
#ifdef FGGL_ALLOW_DEFERRED_UPLOAD #ifdef FGGL_ALLOW_DEFERRED_UPLOAD
......
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