doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Model_sprite.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #include "../idlib/precompiled.h"
30 #pragma hdrstop
31 
32 #include "tr_local.h"
33 #include "Model_local.h"
34 
35 
36 /*
37 
38 A simple sprite model that always faces the view axis.
39 
40 */
41 
42 static const char *sprite_SnapshotName = "_sprite_Snapshot_";
43 
44 /*
45 ===============
46 idRenderModelBeam::IsDynamicModel
47 ===============
48 */
50  return DM_CONTINUOUS;
51 }
52 
53 /*
54 ===============
55 idRenderModelBeam::IsLoaded
56 ===============
57 */
59  return true;
60 }
61 
62 /*
63 ===============
64 idRenderModelSprite::InstantiateDynamicModel
65 ===============
66 */
67 idRenderModel * idRenderModelSprite::InstantiateDynamicModel( const struct renderEntity_s *renderEntity, const struct viewDef_s *viewDef, idRenderModel *cachedModel ) {
68  idRenderModelStatic *staticModel;
69  srfTriangles_t *tri;
70  modelSurface_t surf;
71 
72  if ( cachedModel && !r_useCachedDynamicModels.GetBool() ) {
73  delete cachedModel;
74  cachedModel = NULL;
75  }
76 
77  if ( renderEntity == NULL || viewDef == NULL ) {
78  delete cachedModel;
79  return NULL;
80  }
81 
82  if ( cachedModel != NULL ) {
83 
84  assert( dynamic_cast<idRenderModelStatic *>( cachedModel ) != NULL );
85  assert( idStr::Icmp( cachedModel->Name(), sprite_SnapshotName ) == 0 );
86 
87  staticModel = static_cast<idRenderModelStatic *>( cachedModel );
88  surf = *staticModel->Surface( 0 );
89  tri = surf.geometry;
90 
91  } else {
92 
93  staticModel = new idRenderModelStatic;
94  staticModel->InitEmpty( sprite_SnapshotName );
95 
96  tri = R_AllocStaticTriSurf();
97  R_AllocStaticTriSurfVerts( tri, 4 );
99 
100  tri->verts[ 0 ].Clear();
101  tri->verts[ 0 ].normal.Set( 1.0f, 0.0f, 0.0f );
102  tri->verts[ 0 ].tangents[0].Set( 0.0f, 1.0f, 0.0f );
103  tri->verts[ 0 ].tangents[1].Set( 0.0f, 0.0f, 1.0f );
104  tri->verts[ 0 ].st[ 0 ] = 0.0f;
105  tri->verts[ 0 ].st[ 1 ] = 0.0f;
106 
107  tri->verts[ 1 ].Clear();
108  tri->verts[ 1 ].normal.Set( 1.0f, 0.0f, 0.0f );
109  tri->verts[ 1 ].tangents[0].Set( 0.0f, 1.0f, 0.0f );
110  tri->verts[ 1 ].tangents[1].Set( 0.0f, 0.0f, 1.0f );
111  tri->verts[ 1 ].st[ 0 ] = 1.0f;
112  tri->verts[ 1 ].st[ 1 ] = 0.0f;
113 
114  tri->verts[ 2 ].Clear();
115  tri->verts[ 2 ].normal.Set( 1.0f, 0.0f, 0.0f );
116  tri->verts[ 2 ].tangents[0].Set( 0.0f, 1.0f, 0.0f );
117  tri->verts[ 2 ].tangents[1].Set( 0.0f, 0.0f, 1.0f );
118  tri->verts[ 2 ].st[ 0 ] = 1.0f;
119  tri->verts[ 2 ].st[ 1 ] = 1.0f;
120 
121  tri->verts[ 3 ].Clear();
122  tri->verts[ 3 ].normal.Set( 1.0f, 0.0f, 0.0f );
123  tri->verts[ 3 ].tangents[0].Set( 0.0f, 1.0f, 0.0f );
124  tri->verts[ 3 ].tangents[1].Set( 0.0f, 0.0f, 1.0f );
125  tri->verts[ 3 ].st[ 0 ] = 0.0f;
126  tri->verts[ 3 ].st[ 1 ] = 1.0f;
127 
128  tri->indexes[ 0 ] = 0;
129  tri->indexes[ 1 ] = 1;
130  tri->indexes[ 2 ] = 3;
131  tri->indexes[ 3 ] = 1;
132  tri->indexes[ 4 ] = 2;
133  tri->indexes[ 5 ] = 3;
134 
135  tri->numVerts = 4;
136  tri->numIndexes = 6;
137 
138  surf.geometry = tri;
139  surf.id = 0;
140  surf.shader = tr.defaultMaterial;
141  staticModel->AddSurface( surf );
142  }
143 
144  int red = idMath::FtoiFast( renderEntity->shaderParms[ SHADERPARM_RED ] * 255.0f );
145  int green = idMath::FtoiFast( renderEntity->shaderParms[ SHADERPARM_GREEN ] * 255.0f );
146  int blue = idMath::FtoiFast( renderEntity->shaderParms[ SHADERPARM_BLUE ] * 255.0f );
147  int alpha = idMath::FtoiFast( renderEntity->shaderParms[ SHADERPARM_ALPHA ] * 255.0f );
148 
149  idVec3 right = idVec3( 0.0f, renderEntity->shaderParms[ SHADERPARM_SPRITE_WIDTH ] * 0.5f, 0.0f );
150  idVec3 up = idVec3( 0.0f, 0.0f, renderEntity->shaderParms[ SHADERPARM_SPRITE_HEIGHT ] * 0.5f );
151 
152  tri->verts[ 0 ].xyz = up + right;
153  tri->verts[ 0 ].color[ 0 ] = red;
154  tri->verts[ 0 ].color[ 1 ] = green;
155  tri->verts[ 0 ].color[ 2 ] = blue;
156  tri->verts[ 0 ].color[ 3 ] = alpha;
157 
158  tri->verts[ 1 ].xyz = up - right;
159  tri->verts[ 1 ].color[ 0 ] = red;
160  tri->verts[ 1 ].color[ 1 ] = green;
161  tri->verts[ 1 ].color[ 2 ] = blue;
162  tri->verts[ 1 ].color[ 3 ] = alpha;
163 
164  tri->verts[ 2 ].xyz = - right - up;
165  tri->verts[ 2 ].color[ 0 ] = red;
166  tri->verts[ 2 ].color[ 1 ] = green;
167  tri->verts[ 2 ].color[ 2 ] = blue;
168  tri->verts[ 2 ].color[ 3 ] = alpha;
169 
170  tri->verts[ 3 ].xyz = right - up;
171  tri->verts[ 3 ].color[ 0 ] = red;
172  tri->verts[ 3 ].color[ 1 ] = green;
173  tri->verts[ 3 ].color[ 2 ] = blue;
174  tri->verts[ 3 ].color[ 3 ] = alpha;
175 
176  R_BoundTriSurf( tri );
177 
178  staticModel->bounds = tri->bounds;
179 
180  return staticModel;
181 }
182 
183 /*
184 ===============
185 idRenderModelSprite::Bounds
186 ===============
187 */
188 idBounds idRenderModelSprite::Bounds( const struct renderEntity_s *renderEntity ) const {
189  idBounds b;
190 
191  b.Zero();
192  if ( renderEntity == NULL ) {
193  b.ExpandSelf( 8.0f );
194  } else {
195  b.ExpandSelf( Max( renderEntity->shaderParms[ SHADERPARM_SPRITE_WIDTH ], renderEntity->shaderParms[ SHADERPARM_SPRITE_HEIGHT ] ) * 0.5f );
196  }
197  return b;
198 }
assert(prefInfo.fullscreenBtn)
void Clear(void)
Definition: DrawVert.h:73
int numVerts
Definition: Model.h:98
void Zero(void)
Definition: Bounds.h:206
ID_INLINE T Max(T x, T y)
Definition: Lib.h:158
void Set(const float x, const float y, const float z)
Definition: Vector.h:409
idVec3 xyz
Definition: DrawVert.h:42
virtual const modelSurface_t * Surface(int surfaceNum) const
Definition: Model.cpp:403
idVec3 tangents[2]
Definition: DrawVert.h:45
const idMaterial * shader
Definition: Model.h:146
const int SHADERPARM_SPRITE_WIDTH
Definition: RenderWorld.h:68
Definition: Vector.h:316
const int SHADERPARM_GREEN
Definition: RenderWorld.h:47
virtual const char * Name() const =0
GLclampf GLclampf blue
Definition: glext.h:2843
GLclampf GLclampf GLclampf alpha
Definition: glext.h:2843
srfTriangles_t * R_AllocStaticTriSurf(void)
Definition: tr_trisurf.cpp:523
GLdouble right
Definition: qgl.h:273
int Icmp(const char *text) const
Definition: Str.h:667
const int SHADERPARM_ALPHA
Definition: RenderWorld.h:49
const int SHADERPARM_BLUE
Definition: RenderWorld.h:48
idVec2 st
Definition: DrawVert.h:43
virtual void InitEmpty(const char *name)
Definition: Model.cpp:332
virtual idRenderModel * InstantiateDynamicModel(const struct renderEntity_s *ent, const struct viewDef_s *view, idRenderModel *cachedModel)
static int FtoiFast(float f)
Definition: Math.h:801
idBounds bounds
Definition: Model.h:87
#define NULL
Definition: Lib.h:88
srfTriangles_t * geometry
Definition: Model.h:147
idVec3 normal
Definition: DrawVert.h:44
void R_BoundTriSurf(srfTriangles_t *tri)
Definition: tr_trisurf.cpp:704
GLubyte GLubyte b
Definition: glext.h:4662
virtual idBounds Bounds(const struct renderEntity_s *ent) const
idCVar r_useCachedDynamicModels("r_useCachedDynamicModels","1", CVAR_RENDERER|CVAR_BOOL,"cache snapshots of dynamic models")
const int SHADERPARM_RED
Definition: RenderWorld.h:46
bool GetBool(void) const
Definition: CVarSystem.h:142
const int SHADERPARM_SPRITE_HEIGHT
Definition: RenderWorld.h:69
tuple f
Definition: idal.py:89
dynamicModel_t
Definition: Model.h:150
virtual bool IsLoaded() const
byte color[4]
Definition: DrawVert.h:46
glIndex_t * indexes
Definition: Model.h:102
idRenderSystemLocal tr
void R_AllocStaticTriSurfIndexes(srfTriangles_t *tri, int numIndexes)
Definition: tr_trisurf.cpp:565
idBounds & ExpandSelf(const float d)
Definition: Bounds.h:322
int numIndexes
Definition: Model.h:101
virtual dynamicModel_t IsDynamicModel() const
void R_AllocStaticTriSurfVerts(srfTriangles_t *tri, int numVerts)
Definition: tr_trisurf.cpp:555
float shaderParms[MAX_ENTITY_SHADER_PARMS]
Definition: RenderWorld.h:127
const idMaterial * defaultMaterial
Definition: tr_local.h:779
idDrawVert * verts
Definition: Model.h:99
GLclampf green
Definition: glext.h:2843
virtual void AddSurface(modelSurface_t surface)
Definition: Model.cpp:355