-
Notifications
You must be signed in to change notification settings - Fork 1
/
DefaultTextMesh.cpp
131 lines (118 loc) · 3.9 KB
/
DefaultTextMesh.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <d3dx9.h>
#include "DefaultTextMesh.h"
#include "defaulttextreader.h"
#include "GameEngine.h"
CDefaultTextMesh::CDefaultTextMesh(void)
{
m_vertexSize = sizeof(VERTEX_MESH_DEFAULT);
m_indexSize = 4;
}
CDefaultTextMesh::~CDefaultTextMesh( void )
{
Destroy();
}
bool CDefaultTextMesh::LoadFromFile( LPDIRECT3DDEVICE9 device, const char * filename )
{
Init(device);
DefaultTextReader reader;
if (!reader.readFile(filename))
return false;
m_vertexCount = reader.vertexCount;
DWORD SizeVertices = (DWORD)m_vertexCount * sizeof(VERTEX_MESH_DEFAULT);
if ( FAILED(m_device->CreateVertexBuffer(
SizeVertices,
0,
VERTEX_MESH_DEFAULT::FVF,
D3DPOOL_MANAGED,
&m_pVB,
NULL)))
{
MessageBox(GetWindow(), "Mesh DefaultText CreateVB Error", "error", 0);
return false;
}
VERTEX_MESH_DEFAULT* pVertices;
if ( FAILED(m_pVB->Lock( 0, SizeVertices, (VOID **)&pVertices, 0 ) ) )
{
MessageBox(GetWindow(), "Mesh DefaultText LockVB Error", "error", 0);
return false;
}
for (size_t i = 0; i < reader.vertexCount; i++)
{
pVertices[i].pos.x = (float)reader.vertices[i * 3];
pVertices[i].pos.y = (float)reader.vertices[i * 3 + 1];
pVertices[i].pos.z = (float)reader.vertices[i * 3 + 2];
if (reader.normals)
{
pVertices[i].norm.x = (float)reader.normals[i * 3];
pVertices[i].norm.y = (float)reader.normals[i * 3 + 1];
pVertices[i].norm.z = (float)reader.normals[i * 3 + 2];
}
if (reader.tex)
{
pVertices[i].tu = (float)reader.tex[i * 2];
pVertices[i].tv = (float)reader.tex[i * 2 + 1];
}
pVertices[i].color = 0xFFFFFFFF;
}
m_indexCount = reader.faceCount * 3;
DWORD SizeIndices = (DWORD)m_indexCount * sizeof(DWORD);
if ( FAILED(m_device->CreateIndexBuffer(SizeIndices, 0, D3DFMT_INDEX32, D3DPOOL_MANAGED, &m_pIB, NULL) ) )
{
MessageBox(GetWindow(), "Mesh DefaultText CreateIB Error", "error", 0);
return false;
}
DWORD * pIndices;
if ( FAILED(m_pIB->Lock( 0, SizeIndices, (VOID **)&pIndices, 0 ) ) )
{
MessageBox(GetWindow(), "Mesh DefaultText LockIB Error", "error", 0);
return false;
}
for (size_t i = 0; i < reader.faceCount; i++)
{
if (reader.inversed)
{
pIndices[i * 3] = (DWORD)reader.vertexIndices[i * 3];
pIndices[i * 3 + 2] = (DWORD)reader.vertexIndices[i * 3 + 1];
pIndices[i * 3 + 1] = (DWORD)reader.vertexIndices[i * 3 + 2];
}
else
{
pIndices[i * 3] = (DWORD)reader.vertexIndices[i * 3];
pIndices[i * 3 + 1] = (DWORD)reader.vertexIndices[i * 3 + 1];
pIndices[i * 3 + 2] = (DWORD)reader.vertexIndices[i * 3 + 2];
}
}
if (!reader.normals)
CalcNormals(pVertices, pIndices);
m_pIB->Unlock();
m_pVB->Unlock();
ComputeBoundBox();
return true;
}
void CDefaultTextMesh::CalcNormals(VERTEX_MESH_DEFAULT* vertices, DWORD* indices)
{
for (size_t i = 0; i < m_indexCount / 3; i++)
{
Point3 norm = ((vertices[indices[i * 3]].pos - vertices[indices[i * 3 + 1]].pos)
% (vertices[indices[i * 3]].pos - vertices[indices[i * 3 + 2]].pos));
vertices[indices[i * 3]].norm += norm;
vertices[indices[i * 3 + 1]].norm += norm;
vertices[indices[i * 3 + 2]].norm += norm;
}
for (UINT i = 0; i < m_vertexCount; i++)
{
vertices[i].norm = vertices[i].norm.Normalize();
}
}
void CDefaultTextMesh::Render()
{
m_device->SetStreamSource(0, m_pVB, 0, sizeof(VERTEX_MESH_DEFAULT));
m_device->SetIndices(m_pIB);
m_device->SetFVF(VERTEX_MESH_DEFAULT::FVF);
m_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
0,
0,
(UINT) m_vertexCount,
0,
(UINT) m_indexCount / 3);
}