summaryrefslogtreecommitdiff
path: root/engine/shadowmgr.h
blob: ef17e8282c7f94eb161bc1f51dee86f8aa27555f (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//===========================================================================//

#ifndef SHADOWMGR_H
#define SHADOWMGR_H

#ifdef _WIN32
#pragma once
#endif

#include "engine/ishadowmgr.h"
#include "mathlib/vmatrix.h"
#include "surfacehandle.h"
#include "mathlib/ssemath.h"

//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class CDispInfo;


//-----------------------------------------------------------------------------
// Shadow decals are applied to a single surface
//-----------------------------------------------------------------------------
typedef unsigned short ShadowDecalHandle_t;

enum
{
	SHADOW_DECAL_HANDLE_INVALID = (ShadowDecalHandle_t)~0
};


//-----------------------------------------------------------------------------
// This structure contains the vertex information for shadows
//-----------------------------------------------------------------------------
struct ShadowVertex_t
{
	Vector	m_Position;
	Vector	m_ShadowSpaceTexCoord;
};


//-----------------------------------------------------------------------------
// This structure contains info to accelerate shadow darkness computation
//-----------------------------------------------------------------------------
struct ShadowDecalRenderInfo_t
{
	Vector2D m_vTexOrigin;
	Vector2D m_vTexSize;
	float m_flFalloffOffset;
	float m_flOOZFalloffDist;
	float m_flFalloffAmount;
	float m_flFalloffBias;
};


//-----------------------------------------------------------------------------
// Shadow-related functionality internal to the engine
//-----------------------------------------------------------------------------
abstract_class IShadowMgrInternal : public IShadowMgr
{
public:
	virtual void LevelInit( int nSurfCount ) = 0;
	virtual void LevelShutdown() = 0;

	// This surface has been rendered; and we'll want to render the shadows
	// on this surface
	virtual void AddShadowsOnSurfaceToRenderList( ShadowDecalHandle_t decalHandle ) = 0;

	// This will render all shadows that were previously added using
	// AddShadowsOnSurfaceToRenderList. If there's a model to world transform
	// for the shadow receiver, then send it in!
	// NOTE: This draws both shadows and projected textures.
	virtual void RenderProjectedTextures( VMatrix const* pModelToWorld = 0 ) = 0;

	// NOTE: This draws shadows
	virtual void RenderShadows( VMatrix const* pModelToWorld = 0 ) = 0;

	// NOTE: This draws flashlights
	virtual void RenderFlashlights( bool bDoMasking, VMatrix const* pModelToWorld = 0 ) = 0;

	// Clears the list of shadows to render 
	virtual void ClearShadowRenderList() = 0;

	// Projects + clips shadows
	// count + ppPosition describe an array of pointers to vertex positions
	// of the unclipped shadow
	// ppOutVertex is pointed to the head of an array of pointers to
	// clipped vertices the function returns the number of clipped vertices
	virtual int ProjectAndClipVertices( ShadowHandle_t handle, int count, 
		Vector** ppPosition, ShadowVertex_t*** ppOutVertex ) = 0;

	// Computes information for rendering
	virtual void ComputeRenderInfo( ShadowDecalRenderInfo_t* pInfo, ShadowHandle_t handle ) const = 0;

	// Shadow state...
	virtual unsigned short InvalidShadowIndex( ) = 0;
	virtual void SetModelShadowState( ModelInstanceHandle_t instance ) = 0;

	virtual void SetNumWorldMaterialBuckets( int numMaterialSortBins ) = 0;

	virtual void DrawFlashlightDecals( int sortGroup, bool bDoMasking ) = 0;

	virtual void DrawFlashlightDecalsOnSingleSurface( SurfaceHandle_t surfID, bool bDoMasking ) = 0;

	virtual void DrawFlashlightOverlays( int nSortGroup, bool bDoMasking ) = 0;

	virtual void DrawFlashlightDepthTexture( ) = 0;

	virtual void DrawFlashlightDecalsOnDisplacements( int sortGroup, CDispInfo **visibleDisps, int nVisibleDisps, bool bDoMasking ) = 0;

	virtual void SetFlashlightStencilMasks( bool bDoMasking ) = 0;
	virtual bool ModelHasShadows( ModelInstanceHandle_t instance ) = 0;

	// Converts z value to darkness
	inline unsigned char ComputeDarkness( float z, const ShadowDecalRenderInfo_t& info ) const;
};


//-----------------------------------------------------------------------------
// inline methods
//-----------------------------------------------------------------------------
inline unsigned char IShadowMgrInternal::ComputeDarkness( float z, const ShadowDecalRenderInfo_t& info ) const
{
	// NOTE: 0 means black, non-zero adds towards white...
	z = ( z - info.m_flFalloffOffset ) * info.m_flOOZFalloffDist;
	z = fsel( z, z, 0.0f );
	z = info.m_flFalloffBias + z * info.m_flFalloffAmount;
	z = fsel( z - 255.0f, 255.0f, z );
	return z;
}


//-----------------------------------------------------------------------------
// Singleton
//-----------------------------------------------------------------------------
extern IShadowMgrInternal* g_pShadowMgr;

#endif