Skip to content

Commit

Permalink
Introduce enum for Metal or OpenGL ShaderRenderer default camera init…
Browse files Browse the repository at this point in the history
…ialization
  • Loading branch information
nicolassavva-autodesk committed Aug 23, 2023
1 parent 649ac88 commit c00d0b4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
14 changes: 9 additions & 5 deletions source/MaterialXRender/ShaderRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,26 @@ const float DEFAULT_FAR_PLANE = 100.0f;
// ShaderRenderer methods
//

ShaderRenderer::ShaderRenderer(unsigned int width, unsigned int height, Image::BaseType baseType) :
ShaderRenderer::ShaderRenderer(unsigned int width, unsigned int height, Image::BaseType baseType, ConventionAPI conventionAPI) :
_width(width),
_height(height),
_baseType(baseType)
_baseType(baseType),
_conventionAPI(conventionAPI)
{
// Initialize a default camera.
float fH = std::tan(DEFAULT_FIELD_OF_VIEW / 360.0f * PI) * DEFAULT_NEAR_PLANE;
float fW = fH * 1.0f;
_camera = Camera::create();
_camera->setViewMatrix(Camera::createViewMatrix(DEFAULT_EYE_POSITION, DEFAULT_TARGET_POSITION, DEFAULT_UP_VECTOR));

#if defined (__APPLE__)
if (_conventionAPI == ShaderRenderer::ConventionAPI::METAL)
{
_camera->setProjectionMatrix(Camera::createPerspectiveMatrixZP(-fW, fW, -fH, fH, DEFAULT_NEAR_PLANE, DEFAULT_FAR_PLANE));
#else
}
else // ConventionAPI::OPENGL (default)
{
_camera->setProjectionMatrix(Camera::createPerspectiveMatrix(-fW, fW, -fH, fH, DEFAULT_NEAR_PLANE, DEFAULT_FAR_PLANE));
#endif
}
}

void ShaderRenderer::createProgram(ShaderPtr)
Expand Down
11 changes: 10 additions & 1 deletion source/MaterialXRender/ShaderRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ using ShaderRendererPtr = std::shared_ptr<class ShaderRenderer>;
class MX_RENDER_API ShaderRenderer
{
public:
/// API viewing conventions designation (default to OpenGL).
enum class ConventionAPI
{
OPENGL = 0,
METAL = 1
};
/// A map with name and source code for each shader stage.
using StageMap = StringMap;

Expand Down Expand Up @@ -123,13 +129,16 @@ class MX_RENDER_API ShaderRenderer
/// @}

protected:
ShaderRenderer(unsigned int width, unsigned int height, Image::BaseType baseType);
ShaderRenderer(unsigned int width, unsigned int height, Image::BaseType baseType,
ConventionAPI conventionAPI = ConventionAPI::OPENGL);

protected:
unsigned int _width;
unsigned int _height;
Image::BaseType _baseType;

ConventionAPI _conventionAPI;

CameraPtr _camera;
ImageHandlerPtr _imageHandler;
GeometryHandlerPtr _geometryHandler;
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXRenderGlsl/GlslRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ GlslRendererPtr GlslRenderer::create(unsigned int width, unsigned int height, Im
}

GlslRenderer::GlslRenderer(unsigned int width, unsigned int height, Image::BaseType baseType) :
ShaderRenderer(width, height, baseType),
ShaderRenderer(width, height, baseType, ConventionAPI::OPENGL),
_initialized(false),
_screenColor(DEFAULT_SCREEN_COLOR_LIN_REC709)
{
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXRenderMsl/MslRenderer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}

MslRenderer::MslRenderer(unsigned int width, unsigned int height, Image::BaseType baseType) :
ShaderRenderer(width, height, baseType),
ShaderRenderer(width, height, baseType, ConventionAPI::METAL),
_initialized(false),
_screenColor(DEFAULT_SCREEN_COLOR_LIN_REC709)
{
Expand Down

0 comments on commit c00d0b4

Please sign in to comment.