Debian Codegraph @

prboom2/src/gl_vertex.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2000 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
11 * Copyright 2005, 2006 by
12 * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 * 02111-1307, USA.
28 *
29 * DESCRIPTION:
30 *
31 *---------------------------------------------------------------------
32 */
33
34 /*
35 ** gl_vertex.cpp
36 **
37 **---------------------------------------------------------------------------
38 ** Copyright 2006 Christoph Oelckers
39 ** All rights reserved.
40 **
41 ** Redistribution and use in source and binary forms, with or without
42 ** modification, are permitted provided that the following conditions
43 ** are met:
44 **
45 ** 1. Redistributions of source code must retain the above copyright
46 ** notice, this list of conditions and the following disclaimer.
47 ** 2. Redistributions in binary form must reproduce the above copyright
48 ** notice, this list of conditions and the following disclaimer in the
49 ** documentation and/or other materials provided with the distribution.
50 ** 3. The name of the author may not be used to endorse or promote products
51 ** derived from this software without specific prior written permission.
52 ** 4. When not used as part of GZDoom or a GZDoom derivative, this code will be
53 ** covered by the terms of the GNU Lesser General Public License as published
54 ** by the Free Software Foundation; either version 2.1 of the License, or (at
55 ** your option) any later version.
56 **
57 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 **---------------------------------------------------------------------------
68 **
69 */
70
71 // Adapted for prboom(-plus) by entryway
72
73 #ifdef HAVE_CONFIG_H
74 #include "config.h"
75 #endif
76
77 #include "gl_opengl.h"
78
79 #include "z_zone.h"
80 #include "v_video.h"
81 #include "gl_intern.h"
82 #include "r_main.h"
83
84 typedef struct vertexsplit_info_s
85 {
86 dboolean changed;
87 int numheights;
88 int numsectors;
89 sector_t **sectors;
90 float *heightlist;
91 byte validcount;
92 } vertexsplit_info_t;
93
94 static vertexsplit_info_t * gl_vertexsplit = NULL;
95
96 typedef struct splitsbysector_s
97 {
98 int numsplits;
99 vertexsplit_info_t **splits;
100 } splitsbysector_t;
101 static splitsbysector_t * gl_splitsbysector = NULL;
102
103 //==========================================================================
104 //
105 // Split left edge of wall
106 //
107 //==========================================================================
108 void gld_SplitLeftEdge(const GLWall *wall)
109 {
110 vertex_t *v;
111 vertexsplit_info_t *vi;
112
113 v = wall->seg->linedef->v1;
114
115 if (v == NULL)
116 return;
117
118 vi = &gl_vertexsplit[v - vertexes];
119
120 if (vi->numheights)
121 {
122 int i = 0;
123
124 float polyh1 = wall->ytop - wall->ybottom;
125 float factv1 = (polyh1 ? (wall->vt - wall->vb) / polyh1 : 0);
126 float factu1 = (polyh1 ? (wall->ul - wall->ul) / polyh1 : 0);
127
128 while (i < vi->numheights && vi->heightlist[i] <= wall->ybottom)
129 i++;
130
131 while (i < vi->numheights && vi->heightlist[i] < wall->ytop)
132 {
133 GLfloat s = factu1 * (vi->heightlist[i] - wall->ytop) + wall->ul;
134 GLfloat t = factv1 * (vi->heightlist[i] - wall->ytop) + wall->vt;
135
136 glTexCoord2f(s, t);
137
138 glVertex3f(wall->glseg->x1, vi->heightlist[i], wall->glseg->z1);
139 i++;
140 }
141 }
142 }
143
144 //==========================================================================
145 //
146 // Split right edge of wall
147 //
148 //==========================================================================
149 void gld_SplitRightEdge(const GLWall *wall)
150 {
151 vertex_t *v;
152 vertexsplit_info_t * vi;
153
154 v = wall->seg->linedef->v2;
155
156 if (v == NULL)
157 return;
158
159 vi = &gl_vertexsplit[v - vertexes];
160
161 if (vi->numheights)
162 {
163 int i = vi->numheights - 1;
164
165 float polyh2 = wall->ytop - wall->ybottom;
166 float factv2 = (polyh2 ? (wall->vt - wall->vb) / polyh2 : 0);
167 float factu2 = (polyh2 ? (wall->ur - wall->ur) / polyh2 : 0);
168
169 while (i > 0 && vi->heightlist[i] >= wall->ytop)
170 i--;
171
172 while (i > 0 && vi->heightlist[i] > wall->ybottom)
173 {
174 GLfloat s = factu2 * (vi->heightlist[i] - wall->ytop) + wall->ur;
175 GLfloat t = factv2 * (vi->heightlist[i] - wall->ytop) + wall->vt;
176
177 glTexCoord2f(s, t);
178
179 glVertex3f(wall->glseg->x2, vi->heightlist[i], wall->glseg->z2);
180 i--;
181 }
182 }
183 }
184
185 //==========================================================================
186 //
187 // Recalculate all heights affectting this vertex.
188 //
189 //==========================================================================
190 void gld_RecalcVertexHeights(const vertex_t *v)
191 {
192 int i,j,k;
193 float height;
194 vertexsplit_info_t *vi;
195
196 vi = &gl_vertexsplit[v - vertexes];
197
198 if (vi->validcount == rendermarker)
199 return;
200
201 vi->validcount = rendermarker;
202
203 if (!vi->changed)
204 return;
205
206 vi->changed = false;
207
208 vi->numheights = 0;
209 for(i = 0; i < vi->numsectors; i++)
210 {
211 for(j = 0; j < 2; j++)
212 {
213 if (j == 0)
214 height = (float)vi->sectors[i]->ceilingheight/MAP_SCALE;
215 else
216 height = (float)vi->sectors[i]->floorheight/MAP_SCALE;
217
218 for(k = 0; k < vi->numheights; k++)
219 {
220 if (height == vi->heightlist[k])
221 break;
222
223 if (height < vi->heightlist[k])
224 {
225 memmove(&vi->heightlist[k + 1], &vi->heightlist[k], sizeof(vi->heightlist[0]) * (vi->numheights - k));
226 vi->heightlist[k] = height;
227 vi->numheights++;
228 break;
229 }
230 }
231 if (k == vi->numheights)
232 vi->heightlist[vi->numheights++] = height;
233 }
234 }
235
236 if (vi->numheights <= 2)
237 vi->numheights = 0; // is not in need of any special attention
238 }
239
240 //==========================================================================
241 //
242 //
243 //
244 //==========================================================================
245 static void AddToVertex(const sector_t *sec, int **list, unsigned int *size)
246 {
247 unsigned int i;
248 int secno = sec->iSectorID;
249
250 for(i = 0; i < (*size); i++)
251 {
252 if ((*list)[i] == secno)
253 return;
254 }
255 (*list) = Z_Realloc((*list), sizeof(*list) * ((*size) + 1));
256 (*list)[(*size)] = secno;
257 (*size)++;
258 }
259
260 //==========================================================================
261 //
262 //
263 //
264 //==========================================================================
265 static void AddToSplitBySector(vertexsplit_info_t *vi, splitsbysector_t *splitsbysector)
266 {
267 int i;
268 for(i = 0; i < splitsbysector->numsplits; i++)
269 {
270 if (splitsbysector->splits[i] == vi)
271 return;
272 }
273 splitsbysector->splits = Z_Realloc(
274 splitsbysector->splits,
275 sizeof(splitsbysector->splits) * (splitsbysector->numsplits + 1));
276 splitsbysector->splits[splitsbysector->numsplits] = vi;
277 splitsbysector->numsplits++;
278 }
279
280 //==========================================================================
281 //
282 //
283 //
284 //==========================================================================
285 void gld_InitVertexData()
286 {
287 int i, j, k;
288 int vertexes_count, gl_vertexsplit_size, pos;
289 int ** vt_sectorlists;
290 unsigned int * vt_sectorlists_size;
291
292 if (gl_vertexsplit)
293 return;
294
295 vt_sectorlists = Z_Calloc(sizeof(vt_sectorlists[0]), numvertexes);
296 vt_sectorlists_size = Z_Calloc(sizeof(vt_sectorlists_size[0]), numvertexes);
297
298 for(i = 0; i < numlines; i++)
299 {
300 line_t *line = &lines[i];
301
302 for(j = 0; j < 2; j++)
303 {
304 vertex_t *v = (j==0 ? line->v1 : line->v2);
305
306 for(k = 0; k < 2; k++)
307 {
308 sector_t *sec = (k == 0 ? line->frontsector : line->backsector);
309
310 if (sec)
311 {
312 AddToVertex(sec, &vt_sectorlists[v-vertexes], &vt_sectorlists_size[v-vertexes]);
313 if (sec->heightsec >= 0)
314 {
315 AddToVertex(&sectors[sec->heightsec], &vt_sectorlists[v-vertexes], &vt_sectorlists_size[v-vertexes]);
316 }
317 }
318 }
319 }
320 }
321
322 vertexes_count = 0;
323 for(i = 0; i < numvertexes; i++)
324 {
325 if (vt_sectorlists_size[i] > 1)
326 {
327 vertexes_count += vt_sectorlists_size[i];
328 }
329 }
330
331 gl_vertexsplit_size =
332 numvertexes * sizeof(vertexsplit_info_t) +
333 vertexes_count * sizeof(gl_vertexsplit->sectors[0]) +
334 2 * vertexes_count * sizeof(gl_vertexsplit->heightlist[0]);
335
336 gl_vertexsplit = Z_Malloc(gl_vertexsplit_size);
337 memset(gl_vertexsplit, 0, gl_vertexsplit_size);
338
339 pos = numvertexes * sizeof(vertexsplit_info_t);
340 for(i = 0; i < numvertexes; i++)
341 {
342 int cnt = vt_sectorlists_size[i];
343 vertexsplit_info_t *vi = &gl_vertexsplit[i];
344
345 vi->validcount = -1;
346 vi->numheights = 0;
347 if (cnt > 1)
348 {
349 vi->changed = true;
350 vi->numsectors = cnt;
351
352 vi->sectors = (sector_t **)((unsigned char*)gl_vertexsplit + pos);
353 pos += sizeof(vi->sectors[0]) * cnt;
354 vi->heightlist = (float *)((unsigned char*)gl_vertexsplit + pos);
355 pos += sizeof(vi->heightlist[0]) * cnt * 2;
356
357 for(j = 0; j < cnt; j++)
358 {
359 vi->sectors[j] = &sectors[vt_sectorlists[i][j]];
360 }
361 }
362 else
363 {
364 vi->numsectors=0;
365 }
366 }
367
368 gl_splitsbysector = Z_Malloc(sizeof(gl_splitsbysector[0]) * numsectors);
369 memset(gl_splitsbysector, 0, sizeof(gl_splitsbysector[0]) * numsectors);
370
371 for(i = 0; i < numsectors; i++)
372 {
373 for(j = 0; j < numvertexes; j++)
374 {
375 vertexsplit_info_t *vi = &gl_vertexsplit[j];
376
377 for(k = 0; k < vi->numsectors; k++)
378 {
379 if (vi->sectors[k] == &sectors[i])
380 AddToSplitBySector(vi, &gl_splitsbysector[i]);
381 }
382 }
383 }
384
385 for(i = 0; i < numvertexes; i++)
386 gld_RecalcVertexHeights(&vertexes[i]);
387
388 Z_Free(vt_sectorlists);
389 Z_Free(vt_sectorlists_size);
390 }
391
392 //==========================================================================
393 //
394 //
395 //
396 //==========================================================================
397 void gld_UpdateSplitData(sector_t *sector)
398 {
399 int i;
400
401 if (gl_splitsbysector)
402 {
403 splitsbysector_t *splitsbysector = &gl_splitsbysector[sector->iSectorID];
404 for (i = 0; i < splitsbysector->numsplits; i++)
405 {
406 splitsbysector->splits[i]->changed = true;
407 }
408 }
409 }
410
411 //==========================================================================
412 //
413 //
414 //
415 //==========================================================================
416 void gld_CleanVertexData()
417 {
418 if (gl_vertexsplit)
419 {
420 Z_Free(gl_vertexsplit);
421 gl_vertexsplit = NULL;
422 }
423
424 if (gl_splitsbysector)
425 {
426 int i;
427 for(i = 0; i < numsectors; i++)
428 {
429 if (gl_splitsbysector[i].numsplits > 0)
430 {
431 Z_Free(gl_splitsbysector[i].splits);
432 }
433 }
434 Z_Free(gl_splitsbysector);
435 gl_splitsbysector = NULL;
436 }
437 }