Problem with Code On Frustum Culling

Patrizio
Posts: 5
Joined: Tue May 14, 2013 10:34 pm

Problem with Code On Frustum Culling

Post by Patrizio »

Hi to all, i'm new here :)

I got a problem while tryng to import Bullet in my custom Engine and couldn't find a solution searching around :( so i decided to ask :)


Here it's my code for checking Frustum culling:


It implements a Class that is supposed to fill an array of all the visible objects for future uses

Code: Select all


class REDER_CAMERA_FRUSTUM_CULLER
{
	public:
		REDER_CAMERA_FRUSTUM_CULLER();	
		~REDER_CAMERA_FRUSTUM_CULLER();
		void performFrustumCulling(REDER_CAMERA *cm);	// Fills m_objectsInFrustum
		btAlignedObjectArray<btCollisionObject*> m_objectsInFrustum;
};


//Implementation

REDER_CAMERA_FRUSTUM_CULLER::REDER_CAMERA_FRUSTUM_CULLER()
{
}		
REDER_CAMERA_FRUSTUM_CULLER::~REDER_CAMERA_FRUSTUM_CULLER()	
{
}

// Main stuct for btDbvt handling
struct	DbvtBroadphaseFrustumCulling : btDbvt::ICollide	
	{
				btAlignedObjectArray<btCollisionObject*>* m_pCollisionObjectArray;
				short int m_collisionFilterMask;
				btCollisionObject* m_additionalCollisionObjectToExclude;	// Unused in this demo

				DbvtBroadphaseFrustumCulling(btAlignedObjectArray<btCollisionObject*>* _pArray=NULL) 
					: 	m_pCollisionObjectArray(_pArray),m_collisionFilterMask(btBroadphaseProxy::AllFilter & ~btBroadphaseProxy::SensorTrigger),m_additionalCollisionObjectToExclude(NULL)
					{
					}	


				void	Process(const btDbvtNode* node,btScalar depth)	
				{
					Process(node);
				}
				void	Process(const btDbvtNode* leaf)	
					{	
					btBroadphaseProxy*	proxy=static_cast < btBroadphaseProxy*> (leaf->data);
					btCollisionObject* co = static_cast < btCollisionObject* > (proxy->m_clientObject);		
					if ((proxy->m_collisionFilterGroup & m_collisionFilterMask) != 0 && co != m_additionalCollisionObjectToExclude)	
						{
							m_pCollisionObjectArray->push_back( co );
						}	
					}			
	}g_DBFC;



// This fills m_objectsInFrustum:
void REDER_CAMERA_FRUSTUM_CULLER::performFrustumCulling(REDER_CAMERA *cm)	
{	
	m_objectsInFrustum.resize(0);	// clear() is probably slower
	btDbvtBroadphase*	dbvtBroadphase = (btDbvtBroadphase *)EngineMainWorldHandler.mPhysicsCache;

	D3DXMATRIX viewProjection;
    D3DXMatrixMultiply( &viewProjection, &cm->view, &cm->projection );
    //D3DXMatrixMultiply( &viewProjection, &m_mView, &m_mProjection );
    // Left plane
	D3DXPLANE   m_frustum[6];
    m_frustum[0].a = viewProjection._14 + viewProjection._11;
    m_frustum[0].b = viewProjection._24 + viewProjection._21;
    m_frustum[0].c = viewProjection._34 + viewProjection._31;
    m_frustum[0].d = viewProjection._44 + viewProjection._41;
 
    // Right plane
    m_frustum[1].a = viewProjection._14 - viewProjection._11;
    m_frustum[1].b = viewProjection._24 - viewProjection._21;
    m_frustum[1].c = viewProjection._34 - viewProjection._31;
    m_frustum[1].d = viewProjection._44 - viewProjection._41;
 
    // Top plane
    m_frustum[2].a = viewProjection._14 - viewProjection._12;
    m_frustum[2].b = viewProjection._24 - viewProjection._22;
    m_frustum[2].c = viewProjection._34 - viewProjection._32;
    m_frustum[2].d = viewProjection._44 - viewProjection._42;
 
    // Bottom plane
    m_frustum[3].a = viewProjection._14 + viewProjection._12;
    m_frustum[3].b = viewProjection._24 + viewProjection._22;
    m_frustum[3].c = viewProjection._34 + viewProjection._32;
    m_frustum[3].d = viewProjection._44 + viewProjection._42;
 
    // Near plane
    m_frustum[4].a = viewProjection._13;
    m_frustum[4].b = viewProjection._23;
    m_frustum[4].c = viewProjection._33;
    m_frustum[4].d = viewProjection._43;
 
    // Far plane
    m_frustum[5].a = viewProjection._14 - viewProjection._13;
    m_frustum[5].b = viewProjection._24 - viewProjection._23;
    m_frustum[5].c = viewProjection._34 - viewProjection._33;
    m_frustum[5].d = viewProjection._44 - viewProjection._43;
 
	// Normalize planes
	btVector3 planes_n[5];
	btScalar planes_o[5];
    for ( int i = 0; i < 5; i++)
    {
        D3DXPlaneNormalize( &m_frustum[i], &m_frustum[i] );
		planes_n[i] = btVector3(m_frustum[i].a,m_frustum[i].b,m_frustum[i].c);
		planes_o[i] = m_frustum[i].d;
	}
	btVector3 dir(cm->cameraDirNormalized.x,cm->cameraDirNormalized.y,cm->cameraDirNormalized.z);

	

	g_DBFC.m_pCollisionObjectArray = &m_objectsInFrustum;	
	g_DBFC.m_collisionFilterMask = btBroadphaseProxy::AllFilter & ~btBroadphaseProxy::SensorTrigger;//btBroadphaseProxy::AllFilter & ~btBroadphaseProxy::SensorTrigger;	// This won't display sensors...
	g_DBFC.m_additionalCollisionObjectToExclude = NULL;//btCamera;
	btDbvt::collideOCL(dbvtBroadphase->m_sets[1].m_root,planes_n,planes_o,dir,5,g_DBFC);
	btDbvt::collideOCL(dbvtBroadphase->m_sets[0].m_root,planes_n,planes_o,dir,5,g_DBFC);		
	// traverse the tree and call ICollide::Process(node) for all leaves located inside/on a set of planes.
	// End Perform Frustum Culling		
}


