summaryrefslogtreecommitdiff
path: root/hammer/subdiv.h
blob: 8ef1503e1de91433282386ef032dd172b8efec26 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#ifndef SUBDIV_H
#define SUBDIV_H
#pragma once

class CMapDisp;
class CSubdivEdge;
class CSubdivQuad;

//=============================================================================
//
// Class Subdivision Point
//
class CSubdivPoint
{
public:

	enum { POINT_ORDINARY = 0,
		   POINT_CORNER = 1,
		   POINT_CREASE = 2 };
	
	enum { NUM_SUBDIV_EDGES = 8 };

	Vector		m_Point;
	Vector		m_Normal;
	Vector		m_NewPoint;
	Vector		m_NewNormal;
	int			m_Type;
	int			m_Valence;
	CSubdivEdge	*m_pEdges[NUM_SUBDIV_EDGES];

	void Clear( void );
	void Copy( const CSubdivPoint *pFrom );

	void CalcNewVertexPoint( void );
	void CalcNewVertexNormal( void );

	friend bool CompareSubdivPoints( const CSubdivPoint *pPoint1, const CSubdivPoint *pPoint2, float tolerance );
	friend bool CompareSubdivPointToPoint( const CSubdivPoint *pSubdivPoint, const Vector& point, float tolerance );
};


//=============================================================================
//
// Class Subdivision Edge
//
class CSubdivEdge
{
public:

	short		m_ndxPoint[2];
	CSubdivQuad	*m_pQuads[2];
	short		m_ndxQuadEdge[2];
	float		m_Sharpness;
	Vector		m_NewEdgePoint;
	Vector		m_NewEdgeNormal;
	bool		m_Active;

	void Clear( void );
	void Copy( const CSubdivEdge *pFrom );

	void CalcNewEdgePoint( void );
	void CalcNewEdgeNormal( void );

	friend bool CompareSubdivEdges( const CSubdivEdge *pEdge1, const CSubdivEdge *pEdge2 );
};


//=============================================================================
//
// Class Subdivision Quad
//
class CSubdivQuad
{
public:

	short	m_ndxQuad[4];		// quad indices -- see CSubdivManager
	short	m_ndxVert[4];		// vert indices -- see CSubdivManager
	short	m_ndxEdge[4];		// edge indices -- see CSubdivManager
	Vector	m_Centroid;			// center of quad
	Vector  m_Normal;			// quad normal

	void GetCentroid( Vector& centroid );
	void CalcCentroid( void );

	void GetNormal( Vector& normal );
	void CalcNormal( void );
};


//=============================================================================
//
// Class Subdivision Mesh
//
class CSubdivMesh
{
public:

	//=========================================================================
	//
	// Creation/Destruction
	//
	CSubdivMesh();
	~CSubdivMesh();

	//=========================================================================
	//
	// 
	//
	inline void Clear( void );
	void DoSubdivide( void );

	//=========================================================================
	//
	// 
	//
	inline int GetPointCount( void );
	int AddPoint( const Vector& point, const Vector& normal );
	void RemovePoint( Vector& point );
	inline void GetPoint( int index, Vector& point );
	inline void GetNormal( int index, Vector& normal );

	inline int GetEdgeCount( void );
	int AddEdge( CSubdivEdge *edge );
	void RemoveEdge( CSubdivEdge *edge );
	inline void GetEdge( int index, CSubdivEdge *edge );

private:

//	enum { MAX_SUBDIV_POINTS = 32000 };
//	enum { MAX_TREES = 64 };

	int				m_PointCount;
	int				m_MaxPointCount;
	CSubdivPoint	*m_pPoints;
//	CSubdivPoint	m_Points[MAX_SUBDIV_POINTS];		// mesh list of subdivision verts

	int				m_EdgeCount;
	int				m_MaxEdgeCount;
	CSubdivEdge		*m_pEdges;
//	CSubdivEdge	    m_Edges[MAX_SUBDIV_POINTS];			// mesh list of subdivision edges

	int				m_TreeCount;
	int				m_MaxTreeCount;
	CSubdivQuad		**m_ppTrees;
//	CSubdivQuad		*m_pTrees[MAX_TREES];

	void CatmullClarkSubdivide( void );
	int AddTree( CSubdivQuad *pTree );
	int GetStartIndexFromLevel( int levelIndex );
	int GetEndIndexFromLevel( int levelIndex );
	void AddQuadToMesh( CSubdivQuad *pQuad );

	inline void ClearEdges( void );

	void CreateChildQuads( CSubdivQuad *pRoot, int quadIndex );
	void SetEdgeData( CSubdivQuad *pRoot, int index, int parentIndex, int subdivIndex );
	void CreateChildQuad1( CSubdivQuad *pRoot, int index, int parentIndex );
	void CreateChildQuad2( CSubdivQuad *pRoot, int index, int parentIndex );
	void CreateChildQuad3( CSubdivQuad *pRoot, int index, int parentIndex );
	void CreateChildQuad4( CSubdivQuad *pRoot, int index, int parentIndex );

	bool PreSubdivide( void );
	void Subdivide( void );
	void PostSubdivide( void );

	bool AllocCache( int dispCount );
	void FreeCache( void );
};


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline void CSubdivMesh::Clear( void )
{
	m_PointCount = 0;
	m_EdgeCount = 0;
	m_TreeCount = 0;
	
	m_MaxPointCount = 0;
	m_MaxEdgeCount = 0;
	m_MaxTreeCount = 0;

	m_pPoints = NULL;
	m_pEdges = NULL;
	m_ppTrees = NULL;
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline int CSubdivMesh::GetPointCount( void )
{
	return m_PointCount;
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline void CSubdivMesh::GetPoint( int index, Vector& point )
{
	assert( index >= 0 );
	assert( index < m_PointCount );

	point = m_pPoints[index].m_Point;
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline void CSubdivMesh::GetNormal( int index, Vector& normal )
{
	assert( index >= 0 );
	assert( index < m_PointCount );

	normal = m_pPoints[index].m_Normal;
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline int CSubdivMesh::GetEdgeCount( void )
{
	return m_EdgeCount;
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline void CSubdivMesh::GetEdge( int index, CSubdivEdge *edge )
{
	assert( index >= 0 );
	assert( index < m_EdgeCount );

	edge->Copy( &m_pEdges[index] );
}


#endif // SUBDIV_H