Removed unnecessary copy, precompue area of triangle to reduce

work when smoothing normals.
This commit is contained in:
hiker 2015-04-14 16:21:43 +10:00
parent a4264cc063
commit 2c44bd1201
3 changed files with 33 additions and 20 deletions

View File

@ -999,7 +999,7 @@ void btKart::debugDraw(btIDebugDraw* debugDrawer)
// Draw the (interpolated) normal of the ground at the wheel position
debugDrawer->drawLine(getWheelInfo(v).m_raycastInfo.m_contactPointWS,
getWheelInfo(v).m_raycastInfo.m_contactPointWS+getWheelInfo(v).m_raycastInfo.m_contactNormalWS,
btVector3(0, 0, 1));
btVector3(0.5f, 0.5f, 0.5f));
} // for i < getNumWheels
btVector3 yellow(1.0f, 1.0f, 0.0f);

View File

@ -77,6 +77,11 @@ void TriangleMesh::addTriangle(const btVector3 &t1, const btVector3 &t2,
m_normals.push_back( normal.angle(n3)>stk_config->m_smooth_angle_limit
? normal : n3 );
m_mesh.addTriangle(t1, t2, t3);
// Area of triangle ABC
btVector3 edge1 = t2 - t1;
btVector3 edge2 = t3 - t1;
m_p1p2p3.push_back(edge1.cross(edge2).length2());
} // addTriangle
// -----------------------------------------------------------------------------
@ -228,24 +233,22 @@ void TriangleMesh::removeAll()
btVector3 TriangleMesh::getInterpolatedNormal(unsigned int index,
const btVector3 &position) const
{
btVector3 p1, p2, p3;
btVector3 *p1, *p2, *p3;
getTriangle(index, &p1, &p2, &p3);
btVector3 n1, n2, n3;
const btVector3 *n1, *n2, *n3;
getNormals(index, &n1, &n2, &n3);
// Compute the Barycentric coordinates of position inside triangle
// p1, p2, p3.
btVector3 edge1 = p2 - p1;
btVector3 edge2 = p3 - p1;
// Area of triangle ABC
btScalar p1p2p3 = edge1.cross(edge2).length2();
btScalar p1p2p3 = getP1P2P3(index);
// Area of BCP
btScalar p2p3p = (p3 - p2).cross(position - p2).length2();
btScalar p2p3p = (*p3 - *p2).cross(position - *p2).length2();
// Area of CAP
btScalar p3p1p = edge2.cross(position - p3).length2();
btVector3 edge2 = *p3 - *p1;
btScalar p3p1p = edge2.cross(position - *p3).length2();
btScalar s = btSqrt(p2p3p / p1p2p3);
btScalar t = btSqrt(p3p1p / p1p2p3);
btScalar w = 1.0f - s - t;
@ -266,7 +269,7 @@ btVector3 TriangleMesh::getInterpolatedNormal(unsigned int index,
}
#endif
return s*n1 + t*n2 + w*n3;
return s*(*n1) + t*(*n2) + w*(*n3);
} // getInterpolatedNormal
// ----------------------------------------------------------------------------

View File

@ -47,6 +47,8 @@ private:
btCollisionShape *m_collision_shape;
/** The three normals for each triangle. */
AlignedArray<btVector3> m_normals;
/** Pre-compute value used in smoothing. */
AlignedArray<float> m_p1p2p3;
public:
TriangleMesh();
~TriangleMesh();
@ -92,28 +94,36 @@ public:
/** Returns the points of the 'indx' triangle.
* \param indx Index of the triangle to get.
* \param p1,p2,p3 On return the three points of the triangle. */
void getTriangle(unsigned int indx, btVector3 *p1, btVector3 *p2,
btVector3 *p3) const
void getTriangle(unsigned int indx, btVector3 **p1, btVector3 **p2,
btVector3 **p3) const
{
const IndexedMeshArray &m = m_mesh.getIndexedMeshArray();
btVector3 *p = &(((btVector3*)(m[0].m_vertexBase))[3*indx]);
*p1 = p[0];
*p2 = p[1];
*p3 = p[2];
*p1 = &(p[0]);
*p2 = &(p[1]);
*p3 = &(p[2]);
} // getTriangle
// ------------------------------------------------------------------------
/** Returns the normals of the triangle with the given index.
* \param indx Index of the triangle to get the three normals of.
* \result n1,n2,n3 The three normals. */
void getNormals(unsigned int indx, btVector3 *n1, btVector3 *n2,
btVector3 *n3) const
void getNormals(unsigned int indx, const btVector3 **n1,
const btVector3 **n2, const btVector3 **n3) const
{
assert(indx < m_triangleIndex2Material.size());
unsigned int n = indx*3;
*n1 = m_normals[n ];
*n2 = m_normals[n+1];
*n3 = m_normals[n+2];
*n1 = &(m_normals[n ]);
*n2 = &(m_normals[n+1]);
*n3 = &(m_normals[n+2]);
} // getNormals
// ------------------------------------------------------------------------
/** Returns basically the area of the triangle, which is needed when
* smoothing the normals. */
float getP1P2P3(unsigned int indx) const
{
assert(indx < m_p1p2p3.size());
return m_p1p2p3[indx];
}
};
#endif
/* EOF */