Debian Codegraph @

code/cgame/cg_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 //
23 // cg_marks.c -- wall marks
24
25 #include "cg_local.h"
26
27 /*
28 ===================================================================
29
30 MARK POLYS
31
32 ===================================================================
33 */
34
35
36 markPoly_t cg_activeMarkPolys; // double linked list
37 markPoly_t *cg_freeMarkPolys; // single linked list
38 markPoly_t cg_markPolys[MAX_MARK_POLYS];
39 static int markTotal;
40
41 /*
42 ===================
43 CG_InitMarkPolys
44
45 This is called at startup and for tournement restarts
46 ===================
47 */
48 void CG_InitMarkPolys( void ) {
49 int i;
50
51 memset( cg_markPolys, 0, sizeof(cg_markPolys) );
52
53 cg_activeMarkPolys.nextMark = &cg_activeMarkPolys;
54 cg_activeMarkPolys.prevMark = &cg_activeMarkPolys;
55 cg_freeMarkPolys = cg_markPolys;
56 for ( i = 0 ; i < MAX_MARK_POLYS - 1 ; i++ ) {
57 cg_markPolys[i].nextMark = &cg_markPolys[i+1];
58 }
59 }
60
61
62 /*
63 ==================
64 CG_FreeMarkPoly
65 ==================
66 */
67 void CG_FreeMarkPoly( markPoly_t *le ) {
68 if ( !le->prevMark || !le->nextMark ) {
69 CG_Error( "CG_FreeLocalEntity: not active" );
70 }
71
72 // remove from the doubly linked active list
73 le->prevMark->nextMark = le->nextMark;
74 le->nextMark->prevMark = le->prevMark;
75
76 // the free list is only singly linked
77 le->nextMark = cg_freeMarkPolys;
78 cg_freeMarkPolys = le;
79 }
80
81 /*
82 ===================
83 CG_AllocMark
84
85 Will allways succeed, even if it requires freeing an old active mark
86 ===================
87 */
88 markPoly_t *CG_AllocMark( void ) {
89 markPoly_t *le;
90 int time;
91
92 if ( !cg_freeMarkPolys ) {
93 // no free entities, so free the one at the end of the chain
94 // remove the oldest active entity
95 time = cg_activeMarkPolys.prevMark->time;
96 while (cg_activeMarkPolys.prevMark && time == cg_activeMarkPolys.prevMark->time) {
97 CG_FreeMarkPoly( cg_activeMarkPolys.prevMark );
98 }
99 }
100
101 le = cg_freeMarkPolys;
102 cg_freeMarkPolys = cg_freeMarkPolys->nextMark;
103
104 memset( le, 0, sizeof( *le ) );
105
106 // link into the active list
107 le->nextMark = cg_activeMarkPolys.nextMark;
108 le->prevMark = &cg_activeMarkPolys;
109 cg_activeMarkPolys.nextMark->prevMark = le;
110 cg_activeMarkPolys.nextMark = le;
111 return le;
112 }
113
114
115
116 /*
117 =================
118 CG_ImpactMark
119
120 origin should be a point within a unit of the plane
121 dir should be the plane normal
122
123 temporary marks will not be stored or randomly oriented, but immediately
124 passed to the renderer.
125 =================
126 */
127 #define MAX_MARK_FRAGMENTS 128
128 #define MAX_MARK_POINTS 384
129
130 void CG_ImpactMark( qhandle_t markShader, const vec3_t origin, const vec3_t dir,
131 float orientation, float red, float green, float blue, float alpha,
132 qboolean alphaFade, float radius, qboolean temporary ) {
133 vec3_t axis[3];
134 float texCoordScale;
135 vec3_t originalPoints[4];
136 byte colors[4];
137 int i, j;
138 int numFragments;
139 markFragment_t markFragments[MAX_MARK_FRAGMENTS], *mf;
140 vec3_t markPoints[MAX_MARK_POINTS];
141 vec3_t projection;
142
143 if ( !cg_addMarks.integer ) {
144 return;
145 }
146
147 if ( radius <= 0 ) {
148 CG_Error( "CG_ImpactMark called with <= 0 radius" );
149 }
150
151 //if ( markTotal >= MAX_MARK_POLYS ) {
152 // return;
153 //}
154
155 // create the texture axis
156 VectorNormalize2( dir, axis[0] );
157 PerpendicularVector( axis[1], axis[0] );
158 RotatePointAroundVector( axis[2], axis[0], axis[1], orientation );
159 CrossProduct( axis[0], axis[2], axis[1] );
160
161 texCoordScale = 0.5 * 1.0 / radius;
162
163 // create the full polygon
164 for ( i = 0 ; i < 3 ; i++ ) {
165 originalPoints[0][i] = origin[i] - radius * axis[1][i] - radius * axis[2][i];
166 originalPoints[1][i] = origin[i] + radius * axis[1][i] - radius * axis[2][i];
167 originalPoints[2][i] = origin[i] + radius * axis[1][i] + radius * axis[2][i];
168 originalPoints[3][i] = origin[i] - radius * axis[1][i] + radius * axis[2][i];
169 }
170
171 // get the fragments
172 VectorScale( dir, -20, projection );
173 numFragments = trap_CM_MarkFragments( 4, (void *)originalPoints,
174 projection, MAX_MARK_POINTS, markPoints[0],
175 MAX_MARK_FRAGMENTS, markFragments );
176
177 colors[0] = red * 255;
178 colors[1] = green * 255;
179 colors[2] = blue * 255;
180 colors[3] = alpha * 255;
181
182 for ( i = 0, mf = markFragments ; i < numFragments ; i++, mf++ ) {
183 polyVert_t *v;
184 polyVert_t verts[MAX_VERTS_ON_POLY];
185 markPoly_t *mark;
186
187 // we have an upper limit on the complexity of polygons
188 // that we store persistantly
189 if ( mf->numPoints > MAX_VERTS_ON_POLY ) {
190 mf->numPoints = MAX_VERTS_ON_POLY;
191 }
192 for ( j = 0, v = verts ; j < mf->numPoints ; j++, v++ ) {
193 vec3_t delta;
194
195 VectorCopy( markPoints[mf->firstPoint + j], v->xyz );
196
197 VectorSubtract( v->xyz, origin, delta );
198 v->st[0] = 0.5 + DotProduct( delta, axis[1] ) * texCoordScale;
199 v->st[1] = 0.5 + DotProduct( delta, axis[2] ) * texCoordScale;
200 *(int *)v->modulate = *(int *)colors;
201 }
202
203 // if it is a temporary (shadow) mark, add it immediately and forget about it
204 if ( temporary ) {
205 trap_R_AddPolyToScene( markShader, mf->numPoints, verts );
206 continue;
207 }
208
209 // otherwise save it persistantly
210 mark = CG_AllocMark();
211 mark->time = cg.time;
212 mark->alphaFade = alphaFade;
213 mark->markShader = markShader;
214 mark->poly.numVerts = mf->numPoints;
215 mark->color[0] = red;
216 mark->color[1] = green;
217 mark->color[2] = blue;
218 mark->color[3] = alpha;
219 memcpy( mark->verts, verts, mf->numPoints * sizeof( verts[0] ) );
220 markTotal++;
221 }
222 }
223
224
225 /*
226 ===============
227 CG_AddMarks
228 ===============
229 */
230 #define MARK_TOTAL_TIME 10000
231 #define MARK_FADE_TIME 1000
232
233 void CG_AddMarks( void ) {
234 int j;
235 markPoly_t *mp, *next;
236 int t;
237 int fade;
238
239 if ( !cg_addMarks.integer ) {
240 return;
241 }
242
243 mp = cg_activeMarkPolys.nextMark;
244 for ( ; mp != &cg_activeMarkPolys ; mp = next ) {
245 // grab next now, so if the local entity is freed we
246 // still have it
247 next = mp->nextMark;
248
249 // see if it is time to completely remove it
250 if ( cg.time > mp->time + MARK_TOTAL_TIME ) {
251 CG_FreeMarkPoly( mp );
252 continue;
253 }
254
255 // fade out the energy bursts
256 if ( mp->markShader == cgs.media.energyMarkShader ) {
257
258 fade = 450 - 450 * ( (cg.time - mp->time ) / 3000.0 );
259 if ( fade < 255 ) {
260 if ( fade < 0 ) {
261 fade = 0;
262 }
263 if ( mp->verts[0].modulate[0] != 0 ) {
264 for ( j = 0 ; j < mp->poly.numVerts ; j++ ) {
265 mp->verts[j].modulate[0] = mp->color[0] * fade;
266 mp->verts[j].modulate[1] = mp->color[1] * fade;
267 mp->verts[j].modulate[2] = mp->color[2] * fade;
268 }
269 }
270 }
271 }
272
273 // fade all marks out with time
274 t = mp->time + MARK_TOTAL_TIME - cg.time;
275 if ( t < MARK_FADE_TIME ) {
276 fade = 255 * t / MARK_FADE_TIME;
277 if ( mp->alphaFade ) {
278 for ( j = 0 ; j < mp->poly.numVerts ; j++ ) {
279 mp->verts[j].modulate[3] = fade;
280 }
281 } else {
282 for ( j = 0 ; j < mp->poly.numVerts ; j++ ) {
283 mp->verts[j].modulate[0] = mp->color[0] * fade;
284 mp->verts[j].modulate[1] = mp->color[1] * fade;
285 mp->verts[j].modulate[2] = mp->color[2] * fade;
286 }
287 }
288 }
289
290
291 trap_R_AddPolyToScene( mp->markShader, mp->poly.numVerts, mp->verts );
292 }
293 }