Debian Codegraph @

code/renderergl1/tr_mesh.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 // tr_mesh.c: triangle model functions
23
24 #include "tr_local.h"
25
26 static float ProjectRadius( float r, vec3_t location )
27 {
28 float pr;
29 float dist;
30 float c;
31 vec3_t p;
32 float projected[4];
33
34 c = DotProduct( tr.viewParms.or.axis[0], tr.viewParms.or.origin );
35 dist = DotProduct( tr.viewParms.or.axis[0], location ) - c;
36
37 if ( dist <= 0 )
38 return 0;
39
40 p[0] = 0;
41 p[1] = fabs( r );
42 p[2] = -dist;
43
44 projected[0] = p[0] * tr.viewParms.projectionMatrix[0] +
45 p[1] * tr.viewParms.projectionMatrix[4] +
46 p[2] * tr.viewParms.projectionMatrix[8] +
47 tr.viewParms.projectionMatrix[12];
48
49 projected[1] = p[0] * tr.viewParms.projectionMatrix[1] +
50 p[1] * tr.viewParms.projectionMatrix[5] +
51 p[2] * tr.viewParms.projectionMatrix[9] +
52 tr.viewParms.projectionMatrix[13];
53
54 projected[2] = p[0] * tr.viewParms.projectionMatrix[2] +
55 p[1] * tr.viewParms.projectionMatrix[6] +
56 p[2] * tr.viewParms.projectionMatrix[10] +
57 tr.viewParms.projectionMatrix[14];
58
59 projected[3] = p[0] * tr.viewParms.projectionMatrix[3] +
60 p[1] * tr.viewParms.projectionMatrix[7] +
61 p[2] * tr.viewParms.projectionMatrix[11] +
62 tr.viewParms.projectionMatrix[15];
63
64
65 pr = projected[1] / projected[3];
66
67 if ( pr > 1.0f )
68 pr = 1.0f;
69
70 return pr;
71 }
72
73 /*
74 =============
75 R_CullModel
76 =============
77 */
78 static int R_CullModel( md3Header_t *header, trRefEntity_t *ent ) {
79 vec3_t bounds[2];
80 md3Frame_t *oldFrame, *newFrame;
81 int i;
82
83 // compute frame pointers
84 newFrame = ( md3Frame_t * ) ( ( byte * ) header + header->ofsFrames ) + ent->e.frame;
85 oldFrame = ( md3Frame_t * ) ( ( byte * ) header + header->ofsFrames ) + ent->e.oldframe;
86
87 // cull bounding sphere ONLY if this is not an upscaled entity
88 if ( !ent->e.nonNormalizedAxes )
89 {
90 if ( ent->e.frame == ent->e.oldframe )
91 {
92 switch ( R_CullLocalPointAndRadius( newFrame->localOrigin, newFrame->radius ) )
93 {
94 case CULL_OUT:
95 tr.pc.c_sphere_cull_md3_out++;
96 return CULL_OUT;
97
98 case CULL_IN:
99 tr.pc.c_sphere_cull_md3_in++;
100 return CULL_IN;
101
102 case CULL_CLIP:
103 tr.pc.c_sphere_cull_md3_clip++;
104 break;
105 }
106 }
107 else
108 {
109 int sphereCull, sphereCullB;
110
111 sphereCull = R_CullLocalPointAndRadius( newFrame->localOrigin, newFrame->radius );
112 if ( newFrame == oldFrame ) {
113 sphereCullB = sphereCull;
114 } else {
115 sphereCullB = R_CullLocalPointAndRadius( oldFrame->localOrigin, oldFrame->radius );
116 }
117
118 if ( sphereCull == sphereCullB )
119 {
120 if ( sphereCull == CULL_OUT )
121 {
122 tr.pc.c_sphere_cull_md3_out++;
123 return CULL_OUT;
124 }
125 else if ( sphereCull == CULL_IN )
126 {
127 tr.pc.c_sphere_cull_md3_in++;
128 return CULL_IN;
129 }
130 else
131 {
132 tr.pc.c_sphere_cull_md3_clip++;
133 }
134 }
135 }
136 }
137
138 // calculate a bounding box in the current coordinate system
139 for (i = 0 ; i < 3 ; i++) {
140 bounds[0][i] = oldFrame->bounds[0][i] < newFrame->bounds[0][i] ? oldFrame->bounds[0][i] : newFrame->bounds[0][i];
141 bounds[1][i] = oldFrame->bounds[1][i] > newFrame->bounds[1][i] ? oldFrame->bounds[1][i] : newFrame->bounds[1][i];
142 }
143
144 switch ( R_CullLocalBox( bounds ) )
145 {
146 case CULL_IN:
147 tr.pc.c_box_cull_md3_in++;
148 return CULL_IN;
149 case CULL_CLIP:
150 tr.pc.c_box_cull_md3_clip++;
151 return CULL_CLIP;
152 case CULL_OUT:
153 default:
154 tr.pc.c_box_cull_md3_out++;
155 return CULL_OUT;
156 }
157 }
158
159
160 /*
161 =================
162 R_ComputeLOD
163
164 =================
165 */
166 int R_ComputeLOD( trRefEntity_t *ent ) {
167 float radius;
168 float flod, lodscale;
169 float projectedRadius;
170 md3Frame_t *frame;
171 mdrHeader_t *mdr;
172 mdrFrame_t *mdrframe;
173 int lod;
174
175 if ( tr.currentModel->numLods < 2 )
176 {
177 // model has only 1 LOD level, skip computations and bias
178 lod = 0;
179 }
180 else
181 {
182 // multiple LODs exist, so compute projected bounding sphere
183 // and use that as a criteria for selecting LOD
184
185 if(tr.currentModel->type == MOD_MDR)
186 {
187 int frameSize;
188 mdr = (mdrHeader_t *) tr.currentModel->modelData;
189 frameSize = (size_t) (&((mdrFrame_t *)0)->bones[mdr->numBones]);
190
191 mdrframe = (mdrFrame_t *) ((byte *) mdr + mdr->ofsFrames + frameSize * ent->e.frame);
192
193 radius = RadiusFromBounds(mdrframe->bounds[0], mdrframe->bounds[1]);
194 }
195 else
196 {
197 frame = ( md3Frame_t * ) ( ( ( unsigned char * ) tr.currentModel->md3[0] ) + tr.currentModel->md3[0]->ofsFrames );
198
199 frame += ent->e.frame;
200
201 radius = RadiusFromBounds( frame->bounds[0], frame->bounds[1] );
202 }
203
204 if ( ( projectedRadius = ProjectRadius( radius, ent->e.origin ) ) != 0 )
205 {
206 lodscale = r_lodscale->value;
207 if (lodscale > 20) lodscale = 20;
208 flod = 1.0f - projectedRadius * lodscale;
209 }
210 else
211 {
212 // object intersects near view plane, e.g. view weapon
213 flod = 0;
214 }
215
216 flod *= tr.currentModel->numLods;
217 lod = ri.ftol(flod);
218
219 if ( lod < 0 )
220 {
221 lod = 0;
222 }
223 else if ( lod >= tr.currentModel->numLods )
224 {
225 lod = tr.currentModel->numLods - 1;
226 }
227 }
228
229 lod += r_lodbias->integer;
230
231 if ( lod >= tr.currentModel->numLods )
232 lod = tr.currentModel->numLods - 1;
233 if ( lod < 0 )
234 lod = 0;
235
236 return lod;
237 }
238
239 /*
240 =================
241 R_ComputeFogNum
242
243 =================
244 */
245 int R_ComputeFogNum( md3Header_t *header, trRefEntity_t *ent ) {
246 int i, j;
247 fog_t *fog;
248 md3Frame_t *md3Frame;
249 vec3_t localOrigin;
250
251 if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) {
252 return 0;
253 }
254
255 // FIXME: non-normalized axis issues
256 md3Frame = ( md3Frame_t * ) ( ( byte * ) header + header->ofsFrames ) + ent->e.frame;
257 VectorAdd( ent->e.origin, md3Frame->localOrigin, localOrigin );
258 for ( i = 1 ; i < tr.world->numfogs ; i++ ) {
259 fog = &tr.world->fogs[i];
260 for ( j = 0 ; j < 3 ; j++ ) {
261 if ( localOrigin[j] - md3Frame->radius >= fog->bounds[1][j] ) {
262 break;
263 }
264 if ( localOrigin[j] + md3Frame->radius <= fog->bounds[0][j] ) {
265 break;
266 }
267 }
268 if ( j == 3 ) {
269 return i;
270 }
271 }
272
273 return 0;
274 }
275
276 /*
277 =================
278 R_AddMD3Surfaces
279
280 =================
281 */
282 void R_AddMD3Surfaces( trRefEntity_t *ent ) {
283 int i;
284 md3Header_t *header = NULL;
285 md3Surface_t *surface = NULL;
286 md3Shader_t *md3Shader = NULL;
287 shader_t *shader = NULL;
288 int cull;
289 int lod;
290 int fogNum;
291 qboolean personalModel;
292
293 // don't add third_person objects if not in a portal
294 personalModel = (ent->e.renderfx & RF_THIRD_PERSON) && !tr.viewParms.isPortal;
295
296 if ( ent->e.renderfx & RF_WRAP_FRAMES ) {
297 ent->e.frame %= tr.currentModel->md3[0]->numFrames;
298 ent->e.oldframe %= tr.currentModel->md3[0]->numFrames;
299 }
300
301 //
302 // Validate the frames so there is no chance of a crash.
303 // This will write directly into the entity structure, so
304 // when the surfaces are rendered, they don't need to be
305 // range checked again.
306 //
307 if ( (ent->e.frame >= tr.currentModel->md3[0]->numFrames)
308 || (ent->e.frame < 0)
309 || (ent->e.oldframe >= tr.currentModel->md3[0]->numFrames)
310 || (ent->e.oldframe < 0) ) {
311 ri.Printf( PRINT_DEVELOPER, "R_AddMD3Surfaces: no such frame %d to %d for '%s'\n",
312 ent->e.oldframe, ent->e.frame,
313 tr.currentModel->name );
314 ent->e.frame = 0;
315 ent->e.oldframe = 0;
316 }
317
318 //
319 // compute LOD
320 //
321 lod = R_ComputeLOD( ent );
322
323 header = tr.currentModel->md3[lod];
324
325 //
326 // cull the entire model if merged bounding box of both frames
327 // is outside the view frustum.
328 //
329 cull = R_CullModel ( header, ent );
330 if ( cull == CULL_OUT ) {
331 return;
332 }
333
334 //
335 // set up lighting now that we know we aren't culled
336 //
337 if ( !personalModel || r_shadows->integer > 1 ) {
338 R_SetupEntityLighting( &tr.refdef, ent );
339 }
340
341 //
342 // see if we are in a fog volume
343 //
344 fogNum = R_ComputeFogNum( header, ent );
345
346 //
347 // draw all surfaces
348 //
349 surface = (md3Surface_t *)( (byte *)header + header->ofsSurfaces );
350 for ( i = 0 ; i < header->numSurfaces ; i++ ) {
351
352 if ( ent->e.customShader ) {
353 shader = R_GetShaderByHandle( ent->e.customShader );
354 } else if ( ent->e.customSkin > 0 && ent->e.customSkin < tr.numSkins ) {
355 skin_t *skin;
356 int j;
357
358 skin = R_GetSkinByHandle( ent->e.customSkin );
359
360 // match the surface name to something in the skin file
361 shader = tr.defaultShader;
362 for ( j = 0 ; j < skin->numSurfaces ; j++ ) {
363 // the names have both been lowercased
364 if ( !strcmp( skin->surfaces[j].name, surface->name ) ) {
365 shader = skin->surfaces[j].shader;
366 break;
367 }
368 }
369 if (shader == tr.defaultShader) {
370 ri.Printf( PRINT_DEVELOPER, "WARNING: no shader for surface %s in skin %s\n", surface->name, skin->name);
371 }
372 else if (shader->defaultShader) {
373 ri.Printf( PRINT_DEVELOPER, "WARNING: shader %s in skin %s not found\n", shader->name, skin->name);
374 }
375 } else if ( surface->numShaders <= 0 ) {
376 shader = tr.defaultShader;
377 } else {
378 md3Shader = (md3Shader_t *) ( (byte *)surface + surface->ofsShaders );
379 md3Shader += ent->e.skinNum % surface->numShaders;
380 shader = tr.shaders[ md3Shader->shaderIndex ];
381 }
382
383
384 // we will add shadows even if the main object isn't visible in the view
385
386 // stencil shadows can't do personal models unless I polyhedron clip
387 if ( !personalModel
388 && r_shadows->integer == 2
389 && fogNum == 0
390 && !(ent->e.renderfx & ( RF_NOSHADOW | RF_DEPTHHACK ) )
391 && shader->sort == SS_OPAQUE ) {
392 R_AddDrawSurf( (void *)surface, tr.shadowShader, 0, qfalse );
393 }
394
395 // projection shadows work fine with personal models
396 if ( r_shadows->integer == 3
397 && fogNum == 0
398 && (ent->e.renderfx & RF_SHADOW_PLANE )
399 && shader->sort == SS_OPAQUE ) {
400 R_AddDrawSurf( (void *)surface, tr.projectionShadowShader, 0, qfalse );
401 }
402
403 // don't add third_person objects if not viewing through a portal
404 if ( !personalModel ) {
405 R_AddDrawSurf( (void *)surface, shader, fogNum, qfalse );
406 }
407
408 surface = (md3Surface_t *)( (byte *)surface + surface->ofsEnd );
409 }
410
411 }
412