-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeshLoader.h
112 lines (86 loc) · 2.75 KB
/
MeshLoader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//--------------------------------------------------------------------------------------
// File: MeshLoader.h
//
// Wrapper class for ID3DXMesh interface. Handles loading mesh data from an .obj file
// and resource management for material textures.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#ifndef _MESHLOADER_H_
#define _MESHLOADER_H_
#pragma once
// Vertex format
struct VERTEX
{
D3DXVECTOR3 position;
D3DXVECTOR3 normal;
D3DXVECTOR3 tangent;
D3DXVECTOR3 bitangent;
D3DXVECTOR2 texcoord;
};
// Used for a hashtable vertex cache when creating the mesh from a .obj file
struct CacheEntry
{
UINT index;
CacheEntry* pNext;
};
// Material properties per mesh subset
struct Material
{
WCHAR strName[MAX_PATH];
D3DXVECTOR3 vAmbient;
D3DXVECTOR3 vDiffuse;
D3DXVECTOR3 vSpecular;
int nShininess;
float fAlpha;
bool bSpecular;
WCHAR strDiffuseTex[MAX_PATH];
WCHAR strSpecularTex[MAX_PATH];
WCHAR strNormalTex[MAX_PATH];
float bumpVal;
IDirect3DTexture9* pDiffuseTex;
IDirect3DTexture9* pSpecularTex;
IDirect3DTexture9* pNormalTex;
D3DXHANDLE hTechnique;
};
class CMeshLoader
{
public:
CMeshLoader();
~CMeshLoader();
HRESULT Create( IDirect3DDevice9* pd3dDevice, const WCHAR* strFilename );
void Destroy();
UINT GetNumMaterials() const
{
return m_Materials.GetSize();
}
Material* GetMaterial( UINT iMaterial )
{
return m_Materials.GetAt( iMaterial );
}
ID3DXMesh* GetMesh()
{
return m_pMesh;
}
WCHAR* GetMediaDirectory()
{
return m_strMediaDir;
}
private:
HRESULT LoadGeometryFromOBJ( const WCHAR* strFilename );
HRESULT LoadMaterialsFromMTL( const WCHAR* strFileName );
void InitMaterial( Material* pMaterial );
DWORD AddVertex( UINT hash, VERTEX* pVertex );
void DeleteCache();
void ComputeTBN(VERTEX& v0, VERTEX& v1, VERTEX& v2);
void FillTBN();
IDirect3DDevice9* m_pd3dDevice; // Direct3D Device object associated with this mesh
ID3DXMesh* m_pMesh; // Encapsulated D3DX Mesh
CGrowableArray <CacheEntry*> m_VertexCache; // Hashtable cache for locating duplicate vertices
CGrowableArray <VERTEX> m_Vertices; // Filled and copied to the vertex buffer
CGrowableArray <DWORD> m_Indices; // Filled and copied to the index buffer
CGrowableArray <DWORD> m_Attributes; // Filled and copied to the attribute buffer
CGrowableArray <Material*> m_Materials; // Holds material properties per subset
WCHAR m_strMediaDir[ MAX_PATH ]; // Directory where the mesh was found
};
#endif // _MESHLOADER_H_