Debian Codegraph @

code/renderergl1/tr_marks.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_marks.c -- polygon projection on the world polygons
23
24 #include "tr_local.h"
25 //#include "assert.h"
26
27 #define MAX_VERTS_ON_POLY 64
28
29 #define MARKER_OFFSET 0 // 1
30
31 /*
32 =============
33 R_ChopPolyBehindPlane
34
35 Out must have space for two more vertexes than in
36 =============
37 */
38 #define SIDE_FRONT 0
39 #define SIDE_BACK 1
40 #define SIDE_ON 2
41 static void R_ChopPolyBehindPlane( int numInPoints, vec3_t inPoints[MAX_VERTS_ON_POLY],
42 int *numOutPoints, vec3_t outPoints[MAX_VERTS_ON_POLY],
43 vec3_t normal, vec_t dist, vec_t epsilon) {
44 float dists[MAX_VERTS_ON_POLY+4] = { 0 };
45 int sides[MAX_VERTS_ON_POLY+4] = { 0 };
46 int counts[3];
47 float dot;
48 int i, j;
49 float *p1, *p2, *clip;
50 float d;
51
52 // don't clip if it might overflow
53 if ( numInPoints >= MAX_VERTS_ON_POLY - 2 ) {
54 *numOutPoints = 0;
55 return;
56 }
57
58 counts[0] = counts[1] = counts[2] = 0;
59
60 // determine sides for each point
61 for ( i = 0 ; i < numInPoints ; i++ ) {
62 dot = DotProduct( inPoints[i], normal );
63 dot -= dist;
64 dists[i] = dot;
65 if ( dot > epsilon ) {
66 sides[i] = SIDE_FRONT;
67 } else if ( dot < -epsilon ) {
68 sides[i] = SIDE_BACK;
69 } else {
70 sides[i] = SIDE_ON;
71 }
72 counts[sides[i]]++;
73 }
74 sides[i] = sides[0];
75 dists[i] = dists[0];
76
77 *numOutPoints = 0;
78
79 if ( !counts[0] ) {
80 return;
81 }
82 if ( !counts[1] ) {
83 *numOutPoints = numInPoints;
84 Com_Memcpy( outPoints, inPoints, numInPoints * sizeof(vec3_t) );
85 return;
86 }
87
88 for ( i = 0 ; i < numInPoints ; i++ ) {
89 p1 = inPoints[i];
90 clip = outPoints[ *numOutPoints ];
91
92 if ( sides[i] == SIDE_ON ) {
93 VectorCopy( p1, clip );
94 (*numOutPoints)++;
95 continue;
96 }
97
98 if ( sides[i] == SIDE_FRONT ) {
99 VectorCopy( p1, clip );
100 (*numOutPoints)++;
101 clip = outPoints[ *numOutPoints ];
102 }
103
104 if ( sides[i+1] == SIDE_ON || sides[i+1] == sides[i] ) {
105 continue;
106 }
107
108 // generate a split point
109 p2 = inPoints[ (i+1) % numInPoints ];
110
111 d = dists[i] - dists[i+1];
112 if ( d == 0 ) {
113 dot = 0;
114 } else {
115 dot = dists[i] / d;
116 }
117
118 // clip xyz
119
120 for (j=0 ; j<3 ; j++) {
121 clip[j] = p1[j] + dot * ( p2[j] - p1[j] );
122 }
123
124 (*numOutPoints)++;
125 }
126 }
127
128 /*
129 =================
130 R_BoxSurfaces_r
131
132 =================
133 */
134 void R_BoxSurfaces_r(mnode_t *node, vec3_t mins, vec3_t maxs, surfaceType_t **list, int listsize, int *listlength, vec3_t dir) {
135
136 int s, c;
137 msurface_t *surf, **mark;
138
139 // do the tail recursion in a loop
140 while ( node->contents == -1 ) {
141 s = BoxOnPlaneSide( mins, maxs, node->plane );
142 if (s == 1) {
143 node = node->children[0];
144 } else if (s == 2) {
145 node = node->children[1];
146 } else {
147 R_BoxSurfaces_r(node->children[0], mins, maxs, list, listsize, listlength, dir);
148 node = node->children[1];
149 }
150 }
151
152 // add the individual surfaces
153 mark = node->firstmarksurface;
154 c = node->nummarksurfaces;
155 while (c--) {
156 //
157 if (*listlength >= listsize) break;
158 //
159 surf = *mark;
160 // check if the surface has NOIMPACT or NOMARKS set
161 if ( ( surf->shader->surfaceFlags & ( SURF_NOIMPACT | SURF_NOMARKS ) )
162 || ( surf->shader->contentFlags & CONTENTS_FOG ) ) {
163 surf->viewCount = tr.viewCount;
164 }
165 // extra check for surfaces to avoid list overflows
166 else if (*(surf->data) == SF_FACE) {
167 // the face plane should go through the box
168 s = BoxOnPlaneSide( mins, maxs, &(( srfSurfaceFace_t * ) surf->data)->plane );
169 if (s == 1 || s == 2) {
170 surf->viewCount = tr.viewCount;
171 } else if (DotProduct((( srfSurfaceFace_t * ) surf->data)->plane.normal, dir) > -0.5) {
172 // don't add faces that make sharp angles with the projection direction
173 surf->viewCount = tr.viewCount;
174 }
175 }
176 else if (*(surfaceType_t *) (surf->data) != SF_GRID &&
177 *(surfaceType_t *) (surf->data) != SF_TRIANGLES)
178 surf->viewCount = tr.viewCount;
179 // check the viewCount because the surface may have
180 // already been added if it spans multiple leafs
181 if (surf->viewCount != tr.viewCount) {
182 surf->viewCount = tr.viewCount;
183 list[*listlength] = (surfaceType_t *) surf->data;
184 (*listlength)++;
185 }
186 mark++;
187 }
188 }
189
190 /*
191 =================
192 R_AddMarkFragments
193
194 =================
195 */
196 void R_AddMarkFragments(int numClipPoints, vec3_t clipPoints[2][MAX_VERTS_ON_POLY],
197 int numPlanes, vec3_t *normals, float *dists,
198 int maxPoints, vec3_t pointBuffer,
199 int maxFragments, markFragment_t *fragmentBuffer,
200 int *returnedPoints, int *returnedFragments,
201 vec3_t mins, vec3_t maxs) {
202 int pingPong, i;
203 markFragment_t *mf;
204
205 // chop the surface by all the bounding planes of the to be projected polygon
206 pingPong = 0;
207
208 for ( i = 0 ; i < numPlanes ; i++ ) {
209
210 R_ChopPolyBehindPlane( numClipPoints, clipPoints[pingPong],
211 &numClipPoints, clipPoints[!pingPong],
212 normals[i], dists[i], 0.5 );
213 pingPong ^= 1;
214 if ( numClipPoints == 0 ) {
215 break;
216 }
217 }
218 // completely clipped away?
219 if ( numClipPoints == 0 ) {
220 return;
221 }
222
223 // add this fragment to the returned list
224 if ( numClipPoints + (*returnedPoints) > maxPoints ) {
225 return; // not enough space for this polygon
226 }
227 /*
228 // all the clip points should be within the bounding box
229 for ( i = 0 ; i < numClipPoints ; i++ ) {
230 int j;
231 for ( j = 0 ; j < 3 ; j++ ) {
232 if (clipPoints[pingPong][i][j] < mins[j] - 0.5) break;
233 if (clipPoints[pingPong][i][j] > maxs[j] + 0.5) break;
234 }
235 if (j < 3) break;
236 }
237 if (i < numClipPoints) return;
238 */
239
240 mf = fragmentBuffer + (*returnedFragments);
241 mf->firstPoint = (*returnedPoints);
242 mf->numPoints = numClipPoints;
243 Com_Memcpy( pointBuffer + (*returnedPoints) * 3, clipPoints[pingPong], numClipPoints * sizeof(vec3_t) );
244
245 (*returnedPoints) += numClipPoints;
246 (*returnedFragments)++;
247 }
248
249 /*
250 =================
251 R_MarkFragments
252
253 =================
254 */
255 int R_MarkFragments( int numPoints, const vec3_t *points, const vec3_t projection,
256 int maxPoints, vec3_t pointBuffer, int maxFragments, markFragment_t *fragmentBuffer ) {
257 int numsurfaces, numPlanes;
258 int i, j, k, m, n;
259 surfaceType_t *surfaces[64];
260 vec3_t mins, maxs;
261 int returnedFragments;
262 int returnedPoints;
263 vec3_t normals[MAX_VERTS_ON_POLY+2];
264 float dists[MAX_VERTS_ON_POLY+2];
265 vec3_t clipPoints[2][MAX_VERTS_ON_POLY];
266 int numClipPoints;
267 float *v;
268 srfGridMesh_t *cv;
269 drawVert_t *dv;
270 vec3_t normal;
271 vec3_t projectionDir;
272 vec3_t v1, v2;
273 int *indexes;
274
275 if (numPoints <= 0) {
276 return 0;
277 }
278
279 //increment view count for double check prevention
280 tr.viewCount++;
281
282 //
283 VectorNormalize2( projection, projectionDir );
284 // find all the brushes that are to be considered
285 ClearBounds( mins, maxs );
286 for ( i = 0 ; i < numPoints ; i++ ) {
287 vec3_t temp;
288
289 AddPointToBounds( points[i], mins, maxs );
290 VectorAdd( points[i], projection, temp );
291 AddPointToBounds( temp, mins, maxs );
292 // make sure we get all the leafs (also the one(s) in front of the hit surface)
293 VectorMA( points[i], -20, projectionDir, temp );
294 AddPointToBounds( temp, mins, maxs );
295 }
296
297 if (numPoints > MAX_VERTS_ON_POLY) numPoints = MAX_VERTS_ON_POLY;
298 // create the bounding planes for the to be projected polygon
299 for ( i = 0 ; i < numPoints ; i++ ) {
300 VectorSubtract(points[(i+1)%numPoints], points[i], v1);
301 VectorAdd(points[i], projection, v2);
302 VectorSubtract(points[i], v2, v2);
303 CrossProduct(v1, v2, normals[i]);
304 VectorNormalizeFast(normals[i]);
305 dists[i] = DotProduct(normals[i], points[i]);
306 }
307 // add near and far clipping planes for projection
308 VectorCopy(projectionDir, normals[numPoints]);
309 dists[numPoints] = DotProduct(normals[numPoints], points[0]) - 32;
310 VectorCopy(projectionDir, normals[numPoints+1]);
311 VectorInverse(normals[numPoints+1]);
312 dists[numPoints+1] = DotProduct(normals[numPoints+1], points[0]) - 20;
313 numPlanes = numPoints + 2;
314
315 numsurfaces = 0;
316 R_BoxSurfaces_r(tr.world->nodes, mins, maxs, surfaces, 64, &numsurfaces, projectionDir);
317 //assert(numsurfaces <= 64);
318 //assert(numsurfaces != 64);
319
320 returnedPoints = 0;
321 returnedFragments = 0;
322
323 for ( i = 0 ; i < numsurfaces ; i++ ) {
324
325 if (*surfaces[i] == SF_GRID) {
326
327 cv = (srfGridMesh_t *) surfaces[i];
328 for ( m = 0 ; m < cv->height - 1 ; m++ ) {
329 for ( n = 0 ; n < cv->width - 1 ; n++ ) {
330 // We triangulate the grid and chop all triangles within
331 // the bounding planes of the to be projected polygon.
332 // LOD is not taken into account, not such a big deal though.
333 //
334 // It's probably much nicer to chop the grid itself and deal
335 // with this grid as a normal SF_GRID surface so LOD will
336 // be applied. However the LOD of that chopped grid must
337 // be synced with the LOD of the original curve.
338 // One way to do this; the chopped grid shares vertices with
339 // the original curve. When LOD is applied to the original
340 // curve the unused vertices are flagged. Now the chopped curve
341 // should skip the flagged vertices. This still leaves the
342 // problems with the vertices at the chopped grid edges.
343 //
344 // To avoid issues when LOD applied to "hollow curves" (like
345 // the ones around many jump pads) we now just add a 2 unit
346 // offset to the triangle vertices.
347 // The offset is added in the vertex normal vector direction
348 // so all triangles will still fit together.
349 // The 2 unit offset should avoid pretty much all LOD problems.
350
351 numClipPoints = 3;
352
353 dv = cv->verts + m * cv->width + n;
354
355 VectorCopy(dv[0].xyz, clipPoints[0][0]);
356 VectorMA(clipPoints[0][0], MARKER_OFFSET, dv[0].normal, clipPoints[0][0]);
357 VectorCopy(dv[cv->width].xyz, clipPoints[0][1]);
358 VectorMA(clipPoints[0][1], MARKER_OFFSET, dv[cv->width].normal, clipPoints[0][1]);
359 VectorCopy(dv[1].xyz, clipPoints[0][2]);
360 VectorMA(clipPoints[0][2], MARKER_OFFSET, dv[1].normal, clipPoints[0][2]);
361 // check the normal of this triangle
362 VectorSubtract(clipPoints[0][0], clipPoints[0][1], v1);
363 VectorSubtract(clipPoints[0][2], clipPoints[0][1], v2);
364 CrossProduct(v1, v2, normal);
365 VectorNormalizeFast(normal);
366 if (DotProduct(normal, projectionDir) < -0.1) {
367 // add the fragments of this triangle
368 R_AddMarkFragments(numClipPoints, clipPoints,
369 numPlanes, normals, dists,
370 maxPoints, pointBuffer,
371 maxFragments, fragmentBuffer,
372 &returnedPoints, &returnedFragments, mins, maxs);
373
374 if ( returnedFragments == maxFragments ) {
375 return returnedFragments; // not enough space for more fragments
376 }
377 }
378
379 VectorCopy(dv[1].xyz, clipPoints[0][0]);
380 VectorMA(clipPoints[0][0], MARKER_OFFSET, dv[1].normal, clipPoints[0][0]);
381 VectorCopy(dv[cv->width].xyz, clipPoints[0][1]);
382 VectorMA(clipPoints[0][1], MARKER_OFFSET, dv[cv->width].normal, clipPoints[0][1]);
383 VectorCopy(dv[cv->width+1].xyz, clipPoints[0][2]);
384 VectorMA(clipPoints[0][2], MARKER_OFFSET, dv[cv->width+1].normal, clipPoints[0][2]);
385 // check the normal of this triangle
386 VectorSubtract(clipPoints[0][0], clipPoints[0][1], v1);
387 VectorSubtract(clipPoints[0][2], clipPoints[0][1], v2);
388 CrossProduct(v1, v2, normal);
389 VectorNormalizeFast(normal);
390 if (DotProduct(normal, projectionDir) < -0.05) {
391 // add the fragments of this triangle
392 R_AddMarkFragments(numClipPoints, clipPoints,
393 numPlanes, normals, dists,
394 maxPoints, pointBuffer,
395 maxFragments, fragmentBuffer,
396 &returnedPoints, &returnedFragments, mins, maxs);
397
398 if ( returnedFragments == maxFragments ) {
399 return returnedFragments; // not enough space for more fragments
400 }
401 }
402 }
403 }
404 }
405 else if (*surfaces[i] == SF_FACE) {
406
407 srfSurfaceFace_t *surf = ( srfSurfaceFace_t * ) surfaces[i];
408
409 // check the normal of this face
410 if (DotProduct(surf->plane.normal, projectionDir) > -0.5) {
411 continue;
412 }
413
414 indexes = (int *)( (byte *)surf + surf->ofsIndices );
415 for ( k = 0 ; k < surf->numIndices ; k += 3 ) {
416 for ( j = 0 ; j < 3 ; j++ ) {
417 v = &surf->points[0][0] + VERTEXSIZE * indexes[k+j];
418 VectorMA( v, MARKER_OFFSET, surf->plane.normal, clipPoints[0][j] );
419 }
420
421 // add the fragments of this face
422 R_AddMarkFragments( 3 , clipPoints,
423 numPlanes, normals, dists,
424 maxPoints, pointBuffer,
425 maxFragments, fragmentBuffer,
426 &returnedPoints, &returnedFragments, mins, maxs);
427 if ( returnedFragments == maxFragments ) {
428 return returnedFragments; // not enough space for more fragments
429 }
430 }
431 }
432 else if(*surfaces[i] == SF_TRIANGLES && r_marksOnTriangleMeshes->integer) {
433
434 srfTriangles_t *surf = (srfTriangles_t *) surfaces[i];
435
436 for (k = 0; k < surf->numIndexes; k += 3)
437 {
438 for(j = 0; j < 3; j++)
439 {
440 v = surf->verts[surf->indexes[k + j]].xyz;
441 VectorMA(v, MARKER_OFFSET, surf->verts[surf->indexes[k + j]].normal, clipPoints[0][j]);
442 }
443
444 // add the fragments of this face
445 R_AddMarkFragments(3, clipPoints,
446 numPlanes, normals, dists,
447 maxPoints, pointBuffer,
448 maxFragments, fragmentBuffer, &returnedPoints, &returnedFragments, mins, maxs);
449 if(returnedFragments == maxFragments)
450 {
451 return returnedFragments; // not enough space for more fragments
452 }
453 }
454 }
455 }
456 return returnedFragments;
457 }
458