ModelShape::ModelShape(IGraphicsDevice *device, Model *model) : Shape(device, "model"), mModel(model) { TMeshList &meshes = model->GetMeshes(); for(int i = 0; i < meshes.size(); i++) { ModelMesh *mesh = meshes[i]; VERTEX_BUFFER_DESC desc; desc.Stride = sizeof(ModelVertex); IVertexBuffer *meshBuffer = device->CreateVertexBuffer(desc); meshBuffer->SetData(mesh->GetBuffer(), sizeof(ModelVertex) * mesh->GetTotalFaces()); mMeshVertexBuffers.push_back(meshBuffer); ModelAnimationMaterialProperties *properties = new ModelAnimationMaterialProperties(); mMeshMaterials.push_back(new Material(device, properties)); } TMaterialList &materials = mModel->GetMaterials(); for(int i = 0; i < materials.size(); i++) { ModelMaterial *material = materials[i]; Image *image = material->GetImage(MaterialTextureDiffuse, 0); ITexture *texture = device->CreateTexture(); texture->Initialize(image->GetBits(), CSize(image->GetWidth(), image->GetHeight())); mTextures.push_back(texture); } }
void ModelShape::Draw(IGraphicsDevice *device, RenderState &state) { int animationFrame = mModel->GetCurrentAnimationFrame(); TMeshList &meshes = mModel->GetMeshes(); TBoneList &bones = mModel->GetBones(); for(int i = 0; i < meshes.size(); i++) { ModelMesh *mesh = meshes[i]; Material *material = mMeshMaterials[i]; ModelAnimationMaterialProperties *properties = dynamic_cast<ModelAnimationMaterialProperties *>(material->GetProperties()); if(properties) { int i = 0; for(int i = 0; i < bones.size(); i++) { ModelBone *bone = bones[i]; properties->SetBoneMatrix(i, bone->GetKeyframeTransform(animationFrame)); } ITexture *texture = mTextures[mesh->GetMaterial()]; properties->SetTexture(texture); } state.SetMaterial(material); ITechnique *technique = material->GetTechnique(0); technique->Begin(); while(technique->HasNextPass()) { technique->ProcessNextPass(device, state); mMeshVertexBuffers[i]->Activate(); device->Draw(PrimitiveTypeTriangleList, mesh->GetTotalFaces(), 0); } } }