summaryrefslogtreecommitdiff
path: root/engine/gl_warp.cpp
blob: 881ac7abd6b0b21303aaca0d5ff6fd822881313d (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//===========================================================================//
// gl_warp.c -- sky and water polygons

#include "render_pch.h"
#include "gl_water.h"
#include "zone.h"
#include "gl_model_private.h"
#include "gl_matsysiface.h"
#include "utlvector.h"
#include "materialsystem/imesh.h"
#include "materialsystem/imaterial.h"
#include "tier2/tier2.h"
#include "materialsystem/imaterialsystemhardwareconfig.h"
#include "tier0/vprof.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

#define SQRT3INV		(0.57735f)		// a little less than 1 / sqrt(3)

static ConVar	r_drawskybox(  "r_drawskybox", "1", FCVAR_CHEAT	);

extern ConVar		mat_loadtextures;
static IMaterial	*skyboxMaterials[6] = { NULL, NULL, NULL, NULL, NULL, NULL };

// 1 = s, 2 = t, 3 = 2048
int	st_to_vec[6][3] =
{
	{3,-1,2},
	{-3,1,2},

	{1,3,2},
	{-1,-3,2},

	{-2,-1,3},		// 0 degrees yaw, look straight up
	{2,-1,-3}		// look straight down
};

// s = [0]/[2], t = [1]/[2]

int	skytexorder[6] = {0,2,1,3,4,5};
#define SIGN(d)	((d)<0?-1:1)
static int		gFakePlaneType[6] = {1,-1,2,-2,3,-3};

// (This is pasted from vtf.cpp - just for reference. It shows how the faces
// of the engine's skybox are oriented and mapped).
//
// The vert ordering is lower-left, top-left, top-right, bottom-right.
//
// These were constructed for the engine skybox, which looks like this
// (assuming X goes forward, Y goes left, and Z goes up).
//
//				 6 ------------- 5
//			   /  			   /  
//			 /	 |			 /	 |
//		   /	 |		   /	 |
//		 2 ------------- 1		 |
//		  		 |		  		 |
//		 |		  		 |		  
//		 |		 7 ------|------ 4
//		 |	   /		 |	   /
//		 |	 /			 |	 /
//		   /			   /
//		 3 ------------- 0
//
//int g_skybox_rightFaceVerts[4] = { 7, 6, 5, 4 };
//int g_skybox_leftFaceVerts[4] = { 0, 1, 2, 3 };
//int g_skybox_backFaceVerts[4] = { 3, 2, 6, 7 };
//int g_skybox_frontFaceVerts[4] = { 4, 5, 1, 0 };
//int g_skybox_upFaceVerts[4] = { 6, 2, 1, 5 };
//int g_skybox_downFaceVerts[4] = { 3, 7, 4, 0 };


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void R_UnloadSkys( void )
{
	int i;

	for ( i = 0; i < 6; i++ )
	{
		if( skyboxMaterials[i] )
		{
			skyboxMaterials[ i ]->DecrementReferenceCount();
			skyboxMaterials[ i ] = NULL;
		}
	}	
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *name - 
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool R_LoadNamedSkys( const char *skyname )
{
	char		name[ MAX_OSPATH ];
	IMaterial	*skies[ 6 ];
	bool		success = true;
	const char	*skyboxsuffix[ 6 ] = { "rt", "bk", "lf", "ft", "up", "dn" };

	bool bUseDx8Skyboxes = ( g_pMaterialSystemHardwareConfig->GetDXSupportLevel() < 90 );
	for ( int i = 0; i < 6; i++ )
	{
		skies[i] = NULL;
		if ( bUseDx8Skyboxes )
		{
			Q_snprintf( name, sizeof( name ), "skybox/%s_dx80%s", skyname, skyboxsuffix[i] );
			skies[i] = materials->FindMaterial( name, TEXTURE_GROUP_SKYBOX, false );
			if( IsErrorMaterial( skies[i] ) )
			{
				skies[i] = NULL;
			}
		}

		if ( skies[i] == NULL )
		{
			Q_snprintf( name, sizeof( name ), "skybox/%s%s", skyname, skyboxsuffix[i] );
			skies[i] = materials->FindMaterial( name, TEXTURE_GROUP_SKYBOX );
		}
		if( !IsErrorMaterial( skies[i] ) )
			continue;

		success = false;
		break;
	}

	if ( !success )
	{
		return false;
	}

	// Increment references
	for ( int i = 0; i < 6; i++ )
	{
		// Unload any old skybox
		if ( skyboxMaterials[ i ] )
		{
			skyboxMaterials[ i ]->DecrementReferenceCount();
			skyboxMaterials[ i ] = NULL;
		}
	
		// Use the new one
		assert( skies[ i ] );
		skyboxMaterials[i] = skies[ i ];
		skyboxMaterials[i]->IncrementReferenceCount();
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void R_LoadSkys( void )
{
	bool success = true;

	char requestedsky[ 128 ];

	ConVarRef skyname( "sv_skyname" );
	if ( skyname.IsValid() )
	{
		Q_strncpy( requestedsky, skyname.GetString(), sizeof( requestedsky ) );
	}
	else
	{
		ConDMsg( "Unable to find skyname ConVar!!!\n" );
		return;
	}

	// See if user's sky will work
	if ( !R_LoadNamedSkys( requestedsky ) )
	{
		// Assume failure
		success = false;

		// See if user requested other than the default
		if ( Q_stricmp( requestedsky, "sky_urb01" ) )
		{
			// Try the default
			skyname.SetValue( "sky_urb01" );

			// See if we could load that one now
			if ( R_LoadNamedSkys( skyname.GetString() ) )
			{
				ConDMsg( "Unable to load sky %s, but successfully loaded %s\n", requestedsky, skyname.GetString() );
				success = true;
			}
		}
	}

	if ( !success )
	{
		ConDMsg( "Unable to load sky %s\n", requestedsky );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
#pragma warning (disable : 4701)
void MakeSkyVec( float s, float t, int axis, float zFar, Vector& position, Vector2D &texCoord )
{
	Vector		v, b;
	int			j, k;
	float		width;

	width = zFar * SQRT3INV;

	if ( s < -1 )
		s = -1;
	else if ( s > 1 )
		s = 1;
	if ( t < -1 )
		t = -1;
	else if ( t > 1 )
		t = 1;

	b[0] = s*width;
	b[1] = t*width;
	b[2] = width;

	for (j=0 ; j<3 ; j++)
	{
		k = st_to_vec[axis][j];
		if (k < 0)
			v[j] = -b[-k - 1];
		else
			v[j] = b[k - 1];
		v[j] += CurrentViewOrigin()[j];
	}

	// avoid bilerp seam
	s = (s+1)*0.5;
	t = (t+1)*0.5;

	if (s < 1.0/512)
		s = 1.0/512;
	else if (s > 511.0/512)
		s = 511.0/512;
	if (t < 1.0/512)
		t = 1.0/512;
	else if (t > 511.0/512)
		t = 511.0/512;

	t = 1.0 - t;
	VectorCopy( v, position );
	texCoord[0] = s;
	texCoord[1] = t;
}
#pragma warning (default : 4701)

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void R_DrawSkyBox( float zFar, int nDrawFlags /*= 0x3F*/  )
{
	VPROF("R_DrawSkyBox");
	tmZoneFiltered( TELEMETRY_LEVEL0, 50, TMZF_NONE, "%s %x", __FUNCTION__, nDrawFlags );

	int		i;
	Vector	normal;

	if ( !r_drawskybox.GetInt() || !mat_loadtextures.GetInt() )
	{
		return;
	}

	CMatRenderContextPtr pRenderContext( materials );

	for (i=0 ; i<6 ; i++, nDrawFlags >>= 1 )
	{
		// Don't draw this panel of the skybox if the flag isn't set:
		if ( !(nDrawFlags & 1) )
			continue;

		VectorCopy( vec3_origin, normal );
		switch( gFakePlaneType[i] )
		{
		case 1:
			normal[0] = 1;
			break;

		case -1:
			normal[0] = -1;
			break;

		case 2:
			normal[1] = 1;
			break;

		case -2:
			normal[1] = -1;
			break;

		case 3:
			normal[2] = 1;
			break;

		case -3:
			normal[2] = -1;
			break;
		}

		// Normals are reversed so looking at face dots to 1.0, looking away from is -1.0
		// Reject backfacing surfaces on the inside of the cube to avoid binding their texture
		// Assuming a 90 fov looking at face is 0 degrees, so reject at 107
		if ( DotProduct( CurrentViewForward(), normal ) < -0.29289f )
			continue;

		Vector positionArray[4];
		Vector2D texCoordArray[4];
		if (skyboxMaterials[skytexorder[i]])
		{
			pRenderContext->Bind( skyboxMaterials[skytexorder[i]] );

			MakeSkyVec( -1.0f, -1.0f, i, zFar, positionArray[0], texCoordArray[0] );
			MakeSkyVec( -1.0f, 1.0f, i, zFar, positionArray[1], texCoordArray[1] );
			MakeSkyVec( 1.0f, 1.0f, i, zFar, positionArray[2], texCoordArray[2] );
			MakeSkyVec( 1.0f, -1.0f, i, zFar, positionArray[3], texCoordArray[3] );

			IMesh* pMesh = pRenderContext->GetDynamicMesh();

			CMeshBuilder meshBuilder;
			meshBuilder.Begin( pMesh, MATERIAL_TRIANGLES, 4, 6 );

			// meshbuilder Begin can fail if dynamic mesh is not available (eg, alt-tabbed away)
			if ( meshBuilder.BaseVertexData() == NULL )
				continue;

			for (int j = 0; j < 4; ++j)
			{
				meshBuilder.Position3fv( positionArray[j].Base() );
				meshBuilder.TexCoord2fv( 0, texCoordArray[j].Base() );
				meshBuilder.AdvanceVertex();
			}
			CIndexBuilder &indexBuilder = meshBuilder;
			indexBuilder.FastQuad( 0 );
		
			meshBuilder.End();
			pMesh->Draw();
		}
	}
}