📄 coresubmesh.cpp
字号:
//****************************************************************************//// coresubmesh.cpp //// Copyright (C) 2001, 2002 Bruno 'Beosil' Heidelberger ////****************************************************************************//// This library is free software; you can redistribute it and/or modify it //// under the terms of the GNU Lesser General Public License as published by //// the Free Software Foundation; either version 2.1 of the License, or (at //// your option) any later version. ////****************************************************************************//#ifdef HAVE_CONFIG_H#include "config.h"#endif//****************************************************************************//// Includes ////****************************************************************************//#include "cal3d/coresubmesh.h"#include "cal3d/coresubmorphtarget.h" /*****************************************************************************//** Constructs the core submesh instance. * * This function is the default constructor of the core submesh instance. *****************************************************************************/CalCoreSubmesh::CalCoreSubmesh() : m_coreMaterialThreadId(0), m_lodCount(0){} /*****************************************************************************//** Destructs the core submesh instance. * * This function is the destructor of the core submesh instance. *****************************************************************************/CalCoreSubmesh::~CalCoreSubmesh(){ // destroy all data m_vectorFace.clear(); m_vectorVertex.clear(); m_vectorPhysicalProperty.clear(); m_vectorvectorTextureCoordinate.clear(); m_vectorSpring.clear(); m_vectorTangentsEnabled.clear(); m_vectorvectorTangentSpace.clear(); // destroy all core sub morph targets std::vector<CalCoreSubMorphTarget *>::iterator iteratorCoreSubMorphTarget; for(iteratorCoreSubMorphTarget = m_vectorCoreSubMorphTarget.begin(); iteratorCoreSubMorphTarget != m_vectorCoreSubMorphTarget.end(); ++iteratorCoreSubMorphTarget) { delete (*iteratorCoreSubMorphTarget); } m_vectorCoreSubMorphTarget.clear();} /*****************************************************************************//** Returns the ID of the core material thread. * * This function returns the ID of the core material thread of this core * submesh instance. * * @return The ID of the core material thread. *****************************************************************************/int CalCoreSubmesh::getCoreMaterialThreadId(){ return m_coreMaterialThreadId;} /*****************************************************************************//** Returns the number of faces. * * This function returns the number of faces in the core submesh instance. * * @return The number of faces. *****************************************************************************/int CalCoreSubmesh::getFaceCount(){ return m_vectorFace.size();} /*****************************************************************************//** Returns the number of LOD steps. * * This function returns the number of LOD steps in the core submesh instance. * * @return The number of LOD steps. *****************************************************************************/int CalCoreSubmesh::getLodCount(){ return m_lodCount;} /*****************************************************************************//** Returns the number of springs. * * This function returns the number of springs in the core submesh instance. * * @return The number of springs. *****************************************************************************/int CalCoreSubmesh::getSpringCount(){ return m_vectorSpring.size();} /*****************************************************************************//** Returns true if tangent vectors are enabled. * * This function returns true if the core submesh contains tangent vectors. * * @return True if tangent vectors are enabled. *****************************************************************************/bool CalCoreSubmesh::isTangentsEnabled(int mapId){ if((mapId < 0) || (mapId >= (int)m_vectorTangentsEnabled.size())) return false; return m_vectorTangentsEnabled[mapId];} /*****************************************************************************//** UpdateTangentVector * *****************************************************************************/void CalCoreSubmesh::UpdateTangentVector(int v0, int v1, int v2, int mapId){ std::vector<CalCoreSubmesh::Vertex> &vvtx = getVectorVertex(); std::vector<CalCoreSubmesh::TextureCoordinate> &vtex = m_vectorvectorTextureCoordinate[mapId]; // Step 1. Compute the approximate tangent vector. double du1 = vtex[v1].u - vtex[v0].u; double dv1 = vtex[v1].v - vtex[v0].v; double du2 = vtex[v2].u - vtex[v0].u; double dv2 = vtex[v2].v - vtex[v0].v; double prod1 = (du1*dv2-dv1*du2); double prod2 = (du2*dv1-dv2*du1); if ((fabs(prod1) < 0.000001)||(fabs(prod2) < 0.000001)) return; double x = dv2/prod1; double y = dv1/prod2; CalVector vec1 = vvtx[v1].position - vvtx[v0].position; CalVector vec2 = vvtx[v2].position - vvtx[v0].position; CalVector tangent = (vec1 * ((float)x)) + (vec2 * ((float)y)); // Step 2. Orthonormalize the tangent. double component = (tangent * vvtx[v0].normal); tangent -= (vvtx[v0].normal * ((float)component)); tangent.normalize(); // Step 3: Add the estimated tangent to the overall estimate for the vertex. m_vectorvectorTangentSpace[mapId][v0].tangent+=tangent;} /*****************************************************************************//** Enables (and calculates) or disables the storage of tangent spaces. * * This function enables or disables the storage of tangent space bases. *****************************************************************************/bool CalCoreSubmesh::enableTangents(int mapId, bool enabled){ if((mapId < 0) || (mapId >= (int)m_vectorTangentsEnabled.size())) return false; m_vectorTangentsEnabled[mapId] = enabled; if(!enabled) { m_vectorvectorTangentSpace[mapId].clear(); return true; } m_vectorvectorTangentSpace[mapId].reserve(m_vectorVertex.size()); m_vectorvectorTangentSpace[mapId].resize(m_vectorVertex.size()); int tangentId; for(tangentId=0;tangentId< (int)m_vectorvectorTangentSpace[mapId].size();tangentId++) { m_vectorvectorTangentSpace[mapId][tangentId].tangent= CalVector(0.0f,0.0f,0.0f); m_vectorvectorTangentSpace[mapId][tangentId].crossFactor=1; } int faceId; for(faceId=0;faceId<(int)m_vectorFace.size();faceId++) { UpdateTangentVector(m_vectorFace[faceId].vertexId[0],m_vectorFace[faceId].vertexId[1],m_vectorFace[faceId].vertexId[2],mapId); UpdateTangentVector(m_vectorFace[faceId].vertexId[1],m_vectorFace[faceId].vertexId[2],m_vectorFace[faceId].vertexId[0],mapId); UpdateTangentVector(m_vectorFace[faceId].vertexId[2],m_vectorFace[faceId].vertexId[0],m_vectorFace[faceId].vertexId[1],mapId); } for(tangentId=0;tangentId< (int)m_vectorvectorTangentSpace[mapId].size();tangentId++) { m_vectorvectorTangentSpace[mapId][tangentId].tangent.normalize(); } return true;} /*****************************************************************************//** Returns the face vector. * * This function returns the vector that contains all faces of the core submesh * instance. * * @return A reference to the face vector. *****************************************************************************/std::vector<CalCoreSubmesh::Face>& CalCoreSubmesh::getVectorFace(){ return m_vectorFace;} /*****************************************************************************//** Returns the physical property vector. * * This function returns the vector that contains all physical properties of * the core submesh instance. * * @return A reference to the physical property vector. *****************************************************************************/std::vector<CalCoreSubmesh::PhysicalProperty>& CalCoreSubmesh::getVectorPhysicalProperty(){ return m_vectorPhysicalProperty;} /*****************************************************************************//** Returns the spring vector. * * This function returns the vector that contains all springs of the core * submesh instance. * * @return A reference to the spring vector. *****************************************************************************/std::vector<CalCoreSubmesh::Spring>& CalCoreSubmesh::getVectorSpring(){ return m_vectorSpring;} /*****************************************************************************//** Returns the texture coordinate vector-vector. * * This function returns the vector that contains all texture coordinate * vectors of the core submesh instance. This vector contains another vector * because there can be more than one texture map at each vertex. * * @return A reference to the texture coordinate vector-vector. *****************************************************************************/std::vector<std::vector<CalCoreSubmesh::TextureCoordinate> > & CalCoreSubmesh::getVectorVectorTextureCoordinate(){ return m_vectorvectorTextureCoordinate;} /*****************************************************************************//** Returns the tangent space vector-vector. * * This function returns the vector that contains all tangent space bases of * the core submesh instance. This vector contains another vector * because there can be more than one texture map at each vertex. * * @return A reference to the tangent space vector-vector. *****************************************************************************/std::vector<std::vector<CalCoreSubmesh::TangentSpace> >& CalCoreSubmesh::getVectorVectorTangentSpace(){ return m_vectorvectorTangentSpace;} /*****************************************************************************//** Returns the vertex vector. * * This function returns the vector that contains all vertices of the core * submesh instance. * * @return A reference to the vertex vector. *****************************************************************************/std::vector<CalCoreSubmesh::Vertex>& CalCoreSubmesh::getVectorVertex(){ return m_vectorVertex;} /*****************************************************************************//** Returns the number of vertices. * * This function returns the number of vertices in the core submesh instance. * * @return The number of vertices. *****************************************************************************/int CalCoreSubmesh::getVertexCount(){ return m_vectorVertex.size();} /*****************************************************************************//** Reserves memory for the vertices, faces and texture coordinates. * * This function reserves memory for the vertices, faces, texture coordinates * and springs of the core submesh instance. * * @param vertexCount The number of vertices that this core submesh instance * should be able to hold. * @param textureCoordinateCount The number of texture coordinates that this * core submesh instance should be able to hold. * @param faceCount The number of faces that this core submesh instance should * be able to hold. * @param springCount The number of springs that this core submesh instance * should be able to hold. * * @return One of the following values: * \li \b true if successful * \li \b false if an error happend *****************************************************************************/bool CalCoreSubmesh::reserve(int vertexCount, int textureCoordinateCount, int faceCount, int springCount){ // reserve the space needed in all the vectors m_vectorVertex.reserve(vertexCount); m_vectorVertex.resize(vertexCount); m_vectorTangentsEnabled.reserve(textureCoordinateCount); m_vectorTangentsEnabled.resize(textureCoordinateCount); m_vectorvectorTangentSpace.reserve(textureCoordinateCount);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -