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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Decorator class to make a DME renderable as a MDL
//
//===========================================================================//
#ifndef DMEMDLRENDERABLE_H
#define DMEMDLRENDERABLE_H
#ifdef _WIN32
#pragma once
#endif
#include "toolutils/dmerenderable.h"
#include "movieobjects/dmemdl.h"
#include "movieobjects/dmetransform.h"
#include "datacache/imdlcache.h"
#include "mathlib/mathlib.h"
#include "datamodel/dmehandle.h"
#include "toolutils/enginetools_int.h"
#include "materialsystem/imaterialsystemhardwareconfig.h"
#include "tier3/tier3.h"
//-----------------------------------------------------------------------------
// Deals with the base implementation for turning a Dme into a renderable
//-----------------------------------------------------------------------------
template < class T >
class CDmeMdlRenderable : public CDmeRenderable< T >
{
DEFINE_UNINSTANCEABLE_ELEMENT( CDmeMdlRenderable, CDmeRenderable< T > );
// IClientUnknown implementation.
public:
virtual int GetBody();
virtual int GetSkin();
virtual int DrawModel( int flags );
virtual void GetRenderBounds( Vector& mins, Vector& maxs );
void SetModelName( const char *pMDLName );
protected:
CDmeMDL *GetMDL() { return m_hMDL; }
private:
void SetUpLighting( const Vector &vecCenter );
CDmeHandle<CDmeMDL> m_hMDL;
CDmeHandle<CDmeTransform> m_hTransform;
};
//-----------------------------------------------------------------------------
// Construction, destruction
//-----------------------------------------------------------------------------
template < class T >
void CDmeMdlRenderable<T>::OnConstruction()
{
m_hMDL = CreateElement<CDmeMDL>( "MDLRenderable", GetFileId() );
m_hTransform = CreateElement<CDmeTransform>( "MDLTransform", GetFileId() );
}
template < class T >
void CDmeMdlRenderable<T>::OnDestruction()
{
g_pDataModel->DestroyElement( m_hMDL );
g_pDataModel->DestroyElement( m_hTransform );
}
template < class T >
int CDmeMdlRenderable<T>::GetBody()
{
return m_hMDL->m_nBody;
}
template < class T >
int CDmeMdlRenderable<T>::GetSkin()
{
return m_hMDL->m_nSkin;
}
template < class T >
int CDmeMdlRenderable<T>::DrawModel( int flags )
{
matrix3x4_t mat;
AngleMatrix( GetRenderAngles(), GetRenderOrigin(), mat );
m_hTransform->SetTransform( mat );
m_hMDL->m_flTime = Plat_FloatTime();
SetUpLighting( GetRenderOrigin() );
bool bIsDrawingInEngine = m_hMDL->IsDrawingInEngine();
m_hMDL->DrawInEngine( true );
m_hMDL->Draw( mat );
m_hMDL->DrawInEngine( bIsDrawingInEngine );
return 1;
}
template < class T >
void CDmeMdlRenderable<T>::GetRenderBounds( Vector& mins, Vector& maxs )
{
m_hMDL->GetBoundingBox( &mins, &maxs );
}
template < class T >
void CDmeMdlRenderable<T>::SetModelName( const char *pMDLRelativePath )
{
if ( pMDLRelativePath )
{
MDLHandle_t hMdl = g_pMDLCache->FindMDL( pMDLRelativePath );
m_hMDL->SetMDL( hMdl );
}
else
{
m_hMDL->SetMDL( MDLHANDLE_INVALID );
}
}
//-----------------------------------------------------------------------------
// Set up lighting conditions
//-----------------------------------------------------------------------------
template < class T >
void CDmeMdlRenderable<T>::SetUpLighting( const Vector &vecCenter )
{
// Set up lighting conditions
Vector vecAmbient[6];
Vector4D vecAmbient4D[6];
LightDesc_t desc[2];
int nLightCount = enginetools->GetLightingConditions( vecCenter, vecAmbient, 2, desc );
int nMaxLights = g_pMaterialSystemHardwareConfig->MaxNumLights();
if( nLightCount > nMaxLights )
{
nLightCount = nMaxLights;
}
int i;
for( i = 0; i < 6; i++ )
{
VectorCopy( vecAmbient[i], vecAmbient4D[i].AsVector3D() );
vecAmbient4D[i][3] = 1.0f;
}
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
pRenderContext->SetAmbientLightCube( vecAmbient4D );
for( i = 0; i < nLightCount; i++ )
{
LightDesc_t *pLight = &desc[i];
pLight->m_Flags = 0;
if( pLight->m_Attenuation0 != 0.0f )
{
pLight->m_Flags |= LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION0;
}
if( pLight->m_Attenuation1 != 0.0f )
{
pLight->m_Flags |= LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION1;
}
if( pLight->m_Attenuation2 != 0.0f )
{
pLight->m_Flags |= LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION2;
}
pRenderContext->SetLight( i, desc[i] );
}
for( ; i < nMaxLights; i++ )
{
LightDesc_t disableDesc;
disableDesc.m_Type = MATERIAL_LIGHT_DISABLE;
pRenderContext->SetLight( i, disableDesc );
}
}
#endif // DMEMDLRENDERABLE_H
|