mirror of https://github.com/mosra/magnum.git
Browse Source
Using common base for Box and AABB, as the primitives are the same, but only draw function differs.pull/7/head
10 changed files with 281 additions and 56 deletions
@ -0,0 +1,70 @@
|
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include "AbstractBoxRenderer.h" |
||||
|
||||
#include "Buffer.h" |
||||
#include "Physics/DebugDrawResourceManager.h" |
||||
#include "Primitives/Cube.h" |
||||
#include "Primitives/Square.h" |
||||
#include "Shaders/FlatShader.h" |
||||
|
||||
namespace Magnum { namespace Physics { namespace Implementation { |
||||
|
||||
namespace { |
||||
template<std::uint8_t> struct BoxMesh {}; |
||||
|
||||
template<> struct BoxMesh<2> { |
||||
static ResourceKey shader() { return {"shader2d"}; } |
||||
static ResourceKey key() { return {"box2d"}; } |
||||
|
||||
static Mesh* mesh(Buffer* buffer) { |
||||
Primitives::Square square; |
||||
Mesh* mesh = new Mesh; |
||||
buffer->setData(*square.positions(0), Buffer::Usage::StaticDraw); |
||||
return mesh->setPrimitive(square.primitive()) |
||||
->setVertexCount(square.positions(0)->size()) |
||||
->addVertexBuffer(buffer, Shaders::FlatShader<2>::Position()); |
||||
} |
||||
}; |
||||
|
||||
template<> struct BoxMesh<3> { |
||||
static ResourceKey shader() { return {"shader3d"}; } |
||||
static ResourceKey key() { return {"box3d"}; } |
||||
|
||||
static Mesh* mesh(Buffer* buffer) { |
||||
Primitives::Cube cube; |
||||
Mesh* mesh = new Mesh; |
||||
buffer->setData(*cube.positions(0), Buffer::Usage::StaticDraw); |
||||
return mesh->setPrimitive(cube.primitive()) |
||||
->setVertexCount(cube.positions(0)->size()) |
||||
->addVertexBuffer(buffer, Shaders::FlatShader<2>::Position()); |
||||
} |
||||
}; |
||||
} |
||||
|
||||
template<std::uint8_t dimensions> AbstractBoxRenderer<dimensions>::AbstractBoxRenderer(): AbstractDebugRenderer<dimensions>(BoxMesh<dimensions>::shader(), BoxMesh<dimensions>::key()), buffer(DebugDrawResourceManager::instance()->get<Buffer>(BoxMesh<dimensions>::key())) { |
||||
if(!this->mesh) { |
||||
DebugDrawResourceManager::instance()->set(this->buffer.key(), new Buffer, ResourceDataState::Final, ResourcePolicy::Manual); |
||||
DebugDrawResourceManager::instance()->set<Mesh>(this->mesh.key(), BoxMesh<dimensions>::mesh(buffer), ResourceDataState::Final, ResourcePolicy::Manual); |
||||
} |
||||
} |
||||
|
||||
template<std::uint8_t dimensions> AbstractBoxRenderer<dimensions>::~AbstractBoxRenderer() {} |
||||
|
||||
template class AbstractBoxRenderer<2>; |
||||
template class AbstractBoxRenderer<3>; |
||||
|
||||
}}} |
||||
@ -0,0 +1,40 @@
|
||||
#ifndef Magnum_Physics_Implementation_AbstractBoxRenderer_h |
||||
#define Magnum_Physics_Implementation_AbstractBoxRenderer_h |
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include "AbstractDebugRenderer.h" |
||||
|
||||
#include "magnumCompatibility.h" |
||||
|
||||
namespace Magnum { |
||||
|
||||
class Buffer; |
||||
|
||||
namespace Physics { namespace Implementation { |
||||
|
||||
template<std::uint8_t dimensions> class AbstractBoxRenderer: public AbstractDebugRenderer<dimensions> { |
||||
public: |
||||
AbstractBoxRenderer(); |
||||
|
||||
~AbstractBoxRenderer(); |
||||
|
||||
protected: |
||||
Resource<Buffer> buffer; |
||||
}; |
||||
|
||||
}}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,32 @@
|
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include "AbstractShapeRenderer.h" |
||||
|
||||
#include "AbstractShaderProgram.h" |
||||
#include "Mesh.h" |
||||
#include "Physics/DebugDrawResourceManager.h" |
||||
#include "Shaders/FlatShader.h" |
||||
|
||||
namespace Magnum { namespace Physics { namespace Implementation { |
||||
|
||||
template<std::uint8_t dimensions> AbstractShapeRenderer<dimensions>::AbstractShapeRenderer(ResourceKey shader, ResourceKey mesh): shader(DebugDrawResourceManager::instance()->get<AbstractShaderProgram, Shaders::FlatShader<dimensions>>(shader)), mesh(DebugDrawResourceManager::instance()->get<Mesh>(mesh)) {} |
||||
|
||||
template<std::uint8_t dimensions> AbstractShapeRenderer<dimensions>::~AbstractShapeRenderer() {} |
||||
|
||||
template class AbstractShapeRenderer<2>; |
||||
template class AbstractShapeRenderer<3>; |
||||
|
||||
}}} |
||||
@ -0,0 +1,50 @@
|
||||
#ifndef Magnum_Physics_Implementation_AbstractShapeRenderer_h |
||||
#define Magnum_Physics_Implementation_AbstractShapeRenderer_h |
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include "DimensionTraits.h" |
||||
#include "ResourceManager.h" |
||||
#include "SceneGraph/SceneGraph.h" |
||||
|
||||
namespace Magnum { |
||||
|
||||
class AbstractShaderProgram; |
||||
class Mesh; |
||||
|
||||
namespace Shaders { |
||||
template<std::uint8_t> class FlatShader; |
||||
} |
||||
|
||||
namespace Physics { namespace Implementation { |
||||
|
||||
struct Options; |
||||
|
||||
template<std::uint8_t dimensions> class AbstractShapeRenderer { |
||||
public: |
||||
AbstractShapeRenderer(ResourceKey shader, ResourceKey mesh); |
||||
|
||||
virtual ~AbstractShapeRenderer(); |
||||
|
||||
virtual void draw(Resource<Options>& options, const typename DimensionTraits<dimensions>::MatrixType& transformationMatrix, SceneGraph::AbstractCamera<dimensions, GLfloat>* camera) = 0; |
||||
|
||||
protected: |
||||
Resource<AbstractShaderProgram, Shaders::FlatShader<dimensions>> shader; |
||||
Resource<Mesh> mesh; |
||||
}; |
||||
|
||||
}}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,38 @@
|
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include "AxisAlignedBoxRenderer.h" |
||||
|
||||
#include "Mesh.h" |
||||
#include "Physics/DebugDrawResourceManager.h" |
||||
#include "SceneGraph/AbstractCamera.h" |
||||
#include "Shaders/FlatShader.h" |
||||
|
||||
namespace Magnum { namespace Physics { namespace Implementation { |
||||
|
||||
template<std::uint8_t dimensions> void AxisAlignedBoxRenderer<dimensions>::draw(Resource<Options>& options, const typename DimensionTraits<dimensions, GLfloat>::MatrixType&, typename SceneGraph::AbstractCamera<dimensions>* camera) { |
||||
typename DimensionTraits<dimensions>::MatrixType transformation = |
||||
DimensionTraits<dimensions>::MatrixType::translation(axisAlignedBox.transformedPosition())* |
||||
DimensionTraits<dimensions>::MatrixType::scaling(axisAlignedBox.transformedSize()); |
||||
this->shader->setTransformationProjection(camera->projectionMatrix()*camera->cameraMatrix()*transformation) |
||||
->setColor(options->color) |
||||
->use(); |
||||
this->mesh->draw(); |
||||
} |
||||
|
||||
template class AxisAlignedBoxRenderer<2>; |
||||
template class AxisAlignedBoxRenderer<3>; |
||||
|
||||
}}} |
||||
@ -0,0 +1,38 @@
|
||||
#ifndef Magnum_Physics_Implementation_AxisAlignedBoxRenderer_h |
||||
#define Magnum_Physics_Implementation_AxisAlignedBoxRenderer_h |
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include "AbstractBoxRenderer.h" |
||||
|
||||
#include "Physics/AxisAlignedBox.h" |
||||
|
||||
#include "magnumCompatibility.h" |
||||
|
||||
namespace Magnum { namespace Physics { namespace Implementation { |
||||
|
||||
template<std::uint8_t dimensions> class AxisAlignedBoxRenderer: public AbstractBoxRenderer<dimensions> { |
||||
public: |
||||
inline AxisAlignedBoxRenderer(AxisAlignedBox<dimensions>& axisAlignedBox): axisAlignedBox(axisAlignedBox) {} |
||||
|
||||
void draw(Resource<Options>& options, const typename DimensionTraits<dimensions>::MatrixType& transformation, typename SceneGraph::AbstractCamera<dimensions>* camera) override; |
||||
|
||||
private: |
||||
AxisAlignedBox<dimensions>& axisAlignedBox; |
||||
}; |
||||
|
||||
}}} |
||||
|
||||
#endif |
||||
Loading…
Reference in new issue