and this is the code i'm using to add elements to the scene

static terrain:

Code: Select all


for(unsigned int y = 0;y < md->Mesh_Vec[i].num_ind;y+=3)
		 {
			 btVector3 t1(vp[vm->IndexBoneBuffer[y]].x,vp[vm->IndexBoneBuffer[y]].y,vp[vm->IndexBoneBuffer[y]].z);
			 btVector3 t2(vp[vm->IndexBoneBuffer[y + 1]].x,vp[vm->IndexBoneBuffer[y + 1]].y,vp[vm->IndexBoneBuffer[y + 1]].z);
			 btVector3 t3(vp[vm->IndexBoneBuffer[y + 2]].x,vp[vm->IndexBoneBuffer[y + 2]].y,vp[vm->IndexBoneBuffer[y + 2]].z);
			 data->addTriangle(t1,t2,t3,false);
		 }
		 btBvhTriangleMeshShape* terrainShape=new btBvhTriangleMeshShape(data,true,true);
		 //by default phisic is set to not moving no mass
		btTransform startTransform;
		startTransform.setIdentity();
		startTransform.setFromOpenGLMatrix((btScalar*)md->Mesh_Vec[i].start_trasform.m);
		btDefaultMotionState* ms = new btDefaultMotionState(startTransform);
		btVector3 localInertia(0,0,0);
		btRigidBody::btRigidBodyConstructionInfo rbInfo(0.0f,ms,md->Mesh_Vec[i].terrainShape,localInertia);
		btRigidBody* body = new btRigidBody(rbInfo);
		body->setActivationState(ACTIVE_TAG);
		body->setCollisionFlags(btCollisionObject::CF_STATIC_OBJECT);
	    EngineMainWorldHandler.mPhysicsWorld->addRigidBody(body);


and models

Code: Select all

btTransform startTransform;
	startTransform.setIdentity();
	startTransform.setFromOpenGLMatrix((btScalar*)place);
	btDefaultMotionState* ms = new btDefaultMotionState(startTransform);
	btVector3 localInertia(inertia->x,inertia->y,inertia->z);
	btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,ms,EngineMainWorldHandler.ModelsInstances[posInstance]->origin_model->ModelBoxShape,localInertia);
	btRigidBody* body = new btRigidBody(rbInfo);
	body->setActivationState(ACTIVE_TAG);
	body->setCollisionFlags(btCollisionObject::CF_KINEMATIC_OBJECT);
	EngineMainWorldHandler.mPhysicsWorld->addRigidBody(body);
	EngineMainWorldHandler.ModelsInstances[posInstance]->body = body;
SHould be userfrienfly to read :)

Hope someone can help me :|

i get an exception if i try to add static terrain, precisally when creating the collision mesh too :|
(line: btBvhTriangleMeshShape* terrainShape=new btBvhTriangleMeshShape(data,true,true);)

and if i try to add a model(Using BB) all goes right but simply no object results in the frustum(tried many test cases,seems that nothing passes the test :| )