Show room.cpp syntax highlighted
/***************************************************************************
room.h - Main ABT room class
-------------------
begin : Tue Jul 8 2003
copyright : (C) 2003 by Reality Rift Studios
email : mattias@realityrift.com
***************************************************************************
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is the NeoEngine, NeoABT, room.cpp
The Initial Developer of the Original Code is Mattias Jansson.
Portions created by Mattias Jansson are Copyright (C) 2003
Reality Rift Studios. All Rights Reserved.
***************************************************************************/
#include "room.h"
#include "node.h"
#include <limits>
using namespace NeoEngine;
using namespace std;
namespace NeoABT
{
RenderPrimitive ABTRoom::s_kRenderPrimitive;
ABTRoom::ABTRoom( const HashString &rstrName ) :
NeoEngine::Room( rstrName )
{
m_pkRoot = new Node( 0, 0 );
}
ABTRoom::~ABTRoom()
{
s_kRenderPrimitive.m_pkPolygonBuffer = 0;
s_kRenderPrimitive.m_pkPolygonStripBuffer = 0;
s_kRenderPrimitive.m_pkVertexBuffer = 0;
s_kRenderPrimitive.m_pkMaterial = 0;
delete m_pkRoot;
}
void ABTRoom::AddGeometry( const PolygonBufferPtr &pkPolygonBuffer, const VertexBufferPtr &pkVertexBuffer, const MaterialPtr &pkMaterial )
{
PolygonBufferPtr pkPBuffer( pkPolygonBuffer );
VertexBufferPtr pkVBuffer( pkVertexBuffer );
Vector3d kMin( numeric_limits< float >::max(), numeric_limits< float >::max(), numeric_limits< float >::max() );
Vector3d kMax( -kMin );
pkPBuffer->Lock( Buffer::READ );
pkVBuffer->Lock( Buffer::READ );
unsigned char *pucVertex = (unsigned char*)pkVBuffer->GetVertex();
unsigned int uiNumVertices = pkVBuffer->GetNumElements();
unsigned int uiVertexSize = pkVBuffer->GetVertexSize();
for( unsigned int uiVertex = 0; uiVertex < uiNumVertices; ++uiVertex, pucVertex += uiVertexSize )
{
Vector3d *pkPos = (Vector3d*)pucVertex;
if( pkPos->x < kMin.x )
kMin.x = pkPos->x;
if( pkPos->y < kMin.y )
kMin.y = pkPos->y;
if( pkPos->z < kMin.z )
kMin.z = pkPos->z;
if( pkPos->x > kMax.x )
kMax.x = pkPos->x;
if( pkPos->y > kMax.y )
kMax.y = pkPos->y;
if( pkPos->z > kMax.z )
kMax.z = pkPos->z;
}
Vector3d kDim = ( kMax - kMin ) * 0.5f;
if( kDim.x < EPSILON )
{
kDim.x = 1.0f;
kMin.x -= 1.0f;
}
if( kDim.y < EPSILON )
{
kDim.y = 1.0f;
kMin.y -= 1.0f;
}
if( kDim.z < EPSILON )
{
kDim.z = 1.0f;
kMin.z -= 1.0f;
}
pkVBuffer->Unlock();
pkPBuffer->Unlock();
Geometry *pkGeometry = new Geometry;
AABB *pkAABB = new AABB( kDim );
pkAABB->SetTranslation( kMin + kDim );
pkGeometry->SetTranslation( pkAABB->GetTranslation() + pkAABB->m_kDelta );
pkGeometry->SetBoundingVolume( pkAABB );
pkPBuffer->m_pkVertexBuffer = pkVBuffer;
pkPBuffer->m_pkMaterial = pkMaterial;
pkGeometry->m_pkPolygons = pkPBuffer;
vector< Geometry* > vpkGeometries;
pkGeometry->Partition( &vpkGeometries, 0 );
vector< Geometry* >::iterator ppkGeometry = vpkGeometries.begin();
vector< Geometry* >::iterator ppkGeometryEnd = vpkGeometries.end();
neolog << LogLevel( DEBUG ) << "*** Partitioned geometry: " << vpkGeometries.size() << " final geometry nodes" << endl;
for( ; ppkGeometry != ppkGeometryEnd; ++ppkGeometry )
m_pkRoot->AddNode( *ppkGeometry );
}
void ABTRoom::SliceGeometry( const Vector3d &rkV0, const Vector3d &rkV1, const Vector3d &rkV2, std::vector< Vector3d > *pvkPoints )
{
vector< SceneNode* >::iterator ppkNode = m_vpkChildren.begin();
vector< SceneNode* >::iterator ppkNodeEnd = m_vpkChildren.end();
for( ; ppkNode != ppkNodeEnd; ++ppkNode )
{
Geometry *pkGeometry = dynamic_cast< Geometry* >( *ppkNode );
if( pkGeometry && pkGeometry->GetBoundingVolume()->Intersection( rkV0, rkV1, rkV2 ) )
{
pkGeometry->m_pkPolygons->Lock( Buffer::READ );
Polygon *pkPolygon = pkGeometry->m_pkPolygons->GetPolygon();
int iNumPolygons = pkGeometry->m_pkPolygons->GetNumElements();
VertexBufferPtr &pkVB = pkGeometry->m_pkPolygons->m_pkVertexBuffer;
pkVB->Lock( Buffer::READ );
Vector3d kIntersect[2];
for( int iPoly = 0; iPoly < iNumPolygons; ++iPoly, ++pkPolygon )
if( Slice( *(Vector3d*)pkVB->GetVertex( pkPolygon->v[0] ), *(Vector3d*)pkVB->GetVertex( pkPolygon->v[1] ), *(Vector3d*)pkVB->GetVertex( pkPolygon->v[2] ), rkV0, rkV1, rkV2, &kIntersect[0], &kIntersect[1], 0 ) )
{
pvkPoints->push_back( kIntersect[0] );
pvkPoints->push_back( kIntersect[1] );
}
pkVB->Unlock();
pkGeometry->m_pkPolygons->Unlock();
}
}
}
int ABTRoom::AttachNode( SceneNode *pkNode, bool bKeepWorldSRT )
{
m_pkRoot->AddNode( pkNode );
pkNode->m_pkRoom = this;
return 0;
}
int ABTRoom::DetachNode( SceneNode *pkNode )
{
if( pkNode->m_pkRoom == this )
pkNode->m_pkRoom = 0;
return m_pkRoot->RemoveNode( pkNode );
}
bool ABTRoom::Render( Frustum *pkFrustum, bool bForce )
{
if( !Room::Render( pkFrustum, bForce ) )
return false;
if( m_pkRoot->m_kAABB.Intersection( pkFrustum ) )
{
m_pkRoot->Render( pkFrustum, bForce );
return true;
}
return false;
}
void ABTRoom::Update( float fDeltaTime )
{
Room::Update( fDeltaTime );
m_pkRoot->Update( fDeltaTime );
}
bool ABTRoom::Intersection( BoundingVolume *pkObj, ContactSet *pkContactSet )
{
return m_pkRoot->Intersection( pkObj, pkContactSet );
}
bool ABTRoom::Intersection( const Ray &rkRay, ContactSet *pkContactSet )
{
return m_pkRoot->Intersection( rkRay, pkContactSet );
}
SceneNode *ABTRoom::GetByName( const HashString &rstrName, NODESEARCHMODE eMode, bool bInitSearch )
{
if( bInitSearch && GetName() == rstrName )
return Get();
return m_pkRoot->GetByName( rstrName, eMode );
}
void ABTRoom::Traverse( BaseVisitor &rkVisitor, NODESEARCHMODE eMode, int iDirection, bool bInitSearch )
{
if( eMode == BREADTH_FIRST)
{
if ( bInitSearch )
TraverseNode( rkVisitor );
m_pkRoot->Traverse( rkVisitor, eMode, iDirection );
return;
}
if( iDirection >= 0 )
TraverseNode( rkVisitor );
m_pkRoot->Traverse( rkVisitor, eMode, iDirection );
if( iDirection < 0 )
TraverseNode( rkVisitor );
}
};
See more files for this project here