Skip to content

Commit

Permalink
Use Phong shader instead of Flat.
Browse files Browse the repository at this point in the history
  • Loading branch information
mosra committed Aug 19, 2019
1 parent 9575dcd commit e05f9a5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/esp/assets/GltfMeshData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ void GltfMeshData::uploadBuffersToGPU(bool forceReload) {
renderingBuffer_.reset();
renderingBuffer_ = std::make_unique<GltfMeshData::RenderingBuffer>();
// position, normals, uv, colors are bound to corresponding attributes
renderingBuffer_->mesh = Magnum::MeshTools::compile(*meshData_);
renderingBuffer_->mesh = Magnum::MeshTools::compile(
*meshData_, Magnum::MeshTools::CompileFlag::GenerateSmoothNormals);
buffersOnGPU_ = true;
}

Expand Down
37 changes: 27 additions & 10 deletions src/esp/assets/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <Magnum/Math/Range.h>
#include <Magnum/Math/Tags.h>
#include <Magnum/PixelFormat.h>
#include <Magnum/Shaders/Flat.h>
#include <Magnum/Shaders/Phong.h>
#include <Magnum/Trade/AbstractImporter.h>
#include <Magnum/Trade/ImageData.h>
#include <Magnum/Trade/MeshObjectData3D.h>
Expand Down Expand Up @@ -657,28 +657,45 @@ Magnum::GL::AbstractShaderProgram* ResourceManager::getShaderProgram(

case COLORED_SHADER: {
shaderPrograms_[COLORED_SHADER] =
std::make_shared<Magnum::Shaders::Flat3D>(
Magnum::Shaders::Flat3D::Flag::ObjectId);
std::make_shared<Magnum::Shaders::Phong>(
Magnum::Shaders::Phong::Flag::ObjectId, 3 /*lights*/);
} break;

case VERTEX_COLORED_SHADER: {
shaderPrograms_[VERTEX_COLORED_SHADER] =
std::make_shared<Magnum::Shaders::Flat3D>(
Magnum::Shaders::Flat3D::Flag::ObjectId |
Magnum::Shaders::Flat3D::Flag::VertexColor);
std::make_shared<Magnum::Shaders::Phong>(
Magnum::Shaders::Phong::Flag::ObjectId |
Magnum::Shaders::Phong::Flag::VertexColor,
3 /*lights*/);
} break;

case TEXTURED_SHADER: {
shaderPrograms_[TEXTURED_SHADER] =
std::make_shared<Magnum::Shaders::Flat3D>(
Magnum::Shaders::Flat3D::Flag::ObjectId |
Magnum::Shaders::Flat3D::Flag::Textured);
std::make_shared<Magnum::Shaders::Phong>(
Magnum::Shaders::Phong::Flag::ObjectId |
Magnum::Shaders::Phong::Flag::DiffuseTexture,
3 /*lights*/);
} break;

default:
return nullptr;
break;
}

/* Default setup for Phong, shared by all models */
if (type == COLORED_SHADER || type == VERTEX_COLORED_SHADER ||
type == TEXTURED_SHADER) {
using namespace Magnum::Math::Literals;

static_cast<Magnum::Shaders::Phong&>(*shaderPrograms_[TEXTURED_SHADER])
.setLightPositions({Magnum::Vector3{10.0f, 10.0f, 10.0f} * 100.0f,
Magnum::Vector3{-5.0f, -5.0f, 10.0f} * 100.0f,
Magnum::Vector3{0.0f, 10.0f, -10.0f} * 100.0f})
.setLightColors({0xffffff_rgbf * 0.8f, 0xffcccc_rgbf * 0.8f,
0xccccff_rgbf * 0.8f})
.setSpecularColor(0x11111100_rgbaf)
.setShininess(80.0f);
}
}
return shaderPrograms_[type].get();
}
Expand Down Expand Up @@ -1088,7 +1105,7 @@ gfx::Drawable& ResourceManager::createDrawable(
texture};
} else { // all other shaders use GenericShader
auto* shader =
static_cast<Magnum::Shaders::Flat3D*>(getShaderProgram(shaderType));
static_cast<Magnum::Shaders::Phong*>(getShaderProgram(shaderType));
drawable = new gfx::GenericDrawable{node, *shader, mesh, group,
texture, objectId, color};
}
Expand Down
26 changes: 14 additions & 12 deletions src/esp/gfx/GenericDrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "GenericDrawable.h"

#include <Magnum/Shaders/Flat.h>
#include <Magnum/Shaders/Phong.h>

#include "esp/scene/SceneNode.h"

Expand All @@ -13,7 +13,7 @@ namespace gfx {

GenericDrawable::GenericDrawable(
scene::SceneNode& node,
Magnum::Shaders::Flat3D& shader,
Magnum::Shaders::Phong& shader,
Magnum::GL::Mesh& mesh,
Magnum::SceneGraph::DrawableGroup3D* group /* = nullptr */,
Magnum::GL::Texture2D* texture /* = nullptr */,
Expand All @@ -26,20 +26,22 @@ GenericDrawable::GenericDrawable(

void GenericDrawable::draw(const Magnum::Matrix4& transformationMatrix,
Magnum::SceneGraph::Camera3D& camera) {
Magnum::Shaders::Flat3D& shader =
static_cast<Magnum::Shaders::Flat3D&>(shader_);
shader.setTransformationProjectionMatrix(camera.projectionMatrix() *
transformationMatrix);

if ((shader.flags() & Magnum::Shaders::Flat3D::Flag::Textured) && texture_) {
shader.bindTexture(*texture_);
Magnum::Shaders::Phong& shader =
static_cast<Magnum::Shaders::Phong&>(shader_);
shader.setTransformationMatrix(transformationMatrix)
.setProjectionMatrix(camera.projectionMatrix())
.setNormalMatrix(transformationMatrix.rotationScaling())
.setObjectId(node_.getId());

if ((shader.flags() & Magnum::Shaders::Phong::Flag::DiffuseTexture) &&
texture_) {
shader.bindDiffuseTexture(*texture_);
}

if (!(shader.flags() & Magnum::Shaders::Flat3D::Flag::VertexColor)) {
shader.setColor(color_);
if (!(shader.flags() & Magnum::Shaders::Phong::Flag::VertexColor)) {
shader.setDiffuseColor(color_);
}

shader.setObjectId(node_.getId());
mesh_.draw(shader_);
}

Expand Down
2 changes: 1 addition & 1 deletion src/esp/gfx/GenericDrawable.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class GenericDrawable : public Drawable {
//! Adds drawable to given group and uses provided texture, objectId, and
//! color for textured, object id buffer and color shader output respectively
explicit GenericDrawable(scene::SceneNode& node,
Magnum::Shaders::Flat3D& shader,
Magnum::Shaders::Phong& shader,
Magnum::GL::Mesh& mesh,
Magnum::SceneGraph::DrawableGroup3D* group = nullptr,
Magnum::GL::Texture2D* texture = nullptr,
Expand Down

0 comments on commit e05f9a5

Please sign in to comment.