Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable phong shading and lighting configurations #489

Merged
merged 11 commits into from
Feb 19, 2020
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);
matthewjmay marked this conversation as resolved.
Show resolved Hide resolved
buffersOnGPU_ = true;
}

Expand Down
279 changes: 156 additions & 123 deletions src/esp/assets/ResourceManager.cpp

Large diffs are not rendered by default.

161 changes: 94 additions & 67 deletions src/esp/assets/ResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "MeshData.h"
#include "MeshMetaData.h"
#include "esp/gfx/DrawableGroup.h"
#include "esp/gfx/MaterialData.h"
#include "esp/gfx/ShaderManager.h"
#include "esp/gfx/configure.h"
#include "esp/physics/PhysicsManager.h"
#include "esp/scene/SceneNode.h"
Expand Down Expand Up @@ -63,7 +65,7 @@ namespace assets {
class ResourceManager {
public:
/** @brief Constructor */
explicit ResourceManager(){};
explicit ResourceManager();

/** @brief Destructor */
~ResourceManager() {}
Expand Down Expand Up @@ -338,6 +340,32 @@ class ResourceManager {
scene::SceneNode& node,
DrawableGroup* drawables);

/**
* @brief Set a named @ref LightSetup
*
* If this name already exists, the @ref LightSetup is updated and all @ref
* Drawables using this setup are updated.
*
* @param key Key to identify this @ref LightSetup
* @param setup Light setup this key will now references
*/
void setLightSetup(const Magnum::ResourceKey& key, gfx::LightSetup setup);

/**
* @brief Get a named @ref LightSetup
*/
Magnum::Resource<gfx::LightSetup> getLightSetup(
const Magnum::ResourceKey& key);

//! @brief The @ref ShaderManager key for @ref LightInfo which has no lights
static constexpr char NO_LIGHT_KEY[] = "no_lights";

//! @brief The @ref ShaderManager key for the default @ref LightInfo
static constexpr char DEFAULT_LIGHTING_KEY[] = "default_lighting";

//! @brief The @ref ShaderManager key for the default @ref MaterialInfo
static constexpr char DEFAULT_MATERIAL_KEY[] = "default_material";
mosra marked this conversation as resolved.
Show resolved Hide resolved

protected:
//======== Scene Functions ========

Expand All @@ -354,9 +382,12 @@ class ResourceManager {
* rendered.
* @param meshTransformNode The @ref MeshTransformNode for component
* identifying its mesh, material, transformation, and children.
* @param lightSetup The @ref LightSetup key that will be used
* for the added component.
*/
void addComponent(const MeshMetaData& metaData,
scene::SceneNode& parent,
const Magnum::ResourceKey& lightSetup,
DrawableGroup* drawables,
const MeshTransformNode& meshTransformNode);

Expand Down Expand Up @@ -416,8 +447,36 @@ class ResourceManager {
*
* @param importer The importer already loaded with information for the asset.
* @param metaData The asset's @ref MeshMetaData object.
* @param forceFlatShading Only bind ambient textures and colors.
*/
void loadMaterials(Importer& importer,
MeshMetaData* metaData,
bool forceFlatShading = false);

/**
* @brief Get a @ref PhongMaterialData for use with flat shading
*
* Textures must already be loaded for the asset this material belongs to
*
* @param material Material data with texture IDs
* @param textureBaseIndex Base index of the assets textures in textures_
*/
gfx::PhongMaterialData::uptr getFlatShadedMaterialData(
const Magnum::Trade::PhongMaterialData& material,
int textureBaseIndex);

/**
* @brief Get a @ref PhongMaterialData for use with phong shading
*
* Textures must already be loaded for the asset this material belongs to
*
* @param material Material data with texture IDs
* @param textureBaseIndex Base index of the assets textures in textures_

*/
void loadMaterials(Importer& importer, MeshMetaData* metaData);
gfx::PhongMaterialData::uptr getPhongShadedMaterialData(
const Magnum::Trade::PhongMaterialData& material,
int textureBaseIndex);

/**
* @brief Load a PTex mesh into assets from a file and add it to the scene
Expand Down Expand Up @@ -460,7 +519,8 @@ class ResourceManager {
*/
bool loadGeneralMeshData(const AssetInfo& info,
scene::SceneNode* parent = nullptr,
DrawableGroup* drawables = nullptr);
DrawableGroup* drawables = nullptr,
bool forceFlatShading = false);

/**
* @brief Load a SUNCG mesh into assets from a file. !Deprecated! TODO:
Expand All @@ -476,6 +536,16 @@ class ResourceManager {
scene::SceneNode* parent,
DrawableGroup* drawables);

/**
* @brief initialize default lighting setups in the current ShaderManager
*/
void initDefaultLightSetups();

/**
* @brief initialize default material setups in the current ShaderManager
*/
void initDefaultMaterials();

// ======== Geometry helper functions, data structures ========

/**
Expand Down Expand Up @@ -554,9 +624,9 @@ class ResourceManager {
std::vector<std::shared_ptr<Magnum::GL::Texture2D>> textures_;

/**
* @brief The material data for loaded assets.
* @brief The next available unique ID for loaded materials
*/
std::vector<std::shared_ptr<Magnum::Trade::PhongMaterialData>> materials_;
int nextMaterialID_ = 0;

/**
* @brief A pointer to render mesh data for the most recently loaded instance
Expand All @@ -572,6 +642,12 @@ class ResourceManager {
*/
std::map<std::string, MeshMetaData> resourceDict_; // meshes

/**
* @brief The @ref ShaderManager used to store shader information for
* drawables created by this ResourceManager
*/
gfx::ShaderManager shaderManager_;

// ======== Physical parameter data ========

/**
Expand Down Expand Up @@ -629,6 +705,8 @@ class ResourceManager {
* @param metaData Object meta data for the asset this mesh is linked to.
* @param node The @ref scene::SceneNode which the new @ref gfx::Drawable will
* be attached to.
* @param lightSetup The @ref LightSetup key that will be used
* for the added mesh.
* @param drawables The @ref DrawableGroup with which the new @ref
* gfx::Drawable will be rendered.
* @param objectID The object type identifier or semantic group (e.g.
Expand All @@ -640,66 +718,12 @@ class ResourceManager {
*/
void addMeshToDrawables(const MeshMetaData& metaData,
scene::SceneNode& node,
const Magnum::ResourceKey& lightSetup,
DrawableGroup* drawables,
int objectID,
int meshIDLocal,
int materialIDLocal);

/**
* @brief Enumeration of supported shader program options.
*/
enum ShaderType {
/**
* Shader program for instance mesh data. See @ref gfx::PrimitiveIDShader,
* @ref GenericInstanceMeshData, @ref Mp3dInstanceMeshData, @ref
* AssetType::INSTANCE_MESH, @ref loadInstanceMeshData.
*/
INSTANCE_MESH_SHADER = 0,

/**
* Shader program for PTex mesh data. See @ref gfx::PTexMeshShader, @ref
* gfx::PTexMeshDrawable, @ref loadPTexMeshData, @ref PTexMeshData.
*/
PTEX_MESH_SHADER = 1,

/**
* Shader program for flat shading with uniform color. Used to render object
* identifier or semantic types (e.g. chair, table, etc...). Also the
* default shader for assets with unidentified rendering parameters. See
* @ref Magnum::Shaders::Flat3D.
*/
COLORED_SHADER = 2,

/**
* Shader program for vertex color shading. Used to render meshes with
* per-vertex colors defined.
*/
VERTEX_COLORED_SHADER = 3,

/**
* Shader program for meshes with textured defined.
*/
TEXTURED_SHADER = 4,
};

/**
* @brief Maps @ref ShaderType to specific instances of @ref
* Magnum::GL::AbstractShaderProgram.
*
* See @ref getShaderProgram.
*/
std::map<ShaderType, std::shared_ptr<Magnum::GL::AbstractShaderProgram>>
shaderPrograms_;

/**
* @brief Returns a pointer to the specified shader program.
*
* Creates a new shader program for @ref ShaderType if it does not exist.
* @param type The @ref ShaderType of the desired shader program.
* @return A pointer to the specified shader program.
*/
Magnum::GL::AbstractShaderProgram* getShaderProgram(ShaderType type);

/**
* @brief Create a @ref gfx::Drawable for the specified mesh, node,
* and @ref ShaderType.
Expand All @@ -710,6 +734,10 @@ class ResourceManager {
* @param mesh The render mesh.
* @param node The @ref scene::SceneNode to which the drawable will be
* attached.
* @param lightSetup The @ref LightSetup key that will be used
* for the drawable.
* @param material The @ref MaterialData key that will be used
* for the drawable.
* @param meshID Optional, the index of this mesh component stored in meshes_
* @param group Optional @ref DrawableGroup with which the render the @ref
* gfx::Drawable.
Expand All @@ -719,13 +747,12 @@ class ResourceManager {
* @param color Optional color parameter for the shader program. Defaults to
* white.
*/
void createDrawable(const ShaderType shaderType,
Magnum::GL::Mesh& mesh,
scene::SceneNode& node,
DrawableGroup* group = nullptr,
Magnum::GL::Texture2D* texture = nullptr,
int objectId = ID_UNDEFINED,
const Magnum::Color4& color = Magnum::Color4{1});
void createGenericDrawable(Magnum::GL::Mesh& mesh,
scene::SceneNode& node,
const Magnum::ResourceKey& lightSetup,
const Magnum::ResourceKey& material,
DrawableGroup* group = nullptr,
int objectId = ID_UNDEFINED);

/**
* @brief Flag to denote the desire to compress textures. TODO: unused?
Expand Down
4 changes: 4 additions & 0 deletions src/esp/gfx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ set(gfx_SOURCES
DrawableGroup.h
GenericDrawable.cpp
GenericDrawable.h
LightSetup.cpp
LightSetup.h
MaterialData.h
magnum.h
PrimitiveIDDrawable.cpp
PrimitiveIDDrawable.h
Expand All @@ -20,6 +23,7 @@ set(gfx_SOURCES
WindowlessContext.h
RenderTarget.cpp
RenderTarget.h
ShaderManager.h
)

# If ptex support is enabled add relevant source files
Expand Down
6 changes: 1 addition & 5 deletions src/esp/gfx/Drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ namespace esp {
namespace gfx {

Drawable::Drawable(scene::SceneNode& node,
Magnum::GL::AbstractShaderProgram& shader,
Magnum::GL::Mesh& mesh,
DrawableGroup* group /* = nullptr */)
: Magnum::SceneGraph::Drawable3D{node, group},
node_(node),
shader_(shader),
mesh_(mesh) {}
: Magnum::SceneGraph::Drawable3D{node, group}, node_(node), mesh_(mesh) {}

DrawableGroup* Drawable::drawables() {
CORRADE_ASSERT(
Expand Down
4 changes: 2 additions & 2 deletions src/esp/gfx/Drawable.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class Drawable : public Magnum::SceneGraph::Drawable3D {
* @param group Drawable group this drawable will be added to.
*/
Drawable(scene::SceneNode& node,
Magnum::GL::AbstractShaderProgram& shader, // TODO: remove this
Magnum::GL::Mesh& mesh,
DrawableGroup* group = nullptr);
virtual ~Drawable() {}
Expand All @@ -45,6 +44,8 @@ class Drawable : public Magnum::SceneGraph::Drawable3D {
*/
DrawableGroup* drawables();

virtual void setLightSetup(const Magnum::ResourceKey& lightSetup){};

protected:
/**
* @brief Draw the object using given camera
Expand All @@ -59,7 +60,6 @@ class Drawable : public Magnum::SceneGraph::Drawable3D {
Magnum::SceneGraph::Camera3D& camera) = 0;

scene::SceneNode& node_;
Magnum::GL::AbstractShaderProgram& shader_;
Magnum::GL::Mesh& mesh_;
};

Expand Down
Loading