Debian Codegraph @

code/renderergl2/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( mdvModel_t *model, trRefEntity_t *ent ) {
79 vec3_t bounds[2];
80 mdvFrame_t *oldFrame, *newFrame;
81 int i;
82
83 // compute frame pointers
84 newFrame = model->frames + ent->e.frame;
85 oldFrame = model->frames + 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 mdvFrame_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 frame = tr.currentModel->mdv[0]->frames;
199
200 frame += ent->e.frame;
201
202 radius = RadiusFromBounds( frame->bounds[0], frame->bounds[1] );
203 }
204
205 if ( ( projectedRadius = ProjectRadius( radius, ent->e.origin ) ) != 0 )
206 {
207 lodscale = r_lodscale->value;
208 if (lodscale > 20) lodscale = 20;
209 flod = 1.0f - projectedRadius * lodscale;
210 }
211 else
212 {
213 // object intersects near view plane, e.g. view weapon
214 flod = 0;
215 }
216
217 flod *= tr.currentModel->numLods;
218 lod = ri.ftol(flod);
219
220 if ( lod < 0 )
221 {
222 lod = 0;
223 }
224 else if ( lod >= tr.currentModel->numLods )
225 {
226 lod = tr.currentModel->numLods - 1;
227 }
228 }
229
230 lod += r_lodbias->integer;
231
232 if ( lod >= tr.currentModel->numLods )
233 lod = tr.currentModel->numLods - 1;
234 if ( lod < 0 )
235 lod = 0;
236
237 return lod;
238 }
239
240 /*
241 =================
242 R_ComputeFogNum
243
244 =================
245 */
246 int R_ComputeFogNum( mdvModel_t *model, trRefEntity_t *ent ) {
247 int i, j;
248 fog_t *fog;
249 mdvFrame_t *mdvFrame;
250 vec3_t localOrigin;
251
252 if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) {
253 return 0;
254 }
255
256 // FIXME: non-normalized axis issues
257 mdvFrame = model->frames + ent->e.frame;
258 VectorAdd( ent->e.origin, mdvFrame->localOrigin, localOrigin );
259 for ( i = 1 ; i < tr.world->numfogs ; i++ ) {
260 fog = &tr.world->fogs[i];
261 for ( j = 0 ; j < 3 ; j++ ) {
262 if ( localOrigin[j] - mdvFrame->radius >= fog->bounds[1][j] ) {
263 break;
264 }
265 if ( localOrigin[j] + mdvFrame->radius <= fog->bounds[0][j] ) {
266 break;
267 }
268 }
269 if ( j == 3 ) {
270 return i;
271 }
272 }
273
274 return 0;
275 }
276
277 /*
278 =================
279 R_AddMD3Surfaces
280
281 =================
282 */
283 void R_AddMD3Surfaces( trRefEntity_t *ent ) {
284 int i;
285 mdvModel_t *model;
286 mdvSurface_t *surface;
287 void *drawSurf;
288 shader_t *shader;
289 int cull;
290 int lod;
291 int fogNum;
292 int cubemapIndex;
293 qboolean personalModel;
294
295 // don't add third_person objects if not in a portal
296 personalModel = (ent->e.renderfx & RF_THIRD_PERSON) && !(tr.viewParms.isPortal
297 || (tr.viewParms.flags & (VPF_SHADOWMAP | VPF_DEPTHSHADOW)));
298
299 if ( ent->e.renderfx & RF_WRAP_FRAMES ) {
300 ent->e.frame %= tr.currentModel->mdv[0]->numFrames;
301 ent->e.oldframe %= tr.currentModel->mdv[0]->numFrames;
302 }
303
304 //
305 // Validate the frames so there is no chance of a crash.
306 // This will write directly into the entity structure, so
307 // when the surfaces are rendered, they don't need to be
308 // range checked again.
309 //
310 if ( (ent->e.frame >= tr.currentModel->mdv[0]->numFrames)
311 || (ent->e.frame < 0)
312 || (ent->e.oldframe >= tr.currentModel->mdv[0]->numFrames)
313 || (ent->e.oldframe < 0) ) {
314 ri.Printf( PRINT_DEVELOPER, "R_AddMD3Surfaces: no such frame %d to %d for '%s'\n",
315 ent->e.oldframe, ent->e.frame,
316 tr.currentModel->name );
317 ent->e.frame = 0;
318 ent->e.oldframe = 0;
319 }
320
321 //
322 // compute LOD
323 //
324 lod = R_ComputeLOD( ent );
325
326 model = tr.currentModel->mdv[lod];
327
328 //
329 // cull the entire model if merged bounding box of both frames
330 // is outside the view frustum.
331 //
332 cull = R_CullModel ( model, ent );
333 if ( cull == CULL_OUT ) {
334 return;
335 }
336
337 //
338 // set up lighting now that we know we aren't culled
339 //
340 if ( !personalModel || r_shadows->integer > 1 ) {
341 R_SetupEntityLighting( &tr.refdef, ent );
342 }
343
344 //
345 // see if we are in a fog volume
346 //
347 fogNum = R_ComputeFogNum( model, ent );
348
349 cubemapIndex = R_CubemapForPoint(ent->e.origin);
350
351 //
352 // draw all surfaces
353 //
354 surface = model->surfaces;
355 for ( i = 0 ; i < model->numSurfaces ; i++ ) {
356
357 if ( ent->e.customShader ) {
358 shader = R_GetShaderByHandle( ent->e.customShader );
359 } else if ( ent->e.customSkin > 0 && ent->e.customSkin < tr.numSkins ) {
360 skin_t *skin;
361 int j;
362
363 skin = R_GetSkinByHandle( ent->e.customSkin );
364
365 // match the surface name to something in the skin file
366 shader = tr.defaultShader;
367 for ( j = 0 ; j < skin->numSurfaces ; j++ ) {
368 // the names have both been lowercased
369 if ( !strcmp( skin->surfaces[j].name, surface->name ) ) {
370 shader = skin->surfaces[j].shader;
371 break;
372 }
373 }
374 if (shader == tr.defaultShader) {
375 ri.Printf( PRINT_DEVELOPER, "WARNING: no shader for surface %s in skin %s\n", surface->name, skin->name);
376 }
377 else if (shader->defaultShader) {
378 ri.Printf( PRINT_DEVELOPER, "WARNING: shader %s in skin %s not found\n", shader->name, skin->name);
379 }
380 } else if ( surface->numShaderIndexes <= 0 ) {
381 shader = tr.defaultShader;
382 } else {
383 shader = tr.shaders[ surface->shaderIndexes[ ent->e.skinNum % surface->numShaderIndexes ] ];
384 }
385
386 if ( model->numVaoSurfaces > 0 ) {
387 drawSurf = &model->vaoSurfaces[i];
388 } else {
389 drawSurf = surface;
390 }
391
392 // we will add shadows even if the main object isn't visible in the view
393
394 // stencil shadows can't do personal models unless I polyhedron clip
395 if ( !personalModel
396 && r_shadows->integer == 2
397 && fogNum == 0
398 && !(ent->e.renderfx & ( RF_NOSHADOW | RF_DEPTHHACK ) )
399 && shader->sort == SS_OPAQUE ) {
400 R_AddDrawSurf( drawSurf, tr.shadowShader, 0, qfalse, qfalse, 0 );
401 }
402
403 // projection shadows work fine with personal models
404 if ( r_shadows->integer == 3
405 && fogNum == 0
406 && (ent->e.renderfx & RF_SHADOW_PLANE )
407 && shader->sort == SS_OPAQUE ) {
408 R_AddDrawSurf( drawSurf, tr.projectionShadowShader, 0, qfalse, qfalse, 0 );
409 }
410
411 // don't add third_person objects if not viewing through a portal
412 if ( !personalModel ) {
413 R_AddDrawSurf( drawSurf, shader, fogNum, qfalse, qfalse, cubemapIndex );
414 }
415
416 surface++;
417 }
418
419 }
420
421
422
423
424