Commit afcad9b3 authored by Mathias Goldau's avatar Mathias Goldau
Browse files

[CHANGE] moved graphics engine helper functions in own classes

parent fdfb0d6e
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Vec3>
#include <osg/Array>
#include "../math/WPosition.h"
#include "WGEGeodeUtils.h"
#include "WGEUtils.h"
osg::ref_ptr< osg::Geode > wge::generateBoundingBoxGeode( const wmath::WPosition& pos1, const wmath::WPosition& pos2, const WColor& color )
{
assert( pos1[0] <= pos2[0] && pos1[1] <= pos2[1] && pos1[2] <= pos2[2] && "pos1 doesn't seem to be the frontLowerLeft corner of the BB!" );
using osg::ref_ptr;
ref_ptr< osg::Vec3Array > vertices = ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
ref_ptr< osg::Vec4Array > colors = ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
ref_ptr< osg::Geometry > geometry = ref_ptr< osg::Geometry >( new osg::Geometry );
vertices->push_back( osg::Vec3( pos1[0], pos1[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos1[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos2[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos1[0], pos2[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos1[0], pos1[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos1[0], pos1[1], pos2[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos1[1], pos2[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos2[1], pos2[2] ) );
vertices->push_back( osg::Vec3( pos1[0], pos2[1], pos2[2] ) );
vertices->push_back( osg::Vec3( pos1[0], pos1[1], pos2[2] ) );
geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP, 0, vertices->size() ) );
vertices->push_back( osg::Vec3( pos1[0], pos2[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos1[0], pos2[1], pos2[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos2[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos2[1], pos2[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos1[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos1[1], pos2[2] ) );
geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::LINES, vertices->size() - 6, 6 ) );
geometry->setVertexArray( vertices );
colors->push_back( wge::osgColor( color ) );
geometry->setColorArray( colors );
geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
osg::ref_ptr< osg::Geode > geode = osg::ref_ptr< osg::Geode >( new osg::Geode );
geode->addDrawable( geometry );
// disable light for this geode as lines can't be lit properly
osg::StateSet* state = geode->getOrCreateStateSet();
state->setMode( GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
return geode;
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WGEGEODEUTILS_H
#define WGEGEODEUTILS_H
#include <osg/Geode>
#include "../common/WColor.h"
#include "../math/WPosition.h"
namespace wge
{
/**
* Generates an OSG geode for the bounding box.
*
* \param pos1 Front lower left corner
* \param pos2 Back upper right corner
* \param color The color in which the bounding box should be generated
*
* \return The OSG geode containing the 12 edges of the box.
*/
osg::ref_ptr< osg::Geode > generateBoundingBoxGeode( const wmath::WPosition& pos1, const wmath::WPosition& pos2, const WColor& color );
} // end of namespace wge
#endif // WGEGEODEUTILS_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <vector>
#include <osg/Array>
#include "../math/WPosition.h"
#include "WGEGeometryUtils.h"
#include "WGEUtils.h"
osg::ref_ptr< osg::Vec3Array > wge::generateCuboidQuads( const std::vector< wmath::WPosition >& corners )
{
osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
// Surfaces
vertices->push_back( wge::osgVec3( corners[0] ) );
vertices->push_back( wge::osgVec3( corners[1] ) );
vertices->push_back( wge::osgVec3( corners[2] ) );
vertices->push_back( wge::osgVec3( corners[3] ) );
vertices->push_back( wge::osgVec3( corners[1] ) );
vertices->push_back( wge::osgVec3( corners[5] ) );
vertices->push_back( wge::osgVec3( corners[6] ) );
vertices->push_back( wge::osgVec3( corners[2] ) );
vertices->push_back( wge::osgVec3( corners[5] ) );
vertices->push_back( wge::osgVec3( corners[4] ) );
vertices->push_back( wge::osgVec3( corners[7] ) );
vertices->push_back( wge::osgVec3( corners[6] ) );
vertices->push_back( wge::osgVec3( corners[4] ) );
vertices->push_back( wge::osgVec3( corners[0] ) );
vertices->push_back( wge::osgVec3( corners[3] ) );
vertices->push_back( wge::osgVec3( corners[7] ) );
vertices->push_back( wge::osgVec3( corners[3] ) );
vertices->push_back( wge::osgVec3( corners[2] ) );
vertices->push_back( wge::osgVec3( corners[6] ) );
vertices->push_back( wge::osgVec3( corners[7] ) );
vertices->push_back( wge::osgVec3( corners[0] ) );
vertices->push_back( wge::osgVec3( corners[1] ) );
vertices->push_back( wge::osgVec3( corners[5] ) );
vertices->push_back( wge::osgVec3( corners[4] ) );
return vertices;
}
osg::Vec3 wge::getQuadNormal( const wmath::WPosition& a,
const wmath::WPosition& b,
const wmath::WPosition& c )
{
wmath::WPosition vec1 = a - b;
wmath::WPosition vec2 = c - b;
wmath::WPosition normal = vec2.crossProduct( vec1 );
normal.normalize();
return wge::osgVec3( normal );
}
osg::ref_ptr< osg::Vec3Array > wge::generateCuboidQuadNormals( const std::vector< wmath::WPosition >& corners )
{
osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
vertices->push_back( getQuadNormal( corners[0], corners[1], corners[2] ) );
vertices->push_back( getQuadNormal( corners[1], corners[5], corners[6] ) );
vertices->push_back( getQuadNormal( corners[5], corners[4], corners[7] ) );
vertices->push_back( getQuadNormal( corners[4], corners[0], corners[3] ) );
vertices->push_back( getQuadNormal( corners[3], corners[2], corners[6] ) );
vertices->push_back( getQuadNormal( corners[0], corners[1], corners[5] ) );
return vertices;
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WGEGEOMETRYUTILS_H
#define WGEGEOMETRYUTILS_H
#include <vector>
#include <osg/Array>
#include "../math/WPosition.h"
namespace wge
{
/**
* Creates out of eight corner vertices QUAD vertices.
*
* \param corners The eight corner vertices which must be in the following order:
*
* \verbatim
z-axis y-axis
| /
| h___/_g
|/: /|
d_:___c |
| :...|.|
|.e | f
|_____|/ ____x-axis
a b
\endverbatim
*
* The QUADS are generated in the following order:
* - a,b,c,d
* - b,f,g,c
* - f,e,h,g
* - e,a,d,h
* - d,c,g,h
* - a,b,f,e
*
* \return OSG vertex array where every four vertices describing a QUAD.
*/
osg::ref_ptr< osg::Vec3Array > generateCuboidQuads( const std::vector< wmath::WPosition >& corners );
/**
* Generates for a QUAD given via 3 three points ( the fourth is not needed ) the
* normal.
*
*\param a First point of the QUAD
*\param b Second point of the QUAD
*\param c Third point of the QUAD
*
*\return OSG Vector of the normal of the QUAD
*/
osg::Vec3 getQuadNormal( const wmath::WPosition& a, const wmath::WPosition& b, const wmath::WPosition& c );
/**
* Generates for all QUADS of the Cuboid the normals in the following order:
*
* \verbatim
z-axis y-axis
| /
| h___/_g
|/: /|
d_:___c |
| :...|.|
|.e | f
|_____|/ ____x-axis
a b
\endverbatim
*
* - a,b,c,d
* - b,f,g,c
* - f,e,h,g
* - e,a,d,h
* - d,c,g,h
* - a,b,f,e
*
*\param corners Corner points of the cuboid.
*
*\return Array of normals in the order as shown above.
*/
osg::ref_ptr< osg::Vec3Array > generateCuboidQuadNormals( const std::vector< wmath::WPosition >& corners );
}
#endif // WGEGEOMETRYUTILS_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <vector>
#include <osg/Array>
#include "../math/WPosition.h"
#include "WGEUtils.h"
osg::ref_ptr< osg::Vec3Array > wge::osgVec3Array( const std::vector< wmath::WPosition >& posArray )
{
osg::ref_ptr< osg::Vec3Array > result = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
result->reserve( posArray.size() );
std::vector< wmath::WPosition >::const_iterator cit;
for( cit = posArray.begin(); cit != posArray.end(); ++cit )
{
result->push_back( wge::osgVec3( *cit ) );
}
return result;
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WGEUTILS_H
#define WGEUTILS_H
#include <vector>
#include <osg/Vec3>
#include <osg/Vec4>
#include "../common/WColor.h"
#include "../math/WPosition.h"
namespace wge
{
/**
* Transforms a direction given via two points into a RGB color.
*
* \param pos1 First point
* \param pos2 Second point
*/
WColor getRGBAColorFromDirection( const wmath::WPosition &pos1, const wmath::WPosition &pos2 );
/**
* Converts a WColor to an OSG compatible color
*
* \param color The color in WColor format
* \return A color which may be used inside of OSG
*/
osg::Vec4 osgColor( const WColor& color );
/**
* Converts a given WPosition into an osg::Vec3.
*
* \param pos The WPosition which should be converted
*
* \return The osg::Vec3 vector of pos
*/
osg::Vec3 osgVec3( const wmath::WPosition& pos );
/**
* Converts a whole vector of WPositions into an osg::Vec3Array.
*
* \param posArray The given positions vector
*
* \return Refernce to the same vector but as osg::Vec3Array.
*/
osg::ref_ptr< osg::Vec3Array > osgVec3Array( const std::vector< wmath::WPosition >& posArray );
}
inline WColor wge::getRGBAColorFromDirection( const wmath::WPosition &pos1, const wmath::WPosition &pos2 )
{
wmath::WPosition direction( ( pos2 - pos1 ) );
direction.normalize();
return WColor( std::abs( direction[0] ), std::abs( direction[1] ), std::abs( direction[2] ) );
}
inline osg::Vec4 wge::osgColor( const WColor& color )
{
return osg::Vec4( color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha() );
}
inline osg::Vec3 wge::osgVec3( const wmath::WPosition& pos )
{
return osg::Vec3( pos[0], pos[1], pos[2] );
}
#endif // WGEUTILS_H
......@@ -204,130 +204,3 @@ boost::signals2::connection WGraphicsEngine::subscribeSignal( GE_SIGNAL signal,
break;
}
}
osg::Vec4 wge::osgColor( const WColor& color )
{
return osg::Vec4( color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha() );
}
osg::Vec3 wge::osgVec3( const wmath::WPosition& pos )
{
return osg::Vec3( pos[0], pos[1], pos[2] );
}
osg::ref_ptr< osg::Vec3Array > wge::osgVec3Array( const std::vector< wmath::WPosition >& posArray )
{
osg::ref_ptr< osg::Vec3Array > result = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
result->reserve( posArray.size() );
std::vector< wmath::WPosition >::const_iterator cit;
for( cit = posArray.begin(); cit != posArray.end(); ++cit )
{
result->push_back( osgVec3( *cit ) );
}
return result;
}
osg::ref_ptr< osg::Vec3Array > wge::generateCuboidQuads( const std::vector< wmath::WPosition >& corners )
{
osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
// Surfaces
vertices->push_back( wge::osgVec3( corners[0] ) );
vertices->push_back( wge::osgVec3( corners[1] ) );
vertices->push_back( wge::osgVec3( corners[2] ) );
vertices->push_back( wge::osgVec3( corners[3] ) );
vertices->push_back( wge::osgVec3( corners[1] ) );
vertices->push_back( wge::osgVec3( corners[5] ) );
vertices->push_back( wge::osgVec3( corners[6] ) );
vertices->push_back( wge::osgVec3( corners[2] ) );
vertices->push_back( wge::osgVec3( corners[5] ) );
vertices->push_back( wge::osgVec3( corners[4] ) );
vertices->push_back( wge::osgVec3( corners[7] ) );
vertices->push_back( wge::osgVec3( corners[6] ) );
vertices->push_back( wge::osgVec3( corners[4] ) );
vertices->push_back( wge::osgVec3( corners[0] ) );
vertices->push_back( wge::osgVec3( corners[3] ) );
vertices->push_back( wge::osgVec3( corners[7] ) );
vertices->push_back( wge::osgVec3( corners[3] ) );
vertices->push_back( wge::osgVec3( corners[2] ) );
vertices->push_back( wge::osgVec3( corners[6] ) );
vertices->push_back( wge::osgVec3( corners[7] ) );
vertices->push_back( wge::osgVec3( corners[0] ) );
vertices->push_back( wge::osgVec3( corners[1] ) );
vertices->push_back( wge::osgVec3( corners[5] ) );
vertices->push_back( wge::osgVec3( corners[4] ) );
return vertices;
}
osg::Vec3 wge::getQuadNormal( const wmath::WPosition& a,
const wmath::WPosition& b,
const wmath::WPosition& c )
{
wmath::WPosition vec1 = a - b;
wmath::WPosition vec2 = c - b;
wmath::WPosition normal = vec2.crossProduct( vec1 );
normal.normalize();
return osgVec3( normal );
}
osg::ref_ptr< osg::Vec3Array > wge::generateCuboidQuadNormals( const std::vector< wmath::WPosition >& corners )
{
osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
vertices->push_back( getQuadNormal( corners[0], corners[1], corners[2] ) );
vertices->push_back( getQuadNormal( corners[1], corners[5], corners[6] ) );
vertices->push_back( getQuadNormal( corners[5], corners[4], corners[7] ) );
vertices->push_back( getQuadNormal( corners[4], corners[0], corners[3] ) );
vertices->push_back( getQuadNormal( corners[3], corners[2], corners[6] ) );
vertices->push_back( getQuadNormal( corners[0], corners[1], corners[5] ) );
return vertices;
}
osg::ref_ptr< osg::Geode > wge::generateBoundingBoxGeode( const wmath::WPosition& pos1, const wmath::WPosition& pos2, const WColor& color )
{
assert( pos1[0] <= pos2[0] && pos1[1] <= pos2[1] && pos1[2] <= pos2[2] && "pos1 doesn't seem to be the frontLowerLeft corner of the BB!" );
using osg::ref_ptr;
ref_ptr< osg::Vec3Array > vertices = ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
ref_ptr< osg::Vec4Array > colors = ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
ref_ptr< osg::Geometry > geometry = ref_ptr< osg::Geometry >( new osg::Geometry );
vertices->push_back( osg::Vec3( pos1[0], pos1[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos1[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos2[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos1[0], pos2[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos1[0], pos1[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos1[0], pos1[1], pos2[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos1[1], pos2[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos2[1], pos2[2] ) );
vertices->push_back( osg::Vec3( pos1[0], pos2[1], pos2[2] ) );
vertices->push_back( osg::Vec3( pos1[0], pos1[1], pos2[2] ) );
geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP, 0, vertices->size() ) );
vertices->push_back( osg::Vec3( pos1[0], pos2[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos1[0], pos2[1], pos2[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos2[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos2[1], pos2[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos1[1], pos1[2] ) );
vertices->push_back( osg::Vec3( pos2[0], pos1[1], pos2[2] ) );
geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::LINES, vertices->size() - 6, 6 ) );
geometry->setVertexArray( vertices );
colors->push_back( osgColor( color ) );
geometry->setColorArray( colors );
geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
osg::ref_ptr< osg::Geode > geode = osg::ref_ptr< osg::Geode >( new osg::Geode );
geode->addDrawable( geometry );
// disable light for this geode as lines can't be lit properly
osg::StateSet* state = geode->getOrCreateStateSet();
state->setMode( GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );