Model_sprite.cpp (6124B)
1 /* 2 =========================================================================== 3 4 Doom 3 BFG Edition GPL Source Code 5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 6 7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). 8 9 Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>. 21 22 In addition, the Doom 3 BFG Edition 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 BFG Edition 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 #pragma hdrstop 30 #include "../idlib/precompiled.h" 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 idRenderModelSprite::IsDynamicModel 47 =============== 48 */ 49 dynamicModel_t idRenderModelSprite::IsDynamicModel() const { 50 return DM_CONTINUOUS; 51 } 52 53 /* 54 =============== 55 idRenderModelSprite::IsLoaded 56 =============== 57 */ 58 bool idRenderModelSprite::IsLoaded() const { 59 return true; 60 } 61 62 /* 63 =============== 64 idRenderModelSprite::InstantiateDynamicModel 65 =============== 66 */ 67 idRenderModel * idRenderModelSprite::InstantiateDynamicModel( const struct renderEntity_s *renderEntity, const viewDef_t *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 (TAG_MODEL) idRenderModelStatic; 94 staticModel->InitEmpty( sprite_SnapshotName ); 95 96 tri = R_AllocStaticTriSurf(); 97 R_AllocStaticTriSurfVerts( tri, 4 ); 98 R_AllocStaticTriSurfIndexes( tri, 6 ); 99 100 tri->verts[ 0 ].Clear(); 101 tri->verts[ 0 ].SetNormal( 1.0f, 0.0f, 0.0f ); 102 tri->verts[ 0 ].SetTangent( 0.0f, 1.0f, 0.0f ); 103 tri->verts[ 0 ].SetBiTangent( 0.0f, 0.0f, 1.0f ); 104 tri->verts[ 0 ].SetTexCoord( 0.0f, 0.0f ); 105 106 tri->verts[ 1 ].Clear(); 107 tri->verts[ 1 ].SetNormal( 1.0f, 0.0f, 0.0f ); 108 tri->verts[ 1 ].SetTangent( 0.0f, 1.0f, 0.0f ); 109 tri->verts[ 1 ].SetBiTangent( 0.0f, 0.0f, 1.0f ); 110 tri->verts[ 1 ].SetTexCoord( 1.0f, 0.0f ); 111 112 tri->verts[ 2 ].Clear(); 113 tri->verts[ 2 ].SetNormal( 1.0f, 0.0f, 0.0f ); 114 tri->verts[ 2 ].SetTangent( 0.0f, 1.0f, 0.0f ); 115 tri->verts[ 2 ].SetBiTangent( 0.0f, 0.0f, 1.0f ); 116 tri->verts[ 2 ].SetTexCoord( 1.0f, 1.0f ); 117 118 tri->verts[ 3 ].Clear(); 119 tri->verts[ 3 ].SetNormal( 1.0f, 0.0f, 0.0f ); 120 tri->verts[ 3 ].SetTangent( 0.0f, 1.0f, 0.0f ); 121 tri->verts[ 3 ].SetBiTangent( 0.0f, 0.0f, 1.0f ); 122 tri->verts[ 3 ].SetTexCoord( 0.0f, 1.0f ); 123 124 tri->indexes[ 0 ] = 0; 125 tri->indexes[ 1 ] = 1; 126 tri->indexes[ 2 ] = 3; 127 tri->indexes[ 3 ] = 1; 128 tri->indexes[ 4 ] = 2; 129 tri->indexes[ 5 ] = 3; 130 131 tri->numVerts = 4; 132 tri->numIndexes = 6; 133 134 surf.geometry = tri; 135 surf.id = 0; 136 surf.shader = tr.defaultMaterial; 137 staticModel->AddSurface( surf ); 138 } 139 140 int red = idMath::Ftoi( renderEntity->shaderParms[ SHADERPARM_RED ] * 255.0f ); 141 int green = idMath::Ftoi( renderEntity->shaderParms[ SHADERPARM_GREEN ] * 255.0f ); 142 int blue = idMath::Ftoi( renderEntity->shaderParms[ SHADERPARM_BLUE ] * 255.0f ); 143 int alpha = idMath::Ftoi( renderEntity->shaderParms[ SHADERPARM_ALPHA ] * 255.0f ); 144 145 idVec3 right = idVec3( 0.0f, renderEntity->shaderParms[ SHADERPARM_SPRITE_WIDTH ] * 0.5f, 0.0f ); 146 idVec3 up = idVec3( 0.0f, 0.0f, renderEntity->shaderParms[ SHADERPARM_SPRITE_HEIGHT ] * 0.5f ); 147 148 tri->verts[ 0 ].xyz = up + right; 149 tri->verts[ 0 ].color[ 0 ] = red; 150 tri->verts[ 0 ].color[ 1 ] = green; 151 tri->verts[ 0 ].color[ 2 ] = blue; 152 tri->verts[ 0 ].color[ 3 ] = alpha; 153 154 tri->verts[ 1 ].xyz = up - right; 155 tri->verts[ 1 ].color[ 0 ] = red; 156 tri->verts[ 1 ].color[ 1 ] = green; 157 tri->verts[ 1 ].color[ 2 ] = blue; 158 tri->verts[ 1 ].color[ 3 ] = alpha; 159 160 tri->verts[ 2 ].xyz = - right - up; 161 tri->verts[ 2 ].color[ 0 ] = red; 162 tri->verts[ 2 ].color[ 1 ] = green; 163 tri->verts[ 2 ].color[ 2 ] = blue; 164 tri->verts[ 2 ].color[ 3 ] = alpha; 165 166 tri->verts[ 3 ].xyz = right - up; 167 tri->verts[ 3 ].color[ 0 ] = red; 168 tri->verts[ 3 ].color[ 1 ] = green; 169 tri->verts[ 3 ].color[ 2 ] = blue; 170 tri->verts[ 3 ].color[ 3 ] = alpha; 171 172 R_BoundTriSurf( tri ); 173 174 staticModel->bounds = tri->bounds; 175 176 return staticModel; 177 } 178 179 /* 180 =============== 181 idRenderModelSprite::Bounds 182 =============== 183 */ 184 idBounds idRenderModelSprite::Bounds( const struct renderEntity_s *renderEntity ) const { 185 idBounds b; 186 187 b.Zero(); 188 if ( renderEntity == NULL ) { 189 b.ExpandSelf( 8.0f ); 190 } else { 191 b.ExpandSelf( Max( renderEntity->shaderParms[ SHADERPARM_SPRITE_WIDTH ], renderEntity->shaderParms[ SHADERPARM_SPRITE_HEIGHT ] ) * 0.5f ); 192 } 193 return b; 194 }