void FixedRenderer::updateSkinning(Mesh * mesh, Armature * armature) { unsigned int s; unsigned int sSize = mesh->getSubMeshsNumber(); for(s=0; s<sSize; s++) { SubMesh * subMesh = &mesh->getSubMeshs()[s]; // data Vector3 * vertices = subMesh->getVertices(); if(! vertices) continue; SkinData * skinData = subMesh->getSkinData(); if(armature && skinData) { unsigned int verticesSize = subMesh->getVerticesSize(); Vector3 * skinVertices = getVertices(verticesSize); computeSkinning(armature, skinData, vertices, NULL, NULL, skinVertices, NULL, NULL); subMesh->getBoundingBox()->initFromPoints(skinVertices, verticesSize); } } mesh->updateBoundingBox(); }
static void createSubMesh(const aiScene * scene, const aiNode * nd, Mesh * mesh, SubMesh * subMeshs, unsigned int * count, const Matrix4x4 & parentMatrix) { aiMatrix4x4 nodeMat = nd->mTransformation; aiTransposeMatrix4(&nodeMat); Matrix4x4 matrix = parentMatrix * Matrix4x4((float*)&nodeMat); unsigned int m; for(m=0; m<nd->mNumMeshes; m++) { unsigned int i; const aiMesh * nodeMesh = scene->mMeshes[nd->mMeshes[m]]; if(nodeMesh->mPrimitiveTypes != aiPrimitiveType_TRIANGLE) continue; // current sub mesh SubMesh * subMesh = &(subMeshs[*count]); // vertices Vector3 * vertices = subMesh->allocVertices(nodeMesh->mNumVertices); for(i=0; i<nodeMesh->mNumVertices; i++) vertices[i] = matrix * Vector3(nodeMesh->mVertices[i].x, nodeMesh->mVertices[i].y, nodeMesh->mVertices[i].z); // normals if(nodeMesh->mNormals) { Vector3 * normals = subMesh->allocNormals(nodeMesh->mNumVertices); for(i=0; i<nodeMesh->mNumVertices; i++) normals[i] = matrix.getRotatedVector3(Vector3(nodeMesh->mNormals[i].x, nodeMesh->mNormals[i].y, nodeMesh->mNormals[i].z)).getNormalized(); } // tangents if(nodeMesh->mTangents) { Vector3 * tangents = subMesh->allocTangents(nodeMesh->mNumVertices); for(i=0; i<nodeMesh->mNumVertices; i++) tangents[i] = matrix.getRotatedVector3(Vector3(nodeMesh->mTangents[i].x, nodeMesh->mTangents[i].y, nodeMesh->mTangents[i].z)).getNormalized(); } // colors if(nodeMesh->mColors[0]) { Color * colors = subMesh->allocColors(nodeMesh->mNumVertices); for(i=0; i<nodeMesh->mNumVertices; i++) colors[i] = Color(nodeMesh->mColors[0][i].r, nodeMesh->mColors[0][i].g, nodeMesh->mColors[0][i].b, nodeMesh->mColors[0][i].a); } // uvcoords { unsigned int t, nb_texLayers = 0; for(t=0; t<AI_MAX_NUMBER_OF_TEXTURECOORDS; t++) { if(nodeMesh->mTextureCoords[t]) nb_texLayers++; } Vector2 * uvCoords = subMesh->allocTexCoords(nodeMesh->mNumVertices*nb_texLayers); nb_texLayers = 0; for(t=0; t<AI_MAX_NUMBER_OF_TEXTURECOORDS; t++) { if(nodeMesh->mTextureCoords[t]) { unsigned int offset = nodeMesh->mNumVertices*nb_texLayers; subMesh->setMapChannelOffset(t, offset); for(i=0; i<nodeMesh->mNumVertices; i++){ uvCoords[offset+i] = Vector2(nodeMesh->mTextureCoords[t][i].x, 1-nodeMesh->mTextureCoords[t][i].y); } nb_texLayers++; } } } // indices { unsigned int f; if(nodeMesh->mNumVertices < 65536) { unsigned short * indices = (unsigned short *)subMesh->allocIndices(nodeMesh->mNumFaces*3, VAR_USHORT); for(f=0; f<nodeMesh->mNumFaces; f++) { aiFace * face = &nodeMesh->mFaces[f]; for(i=0; i<3; i++){ indices[f*3+i] = face->mIndices[i]; } } } else { unsigned int * indices = (unsigned int *)subMesh->allocIndices(nodeMesh->mNumFaces*3, VAR_UINT); for(f=0; f<nodeMesh->mNumFaces; f++) { aiFace * face = &nodeMesh->mFaces[f]; for(i=0; i<3; i++){ indices[f*3+i] = face->mIndices[i]; } } } } // bones initBones(scene, nodeMesh, mesh, subMesh); // display subMesh->allocDisplays(1); MaterialDisplay * display = subMesh->addNewDisplay(PRIMITIVE_TRIANGLES, 0, subMesh->getIndicesSize()); display->setMaterial(mesh->getMaterial(nodeMesh->mMaterialIndex)); // cull mode int twosided = 0; aiMaterial * mtl = scene->mMaterials[nodeMesh->mMaterialIndex]; if(AI_SUCCESS == aiGetMaterialInteger(mtl, AI_MATKEY_TWOSIDED, &twosided)) { if(twosided != 0) display->setCullMode(CULL_NONE); } // bounding box subMesh->getBoundingBox()->initFromPoints(vertices, subMesh->getVerticesSize()); (*count)++; } for(unsigned int n=0; n<nd->mNumChildren; n++) createSubMesh(scene, nd->mChildren[n], mesh, subMeshs, count, matrix); }