summaryrefslogtreecommitdiff
path: root/vgui2/matsys_controls/vtfpreviewpanel.cpp
blob: abd7afa881aa29c37bef63e4268bfe541955f8fd (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//===========================================================================//

#include "matsys_controls/vtfpreviewpanel.h"
#include "matsys_controls/matsyscontrols.h"
#include "VGuiMatSurface/IMatSystemSurface.h"
#include "materialsystem/MaterialSystemUtil.h"
#include "materialsystem/imaterialsystem.h"
#include "materialsystem/itexture.h"
#include "materialsystem/imesh.h"
#include "tier1/KeyValues.h"


using namespace vgui;


#define FOV 90.0f
#define ZNEAR 0.1f
#define ZFAR 2000.0f
#define ROTATION_SPEED 120.0f		// degrees/sec

//-----------------------------------------------------------------------------
//
// VTF Preview panel
//
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// constructor
//-----------------------------------------------------------------------------
CVTFPreviewPanel::CVTFPreviewPanel( vgui::Panel *pParent, const char *pName ) :
	BaseClass( pParent, pName )
{
	SetVTF( "//platform/materials/vgui/vtfnotloaded", true );
	m_nTextureID = MatSystemSurface()->CreateNewTextureID( false );
}

CVTFPreviewPanel::~CVTFPreviewPanel()
{
	if ( vgui::surface() && m_nTextureID != -1 )
	{
		vgui::surface()->DestroyTextureID( m_nTextureID );
		m_nTextureID = -1;
	}
}


//-----------------------------------------------------------------------------
// Sets the current VTF
//-----------------------------------------------------------------------------
void CVTFPreviewPanel::SetVTF( const char *pFullPath, bool bLoadImmediately )
{
	m_PreviewTexture.Init( pFullPath, "editor texture" );
	m_VTFName = pFullPath;

	KeyValues *pVMTKeyValues = new KeyValues( "UnlitGeneric" );
	if ( m_PreviewTexture->IsCubeMap() )
	{
		pVMTKeyValues->SetString( "$envmap", pFullPath );
	}
	else if ( m_PreviewTexture->IsNormalMap() )
	{
		pVMTKeyValues->SetString( "$bumpmap", pFullPath );
	}
	else
	{
		pVMTKeyValues->SetString( "$basetexture", pFullPath );
	}
	pVMTKeyValues->SetInt( "$nocull", 1 );
	pVMTKeyValues->SetInt( "$nodebug", 1 );
	m_PreviewMaterial.Init( MaterialSystem()->CreateMaterial( pFullPath, pVMTKeyValues ));

	MatSystemSurface()->DrawSetTextureMaterial( m_nTextureID, m_PreviewMaterial );

	// Reset the camera direction
	m_vecCameraDirection.Init( 1.0f, 0.0f, 0.0f );
	m_flLastRotationTime = Plat_FloatTime();
}


//-----------------------------------------------------------------------------
// Gets the current VTF
//-----------------------------------------------------------------------------
const char *CVTFPreviewPanel::GetVTF() const
{
	return m_VTFName;
}


//-----------------------------------------------------------------------------
// Draw a sphere
//-----------------------------------------------------------------------------
void CVTFPreviewPanel::RenderSphere( const Vector &vCenter, float flRadius, int nTheta, int nPhi )
{
	CMatRenderContextPtr pRenderContext( MaterialSystem() );

	int nVertices =  nTheta * nPhi;
	int nIndices = 2 * ( nTheta + 1 ) * ( nPhi - 1 );
	
	pRenderContext->FogMode( MATERIAL_FOG_NONE );
	pRenderContext->SetNumBoneWeights( 0 );
	pRenderContext->Bind( m_PreviewMaterial );

	IMesh* pMesh = pRenderContext->GetDynamicMesh();

	CMeshBuilder meshBuilder;
	meshBuilder.Begin( pMesh, MATERIAL_TRIANGLE_STRIP, nVertices, nIndices );

	//
	// Build the index buffer.
	//
	int i, j;
	for ( i = 0; i < nPhi; ++i )
	{
		for ( j = 0; j < nTheta; ++j )
		{
			float u = j / ( float )(nTheta - 1);
			float v = i / ( float )(nPhi - 1);
			float theta = ( j != nTheta-1 ) ? 2.0f * M_PI * u : 0.0f;
			float phi = M_PI * v;

			Vector vecPos;
			vecPos.x = flRadius * sin(phi) * cos(theta);
			vecPos.y = flRadius * cos(phi);
			vecPos.z = -flRadius * sin(phi) * sin(theta); 
			    
			Vector vecNormal = vecPos;
			VectorNormalize( vecNormal );

			Vector4D vecTangentS;
			Vector vecTangentT;
			vecTangentS.Init( vecPos.z, -vecPos.x, 0.0f, 1.0f );
			if ( VectorNormalize( vecTangentS.AsVector3D() ) == 0.0f )
			{
				vecTangentS.Init( 1.0f, 0.0f, 0.0f, 1.0f );
			}

			CrossProduct( vecNormal, vecTangentS.AsVector3D(), vecTangentT );

			unsigned char red = (int)( u * 255.0f );
			unsigned char green = (int)( v * 255.0f );
			unsigned char blue = (int)( v * 255.0f );
			unsigned char alpha = (int)( v * 255.0f );

			vecPos += vCenter;

			float u1, u2, v1, v2;
			u1 = u2 = u;
			v1 = v2 = v;

			meshBuilder.Position3fv( vecPos.Base() );
			meshBuilder.Normal3fv( vecNormal.Base() );
			meshBuilder.Color4ub( red, green, blue, alpha );
			meshBuilder.TexCoord2f( 0, u, v );
			meshBuilder.TexCoord2f( 1, u1, v1 );
			meshBuilder.TexCoord2f( 2, u2, v2 );
			meshBuilder.TangentS3fv( vecTangentS.Base() );
			meshBuilder.TangentT3fv( vecTangentT.Base() );
			meshBuilder.BoneWeight( 0, 1.0f );
			meshBuilder.BoneMatrix( 0, 0 );
			meshBuilder.UserData( vecTangentS.Base() );
			meshBuilder.AdvanceVertex();
		}
	}

	//
	// Emit the triangle strips.
	//
	int idx = 0;
	for ( i = 0; i < nPhi - 1; ++i )
	{
		for ( j = 0; j < nTheta; ++j )
		{
			idx = nTheta * i + j;

			meshBuilder.FastIndex( idx );
			meshBuilder.FastIndex( idx + nTheta );
		}

		//
		// Emit a degenerate triangle to skip to the next row without
		// a connecting triangle.
		//
		if ( i < nPhi - 2 )
		{
			meshBuilder.FastIndex( idx + 1 );
			meshBuilder.FastIndex( idx + 1 + nTheta );
		}
	}

	meshBuilder.End();
	pMesh->Draw();
}


//-----------------------------------------------------------------------------
// Paints a regular texture
//-----------------------------------------------------------------------------
void CVTFPreviewPanel::PaintStandardTexture( void )
{	 
	int x, y, w, h;
	x = y = 0;
	GetSize( w, h );
	vgui::surface()->DrawSetTexture( m_nTextureID );
	vgui::surface()->DrawSetColor( 255, 255, 255, 255 );

	// Get the aspect ratio of the texture
	int tw = m_PreviewTexture->GetActualWidth();
 	int th = m_PreviewTexture->GetActualHeight();

	if ( th > 0 && h > 0 )
	{
		float screenaspect = (float)tw / (float)th;
		float aspect = (float)w / (float)h;

		float ratio = screenaspect / aspect;

		// Screen is wider, need bars at top and bottom
		if ( ratio > 1.0f )
		{
			int usetall = (float)w / screenaspect;
			y = ( h - usetall ) / 2;
			h = usetall;
		}
		// Screen is narrower, need bars at left/right
		else
		{
			int usewide = (float)h * screenaspect;
			x = ( w - usewide ) / 2;
			w = usewide;
		}
	}

	vgui::surface()->DrawTexturedRect( x, y, x+w, y+h );
}


//-----------------------------------------------------------------------------
// Paints a normalmap texture
//-----------------------------------------------------------------------------
void CVTFPreviewPanel::PaintNormalMapTexture( void )
{
}


//-----------------------------------------------------------------------------
// Paints a volume texture
//-----------------------------------------------------------------------------
void CVTFPreviewPanel::PaintVolumeTexture( void )
{
}


//-----------------------------------------------------------------------------
// Paints a cubemap texture
//-----------------------------------------------------------------------------
void CVTFPreviewPanel::PaintCubeTexture( void )
{
	float flNewTime = Plat_FloatTime();

	// Circle the camera around the origin
	VMatrix rot;
	MatrixBuildRotateZ( rot, ROTATION_SPEED * (flNewTime - m_flLastRotationTime ) );
	Vector vecTemp;
	Vector3DMultiply( rot, m_vecCameraDirection, vecTemp );
	m_vecCameraDirection = vecTemp;
	m_flLastRotationTime = flNewTime;

 	LookAt( vec3_origin, 12.0f );

	// Draw a sphere at the origin
	RenderSphere( vec3_origin, 10.0f,  20, 20 );
}


//-----------------------------------------------------------------------------
// Sets the camera to look at the the thing we're spinning around
//-----------------------------------------------------------------------------
void CVTFPreviewPanel::LookAt( const Vector &vecLookAt, float flRadius )
{
	// Compute the distance to the camera for the object based on its
	// radius and fov.

	// since tan( fov/2 ) = f/d
	// cos( fov/2 ) = r / r' where r = sphere radius, r' = perp distance from sphere center to max extent of camera
	// d/f = r'/d' where d' is distance of camera to sphere
	// d' = r' / tan( fov/2 ) * r' = r / ( cos (fov/2) * tan( fov/2 ) ) = r / sin( fov/2 )
	float flFOVx = FOV;

	// Compute fov/2 in radians
	flFOVx *= M_PI / 360.0f;

	// Compute an effective fov	based on the aspect ratio 
	// if the height is smaller than the width
	int w, h;
	GetSize( w, h );
	if ( h < w )
	{
		flFOVx = atan( h * tan( flFOVx ) / w );
	}

	float flDistance = flRadius / sin( flFOVx );

	Vector vecMDLOrigin = vecLookAt;
	Vector vecCameraOrigin;
	VectorMA( vecMDLOrigin, -flDistance, m_vecCameraDirection, vecCameraOrigin );

	CMatRenderContextPtr pRenderContext( MaterialSystem() );
	QAngle angles;
	VectorAngles( m_vecCameraDirection, angles );

	pRenderContext->MatrixMode( MATERIAL_VIEW );
	pRenderContext->LoadIdentity();

	// convert from a right handed system to a left handed system
	// since dx for wants it that way.  
//	pRenderContext->Scale( 1.0f, 1.0f, -1.0f );

	pRenderContext->Rotate( -90,  1, 0, 0 );	    // put Z going up
	pRenderContext->Rotate( 90,  0, 0, 1 );	    // put Z going up
	pRenderContext->Rotate( -angles[2],  1, 0, 0 );
	pRenderContext->Rotate( -angles[0],  0, 1, 0 );
	pRenderContext->Rotate( -angles[1],  0, 0, 1 );
	pRenderContext->Translate( -vecCameraOrigin[0],  -vecCameraOrigin[1],  -vecCameraOrigin[2] );
}


//-----------------------------------------------------------------------------
// Set up a projection matrix for a 90 degree fov
//-----------------------------------------------------------------------------
void CVTFPreviewPanel::SetupProjectionMatrix( int nWidth, int nHeight )
{
	CMatRenderContextPtr pRenderContext( MaterialSystem() );
	VMatrix proj;
	float flFOV = FOV;
	float flZNear = ZNEAR;
	float flZFar = ZFAR;
	float flApsectRatio = (nHeight != 0.0f) ? (float)nWidth / (float)nHeight : 100.0f;

#if 1
	float halfWidth = tan( flFOV * M_PI / 360.0 );
	float halfHeight = halfWidth / flApsectRatio;
#else
	float halfHeight = tan( flFOV * M_PI / 360.0 );
	float halfWidth = flApsectRatio * halfHeight;
#endif
	memset( proj.Base(), 0, sizeof( proj ) );
	proj[0][0]  = 1.0f / halfWidth;
	proj[1][1]  = 1.0f / halfHeight;
	proj[2][2] = flZFar / ( flZNear - flZFar );
	proj[3][2] = -1.0f;
	proj[2][3] = flZNear * flZFar / ( flZNear - flZFar );

	pRenderContext->MatrixMode( MATERIAL_PROJECTION );
	pRenderContext->LoadMatrix( proj );
}


//-----------------------------------------------------------------------------
// Paints the texture
//-----------------------------------------------------------------------------
void CVTFPreviewPanel::Paint( void )
{
	if ( !m_PreviewTexture->IsCubeMap() && /*!m_PreviewTexture->IsNormalMap() &&*/ !m_PreviewTexture->IsVolumeTexture() )
	{
		PaintStandardTexture();
		return;
	}

	CMatRenderContextPtr pRenderContext( MaterialSystem() );
	int w, h;
	GetSize( w, h );
	vgui::MatSystemSurface()->Begin3DPaint( 0, 0, w, h );

	pRenderContext->ClearColor4ub( 76, 88, 68, 255 ); 
	pRenderContext->ClearBuffers( true, true );
				   
	SetupProjectionMatrix( w, h );

	if ( m_PreviewTexture->IsCubeMap() )
	{
		PaintCubeTexture();
	}
	else if ( m_PreviewTexture->IsNormalMap() )
	{
		PaintNormalMapTexture();
	}
	else if ( m_PreviewTexture->IsVolumeTexture() )
	{
		PaintVolumeTexture();
	}

	vgui::MatSystemSurface()->End3DPaint( );
}