aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/client/particlesphererenderer.h
blob: ac0f96b4d0e10938537430909030ee300d53a5d7 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

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


#include "particlemgr.h"
#include "particle_util.h"


class CParticleSphereRenderer
{
public:

				CParticleSphereRenderer();

	// Initialize and tell it the material you'll be using.
	void		Init( CParticleMgr *pParticleMgr, IMaterial *pMaterial );
	
	// Pass this call through from your particle system too.
	void		StartRender( VMatrix &effectMatrix );

	// Call this to	render a spherical particle.
	void		RenderParticle( 
		ParticleDraw *pDraw, 
		const Vector &vOriginalPos,
		const Vector &vTransformedPos,
		float flAlpha,			// value 0 - 255.4
		float flParticleSize,
		float flAngle = 0.0f ); 

	void		RenderParticle_AddColor( 
		ParticleDraw *pDraw, 
		const Vector &vOriginalPos,
		const Vector &vTransformedPos,
		float flAlpha,				// value 0 - 255.4
		float flParticleSize,
		const Vector &vToAdd0to1	// Add this to the color (value 0-1)
		); 

	
	// Lighting is (base color) + (ambient / dist^2) + bump(directional / dist^2)
	const Vector& GetBaseColor() const;			// Specified as 0-1
	void SetBaseColor( const Vector &vColor );
	
	const CParticleLightInfo& GetAmbientLight() const;
	void SetAmbientLight( const CParticleLightInfo &info );
	
	const CParticleLightInfo& GetDirectionalLight() const;
	void SetDirectionalLight( const CParticleLightInfo &info );


private:

	void		AddLightColor( 
		Vector const *pPos,
		Vector const *pLightPos, 
		Vector const *pLightColor, 
		float flLightIntensity,
		Vector *pOutColor );

	inline void	ClampColor( Vector &vColor );


private:

	int m_iLastTickStartRenderCalled;	// Used for debugging.
	
	CParticleMgr *m_pParticleMgr;
	
	Vector			m_vBaseColor;
	CParticleLightInfo m_AmbientLight;
	CParticleLightInfo m_DirectionalLight;
	bool			m_bUsingPixelShaders;
};


// ------------------------------------------------------------------------ //
// Inlines.
// ------------------------------------------------------------------------ //

inline void CParticleSphereRenderer::AddLightColor( 
	Vector const *pPos,
	Vector const *pLightPos, 
	Vector const *pLightColor, 
	float flLightIntensity,
	Vector *pOutColor )
{
	if( flLightIntensity )
	{
		float fDist = pPos->DistToSqr( *pLightPos );
		float fAmt;
		if( fDist > 0.0001f )
			fAmt = flLightIntensity / fDist;
		else
			fAmt = 1000.f;

		*pOutColor += *pLightColor * fAmt;
	}
}


inline void CParticleSphereRenderer::ClampColor( Vector &vColor )
{
	float flMax = MAX( vColor.x, MAX( vColor.y, vColor.z ) );
	if( flMax > 1 )
	{
		vColor *= 255.0f / flMax;
	}
	else
	{
		vColor *= 255.0;
	}
}


inline const Vector& CParticleSphereRenderer::GetBaseColor() const
{
	return m_vBaseColor;
}

inline void CParticleSphereRenderer::SetBaseColor( const Vector &vColor )
{
	m_vBaseColor = vColor;
}

inline const CParticleLightInfo& CParticleSphereRenderer::GetAmbientLight() const
{
	return m_AmbientLight;
}

inline void CParticleSphereRenderer::SetAmbientLight( const CParticleLightInfo &info )
{
	m_AmbientLight = info;
}

inline const CParticleLightInfo& CParticleSphereRenderer::GetDirectionalLight() const
{
	return m_DirectionalLight;
}

inline void CParticleSphereRenderer::SetDirectionalLight( const CParticleLightInfo &info )
{
	m_DirectionalLight = info;
}

inline void CParticleSphereRenderer::RenderParticle( 
	ParticleDraw *pDraw,
	const Vector &vOriginalPos,
	const Vector &vTransformedPos,
	float flAlpha,
	float flParticleSize,
	float flAngle )
{
	// Make sure they called StartRender on us so we were able to set the directional light parameters.
#ifdef _DEBUG
	if ( pDraw->GetMeshBuilder() )
	{
		Assert( m_iLastTickStartRenderCalled == gpGlobals->tickcount );
	}
#endif

	Vector vColor = m_vBaseColor;
	AddLightColor( &vOriginalPos, &m_AmbientLight.m_vPos, &m_AmbientLight.m_vColor, m_AmbientLight.m_flIntensity, &vColor );
	
	// If the DX8 shader isn't going to handle the directional light color, then add its contribution here.
	if( !m_bUsingPixelShaders )
	{
		AddLightColor( &vOriginalPos, &m_DirectionalLight.m_vPos, &m_DirectionalLight.m_vColor, m_DirectionalLight.m_flIntensity, &vColor );
	}
	
	ClampColor( vColor );

	RenderParticle_Color255SizeNormalAngle(
		pDraw,
		vTransformedPos,
		vColor,			// ambient color
		flAlpha,		// alpha
		flParticleSize,
		vec3_origin,
		flAngle );
}

inline void CParticleSphereRenderer::RenderParticle_AddColor( 
	ParticleDraw *pDraw,
	const Vector &vOriginalPos,
	const Vector &vTransformedPos,
	float flAlpha,
	float flParticleSize,
	const Vector &vToAdd0to1
	)
{
	// Make sure they called StartRender on us so we were able to set the directional light parameters.
#ifdef _DEBUG
	if ( pDraw->GetMeshBuilder() )
	{
		Assert( m_iLastTickStartRenderCalled == gpGlobals->tickcount );
	}
#endif

	Vector vColor = m_vBaseColor + vToAdd0to1;
	AddLightColor( &vOriginalPos, &m_AmbientLight.m_vPos, &m_AmbientLight.m_vColor, m_AmbientLight.m_flIntensity, &vColor );
	
	// If the DX8 shader isn't going to handle the directional light color, then add its contribution here.
	if( !m_bUsingPixelShaders )
	{
		AddLightColor( &vOriginalPos, &m_DirectionalLight.m_vPos, &m_DirectionalLight.m_vColor, m_DirectionalLight.m_flIntensity, &vColor );
	}
	
	ClampColor( vColor );

	RenderParticle_Color255Size(
		pDraw,
		vTransformedPos,
		vColor,			// ambient color
		flAlpha,		// alpha
		flParticleSize );
}


#endif // PARTICLESPHERERENDERER_H