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 "sys/platform.h"
30
31 #include "renderer/tr_local.h"
32 #include "renderer/Model_local.h"
33
34 /*
35
36 A simple sprite model that always faces the view axis.
37
38 */
39
40 static const char *sprite_SnapshotName = "_sprite_Snapshot_";
41
42 /*
43 ===============
44 idRenderModelBeam::IsDynamicModel
45 ===============
46 */
IsDynamicModel() const47 dynamicModel_t idRenderModelSprite::IsDynamicModel() const {
48 return DM_CONTINUOUS;
49 }
50
51 /*
52 ===============
53 idRenderModelBeam::IsLoaded
54 ===============
55 */
IsLoaded() const56 bool idRenderModelSprite::IsLoaded() const {
57 return true;
58 }
59
60 /*
61 ===============
62 idRenderModelSprite::InstantiateDynamicModel
63 ===============
64 */
InstantiateDynamicModel(const struct renderEntity_s * renderEntity,const struct viewDef_s * viewDef,idRenderModel * cachedModel)65 idRenderModel * idRenderModelSprite::InstantiateDynamicModel( const struct renderEntity_s *renderEntity, const struct viewDef_s *viewDef, idRenderModel *cachedModel ) {
66 idRenderModelStatic *staticModel;
67 srfTriangles_t *tri;
68 modelSurface_t surf;
69
70 if ( cachedModel && !r_useCachedDynamicModels.GetBool() ) {
71 delete cachedModel;
72 cachedModel = NULL;
73 }
74
75 if ( renderEntity == NULL || viewDef == NULL ) {
76 delete cachedModel;
77 return NULL;
78 }
79
80 if ( cachedModel != NULL ) {
81
82 assert( dynamic_cast<idRenderModelStatic *>( cachedModel ) != NULL );
83 assert( idStr::Icmp( cachedModel->Name(), sprite_SnapshotName ) == 0 );
84
85 staticModel = static_cast<idRenderModelStatic *>( cachedModel );
86 surf = *staticModel->Surface( 0 );
87 tri = surf.geometry;
88
89 } else {
90
91 staticModel = new idRenderModelStatic;
92 staticModel->InitEmpty( sprite_SnapshotName );
93
94 tri = R_AllocStaticTriSurf();
95 R_AllocStaticTriSurfVerts( tri, 4 );
96 R_AllocStaticTriSurfIndexes( tri, 6 );
97
98 tri->verts[ 0 ].Clear();
99 tri->verts[ 0 ].normal.Set( 1.0f, 0.0f, 0.0f );
100 tri->verts[ 0 ].tangents[0].Set( 0.0f, 1.0f, 0.0f );
101 tri->verts[ 0 ].tangents[1].Set( 0.0f, 0.0f, 1.0f );
102 tri->verts[ 0 ].st[ 0 ] = 0.0f;
103 tri->verts[ 0 ].st[ 1 ] = 0.0f;
104
105 tri->verts[ 1 ].Clear();
106 tri->verts[ 1 ].normal.Set( 1.0f, 0.0f, 0.0f );
107 tri->verts[ 1 ].tangents[0].Set( 0.0f, 1.0f, 0.0f );
108 tri->verts[ 1 ].tangents[1].Set( 0.0f, 0.0f, 1.0f );
109 tri->verts[ 1 ].st[ 0 ] = 1.0f;
110 tri->verts[ 1 ].st[ 1 ] = 0.0f;
111
112 tri->verts[ 2 ].Clear();
113 tri->verts[ 2 ].normal.Set( 1.0f, 0.0f, 0.0f );
114 tri->verts[ 2 ].tangents[0].Set( 0.0f, 1.0f, 0.0f );
115 tri->verts[ 2 ].tangents[1].Set( 0.0f, 0.0f, 1.0f );
116 tri->verts[ 2 ].st[ 0 ] = 1.0f;
117 tri->verts[ 2 ].st[ 1 ] = 1.0f;
118
119 tri->verts[ 3 ].Clear();
120 tri->verts[ 3 ].normal.Set( 1.0f, 0.0f, 0.0f );
121 tri->verts[ 3 ].tangents[0].Set( 0.0f, 1.0f, 0.0f );
122 tri->verts[ 3 ].tangents[1].Set( 0.0f, 0.0f, 1.0f );
123 tri->verts[ 3 ].st[ 0 ] = 0.0f;
124 tri->verts[ 3 ].st[ 1 ] = 1.0f;
125
126 tri->indexes[ 0 ] = 0;
127 tri->indexes[ 1 ] = 1;
128 tri->indexes[ 2 ] = 3;
129 tri->indexes[ 3 ] = 1;
130 tri->indexes[ 4 ] = 2;
131 tri->indexes[ 5 ] = 3;
132
133 tri->numVerts = 4;
134 tri->numIndexes = 6;
135
136 surf.geometry = tri;
137 surf.id = 0;
138 surf.shader = tr.defaultMaterial;
139 staticModel->AddSurface( surf );
140 }
141
142 int red = idMath::FtoiFast( renderEntity->shaderParms[ SHADERPARM_RED ] * 255.0f );
143 int green = idMath::FtoiFast( renderEntity->shaderParms[ SHADERPARM_GREEN ] * 255.0f );
144 int blue = idMath::FtoiFast( renderEntity->shaderParms[ SHADERPARM_BLUE ] * 255.0f );
145 int alpha = idMath::FtoiFast( renderEntity->shaderParms[ SHADERPARM_ALPHA ] * 255.0f );
146
147 idVec3 right = idVec3( 0.0f, renderEntity->shaderParms[ SHADERPARM_SPRITE_WIDTH ] * 0.5f, 0.0f );
148 idVec3 up = idVec3( 0.0f, 0.0f, renderEntity->shaderParms[ SHADERPARM_SPRITE_HEIGHT ] * 0.5f );
149
150 tri->verts[ 0 ].xyz = up + right;
151 tri->verts[ 0 ].color[ 0 ] = red;
152 tri->verts[ 0 ].color[ 1 ] = green;
153 tri->verts[ 0 ].color[ 2 ] = blue;
154 tri->verts[ 0 ].color[ 3 ] = alpha;
155
156 tri->verts[ 1 ].xyz = up - right;
157 tri->verts[ 1 ].color[ 0 ] = red;
158 tri->verts[ 1 ].color[ 1 ] = green;
159 tri->verts[ 1 ].color[ 2 ] = blue;
160 tri->verts[ 1 ].color[ 3 ] = alpha;
161
162 tri->verts[ 2 ].xyz = - right - up;
163 tri->verts[ 2 ].color[ 0 ] = red;
164 tri->verts[ 2 ].color[ 1 ] = green;
165 tri->verts[ 2 ].color[ 2 ] = blue;
166 tri->verts[ 2 ].color[ 3 ] = alpha;
167
168 tri->verts[ 3 ].xyz = right - up;
169 tri->verts[ 3 ].color[ 0 ] = red;
170 tri->verts[ 3 ].color[ 1 ] = green;
171 tri->verts[ 3 ].color[ 2 ] = blue;
172 tri->verts[ 3 ].color[ 3 ] = alpha;
173
174 R_BoundTriSurf( tri );
175
176 staticModel->bounds = tri->bounds;
177
178 return staticModel;
179 }
180
181 /*
182 ===============
183 idRenderModelSprite::Bounds
184 ===============
185 */
Bounds(const struct renderEntity_s * renderEntity) const186 idBounds idRenderModelSprite::Bounds( const struct renderEntity_s *renderEntity ) const {
187 idBounds b;
188
189 b.Zero();
190 if ( renderEntity == NULL ) {
191 b.ExpandSelf( 8.0f );
192 } else {
193 b.ExpandSelf( Max( renderEntity->shaderParms[ SHADERPARM_SPRITE_WIDTH ], renderEntity->shaderParms[ SHADERPARM_SPRITE_HEIGHT ] ) * 0.5f );
194 }
195 return b;
196 }
197