Debian Codegraph @

code/renderergl2/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;
138 int *mark;
139
140 // do the tail recursion in a loop
141 while ( node->contents == -1 ) {
142 s = BoxOnPlaneSide( mins, maxs, node->plane );
143 if (s == 1) {
144 node = node->children[0];
145 } else if (s == 2) {
146 node = node->children[1];
147 } else {
148 R_BoxSurfaces_r(node->children[0], mins, maxs, list, listsize, listlength, dir);
149 node = node->children[1];
150 }
151 }
152
153 // add the individual surfaces
154 mark = tr.world->marksurfaces + node->firstmarksurface;
155 c = node->nummarksurfaces;
156 while (c--) {
157 int *surfViewCount;
158 //
159 if (*listlength >= listsize) break;
160 //
161 surfViewCount = &tr.world->surfacesViewCount[*mark];
162 surf = tr.world->surfaces + *mark;
163 // check if the surface has NOIMPACT or NOMARKS set
164 if ( ( surf->shader->surfaceFlags & ( SURF_NOIMPACT | SURF_NOMARKS ) )
165 || ( surf->shader->contentFlags & CONTENTS_FOG ) ) {
166 *surfViewCount = tr.viewCount;
167 }
168 // extra check for surfaces to avoid list overflows
169 else if (*(surf->data) == SF_FACE) {
170 // the face plane should go through the box
171 s = BoxOnPlaneSide( mins, maxs, &surf->cullinfo.plane );
172 if (s == 1 || s == 2) {
173 *surfViewCount = tr.viewCount;
174 } else if (DotProduct(surf->cullinfo.plane.normal, dir) > -0.5) {
175 // don't add faces that make sharp angles with the projection direction
176 *surfViewCount = tr.viewCount;
177 }
178 }
179 else if (*(surf->data) != SF_GRID &&
180 *(surf->data) != SF_TRIANGLES)
181 *surfViewCount = tr.viewCount;
182 // check the viewCount because the surface may have
183 // already been added if it spans multiple leafs
184 if (*surfViewCount != tr.viewCount) {
185 *surfViewCount = tr.viewCount;
186 list[*listlength] = surf->data;
187 (*listlength)++;
188 }
189 mark++;
190 }
191 }
192
193 /*
194 =================
195 R_AddMarkFragments
196
197 =================
198 */
199 void R_AddMarkFragments(int numClipPoints, vec3_t clipPoints[2][MAX_VERTS_ON_POLY],
200 int numPlanes, vec3_t *normals, float *dists,
201 int maxPoints, vec3_t pointBuffer,
202 int maxFragments, markFragment_t *fragmentBuffer,
203 int *returnedPoints, int *returnedFragments,
204 vec3_t mins, vec3_t maxs) {
205 int pingPong, i;
206 markFragment_t *mf;
207
208 // chop the surface by all the bounding planes of the to be projected polygon
209 pingPong = 0;
210
211 for ( i = 0 ; i < numPlanes ; i++ ) {
212
213 R_ChopPolyBehindPlane( numClipPoints, clipPoints[pingPong],
214 &numClipPoints, clipPoints[!pingPong],
215 normals[i], dists[i], 0.5 );
216 pingPong ^= 1;
217 if ( numClipPoints == 0 ) {
218 break;
219 }
220 }
221 // completely clipped away?
222 if ( numClipPoints == 0 ) {
223 return;
224 }
225
226 // add this fragment to the returned list
227 if ( numClipPoints + (*returnedPoints) > maxPoints ) {
228 return; // not enough space for this polygon
229 }
230 /*
231 // all the clip points should be within the bounding box
232 for ( i = 0 ; i < numClipPoints ; i++ ) {
233 int j;
234 for ( j = 0 ; j < 3 ; j++ ) {
235 if (clipPoints[pingPong][i][j] < mins[j] - 0.5) break;
236 if (clipPoints[pingPong][i][j] > maxs[j] + 0.5) break;
237 }
238 if (j < 3) break;
239 }
240 if (i < numClipPoints) return;
241 */
242
243 mf = fragmentBuffer + (*returnedFragments);
244 mf->firstPoint = (*returnedPoints);
245 mf->numPoints = numClipPoints;
246 Com_Memcpy( pointBuffer + (*returnedPoints) * 3, clipPoints[pingPong], numClipPoints * sizeof(vec3_t) );
247
248 (*returnedPoints) += numClipPoints;
249 (*returnedFragments)++;
250 }
251
252 /*
253 =================
254 R_MarkFragments
255
256 =================
257 */
258 int R_MarkFragments( int numPoints, const vec3_t *points, const vec3_t projection,
259 int maxPoints, vec3_t pointBuffer, int maxFragments, markFragment_t *fragmentBuffer ) {
260 int numsurfaces, numPlanes;
261 int i, j, k, m, n;
262 surfaceType_t *surfaces[64];
263 vec3_t mins, maxs;
264 int returnedFragments;
265 int returnedPoints;
266 vec3_t normals[MAX_VERTS_ON_POLY+2];
267 float dists[MAX_VERTS_ON_POLY+2];
268 vec3_t clipPoints[2][MAX_VERTS_ON_POLY];
269 int numClipPoints;
270 float *v;
271 srfBspSurface_t *cv;
272 glIndex_t *tri;
273 srfVert_t *dv;
274 vec3_t normal;
275 vec3_t projectionDir;
276 vec3_t v1, v2;
277
278 if (numPoints <= 0) {
279 return 0;
280 }
281
282 //increment view count for double check prevention
283 tr.viewCount++;
284
285 //
286 VectorNormalize2( projection, projectionDir );
287 // find all the brushes that are to be considered
288 ClearBounds( mins, maxs );
289 for ( i = 0 ; i < numPoints ; i++ ) {
290 vec3_t temp;
291
292 AddPointToBounds( points[i], mins, maxs );
293 VectorAdd( points[i], projection, temp );
294 AddPointToBounds( temp, mins, maxs );
295 // make sure we get all the leafs (also the one(s) in front of the hit surface)
296 VectorMA( points[i], -20, projectionDir, temp );
297 AddPointToBounds( temp, mins, maxs );
298 }
299
300 if (numPoints > MAX_VERTS_ON_POLY) numPoints = MAX_VERTS_ON_POLY;
301 // create the bounding planes for the to be projected polygon
302 for ( i = 0 ; i < numPoints ; i++ ) {
303 VectorSubtract(points[(i+1)%numPoints], points[i], v1);
304 VectorAdd(points[i], projection, v2);
305 VectorSubtract(points[i], v2, v2);
306 CrossProduct(v1, v2, normals[i]);
307 VectorNormalizeFast(normals[i]);
308 dists[i] = DotProduct(normals[i], points[i]);
309 }
310 // add near and far clipping planes for projection
311 VectorCopy(projectionDir, normals[numPoints]);
312 dists[numPoints] = DotProduct(normals[numPoints], points[0]) - 32;
313 VectorCopy(projectionDir, normals[numPoints+1]);
314 VectorInverse(normals[numPoints+1]);
315 dists[numPoints+1] = DotProduct(normals[numPoints+1], points[0]) - 20;
316 numPlanes = numPoints + 2;
317
318 numsurfaces = 0;
319 R_BoxSurfaces_r(tr.world->nodes, mins, maxs, surfaces, 64, &numsurfaces, projectionDir);
320 //assert(numsurfaces <= 64);
321 //assert(numsurfaces != 64);
322
323 returnedPoints = 0;
324 returnedFragments = 0;
325
326 for ( i = 0 ; i < numsurfaces ; i++ ) {
327
328 if (*surfaces[i] == SF_GRID) {
329
330 cv = (srfBspSurface_t *) surfaces[i];
331 for ( m = 0 ; m < cv->height - 1 ; m++ ) {
332 for ( n = 0 ; n < cv->width - 1 ; n++ ) {
333 // We triangulate the grid and chop all triangles within
334 // the bounding planes of the to be projected polygon.
335 // LOD is not taken into account, not such a big deal though.
336 //
337 // It's probably much nicer to chop the grid itself and deal
338 // with this grid as a normal SF_GRID surface so LOD will
339 // be applied. However the LOD of that chopped grid must
340 // be synced with the LOD of the original curve.
341 // One way to do this; the chopped grid shares vertices with
342 // the original curve. When LOD is applied to the original
343 // curve the unused vertices are flagged. Now the chopped curve
344 // should skip the flagged vertices. This still leaves the
345 // problems with the vertices at the chopped grid edges.
346 //
347 // To avoid issues when LOD applied to "hollow curves" (like
348 // the ones around many jump pads) we now just add a 2 unit
349 // offset to the triangle vertices.
350 // The offset is added in the vertex normal vector direction
351 // so all triangles will still fit together.
352 // The 2 unit offset should avoid pretty much all LOD problems.
353 vec3_t fNormal;
354
355 numClipPoints = 3;
356
357 dv = cv->verts + m * cv->width + n;
358
359 VectorCopy(dv[0].xyz, clipPoints[0][0]);
360 R_VaoUnpackNormal(fNormal, dv[0].normal);
361 VectorMA(clipPoints[0][0], MARKER_OFFSET, fNormal, clipPoints[0][0]);
362 VectorCopy(dv[cv->width].xyz, clipPoints[0][1]);
363 R_VaoUnpackNormal(fNormal, dv[cv->width].normal);
364 VectorMA(clipPoints[0][1], MARKER_OFFSET, fNormal, clipPoints[0][1]);
365 VectorCopy(dv[1].xyz, clipPoints[0][2]);
366 R_VaoUnpackNormal(fNormal, dv[1].normal);
367 VectorMA(clipPoints[0][2], MARKER_OFFSET, fNormal, clipPoints[0][2]);
368 // check the normal of this triangle
369 VectorSubtract(clipPoints[0][0], clipPoints[0][1], v1);
370 VectorSubtract(clipPoints[0][2], clipPoints[0][1], v2);
371 CrossProduct(v1, v2, normal);
372 VectorNormalizeFast(normal);
373 if (DotProduct(normal, projectionDir) < -0.1) {
374 // add the fragments of this triangle
375 R_AddMarkFragments(numClipPoints, clipPoints,
376 numPlanes, normals, dists,
377 maxPoints, pointBuffer,
378 maxFragments, fragmentBuffer,
379 &returnedPoints, &returnedFragments, mins, maxs);
380
381 if ( returnedFragments == maxFragments ) {
382 return returnedFragments; // not enough space for more fragments
383 }
384 }
385
386 VectorCopy(dv[1].xyz, clipPoints[0][0]);
387 R_VaoUnpackNormal(fNormal, dv[1].normal);
388 VectorMA(clipPoints[0][0], MARKER_OFFSET, fNormal, clipPoints[0][0]);
389 VectorCopy(dv[cv->width].xyz, clipPoints[0][1]);
390 R_VaoUnpackNormal(fNormal, dv[cv->width].normal);
391 VectorMA(clipPoints[0][1], MARKER_OFFSET, fNormal, clipPoints[0][1]);
392 VectorCopy(dv[cv->width+1].xyz, clipPoints[0][2]);
393 R_VaoUnpackNormal(fNormal, dv[cv->width + 1].normal);
394 VectorMA(clipPoints[0][2], MARKER_OFFSET, fNormal, clipPoints[0][2]);
395 // check the normal of this triangle
396 VectorSubtract(clipPoints[0][0], clipPoints[0][1], v1);
397 VectorSubtract(clipPoints[0][2], clipPoints[0][1], v2);
398 CrossProduct(v1, v2, normal);
399 VectorNormalizeFast(normal);
400 if (DotProduct(normal, projectionDir) < -0.05) {
401 // add the fragments of this triangle
402 R_AddMarkFragments(numClipPoints, clipPoints,
403 numPlanes, normals, dists,
404 maxPoints, pointBuffer,
405 maxFragments, fragmentBuffer,
406 &returnedPoints, &returnedFragments, mins, maxs);
407
408 if ( returnedFragments == maxFragments ) {
409 return returnedFragments; // not enough space for more fragments
410 }
411 }
412 }
413 }
414 }
415 else if (*surfaces[i] == SF_FACE) {
416
417 srfBspSurface_t *surf = ( srfBspSurface_t * ) surfaces[i];
418
419 // check the normal of this face
420 if (DotProduct(surf->cullPlane.normal, projectionDir) > -0.5) {
421 continue;
422 }
423
424 for(k = 0, tri = surf->indexes; k < surf->numIndexes; k += 3, tri += 3)
425 {
426 for(j = 0; j < 3; j++)
427 {
428 v = surf->verts[tri[j]].xyz;
429 VectorMA(v, MARKER_OFFSET, surf->cullPlane.normal, clipPoints[0][j]);
430 }
431
432 // add the fragments of this face
433 R_AddMarkFragments( 3 , clipPoints,
434 numPlanes, normals, dists,
435 maxPoints, pointBuffer,
436 maxFragments, fragmentBuffer,
437 &returnedPoints, &returnedFragments, mins, maxs);
438 if ( returnedFragments == maxFragments ) {
439 return returnedFragments; // not enough space for more fragments
440 }
441 }
442 }
443 else if(*surfaces[i] == SF_TRIANGLES && r_marksOnTriangleMeshes->integer) {
444
445 srfBspSurface_t *surf = (srfBspSurface_t *) surfaces[i];
446
447 for(k = 0, tri = surf->indexes; k < surf->numIndexes; k += 3, tri += 3)
448 {
449 for(j = 0; j < 3; j++)
450 {
451 vec3_t fNormal;
452 v = surf->verts[tri[j]].xyz;
453 R_VaoUnpackNormal(fNormal, surf->verts[tri[j]].normal);
454 VectorMA(v, MARKER_OFFSET, fNormal, clipPoints[0][j]);
455 }
456
457 // add the fragments of this face
458 R_AddMarkFragments(3, clipPoints,
459 numPlanes, normals, dists,
460 maxPoints, pointBuffer,
461 maxFragments, fragmentBuffer, &returnedPoints, &returnedFragments, mins, maxs);
462 if(returnedFragments == maxFragments)
463 {
464 return returnedFragments; // not enough space for more fragments
465 }
466 }
467 }
468 }
469 return returnedFragments;
470 }
471
472
473
474
475