summaryrefslogtreecommitdiff
path: root/engine/r_decal.h
blob: 8883c3a96c6640c56ee230c8c0e22f7b80cf041f (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#ifndef R_DECAL_H
#define R_DECAL_H
#ifdef _WIN32
#pragma once
#endif

#include "decal_private.h"
#include "decal_clip.h"
#include "utllinkedlist.h"
#include "utlrbtree.h"

#include "tier0/memdbgon.h"

// Initialize and shutdown the decal stuff.
// R_DecalTerm unlinks all the active decals (which frees their counterparts in displacements).
void R_DecalInit();
void R_DecalTerm( worldbrushdata_t *pBrushData, bool term_permanent_decals );
void R_DecalTermAll();
float ComputeDecalLightmapOffset( SurfaceHandle_t surfID );
// get the max vertex/index to lock in a dynamic VB
void R_DecalsGetMaxMesh( IMatRenderContext *pRenderContext, int &nDecalSortMaxVerts, int &nDecalSortMaxIndices );

// --------------------------------------------------------------- //
// Decal functions used for displacements.
// --------------------------------------------------------------- //

// Figure out where the decal maps onto the surface.
void R_SetupDecalClip( 
	CDecalVert* &pOutVerts,
	decal_t *pDecal,
	Vector &vSurfNormal,
	IMaterial *pMaterial,
	Vector textureSpaceBasis[3],
	float decalWorldScale[2] );

//-----------------------------------------------------------------------------
// Gets the decal material and radius based on the decal index
//-----------------------------------------------------------------------------
void R_DecalGetMaterialAndSize( int decalIndex, IMaterial*& pDecalMaterial, float& w, float& h );

//=============================================================================
//
// Decal Sort Structures.
//
#define DECALSORT_RBTREE_SIZE	16

enum 
{
	PERMANENT_LIGHTMAP = 0,
	LIGHTMAP,
	NONLIGHTMAP,

	DECALSORT_TYPE_COUNT,
};

struct DecalSortVertexFormat_t
{
	VertexFormat_t		m_VertexFormat;
	int					m_iSortTree;			// Sort tree index.
};

struct DecalMaterialSortData_t
{
	IMaterial	*m_pMaterial;
	int			m_iLightmapPage; 
	int			m_iBucket;			// Index into the s_aDecalMaterialHead list.
};

struct DecalMaterialBucket_t
{
	int			m_iHead;
	int			m_nCheckCount;
};

inline bool DecalSortTreeSortLessFunc( const DecalMaterialSortData_t &decal1, const DecalMaterialSortData_t &decal2 )
{
	if ( ( decal1.m_iLightmapPage == -1 ) || ( decal2.m_iLightmapPage == -1 ) )
	{
		return ( ( int )decal1.m_pMaterial < ( int )decal2.m_pMaterial );
	}

	if ( ( int )decal1.m_pMaterial == ( int )decal2.m_pMaterial )
	{
		return ( decal1.m_iLightmapPage < decal2.m_iLightmapPage );
	}
	else
	{
		return ( ( int )decal1.m_pMaterial < ( int )decal2.m_pMaterial );
	}
}

struct DecalSortTrees_t
{
	CUtlRBTree<DecalMaterialSortData_t, int>	*m_pTrees[DECALSORT_TYPE_COUNT];
	CUtlVector<DecalMaterialBucket_t>			m_aDecalSortBuckets[MAX_MAT_SORT_GROUPS+1][DECALSORT_TYPE_COUNT];

	DecalSortTrees_t()
	{
		for ( int iSort = 0; iSort < DECALSORT_TYPE_COUNT; ++iSort )
		{
			m_pTrees[iSort] = new CUtlRBTree<DecalMaterialSortData_t, int>( DECALSORT_RBTREE_SIZE, DECALSORT_RBTREE_SIZE, DecalSortTreeSortLessFunc );
		}
	}

	~DecalSortTrees_t()
	{
		for ( int iSort = 0; iSort < DECALSORT_TYPE_COUNT; ++iSort )
		{
			if ( m_pTrees[iSort] )
			{
				m_pTrees[iSort]->RemoveAll();
				delete m_pTrees[iSort];
				m_pTrees[iSort] = NULL;
			}
		}

		for ( int iGroup = 0; iGroup < ( MAX_MAT_SORT_GROUPS + 1 ); ++iGroup )
		{
			for ( int iSort = 0; iSort < DECALSORT_TYPE_COUNT; ++iSort )
			{
				m_aDecalSortBuckets[iGroup][iSort].Purge();
			}
		}
	}
};

extern CUtlVector<DecalSortVertexFormat_t>	g_aDecalFormats;

// Surface decals.
extern CUtlFixedLinkedList<decal_t*>		g_aDecalSortPool;
extern CUtlVector<DecalSortTrees_t>			g_aDecalSortTrees;
extern int									g_nDecalSortCheckCount;
extern int									g_nBrushModelDecalSortCheckCount;

// Displacement decals.
extern CUtlFixedLinkedList<decal_t*>		g_aDispDecalSortPool;
extern CUtlVector<DecalSortTrees_t>			g_aDispDecalSortTrees;
extern int									g_nDispDecalSortCheckCount;

struct DecalBatchList_t
{
	IMaterial		*m_pMaterial;
	void			*m_pProxy;
	int				m_iLightmapPage;
	unsigned short	m_iStartIndex;
	unsigned short	m_nIndexCount;
};

struct DecalMeshList_t
{
	IMesh									*m_pMesh;
	CUtlVectorFixed<DecalBatchList_t, 128>	m_aBatches;
};

void DecalSurfacesInit( bool bBrushModel );
void DecalSurfaceAdd( SurfaceHandle_t surfID, int renderGroup );
void DecalSurfaceDraw( IMatRenderContext *pRenderContext, int renderGroup, float flFade = 1.0f );
void DrawDecalsOnSingleSurface( IMatRenderContext *pRenderContext, SurfaceHandle_t surfID );

void R_DecalReSortMaterials( void );
void R_DecalFlushDestroyList( void );

extern VMatrix g_BrushToWorldMatrix;
#include "tier0/memdbgoff.h"

#endif // R_DECAL_H