/* Copyright © 2010, 2011, 2012 Vladimír Vondruš 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 "ForceRenderer.h" #include "Buffer.h" #include "Mesh.h" #include "DebugTools/ResourceManager.h" #include "SceneGraph/AbstractCamera.h" #include "Shaders/FlatShader.h" #include "DebugTools/Implementation/ForceRendererTransformation.h" namespace Magnum { namespace DebugTools { namespace { template ResourceKey shaderKey(); template<> inline ResourceKey shaderKey<2>() { return ResourceKey("FlatShader2D"); } template<> inline ResourceKey shaderKey<3>() { return ResourceKey("FlatShader3D"); } constexpr std::array positions{{ {0.0f, 0.0f}, {1.0f, 0.0f}, {0.9f, 0.1f}, {0.9f, -0.1f} }}; constexpr std::array indices{{ 0, 1, 1, 2, 1, 3 }}; } template ForceRenderer::ForceRenderer(SceneGraph::AbstractObject* object, const typename DimensionTraits::VectorType& forcePosition, const typename DimensionTraits::VectorType* force, ResourceKey options, SceneGraph::DrawableGroup* drawables): SceneGraph::Drawable(object, drawables), forcePosition(forcePosition), force(force), options(ResourceManager::instance()->get(options)) { /* Shader */ shader = ResourceManager::instance()->get>(shaderKey()); if(!shader) ResourceManager::instance()->set(shader.key(), new Shaders::FlatShader); /* Mesh and vertex buffer */ mesh = ResourceManager::instance()->get("force"); vertexBuffer = ResourceManager::instance()->get("force-vertices"); indexBuffer = ResourceManager::instance()->get("force-indices"); if(mesh) return; /* Create the mesh */ Buffer* vertexBuffer = new Buffer(Buffer::Target::Array); Buffer* indexBuffer = new Buffer(Buffer::Target::ElementArray); vertexBuffer->setData(positions, Buffer::Usage::StaticDraw); ResourceManager::instance()->set(this->vertexBuffer.key(), vertexBuffer, ResourceDataState::Final, ResourcePolicy::Manual); indexBuffer->setData(indices, Buffer::Usage::StaticDraw); ResourceManager::instance()->set(this->indexBuffer.key(), indexBuffer, ResourceDataState::Final, ResourcePolicy::Manual); Mesh* mesh = new Mesh; mesh->setPrimitive(Mesh::Primitive::Lines) ->setIndexCount(indices.size()) ->addVertexBuffer(vertexBuffer, 0, typename Shaders::FlatShader::Position(Shaders::FlatShader::Position::Components::Two)) ->setIndexBuffer(indexBuffer, 0, Mesh::IndexType::UnsignedByte, 0, positions.size()); ResourceManager::instance()->set(this->mesh.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual); } template void ForceRenderer::draw(const typename DimensionTraits::MatrixType& transformationMatrix, SceneGraph::AbstractCamera* camera) { shader->setTransformationProjectionMatrix(camera->projectionMatrix()*Implementation::forceRendererTransformation(transformationMatrix.translation()+forcePosition, *force)*DimensionTraits::MatrixType::scaling(typename DimensionTraits::VectorType(options->scale()))) ->setColor(options->color()) ->use(); mesh->draw(); } template class ForceRenderer<2>; template class ForceRenderer<3>; }}