forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeshreader.h
268 lines (207 loc) · 6.92 KB
/
meshreader.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=====================================================================================//
#ifndef MESHREADER_H
#define MESHREADER_H
#ifdef _WIN32
#pragma once
#endif
//-----------------------------------------------------------------------------
// This is used to read vertex and index data out of already-created meshes.
// xbox uses this a lot so it doesn't have to store sysmem backups of the
// vertex data.
//-----------------------------------------------------------------------------
class CBaseMeshReader : protected MeshDesc_t
{
// Initialization.
public:
CBaseMeshReader();
~CBaseMeshReader();
// Use BeginRead/EndRead to initialize the mesh reader.
void BeginRead(
IMesh* pMesh,
int firstVertex = 0,
int numVertices = 0,
int firstIndex = 0,
int numIndices = 0 );
void EndRead();
// PC can use this if it stores its own copy of meshes around, in case
// locking static buffers is too costly.
void BeginRead_Direct( const MeshDesc_t &desc, int numVertices, int nIndices );
// Resets the mesh builder so it points to the start of everything again
void Reset();
protected:
IMesh *m_pMesh;
int m_MaxVertices;
int m_MaxIndices;
};
// A bunch of accessors for the data that CBaseMeshReader sets up.
class CMeshReader : public CBaseMeshReader
{
public:
// Access to vertex data.
public:
int NumIndices() const;
unsigned short Index( int index ) const;
const Vector& Position( int iVertex ) const;
unsigned int Color( int iVertex ) const;
const float *TexCoord( int iVertex, int stage ) const;
void TexCoord2f( int iVertex, int stage, float &s, float &t ) const;
const Vector2D& TexCoordVector2D( int iVertex, int stage ) const;
int NumBoneWeights() const;
float Wrinkle( int iVertex ) const;
const Vector &Normal( int iVertex ) const;
void Normal( int iVertex, Vector &vNormal ) const;
const Vector &TangentS( int iVertex ) const;
const Vector &TangentT( int iVertex ) const;
float BoneWeight( int iVertex ) const;
#ifdef NEW_SKINNING
float* BoneMatrix( int iVertex ) const;
#else
unsigned char* BoneMatrix( int iVertex ) const;
#endif
};
//-----------------------------------------------------------------------------
// CBaseMeshReader implementation.
//-----------------------------------------------------------------------------
inline CBaseMeshReader::CBaseMeshReader()
{
m_pMesh = NULL;
}
inline CBaseMeshReader::~CBaseMeshReader()
{
Assert( !m_pMesh );
}
inline void CBaseMeshReader::BeginRead(
IMesh* pMesh,
int firstVertex,
int numVertices,
int firstIndex,
int numIndices )
{
Assert( pMesh && (!m_pMesh) );
if ( numVertices < 0 )
{
numVertices = pMesh->VertexCount();
}
if ( numIndices < 0 )
{
numIndices = pMesh->IndexCount();
}
m_pMesh = pMesh;
m_MaxVertices = numVertices;
m_MaxIndices = numIndices;
// UNDONE: support reading from compressed VBs if needed
VertexCompressionType_t compressionType = CompressionType( pMesh->GetVertexFormat() );
Assert( compressionType == VERTEX_COMPRESSION_NONE );
if ( compressionType != VERTEX_COMPRESSION_NONE )
{
Warning( "Cannot use CBaseMeshReader with compressed vertices! Will get junk data or a crash.\n" );
}
// Locks mesh for modifying
pMesh->ModifyBeginEx( true, firstVertex, numVertices, firstIndex, numIndices, *this );
// Point to the start of the buffers..
Reset();
}
inline void CBaseMeshReader::EndRead()
{
Assert( m_pMesh );
m_pMesh->ModifyEnd( *this );
m_pMesh = NULL;
}
inline void CBaseMeshReader::BeginRead_Direct( const MeshDesc_t &desc, int nVertices, int nIndices )
{
MeshDesc_t *pThis = this;
*pThis = desc;
m_MaxVertices = nVertices;
m_MaxIndices = nIndices;
// UNDONE: support reading from compressed verts if necessary
Assert( desc.m_CompressionType == VERTEX_COMPRESSION_NONE );
if ( desc.m_CompressionType != VERTEX_COMPRESSION_NONE )
{
Warning( "Cannot use CBaseMeshReader with compressed vertices!\n" );
}
}
inline void CBaseMeshReader::Reset()
{
}
// -------------------------------------------------------------------------------------- //
// CMeshReader implementation.
// -------------------------------------------------------------------------------------- //
inline int CMeshReader::NumIndices() const
{
return m_MaxIndices;
}
inline unsigned short CMeshReader::Index( int index ) const
{
Assert( (index >= 0) && (index < m_MaxIndices) );
return m_pIndices[index * m_nIndexSize];
}
inline const Vector& CMeshReader::Position( int iVertex ) const
{
Assert( iVertex >= 0 && iVertex < m_MaxVertices );
return *(Vector*)((char*)m_pPosition + iVertex * m_VertexSize_Position);
}
inline unsigned int CMeshReader::Color( int iVertex ) const
{
Assert( iVertex >= 0 && iVertex < m_MaxVertices );
unsigned char *pColor = m_pColor + iVertex * m_VertexSize_Color;
return (pColor[0] << 16) | (pColor[1] << 8) | (pColor[2]) | (pColor[3] << 24);
}
inline const float *CMeshReader::TexCoord( int iVertex, int iStage ) const
{
Assert( iVertex >= 0 && iVertex < m_MaxVertices );
return (float*)( (char*)m_pTexCoord[iStage] + iVertex * m_VertexSize_TexCoord[iStage] );
}
inline void CMeshReader::TexCoord2f( int iVertex, int iStage, float &s, float &t ) const
{
Assert( iVertex >= 0 && iVertex < m_MaxVertices );
float *p = (float*)( (char*)m_pTexCoord[iStage] + iVertex * m_VertexSize_TexCoord[iStage] );
s = p[0];
t = p[1];
}
inline const Vector2D& CMeshReader::TexCoordVector2D( int iVertex, int iStage ) const
{
Assert( iVertex >= 0 && iVertex < m_MaxVertices );
Vector2D *p = (Vector2D*)( (char*)m_pTexCoord[iStage] + iVertex * m_VertexSize_TexCoord[iStage] );
return *p;
}
inline float CMeshReader::Wrinkle( int iVertex ) const
{
Assert( iVertex >= 0 && iVertex < m_MaxVertices );
return *(float*)( (char*)m_pWrinkle + iVertex * m_VertexSize_Wrinkle );
}
inline int CMeshReader::NumBoneWeights() const
{
return m_NumBoneWeights;
}
inline const Vector &CMeshReader::Normal( int iVertex ) const
{
Assert( iVertex >= 0 && iVertex < m_MaxVertices );
return *(const Vector *)(const float*)( (char*)m_pNormal + iVertex * m_VertexSize_Normal );
}
inline void CMeshReader::Normal( int iVertex, Vector &vNormal ) const
{
Assert( iVertex >= 0 && iVertex < m_MaxVertices );
const float *p = (const float*)( (char*)m_pNormal + iVertex * m_VertexSize_Normal );
vNormal.Init( p[0], p[1], p[2] );
}
inline const Vector &CMeshReader::TangentS( int iVertex ) const
{
Assert( iVertex >= 0 && iVertex < m_MaxVertices );
return *(const Vector*)( (char*)m_pTangentS + iVertex * m_VertexSize_TangentS );
}
inline const Vector &CMeshReader::TangentT( int iVertex ) const
{
Assert( iVertex >= 0 && iVertex < m_MaxVertices );
return *(const Vector*)( (char*)m_pTangentT + iVertex * m_VertexSize_TangentT );
}
inline float CMeshReader::BoneWeight( int iVertex ) const
{
Assert( iVertex >= 0 && iVertex < m_MaxVertices );
float *p = (float*)( (char*)m_pBoneWeight + iVertex * m_VertexSize_BoneWeight );
return *p;
}
#endif // MESHREADER_H