mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
4.8 KiB
108 lines
4.8 KiB
|
13 years ago
|
/*
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
13 years ago
|
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
|
||
|
|
|
||
|
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||
|
|
copy of this software and associated documentation files (the "Software"),
|
||
|
|
to deal in the Software without restriction, including without limitation
|
||
|
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||
|
|
and/or sell copies of the Software, and to permit persons to whom the
|
||
|
|
Software is furnished to do so, subject to the following conditions:
|
||
|
|
|
||
|
|
The above copyright notice and this permission notice shall be included
|
||
|
|
in all copies or substantial portions of the Software.
|
||
|
|
|
||
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||
|
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||
|
|
DEALINGS IN THE SOFTWARE.
|
||
|
13 years ago
|
*/
|
||
|
|
|
||
|
|
#include "ForceRenderer.h"
|
||
|
|
|
||
|
|
#include "Buffer.h"
|
||
|
|
#include "Mesh.h"
|
||
|
|
#include "DebugTools/ResourceManager.h"
|
||
|
|
#include "SceneGraph/AbstractCamera.h"
|
||
|
13 years ago
|
#include "Shaders/Flat.h"
|
||
|
13 years ago
|
|
||
|
|
#include "DebugTools/Implementation/ForceRendererTransformation.h"
|
||
|
|
|
||
|
|
namespace Magnum { namespace DebugTools {
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
template<UnsignedInt dimensions> ResourceKey shaderKey();
|
||
|
|
template<> inline ResourceKey shaderKey<2>() { return ResourceKey("FlatShader2D"); }
|
||
|
|
template<> inline ResourceKey shaderKey<3>() { return ResourceKey("FlatShader3D"); }
|
||
|
|
|
||
|
13 years ago
|
/** @bug Why this is not constexpr under GCC 4.6? */
|
||
|
|
#ifndef CORRADE_GCC46_COMPATIBILITY
|
||
|
13 years ago
|
constexpr std::array<Vector2, 4> positions{{
|
||
|
13 years ago
|
#else
|
||
|
|
const std::array<Vector2, 4> positions{{
|
||
|
|
#endif
|
||
|
13 years ago
|
{0.0f, 0.0f},
|
||
|
|
{1.0f, 0.0f},
|
||
|
|
{0.9f, 0.1f},
|
||
|
|
{0.9f, -0.1f}
|
||
|
|
}};
|
||
|
|
|
||
|
13 years ago
|
#ifndef CORRADE_GCC46_COMPATIBILITY
|
||
|
13 years ago
|
constexpr std::array<UnsignedByte, 6> indices{{
|
||
|
13 years ago
|
#else
|
||
|
|
const std::array<UnsignedByte, 6> indices{{
|
||
|
|
#endif
|
||
|
13 years ago
|
0, 1,
|
||
|
|
1, 2,
|
||
|
|
1, 3
|
||
|
|
}};
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
template<UnsignedInt dimensions> ForceRenderer<dimensions>::ForceRenderer(SceneGraph::AbstractObject<dimensions, Float>& object, const typename DimensionTraits<dimensions, Float>::VectorType& forcePosition, const typename DimensionTraits<dimensions, Float>::VectorType& force, ResourceKey options, SceneGraph::DrawableGroup<dimensions, Float>* drawables): SceneGraph::Drawable<dimensions, Float>(object, drawables), forcePosition(forcePosition), force(force), options(ResourceManager::instance().get<ForceRendererOptions>(options)) {
|
||
|
13 years ago
|
/* Shader */
|
||
|
13 years ago
|
shader = ResourceManager::instance().get<AbstractShaderProgram, Shaders::Flat<dimensions>>(shaderKey<dimensions>());
|
||
|
|
if(!shader) ResourceManager::instance().set<AbstractShaderProgram>(shader.key(), new Shaders::Flat<dimensions>);
|
||
|
13 years ago
|
|
||
|
|
/* Mesh and vertex buffer */
|
||
|
13 years ago
|
mesh = ResourceManager::instance().get<Mesh>("force");
|
||
|
|
vertexBuffer = ResourceManager::instance().get<Buffer>("force-vertices");
|
||
|
|
indexBuffer = ResourceManager::instance().get<Buffer>("force-indices");
|
||
|
13 years ago
|
if(mesh) return;
|
||
|
|
|
||
|
|
/* Create the mesh */
|
||
|
|
Buffer* vertexBuffer = new Buffer(Buffer::Target::Array);
|
||
|
|
Buffer* indexBuffer = new Buffer(Buffer::Target::ElementArray);
|
||
|
|
|
||
|
13 years ago
|
vertexBuffer->setData(positions, BufferUsage::StaticDraw);
|
||
|
13 years ago
|
ResourceManager::instance().set(this->vertexBuffer.key(), vertexBuffer, ResourceDataState::Final, ResourcePolicy::Manual);
|
||
|
13 years ago
|
|
||
|
13 years ago
|
indexBuffer->setData(indices, BufferUsage::StaticDraw);
|
||
|
13 years ago
|
ResourceManager::instance().set(this->indexBuffer.key(), indexBuffer, ResourceDataState::Final, ResourcePolicy::Manual);
|
||
|
13 years ago
|
|
||
|
|
Mesh* mesh = new Mesh;
|
||
|
13 years ago
|
mesh->setPrimitive(MeshPrimitive::Lines)
|
||
|
13 years ago
|
.setIndexCount(indices.size())
|
||
|
13 years ago
|
.addVertexBuffer(*vertexBuffer, 0,
|
||
|
13 years ago
|
typename Shaders::Flat<dimensions>::Position(Shaders::Flat<dimensions>::Position::Components::Two))
|
||
|
13 years ago
|
.setIndexBuffer(*indexBuffer, 0, Mesh::IndexType::UnsignedByte, 0, positions.size());
|
||
|
|
ResourceManager::instance().set(this->mesh.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual);
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
template<UnsignedInt dimensions> void ForceRenderer<dimensions>::draw(const typename DimensionTraits<dimensions, Float>::MatrixType& transformationMatrix, SceneGraph::AbstractCamera<dimensions, Float>& camera) {
|
||
|
|
shader->setTransformationProjectionMatrix(camera.projectionMatrix()*Implementation::forceRendererTransformation<dimensions>(transformationMatrix.transformPoint(forcePosition), force)*DimensionTraits<dimensions, Float>::MatrixType::scaling(typename DimensionTraits<dimensions, Float>::VectorType(options->scale())))
|
||
|
13 years ago
|
.setColor(options->color())
|
||
|
|
.use();
|
||
|
13 years ago
|
mesh->draw();
|
||
|
|
}
|
||
|
|
|
||
|
|
template class ForceRenderer<2>;
|
||
|
|
template class ForceRenderer<3>;
|
||
|
|
|
||
|
|
}}
|