summaryrefslogtreecommitdiff
path: root/hammer/mapface.h
blob: df1f8c446b4fd2af2722d8a82fee9610fd106b3e (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#ifndef MAPFACE_H
#define MAPFACE_H

#ifdef _WIN32
#pragma once
#endif


#pragma warning(push, 1)
#pragma warning(disable:4701 4702 4530)
#include <fstream>
#pragma warning(pop)
#include "hammer_mathlib.h"
#include "MapAtom.h"
#include "DispManager.h"
#include "mathlib/Vector4d.h"
#include "utlvector.h"
#include "Color.h"
#include "smoothinggroupmgr.h"
#include "detailobjects.h"

class CCheckFaceInfo;
class IEditorTexture;
class CRender;
class CRender3D;
class CChunkFile;
class CSaveInfo;
class IMaterial;
class CMapWorld;
struct MapFaceRender_t;
class CMeshBuilder;
class IMesh;

struct LoadFace_t;

enum EditorRenderMode_t;
enum ChunkFileResult_t;

#define DEFAULT_TEXTURE_SCALE			0.25
#define DEFAULT_LIGHTMAP_SCALE			16

#define SMOOTHING_GROUP_MAX_COUNT		32
#define SMOOTHING_GROUP_DEFAULT			0

//
// Flags for CMapFace::CopyFrom.
//
#define COPY_FACE_PLANE			0x00000001			// Copies only the face's plane. Used for carving.
#define COPY_FACE_POINTS		0x00000002			// Copies the face's points and plane.


//
// Used for storing the extrema of a face. Each element of the Extents_t array
// contains a point that represents a local extreme along a particular dimension.
//
enum
{
	EXTENTS_XMIN = 0,
	EXTENTS_XMAX,
	EXTENTS_YMIN,
	EXTENTS_YMAX,
	EXTENTS_ZMIN,
	EXTENTS_ZMAX,
	NUM_EXTENTS_DIMS,
};

typedef Vector Extents_t[NUM_EXTENTS_DIMS];


struct PLANE
{
	Vector		normal;
	float		dist;
	Vector		planepts[3];
};


typedef struct
{
	int		numpoints;
	Vector	*p;			// variable sized
} winding_t;


enum FaceOrientation_t
{
	FACE_ORIENTATION_FLOOR = 0,
	FACE_ORIENTATION_CEILING,
	FACE_ORIENTATION_NORTH_WALL,
	FACE_ORIENTATION_SOUTH_WALL,
	FACE_ORIENTATION_EAST_WALL,
	FACE_ORIENTATION_WEST_WALL,
	FACE_ORIENTATION_INVALID
};


//
// Both an enumeration and bitflags. Used as bitflags when querying a face for its texture
// alignment because it could be world aligned and face aligned at the same time.
//
enum TextureAlignment_t
{
	TEXTURE_ALIGN_NONE	= 0x0000,
	TEXTURE_ALIGN_WORLD = 0x0001,
	TEXTURE_ALIGN_FACE	= 0x0002,
	TEXTURE_ALIGN_QUAKE = 0x0004
};


enum TextureJustification_t
{
	TEXTURE_JUSTIFY_NONE = 0,
	TEXTURE_JUSTIFY_TOP,
	TEXTURE_JUSTIFY_BOTTOM,
	TEXTURE_JUSTIFY_LEFT,
	TEXTURE_JUSTIFY_CENTER,
	TEXTURE_JUSTIFY_RIGHT,
	TEXTURE_JUSTIFY_FIT,
	TEXTURE_JUSTIFY_MAX
};


#define INIT_TEXTURE_FORCE			0x0001
#define INIT_TEXTURE_AXES			0x0002
#define INIT_TEXTURE_ROTATION		0x0004
#define INIT_TEXTURE_SHIFT			0x0008
#define INIT_TEXTURE_SCALE			0x0010
#define INIT_TEXTURE_ALL			(INIT_TEXTURE_AXES | INIT_TEXTURE_ROTATION | INIT_TEXTURE_SHIFT | INIT_TEXTURE_SCALE)


//
// Flags for CreateFace.
//
#define CREATE_FACE_PRESERVE_PLANE	0x0001		// Hack to prevent plane from being recalculated while building a solid from its planes.
#define CREATE_FACE_CLIPPING		0x0002

//
// Serialized data structure. Do not modify!
//
struct TEXTURE_21
{
	char	texture[MAX_PATH];
	float	rotate;
	float	shift[2];
	float	scale[2];
	BYTE	smooth;
	BYTE	material;
	DWORD	q2surface;
	DWORD	q2contents;
	DWORD	q2value;
};


//
// Post 2.1 explicit texture U/V axes were added.
//
// Serialized data structure. Do not modify!
//
struct TEXTURE_33
{
	char texture[MAX_PATH];
	float UAxis[4];				// Must remain float[4] for RMF serialization.
	float VAxis[4];				// Must remain float[4] for RMF serialization.
	float rotate;
	float scale[2];
	BYTE smooth;
	BYTE material;
	DWORD q2surface;
	DWORD q2contents;
	int nLightmapScale;
};


struct TEXTURE
{
	char texture[MAX_PATH];
	Vector4D UAxis;
	Vector4D VAxis;
	float rotate;
	float scale[2];
	BYTE smooth;
	BYTE material;
	DWORD q2surface;
	DWORD q2contents;
	int nLightmapScale;

	TEXTURE& operator=( TEXTURE const& src )
	{
		// necessary since operator= is private for UAxis
		memcpy( this, &src, sizeof(TEXTURE) );
		return *this;
	}
};


#define FACE_FLAGS_NOSHADOW 1
#define FACE_FLAGS_NODRAW_IN_LPREVIEW 2



class CMapFace : public CMapAtom
{
public:

	CMapFace(void);
	~CMapFace(void);

	// If bRescaleTextureCoordinates is true, then it will rescale and reoffset the texture coordinates
	// so that the texture is in the same apparent spot as the old texture (if they are different sizes).
	void SetTexture(const char *pszNewTex, bool bRescaleTextureCoordinates = false);
	void SetTexture(IEditorTexture *pTexture, bool bRescaleTextureCoordinates = false);
	void GetTextureName(char *pszName) const;

	inline IEditorTexture *GetTexture(void) const;
		
	// Renders opaque faces
	static void			AddFaceToQueue( CMapFace* pMapFace, IEditorTexture* pTexture, EditorRenderMode_t renderMode, bool selected, SelectionState_t faceSelectionState );
	static void			PushFaceQueue( void );
	static void			PopFaceQueue( void );
	static void			RenderOpaqueFaces( CRender3D* pRender );

	//
	// Serialization.
	//
	ChunkFileResult_t LoadVMF(CChunkFile *pFile);
	ChunkFileResult_t SaveVMF(CChunkFile *pFile, CSaveInfo *pSaveInfo);
	int SerializeRMF(std::fstream&, BOOL);
	int SerializeMAP(std::fstream&, BOOL);

	BOOL CheckFace(CCheckFaceInfo* = NULL);
	BOOL Fix(void);

	float GetNormalDistance(Vector& fPoint);

	inline int GetPointCount(void);
	inline void GetPoint(Vector& Point, int nPoint);

	inline void GetLightmapCoord( Vector2D & LightmapCoord, int nIndex );
	inline void SetLightmapCoord( const Vector2D &LightmapCoord, int nIndex );
	inline void GetTexCoord( Vector2D & TexCoord, int nTexCoord );

	// Texture alignment.
	void GetCenter(Vector& Center);
	FaceOrientation_t GetOrientation(void) const;
	void RotateTextureAxes(float fDegrees);
	void InitializeTextureAxes(TextureAlignment_t eAlignment, DWORD dwFlags);
	void JustifyTexture(TextureJustification_t eJustification);
	void JustifyTextureUsingExtents(TextureJustification_t eJustification, Extents_t Extents);
	void GetFaceBounds(Vector& pfMins, Vector& pfMaxs) const;
	void GetFaceExtents(Extents_t Extents) const;
	void GetTextureExtents(Extents_t Extents, Vector2D & TopLeft, Vector2D & BottomRight) const;
	int GetTextureAlignment(void) const;
	void GetFaceTextureExtents(Vector2D & TopLeft, Vector2D & BottomRight) const;

	void CalcTextureCoordAtPoint( const Vector& pt, Vector2D & texCoord );
	void CalcLightmapCoordAtPoint( const Vector& pt, Vector2D & lightCoord );

	// Returns the max lightmap size for this face
	int MaxLightmapSize() const;

	void NormalizeTextureShifts(void);
	BOOL IsTextureAxisValid(void) const;

	inline void SetCordonFace( bool bCordonFace );
	inline bool IsCordonFace() const;

	// Old code for setting up texture axes. Needed for backwards compatibility.
	void InitializeQuakeStyleTextureAxes(Vector4D& UAxis, Vector4D& VAxis);

	void CreateFace(Vector *pPoints, int nPoints, bool bIsCordonFace = false);
	void CreateFace(winding_t *w, int nFlags = 0);
	CMapFace *CopyFrom(const CMapFace *pFrom, DWORD dwFlags = COPY_FACE_POINTS, bool bUpdateDependencies = true );
	size_t AllocatePoints(int nPoints);

	void OnUndoRedo();

	void CalcPlane(void);
	void CalcPlaneFromFacePoints(void);

	void CalcTextureCoords();
	void OffsetTexture(const Vector &Delta);
	void SetTextureCoords(int nPoint, float u, float v);

	struct TangentSpaceAxes_t
	{
		Vector	tangent;
		Vector	binormal;
	};
		
	void CalcTangentSpaceAxes( void );
	bool AllocTangentSpaceAxes( int count );
	void FreeTangentSpaceAxes( void );

	void Render2D(CRender2D *pRender);
	void Render3D(CRender3D *pRender);
	void Render3DGrid(CRender3D *pRender);
	void RenderVertices(CRender *pRender);

	void OnAddToWorld(CMapWorld *pWorld);
	void OnRemoveFromWorld(void);

	static void SetShowSelection(bool bShowSelection);

	inline void SetRenderAlpha(unsigned char uchAlpha) { m_uchAlpha = uchAlpha; } // HACK: should be in CMapAtom

	inline void GetFaceNormal( Vector& normal );
	bool TraceLine(Vector &HitPos, Vector &HitNormal, Vector const &Start, Vector const &End);
	bool TraceLineInside( Vector &HitPos, Vector &HitNormal, Vector const &Start, Vector const &End, bool bNoDisp = false );

	inline void SetDisp( EditDispHandle_t handle, bool bDestroyPrevious = true );
	inline EditDispHandle_t GetDisp( void );
	inline bool HasDisp( void ) const;

	bool ShouldRenderLast();
	void GetDownVector( int index, Vector& downVect );

	bool GetRender2DBox( Vector& boundMin, Vector& boundMax );
	bool GetCullBox( Vector& boundMin, Vector& boundMax );
	size_t GetDataSize( void );

	inline int GetFaceID(void);
	inline void SetFaceID(int nFaceID);

	// Smoothing group.
	int SmoothingGroupCount( void );
	void AddSmoothingGroup( int iGroup );
	void RemoveSmoothingGroup( int iGroup );
	bool InSmoothingGroup( int iGroup );

	// Indicates this guy should be unlit
	void RenderUnlit( bool enable );

	// (begin serialized information
	TEXTURE texture;					// Texture info.
	Vector *Points;						// Array of face points, dynamically allocated.
	int	nPoints;						// The number of points in the array.
	// end serialized information)

	PLANE plane;

	int m_nFaceFlags;										// FACE_FLAGS_xx

	void DoTransform(const VMatrix &matrix);

	virtual void AddShadowingTriangles( CUtlVector<Vector> &tri_list );

	DetailObjects		*m_pDetailObjects;
	
protected:

	void ComputeColor( CRender3D* pRender, bool bRenderAsSelected, SelectionState_t faceSelectionState,
					   bool ignoreLighting, Color &pColor );

	void DrawFace( Color &pColor, EditorRenderMode_t mode );
	void RenderGridIfCloseEnough( CRender3D* pRender );
	void RenderTextureAxes( CRender3D* pRender );

	// Adds a face's vertices to the meshbuilder
	void AddFaceVertices( CMeshBuilder &builder, CRender3D* pRender, bool bRenderSelected, SelectionState_t faceSelectionState );

	// render texture axes
	static void RenderTextureAxes( CRender3D* pRender, int nCount, CMapFace **ppFaces );
	static void RenderGridsIfCloseEnough( CRender3D* pRender, int nCount, CMapFace **ppFaces );
	static void Render3DGrids( CRender3D* pRender, int nCount, CMapFace **ppFaces );
	static void RenderWireframeFaces( CRender3D* pRender, int nCount, MapFaceRender_t **ppFaces );
	static void RenderFacesBatch( CMeshBuilder &MeshBuilder, IMesh* pMesh, CRender3D* pRender, MapFaceRender_t **ppFaces, int nFaceCount, int nVertexCount, int nIndexCount, bool bWireframe );
	static void RenderFaces( CRender3D* pRender, int nCount, MapFaceRender_t **ppFaces );

	void RenderFace3D( CRender3D* pRender, EditorRenderMode_t renderMode, bool renderSelected, SelectionState_t faceSelectionState );

	//
	// Serialization (chunk handlers).
	//
	static ChunkFileResult_t LoadDispInfoCallback(CChunkFile *pFile, CMapFace *pFace);
	static ChunkFileResult_t LoadKeyCallback(const char *szKey, const char *szValue, LoadFace_t *pLoadFace);

	unsigned char m_uchAlpha;			// HACK: should be in CMapAtom

	int m_nFaceID;						// The unique ID of this face in the world.

	IEditorTexture *m_pTexture;				// Texture that is applied to this face.
	static IEditorTexture *m_pLightmapGrid;	// Lightmap grid texture for use in viewing lightmap scales.
	EditDispHandle_t	m_DispHandle;			// Displacement map applied to this face, NULL if none.

	static bool m_bShowFaceSelection;	// Whether to render faces with a special color when they are selected.

	Vector2D *m_pTextureCoords;			// An array of texture coordinates, one per face point.
	Vector2D *m_pLightmapCoords;			// An array of lightmap coordinates, one per face point.

	bool m_bIsCordonFace : 1;

	// should this be affected by lighting?
	bool m_bIgnoreLighting : 1;

	TangentSpaceAxes_t	*m_pTangentAxes;

	unsigned int		m_fSmoothingGroups;		// 32-bits representing 32 smoothing groups

	void UpdateFaceFlags( void );							// sniff face flags from texture
};


//-----------------------------------------------------------------------------
// Purpose: Returns the unique ID of this face.
//-----------------------------------------------------------------------------
inline int CMapFace::GetFaceID(void)
{
	return(m_nFaceID);
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : TexCoord - 
//			nTexCoord - 
//-----------------------------------------------------------------------------
inline void CMapFace::GetLightmapCoord( Vector2D& LightmapCoord, int nIndex )
{
    Assert( nIndex < nPoints );
    LightmapCoord[0] = m_pLightmapCoords[nIndex][0];
    LightmapCoord[1] = m_pLightmapCoords[nIndex][1];
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : &LightmapCoord - 
//			nIndex - 
// Output : inline void
//-----------------------------------------------------------------------------
inline void CMapFace::SetLightmapCoord( const Vector2D &LightmapCoord, int nIndex )
{
    Assert( nIndex < nPoints );
	m_pLightmapCoords[nIndex][0] = LightmapCoord[0];
	m_pLightmapCoords[nIndex][1] = LightmapCoord[1];
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : TexCoord - 
//			nTexCoord - 
//-----------------------------------------------------------------------------
inline void CMapFace::GetTexCoord( Vector2D& TexCoord, int nTexCoord )
{
    Assert( nTexCoord < nPoints );
    TexCoord[0] = m_pTextureCoords[nTexCoord][0];
    TexCoord[1] = m_pTextureCoords[nTexCoord][1];
}


//-----------------------------------------------------------------------------
// Purpose: Returns a pointer to the texture that is applied to this face, NULL if none.
//-----------------------------------------------------------------------------
inline IEditorTexture *CMapFace::GetTexture(void) const
{
	return(m_pTexture);
}


//-----------------------------------------------------------------------------
// Purpose: Returns the number of vertices that define this face.
//-----------------------------------------------------------------------------
inline int CMapFace::GetPointCount(void)
{
	return(nPoints);
}


//-----------------------------------------------------------------------------
// Purpose: Retrieves a point on this face by its index.
// Input  : Point - Receives point coordinates.
//			nPoint - Index of point to retrieve.
//-----------------------------------------------------------------------------
inline void CMapFace::GetPoint(Vector& Point, int nPoint)
{
	Assert(nPoint < nPoints);
	Point = Points[nPoint];
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline void CMapFace::GetFaceNormal( Vector& normal )
{
	normal = plane.normal;
}


//-----------------------------------------------------------------------------
// Purpose: Sets the unique ID of this face.
//-----------------------------------------------------------------------------
inline void CMapFace::SetFaceID(int nID)
{
	m_nFaceID = nID;
}


//-----------------------------------------------------------------------------
// Purpose: Attaches a displacement surface to this face.
// Input  : handle - Displacement surface handle of surface attached to this face
//-----------------------------------------------------------------------------
inline void CMapFace::SetDisp( EditDispHandle_t handle, bool bDestroyPrevious )
{
	if ( ( m_DispHandle != EDITDISPHANDLE_INVALID ) && bDestroyPrevious )
	{
		// destroy old handle
		EditDispMgr()->Destroy( m_DispHandle );
	}
		
	Assert( ( handle == EDITDISPHANDLE_INVALID ) || ( EditDispMgr()->GetDisp( handle ) != NULL ) );

	m_DispHandle = handle;
}


//-----------------------------------------------------------------------------
// Purpose: Returns the displacement surface applied to this face, 
//          DISPHANDLE_INVALID if none.
//-----------------------------------------------------------------------------
inline EditDispHandle_t CMapFace::GetDisp( void )
{
    return m_DispHandle;
}


//-----------------------------------------------------------------------------
// Purpose: Returns true if this face has a displacement surface, false if not.
//-----------------------------------------------------------------------------
inline bool CMapFace::HasDisp( void ) const
{
	return ( m_DispHandle != EDITDISPHANDLE_INVALID );
}


//-----------------------------------------------------------------------------
// Whether this face belongs to a cordon brush.
//-----------------------------------------------------------------------------
inline void CMapFace::SetCordonFace( bool bCordonFace )
{
	m_bIsCordonFace = bCordonFace;
}


inline bool CMapFace::IsCordonFace() const
{
	return m_bIsCordonFace;
}


//-----------------------------------------------------------------------------
// Defines a container class for a list of face pointers.
//-----------------------------------------------------------------------------
class CMapFaceList : public CUtlVector<CMapFace *>
{
public:

	inline CMapFaceList(void) {}
	inline CMapFaceList(CMapFaceList const &other);
	inline CMapFaceList &CMapFaceList::operator =(CMapFaceList const &other);

	inline int FindFaceID(int nFaceID);
	void Intersect(CMapFaceList &IntersectWith, CMapFaceList &In, CMapFaceList &Out);
};


//-----------------------------------------------------------------------------
// Purpose: Copy constructor.
//-----------------------------------------------------------------------------
CMapFaceList::CMapFaceList(CMapFaceList const &other)
{
	*this = other;
}


//-----------------------------------------------------------------------------
// Purpose: Assignment operator for copying face lists.
// Input  : other - 
//-----------------------------------------------------------------------------
CMapFaceList &CMapFaceList::operator =(CMapFaceList const &other)
{
	AddVectorToTail(other);
	return *this;
}


//-----------------------------------------------------------------------------
// Purpose: Searches the list for a face with the given ID.
// Input  : nFaceID - Numeric face ID to search for.
// Output : Index of found element, -1 if none.
//-----------------------------------------------------------------------------
int CMapFaceList::FindFaceID(int nFaceID)
{
	for (int i = 0; i < Count(); i++)
	{
		if ((Element(i) != NULL) && (Element(i)->GetFaceID() == nFaceID))
		{
			return(i);
		}
	}

	return(-1);
}


//-----------------------------------------------------------------------------
// Defines a container class for a list of face IDs.
//-----------------------------------------------------------------------------
class CMapFaceIDList : public CUtlVector<int>
{
public:

	inline CMapFaceIDList(void) {}
	inline CMapFaceIDList(CMapFaceIDList const &other);
	inline CMapFaceIDList &CMapFaceIDList::operator =(CMapFaceIDList const &other);

	void Intersect(CMapFaceIDList &IntersectWith, CMapFaceIDList &In, CMapFaceIDList &Out);
};


//-----------------------------------------------------------------------------
// Purpose: Copy constructor.
//-----------------------------------------------------------------------------
CMapFaceIDList::CMapFaceIDList(CMapFaceIDList const &other)
{
	*this = other;
}


//-----------------------------------------------------------------------------
// Purpose: Assignment operator for copying face ID lists.
// Input  : other - 
//-----------------------------------------------------------------------------
CMapFaceIDList &CMapFaceIDList::operator =(CMapFaceIDList const &other)
{
	AddVectorToTail(other);
	return *this;
}


#endif // MAPFACE_H