Fichier source en cours : FW - Cube.h






Voici le code source cpp :




	#ifndef CUBE_MESH_H
#define CUBE_MESH_H

namespace fw
{
	template<class D3DVersion>
	class Cube : public D3DMeshSceneNode<D3DVersion>
	{
	public:	
		typedef typename D3DMeshSceneNode<D3DVersion> D3DMeshSceneNode;

		Cube() :
			D3DMeshSceneNode("Cube")
		{
			D3DMeshSceneNode::SetVertexType(VertexLayoutType::PTN_VERTEX);
			D3DMeshSceneNode::SetDrawMethod(D3DMeshSceneNode::DrawMethod::DRAW_INDEXED);
			D3DMeshSceneNode::SetTopology(D3D_PRIMITIVE_TOPOLOGY::D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
			D3DMeshSceneNode::SetIndexFormat(D3DMeshSceneNode::IndexFormat::FORMAT_32_BIT);

			Initialize();

			D3DMeshSceneNode::SetShaderTechnique( SHADER_MANAGER->GetShader("SimpleTexture") );
		}

		virtual ~Cube() 
		{
		}

		PTNVertex CreateVertex(
			const D3DXVECTOR3& pos,
			const D3DXVECTOR2& tex,
			const D3DXVECTOR3& normal)
		{
			PTNVertex v;

			v.position = pos;
			v.texture = tex;
			v.normal = normal;

			return v;
		}

		virtual bool Initialize() override
		{
			if (D3DMeshSceneNode::GetMeshCaches()->MeshBufferExists("Cube"))
			{
				const MeshBuffers<D3DVersion::BufferType>* pMesh = 
					D3DMeshSceneNode::GetMeshCaches()->GetMeshBuffers("Cube");

				D3DMeshSceneNode::SetMeshBuffers(pMesh);
			}
			else
			{
				CreateCubeMesh();

				const MeshBuffers<D3DVersion::BufferType>* pMesh(this);

				D3DMeshSceneNode::GetMeshCaches()->AddMeshBuffer("Cube", pMesh);
			}

			return true;
		}
	
		void CreateCubeMesh()
		{
			PTNVertex v[24];

			v[0] = CreateVertex(D3DXVECTOR3(-1.0f, 1.0f, -1.0f), D3DXVECTOR2(0.0f, 0.0f), D3DXVECTOR3(0.0f, 1.0f, 0.0f));
			v[1] = CreateVertex(D3DXVECTOR3(1.0f, 1.0f, -1.0f), D3DXVECTOR2(1.0f, 0.0f), D3DXVECTOR3(0.0f, 1.0f, 0.0f));
			v[2] = CreateVertex(D3DXVECTOR3(1.0f, 1.0f, 1.0f), D3DXVECTOR2(1.0f, 1.0f), D3DXVECTOR3(0.0f, 1.0f, 0.0f));
			v[3] = CreateVertex(D3DXVECTOR3(-1.0f, 1.0f, 1.0f), D3DXVECTOR2(0.0f, 1.0f), D3DXVECTOR3(0.0f, 1.0f, 0.0f));

			v[4] = CreateVertex(D3DXVECTOR3(-1.0f, -1.0f, -1.0f), D3DXVECTOR2(0.0f, 0.0f), D3DXVECTOR3(0.0f, -1.0f, 0.0f));
			v[5] = CreateVertex(D3DXVECTOR3(1.0f, -1.0f, -1.0f), D3DXVECTOR2(1.0f, 0.0f), D3DXVECTOR3(0.0f, -1.0f, 0.0f));
			v[6] = CreateVertex(D3DXVECTOR3(1.0f, -1.0f, 1.0f), D3DXVECTOR2(1.0f, 1.0f), D3DXVECTOR3(0.0f, -1.0f, 0.0f));
			v[7] = CreateVertex(D3DXVECTOR3(-1.0f, -1.0f, 1.0f), D3DXVECTOR2(0.0f, 1.0f), D3DXVECTOR3(0.0f, -1.0f, 0.0f));

			v[8] = CreateVertex(D3DXVECTOR3(-1.0f, -1.0f, 1.0f), D3DXVECTOR2(0.0f, 0.0f), D3DXVECTOR3(-1.0f, 0.0f, 0.0f));
			v[9] = CreateVertex(D3DXVECTOR3(-1.0f, -1.0f, -1.0f), D3DXVECTOR2(1.0f, 0.0f), D3DXVECTOR3(-1.0f, 0.0f, 0.0f));
			v[10] = CreateVertex(D3DXVECTOR3(-1.0f, 1.0f, -1.0f), D3DXVECTOR2(1.0f, 1.0f), D3DXVECTOR3(-1.0f, 0.0f, 0.0f));
			v[11] = CreateVertex(D3DXVECTOR3(-1.0f, 1.0f, 1.0f), D3DXVECTOR2(0.0f, 1.0f), D3DXVECTOR3(-1.0f, 0.0f, 0.0f));

			v[12] = CreateVertex(D3DXVECTOR3(1.0f, -1.0f, 1.0f), D3DXVECTOR2(0.0f, 0.0f), D3DXVECTOR3(1.0f, 0.0f, 0.0f));
			v[13] = CreateVertex(D3DXVECTOR3(1.0f, -1.0f, -1.0f), D3DXVECTOR2(1.0f, 0.0f), D3DXVECTOR3(1.0f, 0.0f, 0.0f));
			v[14] = CreateVertex(D3DXVECTOR3(1.0f, 1.0f, -1.0f), D3DXVECTOR2(1.0f, 1.0f), D3DXVECTOR3(1.0f, 0.0f, 0.0f));
			v[15] = CreateVertex(D3DXVECTOR3(1.0f, 1.0f, 1.0f), D3DXVECTOR2(0.0f, 1.0f), D3DXVECTOR3(1.0f, 0.0f, 0.0f));

			v[16] = CreateVertex(D3DXVECTOR3(-1.0f, -1.0f, -1.0f), D3DXVECTOR2(0.0f, 0.0f), D3DXVECTOR3(0.0f, 0.0f, -1.0f));
			v[17] = CreateVertex(D3DXVECTOR3(1.0f, -1.0f, -1.0f), D3DXVECTOR2(1.0f, 0.0f), D3DXVECTOR3(0.0f, 0.0f, -1.0f));
			v[18] = CreateVertex(D3DXVECTOR3(1.0f, 1.0f, -1.0f), D3DXVECTOR2(1.0f, 1.0f), D3DXVECTOR3(0.0f, 0.0f, -1.0f));
			v[19] = CreateVertex(D3DXVECTOR3(-1.0f, 1.0f, -1.0f), D3DXVECTOR2(0.0f, 1.0f), D3DXVECTOR3(0.0f, 0.0f, -1.0f));

			v[20] = CreateVertex(D3DXVECTOR3(-1.0f, -1.0f, 1.0f), D3DXVECTOR2(0.0f, 0.0f), D3DXVECTOR3(0.0f, 0.0f, 1.0f));
			v[21] = CreateVertex(D3DXVECTOR3(1.0f, -1.0f, 1.0f), D3DXVECTOR2(1.0f, 0.0f), D3DXVECTOR3(0.0f, 0.0f, 1.0f));
			v[22] = CreateVertex(D3DXVECTOR3(1.0f, 1.0f, 1.0f), D3DXVECTOR2(1.0f, 1.0f), D3DXVECTOR3(0.0f, 0.0f, 1.0f));
			v[23] = CreateVertex(D3DXVECTOR3(-1.0f, 1.0f, 1.0f), D3DXVECTOR2(0.0f, 1.0f), D3DXVECTOR3(0.0f, 0.0f, 1.0f));

			uint32 i[36] = { 3,1,0,
							 2,1,3,
							 6,4,5,
							 7,4,6,
							 11,9,8,
							 10,9,11,
							 14,12,13,
							 15,12,14,
							 19,17,16,
							 18,17,19,
							 22,20,21,
							 23,20,22 };

			// On déclare les tableaux
			std::vector<PTNVertex> vertices;
			std::vector<uint32> indices;

			// On reset les emplacements
			vertices.resize(24);
			indices.resize(36);

			// On affecte les valeurs
			std::copy(v, v + 24, vertices.begin());
			std::copy(i, i + 36, indices.begin());

			D3DMeshSceneNode::VBuildMesh(vertices, indices);
		}

		void SetTexture(const std::string& sTextureFileName)
		{
			ShaderSceneNodeInfo param;
			param.SetTexture(sTextureFileName);

			D3DMeshSceneNode::PushCustomShaderParameter(param);
		}
	};
}

#endif