add support for picking, using point 2 point constraint
allow to remove constraints by unique id added tiny wavefront loader, plan to use this instead of existing slow wavefront loader
This commit is contained in:
@@ -74,6 +74,14 @@ public:
|
||||
|
||||
struct GpuDemoInternalData* getInternalData();
|
||||
|
||||
virtual bool mouseMoveCallback(float x,float y)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
virtual bool mouseButtonCallback(int button, int state, float x, float y)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -187,13 +187,21 @@ void MyButtonCallback(int buttonId, int state)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GpuDemo* sDemo = 0;
|
||||
|
||||
static void MyMouseMoveCallback( float x, float y)
|
||||
{
|
||||
if (gui)
|
||||
{
|
||||
bool handled = gui ->mouseMoveCallback(x,y);
|
||||
if (!handled)
|
||||
b3DefaultMouseMoveCallback(x,y);
|
||||
{
|
||||
if (sDemo)
|
||||
handled = sDemo->mouseMoveCallback(x,y);
|
||||
if (!handled)
|
||||
b3DefaultMouseMoveCallback(x,y);
|
||||
}
|
||||
}
|
||||
}
|
||||
static void MyMouseButtonCallback(int button, int state, float x, float y)
|
||||
@@ -202,7 +210,14 @@ static void MyMouseButtonCallback(int button, int state, float x, float y)
|
||||
{
|
||||
bool handled = gui->mouseButtonCallback(button,state,x,y);
|
||||
if (!handled)
|
||||
b3DefaultMouseButtonCallback(button,state,x,y);
|
||||
{
|
||||
//try picking first
|
||||
if (sDemo)
|
||||
handled = sDemo->mouseButtonCallback(button,state,x,y);
|
||||
|
||||
if (!handled)
|
||||
b3DefaultMouseButtonCallback(button,state,x,y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -647,6 +662,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
{
|
||||
GpuDemo* demo = allDemos[selectedDemo]();
|
||||
sDemo = demo;
|
||||
// demo->myinit();
|
||||
bool useGpu = false;
|
||||
|
||||
@@ -919,6 +935,8 @@ int main(int argc, char* argv[])
|
||||
delete ci.m_instancingRenderer;
|
||||
|
||||
delete demo;
|
||||
sDemo = 0;
|
||||
|
||||
if (detailsFile)
|
||||
{
|
||||
fclose(detailsFile);
|
||||
|
||||
@@ -335,7 +335,7 @@ void GpuRaytraceScene::renderScene2()
|
||||
shadowRays[i].m_from = hits[i].m_hitPoint;
|
||||
shadowRays[i].m_to = lightPos;
|
||||
shadowHits[i].m_hitFraction=1.f;
|
||||
shadowHits[i].m_hitResult2 = hits[i].m_hitResult0;
|
||||
shadowHits[i].m_hitBody = hits[i].m_hitBody;
|
||||
} else
|
||||
{
|
||||
shadowRays[i].m_from.setValue(0,0,0);
|
||||
@@ -377,7 +377,7 @@ void GpuRaytraceScene::renderScene2()
|
||||
m_raytraceData->m_texels[(i)*3+1] = 128+128.f*hits[i].m_hitNormal.y;
|
||||
m_raytraceData->m_texels[(i)*3+2] = 128+128.f*hits[i].m_hitNormal.z;
|
||||
|
||||
if (hits[i].m_hitResult0==0)
|
||||
if (hits[i].m_hitBody==0)
|
||||
{
|
||||
m_raytraceData->m_texels[(i)*3+0] = 255;
|
||||
m_raytraceData->m_texels[(i)*3+1] = 255;
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "Bullet3OpenCL/RigidBody/b3Config.h"
|
||||
#include "GpuRigidBodyDemoInternalData.h"
|
||||
#include "Bullet3Collision/BroadPhaseCollision/b3DynamicBvhBroadphase.h"
|
||||
#include "Bullet3Collision/NarrowPhaseCollision/b3RigidBodyCL.h"
|
||||
|
||||
static b3KeyboardCallback oldCallback = 0;
|
||||
extern bool gReset;
|
||||
@@ -130,6 +131,7 @@ void GpuRigidBodyDemo::initPhysics(const ConstructionInfo& ci)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
m_instancingRenderer->writeTransforms();
|
||||
|
||||
@@ -165,6 +167,7 @@ void GpuRigidBodyDemo::clientMoveAndDisplay()
|
||||
{
|
||||
bool animate=true;
|
||||
int numObjects= m_data->m_rigidBodyPipeline->getNumBodies();
|
||||
//printf("numObjects=%d\n",numObjects);
|
||||
if (numObjects > m_instancingRenderer->getInstanceCapacity())
|
||||
{
|
||||
static bool once = true;
|
||||
@@ -242,3 +245,180 @@ void GpuRigidBodyDemo::clientMoveAndDisplay()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
b3Vector3 GpuRigidBodyDemo::getRayTo(int x,int y)
|
||||
{
|
||||
if (!m_instancingRenderer)
|
||||
return b3Vector3(0,0,0);
|
||||
|
||||
float top = 1.f;
|
||||
float bottom = -1.f;
|
||||
float nearPlane = 1.f;
|
||||
float tanFov = (top-bottom)*0.5f / nearPlane;
|
||||
float fov = b3Scalar(2.0) * b3Atan(tanFov);
|
||||
|
||||
b3Vector3 camPos,camTarget;
|
||||
m_instancingRenderer->getCameraPosition(camPos);
|
||||
m_instancingRenderer->getCameraTargetPosition(camTarget);
|
||||
|
||||
b3Vector3 rayFrom = camPos;
|
||||
b3Vector3 rayForward = (camTarget-camPos);
|
||||
rayForward.normalize();
|
||||
float farPlane = 10000.f;
|
||||
rayForward*= farPlane;
|
||||
|
||||
b3Vector3 rightOffset;
|
||||
b3Vector3 m_cameraUp(0,1,0);
|
||||
b3Vector3 vertical = m_cameraUp;
|
||||
|
||||
b3Vector3 hor;
|
||||
hor = rayForward.cross(vertical);
|
||||
hor.normalize();
|
||||
vertical = hor.cross(rayForward);
|
||||
vertical.normalize();
|
||||
|
||||
float tanfov = tanf(0.5f*fov);
|
||||
|
||||
|
||||
hor *= 2.f * farPlane * tanfov;
|
||||
vertical *= 2.f * farPlane * tanfov;
|
||||
|
||||
b3Scalar aspect;
|
||||
float width = m_instancingRenderer->getScreenWidth();
|
||||
float height = m_instancingRenderer->getScreenHeight();
|
||||
|
||||
aspect = width / height;
|
||||
|
||||
hor*=aspect;
|
||||
|
||||
|
||||
b3Vector3 rayToCenter = rayFrom + rayForward;
|
||||
b3Vector3 dHor = hor * 1.f/width;
|
||||
b3Vector3 dVert = vertical * 1.f/height;
|
||||
|
||||
|
||||
b3Vector3 rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical;
|
||||
rayTo += b3Scalar(x) * dHor;
|
||||
rayTo -= b3Scalar(y) * dVert;
|
||||
return rayTo;
|
||||
}
|
||||
|
||||
|
||||
bool GpuRigidBodyDemo::mouseMoveCallback(float x,float y)
|
||||
{
|
||||
if (m_data->m_pickBody>=0 && m_data->m_pickConstraint>=0)
|
||||
{
|
||||
m_data->m_rigidBodyPipeline->removeConstraintByUid(m_data->m_pickConstraint);
|
||||
b3Vector3 newRayTo = getRayTo(x,y);
|
||||
b3Vector3 rayFrom;
|
||||
b3Vector3 oldPivotInB = m_data->m_pickPivotInB;
|
||||
b3Vector3 newPivotB;
|
||||
m_instancingRenderer->getCameraPosition(rayFrom);
|
||||
b3Vector3 dir = newRayTo-rayFrom;
|
||||
dir.normalize();
|
||||
dir *= m_data->m_pickDistance;
|
||||
newPivotB = rayFrom + dir;
|
||||
m_data->m_pickPivotInB = newPivotB;
|
||||
m_data->m_pickConstraint = m_data->m_rigidBodyPipeline->createPoint2PointConstraint(m_data->m_pickBody,m_data->m_pickFixedBody,m_data->m_pickPivotInA,m_data->m_pickPivotInB);
|
||||
m_data->m_rigidBodyPipeline->writeAllInstancesToGpu();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool GpuRigidBodyDemo::mouseButtonCallback(int button, int state, float x, float y)
|
||||
{
|
||||
if (state==1)
|
||||
{
|
||||
if (button==0)
|
||||
{
|
||||
b3AlignedObjectArray<b3RayInfo> rays;
|
||||
b3AlignedObjectArray<b3RayHit> hitResults;
|
||||
b3Vector3 camPos;
|
||||
m_instancingRenderer->getCameraPosition(camPos);
|
||||
|
||||
b3RayInfo ray;
|
||||
ray.m_from = camPos;
|
||||
ray.m_to = getRayTo(x,y);
|
||||
rays.push_back(ray);
|
||||
b3RayHit hit;
|
||||
hit.m_hitFraction = 1.f;
|
||||
hitResults.push_back(hit);
|
||||
m_data->m_rigidBodyPipeline->castRays(rays,hitResults);
|
||||
if (hitResults[0].m_hitFraction<1.f)
|
||||
{
|
||||
|
||||
int hitBodyA = hitResults[0].m_hitBody;
|
||||
if (m_data->m_np->getBodiesCpu()[hitBodyA].m_invMass)
|
||||
{
|
||||
//printf("hit!\n");
|
||||
m_data->m_np->readbackAllBodiesToCpu();
|
||||
m_data->m_pickBody = hitBodyA;
|
||||
|
||||
|
||||
|
||||
|
||||
//pivotInA
|
||||
b3Vector3 pivotInB;
|
||||
pivotInB.setInterpolate3(ray.m_from,ray.m_to,hitResults[0].m_hitFraction);
|
||||
b3Vector3 posA;
|
||||
b3Quaternion ornA;
|
||||
m_data->m_np->getObjectTransformFromCpu(posA,ornA,hitBodyA);
|
||||
b3Transform tr;
|
||||
tr.setOrigin(posA);
|
||||
tr.setRotation(ornA);
|
||||
b3Vector3 pivotInA = tr.inverse()*pivotInB;
|
||||
if (m_data->m_pickFixedBody<0)
|
||||
{
|
||||
b3Vector3 pos(0,0,0);
|
||||
b3Quaternion orn(0,0,0,1);
|
||||
int fixedSphere = m_data->m_np->registerConvexHullShape(0,0,0,0);//>registerSphereShape(0.1);
|
||||
m_data->m_pickFixedBody = m_data->m_rigidBodyPipeline->registerPhysicsInstance(0,pos,orn,fixedSphere,0,false);
|
||||
|
||||
if (m_data->m_pickGraphicsShapeIndex<0)
|
||||
{
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(point_sphere_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(point_sphere_indices)/sizeof(int);
|
||||
m_data->m_pickGraphicsShapeIndex = m_instancingRenderer->registerShape(&point_sphere_vertices[0],numVertices,point_sphere_indices,numIndices,B3_GL_POINTS);
|
||||
float color[4] ={1,0,0,1};
|
||||
float scaling[4]={1,1,1,1};
|
||||
|
||||
m_data->m_pickGraphicsShapeInstance = m_instancingRenderer->registerGraphicsInstance(m_data->m_pickGraphicsShapeIndex,pivotInB,orn,color,scaling);
|
||||
m_instancingRenderer->writeTransforms();
|
||||
delete m_data->m_instancePosOrnColor;
|
||||
m_data->m_instancePosOrnColor=0;
|
||||
} else
|
||||
{
|
||||
m_instancingRenderer->writeSingleInstanceTransformToCPU(pivotInB,orn,m_data->m_pickGraphicsShapeInstance);
|
||||
m_instancingRenderer->writeSingleInstanceTransformToGPU(pivotInB,orn,m_data->m_pickGraphicsShapeInstance);
|
||||
m_data->m_np->setObjectTransformCpu(pos,orn,m_data->m_pickFixedBody);
|
||||
}
|
||||
|
||||
}
|
||||
pivotInB.w = 0.f;
|
||||
m_data->m_pickPivotInA = pivotInA;
|
||||
m_data->m_pickPivotInB = pivotInB;
|
||||
m_data->m_pickConstraint = m_data->m_rigidBodyPipeline->createPoint2PointConstraint(hitBodyA,m_data->m_pickFixedBody,pivotInA,pivotInB);//hitResults[0].m_hitResult0
|
||||
m_data->m_rigidBodyPipeline->writeAllInstancesToGpu();
|
||||
m_data->m_np->writeAllBodiesToGpu();
|
||||
m_data->m_pickDistance = (pivotInB-camPos).length();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else
|
||||
{
|
||||
if (button==0)
|
||||
{
|
||||
if (m_data->m_pickConstraint>=0)
|
||||
{
|
||||
m_data->m_rigidBodyPipeline->removeConstraintByUid(m_data->m_pickConstraint);
|
||||
m_data->m_pickConstraint=-1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//printf("button=%d, state=%d\n",button,state);
|
||||
return false;
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
#define GPU_RIGID_BODY_DEMO_H
|
||||
|
||||
#include "../GpuDemo.h"
|
||||
#include "Bullet3Common/b3Vector3.h"
|
||||
|
||||
class GpuRigidBodyDemo : public GpuDemo
|
||||
{
|
||||
@@ -38,6 +39,10 @@ public:
|
||||
|
||||
virtual void clientMoveAndDisplay();
|
||||
|
||||
//for picking
|
||||
b3Vector3 getRayTo(int x,int y);
|
||||
virtual bool mouseMoveCallback(float x,float y);
|
||||
virtual bool mouseButtonCallback(int button, int state, float x, float y);
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -18,12 +18,28 @@ struct GpuRigidBodyDemoInternalData
|
||||
class b3GpuSapBroadphase* m_bp;
|
||||
class b3DynamicBvhBroadphase* m_broadphaseDbvt;
|
||||
|
||||
b3Vector3 m_pickPivotInA;
|
||||
b3Vector3 m_pickPivotInB;
|
||||
float m_pickDistance;
|
||||
int m_pickBody;
|
||||
int m_pickConstraint;
|
||||
|
||||
int m_pickFixedBody;
|
||||
int m_pickGraphicsShapeIndex;
|
||||
int m_pickGraphicsShapeInstance;
|
||||
|
||||
GpuRigidBodyDemoInternalData()
|
||||
:m_instancePosOrnColor(0),
|
||||
m_copyTransformsToVBOKernel(0), m_rigidBodyPipeline(0),
|
||||
m_np(0),
|
||||
m_bp(0),
|
||||
m_broadphaseDbvt(0)
|
||||
m_broadphaseDbvt(0),
|
||||
m_pickConstraint(-1),
|
||||
m_pickFixedBody(-1),
|
||||
m_pickGraphicsShapeIndex(-1),
|
||||
m_pickGraphicsShapeInstance(-1),
|
||||
m_pickBody(-1)
|
||||
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user