Browse Source

Moved flat shader from internal physics implementation to Shaders.

It is so general that it can be reused elsewhere.
pull/7/head
Vladimír Vondruš 14 years ago
parent
commit
d46bc34390
  1. 2
      CMakeLists.txt
  2. 4
      modules/FindMagnum.cmake
  3. 10
      src/Physics/CMakeLists.txt
  4. 4
      src/Physics/DebugDrawResourceManager.cpp
  5. 4
      src/Physics/Implementation/AbstractDebugRenderer.cpp
  6. 8
      src/Physics/Implementation/AbstractDebugRenderer.h
  7. 6
      src/Physics/Implementation/BoxRenderer.cpp
  8. 6
      src/Shaders/CMakeLists.txt
  9. 22
      src/Shaders/FlatShader.cpp
  10. 39
      src/Shaders/FlatShader.h
  11. 0
      src/Shaders/FlatShader2D.frag
  12. 0
      src/Shaders/FlatShader2D.vert

2
CMakeLists.txt

@ -15,7 +15,7 @@ cmake_dependent_option(WITH_MESHTOOLS "Build MeshTools library" OFF "NOT WITH_EV
cmake_dependent_option(WITH_PHYSICS "Build Physics library" OFF "NOT WITH_EVERYTHING" ON)
cmake_dependent_option(WITH_PRIMITIVES "Builf Primitives library" OFF "NOT WITH_EVERYTHING" ON)
cmake_dependent_option(WITH_SCENEGRAPH "Build SceneGraph library" OFF "NOT WITH_EVERYTHING;NOT WITH_PHYSICS" ON)
cmake_dependent_option(WITH_SHADERS "Build Shaders library" OFF "NOT WITH_EVERYTHING" ON)
cmake_dependent_option(WITH_SHADERS "Build Shaders library" OFF "NOT WITH_EVERYTHING;NOT WITH_PHYSICS" ON)
option(WITH_GLXWINDOWCONTEXT "Build GlxWindowContext library" OFF)
cmake_dependent_option(WITH_XEGLWINDOWCONTEXT "Build XEglWindowContext library" OFF "TARGET_GLES" OFF)

4
modules/FindMagnum.cmake

@ -16,8 +16,8 @@
# libraries. Additional dependencies are specified by the components. The
# optional components are:
# MeshTools - MeshTools library
# Physics - Physics library (depends on Primitives and SceneGraph
# components)
# Physics - Physics library (depends on Primitives, SceneGraph and
# Shaders components)
# Primitives - Library with stock geometric primitives (static)
# SceneGraph - Scene graph library
# Shaders - Library with stock shaders

10
src/Physics/CMakeLists.txt

@ -1,7 +1,3 @@
corrade_add_resource(MagnumPhysics_RCS MagnumPhysics
Implementation/ShapeShader2D.vert ALIAS ShapeShader2D.vert
Implementation/ShapeShader2D.frag ALIAS ShapeShader2D.frag)
set(MagnumPhysics_SRCS
AbstractShape.cpp
AxisAlignedBox.cpp
@ -17,9 +13,7 @@ set(MagnumPhysics_SRCS
Sphere.cpp
Implementation/AbstractDebugRenderer.cpp
Implementation/BoxRenderer.cpp
Implementation/ShapeShader.cpp
${MagnumPhysics_RCS})
Implementation/BoxRenderer.cpp)
set(MagnumPhysics_HEADERS
AbstractShape.h
AxisAlignedBox.h
@ -39,7 +33,7 @@ set(MagnumPhysics_HEADERS
add_library(MagnumPhysics SHARED ${MagnumPhysics_SRCS})
target_link_libraries(MagnumPhysics Magnum MagnumPrimitives MagnumSceneGraph)
target_link_libraries(MagnumPhysics Magnum MagnumPrimitives MagnumSceneGraph MagnumShaders)
install(TARGETS MagnumPhysics DESTINATION ${MAGNUM_LIBRARY_INSTALL_DIR})
install(FILES ${MagnumPhysics_HEADERS} DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/Physics)

4
src/Physics/DebugDrawResourceManager.cpp

@ -18,13 +18,13 @@
#include "AbstractShaderProgram.h"
#include "Buffer.h"
#include "Mesh.h"
#include "Shaders/FlatShader.h"
#include "AbstractShape.h"
#include "Box.h"
#include "ShapedObject.h"
#include "ShapeGroup.h"
#include "Implementation/AbstractDebugRenderer.h"
#include "Implementation/BoxRenderer.h"
#include "Implementation/ShapeShader.h"
namespace Magnum {
@ -52,7 +52,7 @@ SceneGraph::Object2D* DebugDrawResourceManager::createDebugMesh(SceneGraph::Obje
DebugDrawResourceManager::DebugDrawResourceManager() {
setFallback<Options>(new Options);
set<AbstractShaderProgram>("shader2d", new Implementation::ShapeShader<2>, ResourceDataState::Final, ResourcePolicy::Resident);
set<AbstractShaderProgram>("shader2d", new Shaders::FlatShader<2>, ResourceDataState::Final, ResourcePolicy::Resident);
}
DebugDrawResourceManager::~DebugDrawResourceManager() {}

4
src/Physics/Implementation/AbstractDebugRenderer.cpp

@ -18,11 +18,11 @@
#include "AbstractShaderProgram.h"
#include "Mesh.h"
#include "Physics/DebugDrawResourceManager.h"
#include "ShapeShader.h"
#include "Shaders/FlatShader.h"
namespace Magnum { namespace Physics { namespace Implementation {
template<std::uint8_t dimensions> AbstractDebugRenderer<dimensions>::AbstractDebugRenderer(ResourceKey shader, ResourceKey mesh, ResourceKey options, typename SceneGraph::AbstractObject<dimensions>::ObjectType* parent): SceneGraph::AbstractObject<dimensions>::ObjectType(parent), shader(DebugDrawResourceManager::instance()->get<AbstractShaderProgram, ShapeShader<dimensions>>(shader)), mesh(DebugDrawResourceManager::instance()->get<Mesh>(mesh)), options(DebugDrawResourceManager::instance()->get<Options>(options)) {}
template<std::uint8_t dimensions> AbstractDebugRenderer<dimensions>::AbstractDebugRenderer(ResourceKey shader, ResourceKey mesh, ResourceKey options, typename SceneGraph::AbstractObject<dimensions>::ObjectType* parent): SceneGraph::AbstractObject<dimensions>::ObjectType(parent), shader(DebugDrawResourceManager::instance()->get<AbstractShaderProgram, Shaders::FlatShader<dimensions>>(shader)), mesh(DebugDrawResourceManager::instance()->get<Mesh>(mesh)), options(DebugDrawResourceManager::instance()->get<Options>(options)) {}
template class AbstractDebugRenderer<2>;
template class AbstractDebugRenderer<3>;

8
src/Physics/Implementation/AbstractDebugRenderer.h

@ -24,18 +24,20 @@ namespace Magnum {
class AbstractShaderProgram;
class Mesh;
namespace Shaders {
template<std::uint8_t> class FlatShader;
}
namespace Physics { namespace Implementation {
struct Options;
template<std::uint8_t> class ShapeShader;
template<std::uint8_t dimensions> class AbstractDebugRenderer: public SceneGraph::AbstractObject<dimensions>::ObjectType {
public:
AbstractDebugRenderer(ResourceKey shader, ResourceKey mesh, ResourceKey options, typename SceneGraph::AbstractObject<dimensions>::ObjectType* parent);
protected:
Resource<AbstractShaderProgram, ShapeShader<dimensions>> shader;
Resource<AbstractShaderProgram, Shaders::FlatShader<dimensions>> shader;
Resource<Mesh> mesh;
Resource<Options> options;
};

6
src/Physics/Implementation/BoxRenderer.cpp

@ -20,7 +20,7 @@
#include "Physics/DebugDrawResourceManager.h"
#include "Primitives/Cube.h"
#include "Primitives/Square.h"
#include "ShapeShader.h"
#include "Shaders/FlatShader.h"
namespace Magnum { namespace Physics { namespace Implementation {
@ -37,7 +37,7 @@ namespace {
buffer->setData(*square.positions(0), Buffer::Usage::StaticDraw);
return mesh->setPrimitive(square.primitive())
->setVertexCount(square.positions(0)->size())
->addVertexBuffer(buffer, Implementation::ShapeShader<2>::Position());
->addVertexBuffer(buffer, Shaders::FlatShader<2>::Position());
}
};
@ -51,7 +51,7 @@ namespace {
buffer->setData(*cube.positions(0), Buffer::Usage::StaticDraw);
return mesh->setPrimitive(cube.primitive())
->setVertexCount(cube.positions(0)->size())
->addVertexBuffer(buffer, Implementation::ShapeShader<2>::Position());
->addVertexBuffer(buffer, Shaders::FlatShader<2>::Position());
}
};
}

6
src/Shaders/CMakeLists.txt

@ -1,8 +1,12 @@
corrade_add_resource(MagnumShaders_RCS MagnumShaders PhongShader.frag PhongShader.vert)
corrade_add_resource(MagnumShaders_RCS MagnumShaders
FlatShader2D.vert FlatShader2D.frag
PhongShader.frag PhongShader.vert)
set(MagnumShaders_SRCS
FlatShader.cpp
PhongShader.cpp
${MagnumShaders_RCS})
set(MagnumShaders_HEADERS
FlatShader.h
PhongShader.h
magnumShadersVisibility.h)

22
src/Physics/Implementation/ShapeShader.cpp → src/Shaders/FlatShader.cpp

@ -13,30 +13,30 @@
GNU Lesser General Public License version 3 for more details.
*/
#include "ShapeShader.h"
#include "FlatShader.h"
#include <Utility/Resource.h>
#include "Shader.h"
namespace Magnum { namespace Physics { namespace Implementation {
namespace Magnum { namespace Shaders {
namespace {
template<std::uint8_t dimensions> struct ShaderName {};
template<> struct ShaderName<2> {
constexpr static const char* Vertex = "ShapeShader2D.vert";
constexpr static const char* Fragment = "ShapeShader2D.frag";
constexpr static const char* Vertex = "FlatShader2D.vert";
constexpr static const char* Fragment = "FlatShader2D.frag";
};
template<> struct ShaderName<3> {
constexpr static const char* Vertex = "ShapeShader3D.vert";
constexpr static const char* Fragment = "ShapeShader3D.frag";
constexpr static const char* Vertex = "FlatShader3D.vert";
constexpr static const char* Fragment = "FlatShader3D.frag";
};
}
template<std::uint8_t dimensions> ShapeShader<dimensions>::ShapeShader() {
Corrade::Utility::Resource resource("MagnumPhysics");
template<std::uint8_t dimensions> FlatShader<dimensions>::FlatShader() {
Corrade::Utility::Resource resource("MagnumShaders");
attachShader(Shader::fromData(Version::GL330, Shader::Type::Vertex, resource.get(ShaderName<dimensions>::Vertex)));
attachShader(Shader::fromData(Version::GL330, Shader::Type::Fragment, resource.get(ShaderName<dimensions>::Fragment)));
@ -46,7 +46,7 @@ template<std::uint8_t dimensions> ShapeShader<dimensions>::ShapeShader() {
colorUniform = uniformLocation("color");
}
template class ShapeShader<2>;
template class ShapeShader<3>;
template class FlatShader<2>;
template class FlatShader<3>;
}}}
}}

39
src/Physics/Implementation/ShapeShader.h → src/Shaders/FlatShader.h

@ -1,5 +1,5 @@
#ifndef Magnum_Physics_Implementation_ShapeShader_h
#define Magnum_Physics_Implementation_ShapeShader_h
#ifndef Magnum_Shaders_FlatShader_h
#define Magnum_Shaders_FlatShader_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
@ -15,26 +15,46 @@
GNU Lesser General Public License version 3 for more details.
*/
/** @file
* @brief Class Magnum::Shaders::FlatShader
*/
#include "Math/Matrix3.h"
#include "Math/Matrix4.h"
#include "AbstractShaderProgram.h"
#include "Color.h"
#include "DimensionTraits.h"
namespace Magnum { namespace Physics { namespace Implementation {
#include "magnumShadersVisibility.h"
namespace Magnum { namespace Shaders {
/**
@brief Flat shader
template<std::uint8_t dimensions> class ShapeShader: public AbstractShaderProgram {
Draws whole mesh with one color.
*/
template<std::uint8_t dimensions> class SHADERS_EXPORT FlatShader: public AbstractShaderProgram {
public:
/** @brief Vertex position */
typedef Attribute<0, typename DimensionTraits<dimensions, GLfloat>::PointType> Position;
ShapeShader();
FlatShader();
ShapeShader<dimensions>* setTransformationProjection(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& matrix) {
/**
* @brief Set transformation and projection matrix
* @return Pointer to self (for method chaining)
*/
FlatShader<dimensions>* setTransformationProjection(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& matrix) {
setUniform(transformationProjectionUniform, matrix);
return this;
}
ShapeShader<dimensions>* setColor(const Color3<GLfloat>& color) {
/**
* @brief Set color
* @return Pointer to self (for method chaining)
*/
FlatShader<dimensions>* setColor(const Color3<GLfloat>& color) {
setUniform(colorUniform, color);
return this;
}
@ -44,9 +64,6 @@ template<std::uint8_t dimensions> class ShapeShader: public AbstractShaderProgra
colorUniform;
};
extern template class ShapeShader<2>;
extern template class ShapeShader<3>;
}}}
}}
#endif

0
src/Physics/Implementation/ShapeShader2D.frag → src/Shaders/FlatShader2D.frag

0
src/Physics/Implementation/ShapeShader2D.vert → src/Shaders/FlatShader2D.vert

Loading…
Cancel
Save