/////////////////////////////////////////////////////////////////////////////////////////////////// // Calculate the tangent basis for a triangle on the surface of a model // This vector is needed for most normal mapping shaders void m3dCalculateTangentBasis(M3DVector3f vTangent, const M3DVector3f vTriangle[3], const M3DVector2f vTexCoords[3], const M3DVector3f N) { M3DVector3f dv2v1, dv3v1; float dc2c1t, dc2c1b, dc3c1t, dc3c1b; float M; m3dSubtractVectors3(dv2v1, vTriangle[1], vTriangle[0]); m3dSubtractVectors3(dv3v1, vTriangle[2], vTriangle[0]); dc2c1t = vTexCoords[1][0] - vTexCoords[0][0]; dc2c1b = vTexCoords[1][1] - vTexCoords[0][1]; dc3c1t = vTexCoords[2][0] - vTexCoords[0][0]; dc3c1b = vTexCoords[2][1] - vTexCoords[0][1]; M = (dc2c1t * dc3c1b) - (dc3c1t * dc2c1b); M = 1.0f / M; m3dScaleVector3(dv2v1, dc3c1b); m3dScaleVector3(dv3v1, dc2c1b); m3dSubtractVectors3(vTangent, dv2v1, dv3v1); m3dScaleVector3(vTangent, M); // This potentially changes the direction of the vector m3dNormalizeVector3(vTangent); M3DVector3f B; m3dCrossProduct3(B, N, vTangent); m3dCrossProduct3(vTangent, B, N); m3dNormalizeVector3(vTangent); }
//verlet x = vt + 1/2*at^2 void nextPosition(int currentPtc) { M3DVector3f temp; //get last shift: + vt m3dSubtractVectors3(temp, ClothLnk[currentPtc].position, ClothLnk[currentPtc].pre_position); m3dAddVectors3(ClothLnk[currentPtc].next_position, ClothLnk[currentPtc].position, temp); //if (currentPtc==55) // printf(" [%d].shift {%.1f, %.1f, %.1f} \n", currentPtc, shift[0], shift[1], shift[2]); //get accumulation: + 1/2*at^2 m3dCopyVector3(temp, ClothLnk[currentPtc].force); m3dScaleVector3(temp, (GLfloat)TimeStep*TimeStep*AccumulateStiff/ClothLnk[currentPtc].Mass/2); m3dAddVectors3(ClothLnk[currentPtc].next_position, ClothLnk[currentPtc].next_position, temp); //collide //if (adjustPositionByCollision(ClothLnk[currentPtc].next_position) ) { // m3dCopyVector3(ClothLnk[currentPtc].next_position, ClothLnk[currentPtc].position); //} adjustPositionByCollision(ClothLnk[currentPtc].position, ClothLnk[currentPtc].next_position); M3DVector3f shift; m3dSubtractVectors3(shift, ClothLnk[currentPtc].next_position, ClothLnk[currentPtc].position); GLfloat length = (GLfloat)m3dGetVectorLength(shift); if (length >= shiftRange ) { m3dScaleVector3(shift, 1.0f/length); m3dAddVectors3(ClothLnk[currentPtc].next_position, ClothLnk[currentPtc].position, shift); } }
void normal(float result[], const float A[], const float B[], const float C[]) { float C_min_B[3], A_min_B[3]; m3dSubtractVectors3(C_min_B, C, B); m3dSubtractVectors3(A_min_B, A, B); m3dCrossProduct3(result, C_min_B, A_min_B); m3dNormalizeVector3(result); }
//---------------------------------------------------- void TriangleFace(M3DVector3f a, M3DVector3f b, M3DVector3f c) { M3DVector3f normal, bMa, cMa; m3dSubtractVectors3(bMa, b, a); m3dSubtractVectors3(cMa, c, a); m3dCrossProduct3(normal, bMa, cMa); m3dNormalizeVector3(normal); glVertexAttrib3fv(GLT_ATTRIBUTE_NORMAL, normal); glVertex3fv(a); glVertex3fv(b); glVertex3fv(c); }
void LookAt(GLFrame &frame, const M3DVector3f eye, const M3DVector3f at, const M3DVector3f up) { M3DVector3f forward; m3dSubtractVectors3(forward, at, eye); SetUpFrame(frame, eye, forward, up); }
void LookAt(GLFrame &cameraFrame, const M3DVector3f cameraPosition, const M3DVector3f targetPosition, const M3DVector3f cameraUpDirection) { M3DVector3f forward; m3dSubtractVectors3(forward, targetPosition, cameraPosition); SetUpFrame(cameraFrame, cameraPosition, forward, cameraUpDirection); }
// ditto above... but with floats float m3dClosestPointOnRay(M3DVector3f vPointOnRay, const M3DVector3f vRayOrigin, const M3DVector3f vUnitRayDir, const M3DVector3f vPointInSpace) { M3DVector3f v; m3dSubtractVectors3(v, vPointInSpace, vRayOrigin); float t = m3dDotProduct3(vUnitRayDir, v); // This is the point on the ray vPointOnRay[0] = vRayOrigin[0] + (t * vUnitRayDir[0]); vPointOnRay[1] = vRayOrigin[1] + (t * vUnitRayDir[1]); vPointOnRay[2] = vRayOrigin[2] + (t * vUnitRayDir[2]); return m3dGetDistanceSquared3(vPointOnRay, vPointInSpace); }
void sewClothModel() { M3DVector3f temp, displacement; for (int i = 0; i < PTCAMT; i ++) { m3dLoadVector3(displacement, 0.0f, 0.0f, 0.0f); clearForce(i); getInternalForce(i); //sew together getTensionForce(i, ClothCenter, 0.01f); m3dScaleVector3(ClothLnk[i].force, TensionCoefficient); nextPosition(i); m3dSubtractVectors3(temp, ClothLnk[i].next_position, ClothLnk[i].position); m3dAddVectors3(displacement, displacement, temp); } m3dScaleVector3(displacement, (GLfloat)800000/PTCAMT); //printf(" total displace: %.1f %.1f %.1f\n", displacement[0], displacement[1], displacement[2]); m3dAddVectors3(ClothCenter, ClothCenter, displacement); //m3dLoadVector3(TotalDisplacement, 0.0f, 0.0f, 0.0f); refreshPosition(); }
void look_at(GLFrame & frame, const M3DVector3f eye, const M3DVector3f at, const M3DVector3f up) { M3DVector3f forward; m3dSubtractVectors3(forward, at, eye); set_up_frame(frame, eye, forward, up); }