//- CONSTRUCTOR --------------------------------------------------------------------------------------------- Node::Node() : NodeName(""), NumChildren(0), NumMeshes(0), Transformation(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1), Children(NULL), Meshes(NULL), Parent(NULL), Translate(Vector3s(0, 0, 0)), Rotate(Vector3s(0, 0, 0)), Scale(Vector3s(1, 1, 1)) { }
static bool aabbInHalfPlane( const Array3s& min, const Array3s& max, const StaticPlane& plane ) { // The AABB is in the plane if and only if at least one of its vertices is in the plane if( plane.distanceToPoint( Vector3s( min.x(), min.y(), min.z() ) ) <= 0 ) { return true; } if( plane.distanceToPoint( Vector3s( min.x(), min.y(), max.z() ) ) <= 0 ) { return true; } if( plane.distanceToPoint( Vector3s( min.x(), max.y(), min.z() ) ) <= 0 ) { return true; } if( plane.distanceToPoint( Vector3s( min.x(), max.y(), max.z() ) ) <= 0 ) { return true; } if( plane.distanceToPoint( Vector3s( max.x(), min.y(), min.z() ) ) <= 0 ) { return true; } if( plane.distanceToPoint( Vector3s( max.x(), min.y(), max.z() ) ) <= 0 ) { return true; } if( plane.distanceToPoint( Vector3s( max.x(), max.y(), min.z() ) ) <= 0 ) { return true; } if( plane.distanceToPoint( Vector3s( max.x(), max.y(), max.z() ) ) <= 0 ) { return true; } return false; }
void SceneXMLParser::loadSimpleGravityForces(rapidxml::xml_node<>* node, Scene& scene) { assert(node != NULL); int forcenum = 0; for (rapidxml::xml_node<>* nd = node->first_node("fluidsimplegravityforce"); nd; nd = nd->next_sibling("fluidsimplegravityforce")) { scalar fx, fy, fz; if (nd->first_attribute("fx")) { std::string attribute(nd->first_attribute("fx")->value()); if( !stringutils::extractFromString(attribute,fx) ) { std::cerr << outputmod::startred << "ERROR IN XMLSCENEPARSER:" << outputmod::endred << "Failed to parse fx attribute for simple gravity force. Value must be scalar. Exiting." << std::endl; exit(1); } } else { std::cerr << outputmod::startred << "ERROR IN XMLSCENEPARSER:" << outputmod::endred << "Missing fx attribute for fluid. Value must be scalar. Exiting." << std::endl; exit(1); } if (nd->first_attribute("fy")) { std::string attribute(nd->first_attribute("fy")->value()); if( !stringutils::extractFromString(attribute,fy) ) { std::cerr << outputmod::startred << "ERROR IN XMLSCENEPARSER:" << outputmod::endred << "Failed to parse fy attribute for simple gravity force. Value must be scalar. Exiting." << std::endl; exit(1); } } else { std::cerr << outputmod::startred << "ERROR IN XMLSCENEPARSER:" << outputmod::endred << "Missing fy attribute for fluid. Value must be scalar. Exiting." << std::endl; exit(1); } if (nd->first_attribute("fz")) { std::string attribute(nd->first_attribute("fz")->value()); if( !stringutils::extractFromString(attribute,fz) ) { std::cerr << outputmod::startred << "ERROR IN XMLSCENEPARSER:" << outputmod::endred << "Failed to parse fz attribute for simple gravity force. Value must be scalar. Exiting." << std::endl; exit(1); } } else { std::cerr << outputmod::startred << "ERROR IN XMLSCENEPARSER:" << outputmod::endred << "Missing fz attribute for fluid. Value must be scalar. Exiting." << std::endl; exit(1); } FluidSimpleGravityForce* force = new FluidSimpleGravityForce(Vector3s(fx, fy, fz)); scene.insertFluidForce(force); forcenum++; } }
//------------------------------------------------------------------------------------------------------------------------------------------- void transferGeometricTransformation(FbxNode* fbxNode, Node* node){ FbxVector4 vector4; vector4 = fbxNode->GetGeometricTranslation(FbxNode::eSourcePivot); node->Translate = Vector3s((double)vector4[0], (double)vector4[1], (double)vector4[2]); vector4 = fbxNode->GetGeometricRotation(FbxNode::eSourcePivot); node->Rotate = Vector3s((double)vector4[0], (double)vector4[1], (double)vector4[2]); vector4 = fbxNode->GetGeometricScaling(FbxNode::eSourcePivot); node->Scale = Vector3s((double)vector4[0], (double)vector4[1], (double)vector4[2]); FbxAMatrix globalTransform = fbxNode->EvaluateGlobalTransform(0); const double* flatArray = (const double*)globalTransform; node->Transformation = Matrix4x4s(flatArray[0], flatArray[1], flatArray[2], flatArray[3], flatArray[4], flatArray[5], flatArray[6], flatArray[7], flatArray[8], flatArray[9], flatArray[10], flatArray[11], flatArray[12], flatArray[13], flatArray[14], flatArray[15]); }
//------------------------------------------------------------------------------------------------------------------------------------------- void transferNormals(FbxMesh* fbxMesh, Mesh* mesh){ int numVertices = fbxMesh->GetControlPointsCount(); FbxGeometryElementNormal* normals = fbxMesh->GetElementNormal(0); mesh->InitEmptyNormals(numVertices); for (int i = 0; i < numVertices; i++){ FbxVector4 normal = normals->GetDirectArray().GetAt(i); mesh->Push_back(Mesh::MeshType::NORMAL, &Vector3s((double)normal[0], (double)normal[1], (double)normal[2])); } LOG_DEBUG << "Transfer normals successfully! "; }
//------------------------------------------------------------------------------------------------------------------------------------------- void transferVertices(FbxMesh* fbxMesh, Mesh* mesh){ int numVertices = fbxMesh->GetControlPointsCount(); FbxVector4* vertices = fbxMesh->GetControlPoints(); mesh->InitEmptyVertices(numVertices); for (int i = 0; i < numVertices; i++){ FbxVector4 vertex = vertices[i]; mesh->Push_back(Mesh::MeshType::VERTICLE, &Vector3s((double)vertex[0], (double)vertex[1], (double)vertex[2])); } LOG_DEBUG << "Transfer vertices successfully! "; }
void eqHello::Renderer::_setupCube() { if (_vertexArray) return; _vertices = {// front seq::Vector3f(-0.5, -0.5, 0.5), seq::Vector3f(0.5, -0.5, 0.5), seq::Vector3f(0.5, 0.5, 0.5), seq::Vector3f(-0.5, 0.5, 0.5), // back seq::Vector3f(-0.5, -0.5, -0.5), seq::Vector3f(0.5, -0.5, -0.5), seq::Vector3f(0.5, 0.5, -0.5), seq::Vector3f(-0.5, 0.5, -0.5)}; _triangles = {// front Vector3s(0, 1, 2), Vector3s(2, 3, 0), // top Vector3s(3, 2, 6), Vector3s(6, 7, 3), // back Vector3s(7, 6, 5), Vector3s(5, 4, 7), // bottom Vector3s(4, 5, 1), Vector3s(1, 0, 4), // left Vector3s(4, 0, 3), Vector3s(3, 7, 4), // right Vector3s(1, 5, 6), Vector3s(6, 2, 1)}; _colors = {seq::Vector3f(0.5, 0.5, 0.5), seq::Vector3f(0.5, 0.5, 1.0), seq::Vector3f(0.5, 1.0, 0.5), seq::Vector3f(0.5, 1.0, 1.0), seq::Vector3f(1.0, 0.5, 0.5), seq::Vector3f(1.0, 0.5, 1.0), seq::Vector3f(1.0, 1.0, 0.5), seq::Vector3f(1.0, 1.0, 1.0)}; seq::ObjectManager& om = getObjectManager(); _vertexArray = om.newVertexArray(&_vertexArray); EQ_GL_CALL(glBindVertexArray(_vertexArray)); _vertexBuffer = om.newBuffer(&_vertexBuffer); EQ_GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer)); EQ_GL_CALL(glBufferData(GL_ARRAY_BUFFER, _vertices.size() * sizeof(seq::Vector3f), _vertices.data(), GL_STATIC_DRAW)); EQ_GL_CALL(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0)); _colorBuffer = om.newBuffer(&_colorBuffer); EQ_GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, _colorBuffer)); EQ_GL_CALL(glBufferData(GL_ARRAY_BUFFER, _colors.size() * sizeof(seq::Vector3f), _colors.data(), GL_STATIC_DRAW)); EQ_GL_CALL(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0)); _indexBuffer = om.newBuffer(&_indexBuffer); EQ_GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer)); EQ_GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, _triangles.size() * sizeof(Vector3s), _triangles.data(), GL_STATIC_DRAW)); EQ_GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, 0)); EQ_GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); EQ_GL_CALL(glBindVertexArray(0)); }
// TODO: most of this function can be vectorized void computeMoments( const Matrix3Xsc& vertices, const Matrix3Xuc& indices, scalar& mass, Vector3s& I, Vector3s& center, Matrix3s& R ) { assert( ( indices.array() < unsigned( vertices.cols() ) ).all() ); const scalar oneDiv6{ 1.0 / 6.0 }; const scalar oneDiv24{ 1.0 / 24.0 }; const scalar oneDiv60{ 1.0 / 60.0 }; const scalar oneDiv120{ 1.0 / 120.0 }; // order: 1, x, y, z, x^2, y^2, z^2, xy, yz, zx VectorXs integral{ VectorXs::Zero( 10 ) }; for( int i = 0; i < indices.cols(); ++i ) { // Copy the vertices of triangle i const Vector3s v0{ vertices.col( indices( 0, i ) ) }; const Vector3s v1{ vertices.col( indices( 1, i ) ) }; const Vector3s v2{ vertices.col( indices( 2, i ) ) }; // Compute a normal for the current triangle const Vector3s N{ ( v1 - v0 ).cross( v2 - v0 ) }; // Compute the integral terms scalar tmp0{ v0.x() + v1.x() }; scalar tmp1{ v0.x() * v0.x() }; scalar tmp2{ tmp1 + v1.x() * tmp0 }; const scalar f1x{ tmp0 + v2.x() }; const scalar f2x{ tmp2 + v2.x() * f1x }; const scalar f3x{ v0.x() * tmp1 + v1.x() * tmp2 + v2.x() * f2x }; const scalar g0x{ f2x + v0.x() * ( f1x + v0.x() ) }; const scalar g1x{ f2x + v1.x() * ( f1x + v1.x() ) }; const scalar g2x{ f2x + v2.x() * ( f1x + v2.x() ) }; tmp0 = v0.y() + v1.y(); tmp1 = v0.y() * v0.y(); tmp2 = tmp1 + v1.y() * tmp0; const scalar f1y{ tmp0 + v2.y() }; const scalar f2y{ tmp2 + v2.y() * f1y }; const scalar f3y{ v0.y() * tmp1 + v1.y() * tmp2 + v2.y() * f2y }; const scalar g0y{ f2y + v0.y() * ( f1y + v0.y() ) }; const scalar g1y{ f2y + v1.y() * ( f1y + v1.y() ) }; const scalar g2y{ f2y + v2.y() * ( f1y + v2.y() ) }; tmp0 = v0.z() + v1.z(); tmp1 = v0.z()*v0.z(); tmp2 = tmp1 + v1.z()*tmp0; const scalar f1z{ tmp0 + v2.z() }; const scalar f2z{ tmp2 + v2.z() * f1z }; const scalar f3z{ v0.z() * tmp1 + v1.z() * tmp2 + v2.z() * f2z }; const scalar g0z{ f2z + v0.z() * ( f1z + v0.z() ) }; const scalar g1z{ f2z + v1.z() * ( f1z + v1.z() ) }; const scalar g2z{ f2z + v2.z() * ( f1z + v2.z() ) }; // Update integrals integral(0) += N.x() * f1x; integral(1) += N.x() * f2x; integral(2) += N.y() * f2y; integral(3) += N.z() * f2z; integral(4) += N.x() * f3x; integral(5) += N.y() * f3y; integral(6) += N.z() * f3z; integral(7) += N.x() * ( v0.y() * g0x + v1.y() * g1x + v2.y() * g2x ); integral(8) += N.y() * ( v0.z() * g0y + v1.z() * g1y + v2.z() * g2y ); integral(9) += N.z() * ( v0.x() * g0z + v1.x() * g1z + v2.x() * g2z ); } integral(0) *= oneDiv6; integral(1) *= oneDiv24; integral(2) *= oneDiv24; integral(3) *= oneDiv24; integral(4) *= oneDiv60; integral(5) *= oneDiv60; integral(6) *= oneDiv60; integral(7) *= oneDiv120; integral(8) *= oneDiv120; integral(9) *= oneDiv120; // Mass mass = integral(0); // Center of mass center = Vector3s( integral(1), integral(2), integral(3) )/mass; // Inertia relative to world origin R(0,0) = integral(5) + integral(6); R(0,1) = -integral(7); R(0,2) = -integral(9); R(1,0) = R(0,1); R(1,1) = integral(4) + integral(6); R(1,2) = -integral(8); R(2,0) = R(0,2); R(2,1) = R(1,2); R(2,2) = integral(4) + integral(5); // Comptue the inertia relative to the center of mass R(0,0) -= mass * ( center.y() * center.y() + center.z() * center.z() ); R(0,1) += mass * center.x() * center.y(); R(0,2) += mass * center.z() * center.x(); R(1,0) = R(0,1); R(1,1) -= mass * ( center.z() * center.z() + center.x() * center.x() ); R(1,2) += mass * center.y() * center.z(); R(2,0) = R(0,2); R(2,1) = R(1,2); R(2,2) -= mass * ( center.x() * center.x() + center.y() * center.y() ); // Diagonalize the inertia tensor Matrix3s R0; diagonalizeInertiaTensor( R, R0, I ); // Check that we actually diagonalized the inertia tensor assert( ( R0 * Matrix3s( I.asDiagonal() ) * R0.transpose() - R ).lpNorm<Eigen::Infinity>() <= 1.0e-6 ); assert( ( Matrix3s( I.asDiagonal() ) - R0.transpose() * R * R0 ).lpNorm<Eigen::Infinity>() <= 1.0e-6 ); R = R0; // All inertias should be positive assert( ( I.array() > 0.0 ).all() ); // Check that we have an orthonormal transformation assert( ( R * R.transpose() - Matrix3s::Identity() ).lpNorm<Eigen::Infinity>() <= 1.0e-6 ); assert( fabs( R.determinant() - 1.0 ) <= 1.0e-6 ); }