OSDN Git Service

fix cmake/Macros.cmake
[moflib/moflib.git] / moflib-1.0 / moflib / moflib / mof / Mesh.cpp
1 #include "mof/private/Mesh.hpp"
2 #include "mof/GraphicsDevice.hpp"
3 #include "mof/private/MeshDisposer.hpp"
4 #include "mof/stream/Constant.hpp"
5 #include "mof/ConsoleIO.hpp"
6
7 struct mof::Mesh::Impl{
8         
9         std::shared_ptr<mof::MeshDisposer> pMeshDisposer;
10         unsigned long nMaterials;
11         std::shared_ptr<Material>* materials; 
12         std::shared_ptr<Texture>* textures;
13         mof::Manipulator<mof::Matrix3D>::Handler worldMatrix;
14         mof::FrameNumber m_frame;
15         
16
17         Impl(
18                 std::shared_ptr<mof::MeshDisposer> pMeshDisposer_ ,
19                 unsigned long nMaterials_ 
20                 ) : 
21                 pMeshDisposer(pMeshDisposer_) , 
22                 nMaterials(nMaterials_) , materials(NULL) , textures(NULL) ,
23                 worldMatrix(mof::makeConstantHandler(mof::Matrix3D::createIdentity()))
24                 {}
25
26         ~Impl(){
27                 delete[] materials;
28                 delete[] textures;
29                 }
30 }; 
31
32 mof::Mesh::Mesh(
33         std::shared_ptr<mof::MeshDisposer> pMeshDisposer ,
34         unsigned long nMaterials ,
35         const std::shared_ptr<Material>* materials , 
36         const std::shared_ptr<Texture>* textures 
37         ) : m_pImpl(new Impl(pMeshDisposer , nMaterials )){
38
39         m_pImpl->materials = new std::shared_ptr<mof::Material>[nMaterials];
40         m_pImpl->textures = new std::shared_ptr<mof::Texture>[nMaterials];      
41
42         for(unsigned long i = 0 ; i < nMaterials ; i++ ){
43                 m_pImpl->materials[i] = materials[i];
44                 m_pImpl->textures[i] = textures[i];
45         }
46 }
47
48 mof::Mesh::~Mesh(){
49 }
50         
51
52
53 void mof::Mesh::setWorldMatrix(const mof::Manipulator<mof::Matrix3D>::Handler& handler){
54         m_pImpl->worldMatrix = handler;
55 }
56
57
58
59
60
61 void mof::Mesh::setTexture( 
62                 unsigned int num ,
63                 const std::shared_ptr<mof::Texture>& pTexture){
64
65         if(num < m_pImpl->nMaterials)m_pImpl->textures[num] = pTexture; 
66
67 }
68
69
70 bool mof::Mesh::isVisible() const{
71         return true;
72 }
73
74
75 void mof::Mesh::update(){
76         m_pImpl->m_frame++;
77 }
78
79 void mof::Mesh::draw() const{
80         
81
82         for( DWORD i = 0;  i < m_pImpl->nMaterials ; i++ )
83         {
84         mof::GraphicsDevice::setMaterial(*m_pImpl->materials[i].get()); 
85                 mof::GraphicsDevice::setTexture(m_pImpl->textures[i].get());
86                 mof::GraphicsDevice::setWorldTransform(m_pImpl->worldMatrix->value(m_pImpl->m_frame));
87
88                 HRESULT hr = m_pImpl->pMeshDisposer->pMesh->DrawSubset( i );
89                 if(FAILED(hr))throw std::runtime_error("Failed --- DrawSubset");
90         }
91 }
92