void CalSpringSystem::calculateVertices(CalSubmesh *pSubmesh, float deltaTime) { // get the vertex vector of the submesh std::vector<CalVector>& vectorVertex = pSubmesh->getVectorVertex(); // get the physical property vector of the submesh std::vector<CalSubmesh::PhysicalProperty>& vectorPhysicalProperty = pSubmesh->getVectorPhysicalProperty(); // get the physical property vector of the core submesh std::vector<CalCoreSubmesh::PhysicalProperty>& vectorCorePhysicalProperty = pSubmesh->getCoreSubmesh()->getVectorPhysicalProperty(); // loop through all the vertices int vertexId; for(vertexId = 0; vertexId < (int)vectorVertex.size(); ++vertexId) { // get the vertex CalVector& vertex = vectorVertex[vertexId]; // get the physical property of the vertex CalSubmesh::PhysicalProperty& physicalProperty = vectorPhysicalProperty[vertexId]; // get the physical property of the core vertex CalCoreSubmesh::PhysicalProperty& corePhysicalProperty = vectorCorePhysicalProperty[vertexId]; // store current position for later use CalVector position; position = physicalProperty.position; // only take vertices with a weight > 0 into account if(corePhysicalProperty.weight > 0.0f) { // do the Verlet step physicalProperty.position += (position - physicalProperty.positionOld) * 0.99f + physicalProperty.force / corePhysicalProperty.weight * deltaTime * deltaTime; CalSkeleton *pSkeleton = m_pModel->getSkeleton(); if(m_collision) { std::vector<CalBone *> &m_vectorbone = pSkeleton->getVectorBone(); unsigned long boneId; for(boneId=0; boneId < m_vectorbone.size(); boneId++) { CalBoundingBox p = m_vectorbone[boneId]->getBoundingBox(); bool in=true; float min=1e10; int index=-1; int faceId; for(faceId=0; faceId < 6 ; faceId++) { if(p.plane[faceId].eval(physicalProperty.position)<=0) { in=false; } else { float dist=p.plane[faceId].dist(physicalProperty.position); if(dist<min) { index=faceId; min=dist; } } } if(in && index!=-1) { CalVector normal = CalVector(p.plane[index].a,p.plane[index].b,normal.z = p.plane[index].c); normal.normalize(); physicalProperty.position = physicalProperty.position - min*normal; } in=true; for(faceId=0; faceId < 6 ; faceId++) { if(p.plane[faceId].eval(physicalProperty.position) < 0 ) { in=false; } } if(in) { physicalProperty.position = vectorVertex[vertexId]; } } } } else { physicalProperty.position = vectorVertex[vertexId]; } // make the current position the old one physicalProperty.positionOld = position; // set the new position of the vertex vertex = physicalProperty.position; // clear the accumulated force on the vertex physicalProperty.force.clear(); } // get the spring vector of the core submesh std::vector<CalCoreSubmesh::Spring>& vectorSpring = pSubmesh->getCoreSubmesh()->getVectorSpring(); // iterate a few times to relax the constraints int iterationCount; #define ITERATION_COUNT 2 for(iterationCount = 0; iterationCount < ITERATION_COUNT; ++iterationCount) { // loop through all the springs std::vector<CalCoreSubmesh::Spring>::iterator iteratorSpring; for(iteratorSpring = vectorSpring.begin(); iteratorSpring != vectorSpring.end(); ++iteratorSpring) { // get the spring CalCoreSubmesh::Spring& spring = *iteratorSpring; // compute the difference between the two spring vertices CalVector distance; distance = vectorVertex[spring.vertexId[1]] - vectorVertex[spring.vertexId[0]]; // get the current length of the spring float length; length = distance.length(); if(length > 0.0f) { /*if (spring.springCoefficient == 0) { vectorVertex[spring.vertexId[1]] = vectorVertex[spring.vertexId[0]]; vectorPhysicalProperty[spring.vertexId[1]].position = vectorVertex[spring.vertexId[0]]; } else {*/ float factor[2]; factor[0] = (length - spring.idleLength) / length; factor[1] = factor[0]; if(vectorCorePhysicalProperty[spring.vertexId[0]].weight > 0.0f) { factor[0] /= 2.0f; factor[1] /= 2.0f; } else { factor[0] = 0.0f; } if(vectorCorePhysicalProperty[spring.vertexId[1]].weight <= 0.0f) { factor[0] *= 2.0f; factor[1] = 0.0f; } vectorVertex[spring.vertexId[0]] += distance * factor[0]; vectorPhysicalProperty[spring.vertexId[0]].position = vectorVertex[spring.vertexId[0]]; vectorVertex[spring.vertexId[1]] -= distance * factor[1]; vectorPhysicalProperty[spring.vertexId[1]].position = vectorVertex[spring.vertexId[1]]; //} } } } /* DEBUG-CODE ******************** CalVector spherePosition(Sphere.x, Sphere.y, Sphere.z); float sphereRadius = Sphere.radius; // loop through all the vertices for(vertexId = 0; vertexId < (int)vectorVertex.size(); ++vertexId) { // get the vertex CalVector& vertex = vectorVertex[vertexId]; // get the physical property of the vertex CalSubmesh::PhysicalProperty& physicalProperty = vectorPhysicalProperty[vertexId]; // get the physical property of the core vertex CalCoreSubmesh::PhysicalProperty& corePhysicalProperty = vectorCorePhysicalProperty[vertexId]; // only take vertices with a weight > 0 into account if(corePhysicalProperty.weight > 0.0f) { CalVector position; position = physicalProperty.position; position -= spherePosition; float length; length = position.normalize(); if(length < sphereRadius) { position *= sphereRadius; position += spherePosition; physicalProperty.position = position; physicalProperty.positionOld = position; vertex = physicalProperty.position; } } } *********************************/ }
static void generateTangentAndHandednessBuffer( osgCal::MeshData* m, const CalIndex* indexBuffer ) { if ( !m->texCoordBuffer.valid() ) { return; } int vertexCount = m->vertexBuffer->size(); int faceCount = m->getIndicesCount() / 3; m->tangentAndHandednessBuffer = new TangentAndHandednessBuffer( vertexCount ); CalVector* tan1 = new CalVector[vertexCount]; CalVector* tan2 = new CalVector[vertexCount]; const GLfloat* texCoordBufferData = (GLfloat*) m->texCoordBuffer->getDataPointer(); const GLfloat* vb = (GLfloat*) m->vertexBuffer->getDataPointer(); #ifdef OSG_CAL_BYTE_BUFFERS GLfloat* thb = new GLfloat[ vertexCount*4 ]; const GLfloat* nb = floatNormalBuffer; #else GLfloat* thb = (GLfloat*) m->tangentAndHandednessBuffer->getDataPointer(); // GLshort* thb = (GLshort*) m->tangentAndHandednessBuffer->getDataPointer(); const GLfloat* nb = (GLfloat*) m->normalBuffer->getDataPointer(); #endif for ( int face = 0; face < faceCount; face++ ) { for ( int j = 0; j < 3; j++ ) { // there seems to be no visual difference in calculating // tangent per vertex (as is tan1[i1] += spos(j=0,1,2)) // or per face (tan1[i1,i2,i3] += spos) CalIndex i1 = indexBuffer[face*3+(j+0)%3]; CalIndex i2 = indexBuffer[face*3+(j+1)%3]; CalIndex i3 = indexBuffer[face*3+(j+2)%3]; const float* v1 = &vb[i1*3]; const float* v2 = &vb[i2*3]; const float* v3 = &vb[i3*3]; const float* w1 = &texCoordBufferData[i1*2]; const float* w2 = &texCoordBufferData[i2*2]; const float* w3 = &texCoordBufferData[i3*2]; #define x(_a) (_a[0]) #define y(_a) (_a[1]) #define z(_a) (_a[2]) float x1 = x(v2) - x(v1); float x2 = x(v3) - x(v1); float y1 = y(v2) - y(v1); float y2 = y(v3) - y(v1); float z1 = z(v2) - z(v1); float z2 = z(v3) - z(v1); float s1 = x(w2) - x(w1); float s2 = x(w3) - x(w1); float t1 = y(w2) - y(w1); float t2 = y(w3) - y(w1); #undef x #undef y #undef z //float r = 1.0F / (s1 * t2 - s2 * t1); float r = (s1 * t2 - s2 * t1) < 0 ? -1.0 : 1.0; CalVector sdir((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r); CalVector tdir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r); // sdir & tdir can be 0 (when UV unwrap doesn't exists // or has errors like coincide points) // we ignore them if ( sdir.length() > 0 ) { sdir.normalize(); tan1[i1] += sdir; //tan1[i2] += sdir; //tan1[i3] += sdir; } if ( tdir.length() > 0 ) { tdir.normalize(); tan2[i1] += tdir; //tan2[i2] += tdir; //tan2[i3] += tdir; } } } for (long a = 0; a < vertexCount; a++) { CalVector tangent; CalVector binormal; CalVector t = tan1[a]; CalVector b = tan2[a]; CalVector n = CalVector( nb[a*3+0], nb[a*3+1], nb[a*3+2] ); // tangent & bitangent can be zero when UV unwrap doesn't exists // or has errors like coincide points if ( t.length() > 0 ) { t.normalize(); // Gram-Schmidt orthogonalize tangent = t - n * (n*t); tangent.normalize(); // Calculate handedness binormal = CalVector(n % tangent) * ((((n % t) * b) < 0.0F) ? -1.0f : 1.0f); binormal.normalize(); } else if ( b.length() > 0 ) { b.normalize(); // Gram-Schmidt orthogonalize binormal = b - n * (n*b); binormal.normalize(); // Calculate handedness tangent = CalVector(n % binormal) * ((((n % b) * t) < 0.0F) ? -1.0f : 1.0f); tangent.normalize(); } // std::cout << "t = " << tangent.x << '\t' << tangent.y << '\t' << tangent.z << '\n'; // std::cout << "b = " << binormal.x << '\t' << binormal.y << '\t' << binormal.z << '\n'; // std::cout << "n = " << n.x << '\t' << n.y << '\t' << n.z << '\n'; // thb[a*4+0] = floatToHalf( tangent.x ); //tangent.x * 0x7FFF; // thb[a*4+1] = floatToHalf( tangent.y ); //tangent.y * 0x7FFF; // thb[a*4+2] = floatToHalf( tangent.z ); //tangent.z * 0x7FFF; // thb[a*4+3] = floatToHalf((((n % tangent) * binormal) > 0.0F) ? -1.0f : 1.0f); // handedness thb[a*4+0] = tangent.x; thb[a*4+1] = tangent.y; thb[a*4+2] = tangent.z; thb[a*4+3] = ((((n % tangent) * binormal) > 0.0F) ? -1.0f : 1.0f); // handedness } delete[] tan1; delete[] tan2; #ifdef OSG_CAL_BYTE_BUFFERS GLbyte* tangents = (GLbyte*) tangentBuffer->getDataPointer(); GLbyte* binormals = (GLbyte*) binormalBuffer->getDataPointer(); for ( int i = 0; i < vertexCount*3; i++ ) { tangents[i] = static_cast< GLbyte >( tangentBuffer[i]*127.0 ); binormals[i] = static_cast< GLbyte >( binormalBuffer[i]*127.0 ); //std::cout << (int)tangents[i] << '\n'; } delete[] tangentBuffer; delete[] binormalBuffer; #endif }