Debian Codegraph @

prboom2/src/gl_missingtexture.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 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <assert.h>
39
40 #include "gl_opengl.h"
41
42 #include <SDL.h>
43 #ifdef HAVE_LIBSDL2_IMAGE
44 #include <SDL_image.h>
45 #endif
46 #include "doomstat.h"
47 #include "v_video.h"
48 #include "gl_intern.h"
49 #include "i_system.h"
50 #include "lprintf.h"
51 #include "i_video.h"
52 #include "hu_lib.h"
53 #include "hu_stuff.h"
54 #include "r_main.h"
55 #include "e6y.h"
56
57 typedef struct
58 {
59 int count; // size of the list with adjoining sectors
60 int validcount; // finding of the best sector in the group only once in tic
61 int ceiling; // this group is for ceilings or flats
62 sector_t *sector; // sector with the 'best' height for the sectors in list
63 sector_t **list; // list of adjoining sectors
64 } fakegroup_t;
65
66 typedef struct
67 {
68 sector_t *source; // The sector to receive a fake bleed-through flat
69 sector_t *target; // The floor sector whose properties should be copied
70 enum bleedtype type; // Ceiling or floor, occlusion-based or not
71 } bleedthrough_t;
72
73 static int numfakeplanes = 0;
74 static fakegroup_t *fakeplanes = NULL;
75 static sector_t **sectors2 = NULL;
76 static bleedthrough_t *bleedsectors = NULL;
77 static int numbleedsectors = 0;
78
79 static void gld_PrepareSectorSpecialEffects(void);
80 static void gld_PreprocessFakeSector(int ceiling, sector_t *sector, int groupid);
81 static void gld_RegisterBleedthroughSector(sector_t* source, sector_t* target, enum bleedtype type);
82
83 static void gld_PrepareSectorSpecialEffects(void)
84 {
85 int i, num;
86
87 /* free memory if allocated by previous maps */
88 if (bleedsectors)
89 {
90 Z_Free(bleedsectors);
91 numbleedsectors = 0;
92 bleedsectors = NULL;
93 }
94
95 for (num = 0; num < numsectors; num++)
96 {
97 // the following is for specialeffects. see r_bsp.c in R_Subsector
98 sectors[num].flags |= (NO_TOPTEXTURES | NO_BOTTOMTEXTURES);
99
100 for (i=0; i<sectors[num].linecount; i++)
101 {
102 unsigned short sidenum0 = sectors[num].lines[i]->sidenum[0];
103 unsigned short sidenum1 = sectors[num].lines[i]->sidenum[1];
104 side_t *side0 = (sidenum0 == NO_INDEX ? NULL : &sides[sidenum0]);
105 side_t *side1 = (sidenum1 == NO_INDEX ? NULL : &sides[sidenum1]);
106 side_t *front;
107 side_t *back;
108 dboolean needs_front_lower, needs_front_upper;
109
110 if (!side0 || !side1 || side0->sector == side1->sector)
111 {
112 sectors[num].flags &= ~NO_TOPTEXTURES;
113 sectors[num].flags &= ~NO_BOTTOMTEXTURES;
114 continue;
115 }
116
117 if (side0->sector == &sectors[num])
118 {
119 front = side0;
120 back = side1;
121 }
122 else
123 {
124 front = side1;
125 back = side0;
126 }
127
128 if (front->toptexture != NO_TEXTURE)
129 sectors[num].flags &= ~NO_TOPTEXTURES;
130 if (front->bottomtexture != NO_TEXTURE)
131 sectors[num].flags &= ~NO_BOTTOMTEXTURES;
132 if (back->toptexture != NO_TEXTURE)
133 sectors[num].flags &= ~NO_TOPTEXTURES;
134 if (back->bottomtexture != NO_TEXTURE)
135 sectors[num].flags &= ~NO_BOTTOMTEXTURES;
136
137 needs_front_lower =
138 back->sector->floorpic != skyflatnum &&
139 front->sector->floorheight < back->sector->floorheight;
140 needs_front_upper =
141 back->sector->ceilingpic != skyflatnum &&
142 front->sector->ceilingheight > back->sector->ceilingheight;
143
144 /* now mark the sectors if they require flat bleed-through */
145 if (needs_front_upper && front->toptexture == NO_TEXTURE)
146 {
147 back->sector->flags |= MISSING_TOPTEXTURES;
148 gld_RegisterBleedthroughSector(front->sector, back->sector, BLEED_CEILING);
149 front->sector->flags |= MISSING_TOPTEXTURES;
150 gld_RegisterBleedthroughSector(back->sector, front->sector, BLEED_CEILING | BLEED_OCCLUDE);
151 }
152 if (needs_front_lower && front->bottomtexture == NO_TEXTURE)
153 {
154 back->sector->flags |= MISSING_BOTTOMTEXTURES;
155 gld_RegisterBleedthroughSector(front->sector, back->sector, BLEED_NONE);
156 front->sector->flags |= MISSING_BOTTOMTEXTURES;
157 gld_RegisterBleedthroughSector(back->sector, front->sector, BLEED_OCCLUDE);
158 }
159 }
160 #ifdef PRBOOM_DEBUG
161 if (sectors[num].flags & NO_TOPTEXTURES)
162 lprintf(LO_INFO,"Sector %i has no toptextures\n",num);
163 if (sectors[num].flags & NO_BOTTOMTEXTURES)
164 lprintf(LO_INFO,"Sector %i has no bottomtextures\n",num);
165 #endif
166 }
167 }
168
169 static void gld_RegisterBleedthroughSector(sector_t* source, sector_t* target, enum bleedtype type)
170 {
171 int i;
172 int idx = -1;
173 assert(source);
174 assert(target);
175
176 /* check whether the target sector is processed already */
177 for (i = 0; i < numbleedsectors && idx == -1; i++)
178 if (bleedsectors[i].target == target && bleedsectors[i].type == type)
179 idx = i;
180
181 if (idx == -1)
182 {
183 /* allocate memory for new sector */
184 bleedsectors = (bleedthrough_t*) Z_Realloc(bleedsectors, (numbleedsectors + 1) * sizeof(bleedthrough_t));
185 if(!bleedsectors) I_Error("gld_RegisterBleedthroughSector: Out of memory");
186 memset(&bleedsectors[numbleedsectors], 0, sizeof(bleedthrough_t));
187 numbleedsectors++;
188
189 idx = numbleedsectors - 1;
190 }
191
192 bleedsectors[idx].type = type;
193 bleedsectors[idx].target = target;
194
195 /* either register the proposed source since it is first,
196 * or check if the new proposed source is a better option
197 * and register it instead */
198 if (bleedsectors[idx].source == NULL ||
199 ((type & BLEED_CEILING) && bleedsectors[idx].source->ceilingheight > source->ceilingheight) ||
200 (!(type & BLEED_CEILING) && bleedsectors[idx].source->floorheight < source->floorheight))
201 bleedsectors[idx].source = source;
202 }
203
204 sector_t* GetBestBleedSector(sector_t* target, enum bleedtype type)
205 {
206 int i;
207 for (i = 0; i < numbleedsectors; i++)
208 if (bleedsectors[i].target == target && bleedsectors[i].type == type)
209 return bleedsectors[i].source;
210 return NULL;
211 }
212
213 //
214 // Recursive mark of all adjoining sectors with no bottom/top texture
215 //
216
217 static void gld_PreprocessFakeSector(int ceiling, sector_t *sector, int groupid)
218 {
219 int i;
220
221 if (sector->fakegroup[ceiling] != groupid)
222 {
223 sector->fakegroup[ceiling] = groupid;
224 if (groupid >= numfakeplanes)
225 {
226 fakeplanes = Z_Realloc(fakeplanes, (numfakeplanes + 1) * sizeof(fakegroup_t));
227 memset(&fakeplanes[numfakeplanes], 0, sizeof(fakegroup_t));
228 numfakeplanes++;
229 }
230 sectors2[fakeplanes[groupid].count++] = sector;
231 }
232
233 for (i = 0; i < sector->linecount; i++)
234 {
235 sector_t *sec = NULL;
236 line_t *line = sector->lines[i];
237
238 if (line->frontsector && line->frontsector != sector)
239 {
240 sec = line->frontsector;
241 }
242 else
243 {
244 if (line->backsector && line->backsector != sector)
245 {
246 sec = line->backsector;
247 }
248 }
249
250 if (sec && sec->fakegroup[ceiling] == -1 &&
251 (sec->flags & (ceiling ? NO_TOPTEXTURES : NO_BOTTOMTEXTURES)))
252 {
253 gld_PreprocessFakeSector(ceiling, sec, groupid);
254 }
255 }
256 }
257
258 //
259 // Split of all sectors into groups
260 // with adjoining sectors with no bottom/top texture
261 //
262
263 void gld_PreprocessFakeSectors(void)
264 {
265 int i, j, k, ceiling;
266 int groupid;
267
268 if (gl_use_stencil)
269 {
270 // precalculate NO_TOPTEXTURES and NO_BOTTOMTEXTURES flags
271 gld_PrepareSectorSpecialEffects();
272 return;
273 }
274
275 // free memory
276 if (fakeplanes)
277 {
278 for (i = 0; i < numfakeplanes; i++)
279 {
280 fakeplanes[i].count = 0;
281 Z_Free(fakeplanes[i].list);
282 fakeplanes[i].list = NULL;
283 }
284 numfakeplanes = 0;
285 Z_Free(fakeplanes);
286 fakeplanes = NULL;
287 }
288 if (sectors2)
289 {
290 Z_Free(sectors2);
291 }
292 sectors2 = Z_Malloc(numsectors * sizeof(sector_t*));
293
294 // reset all groups with fake floors and ceils
295 // 0 - floor; 1 - ceil;
296 for (i = 0; i < numsectors; i++)
297 {
298 sectors[i].fakegroup[0] = -1;
299 sectors[i].fakegroup[1] = -1;
300 }
301
302 // precalculate NO_TOPTEXTURES and NO_BOTTOMTEXTURES flags
303 gld_PrepareSectorSpecialEffects();
304
305 groupid = 0;
306
307 for (ceiling = 0; ceiling <= 1; ceiling++)
308 {
309 unsigned int no_texture_flag = (ceiling ? NO_TOPTEXTURES : NO_BOTTOMTEXTURES);
310
311 do
312 {
313 for (i = 0; i < numsectors; i++)
314 {
315 if (!(sectors[i].flags & no_texture_flag)
316 && (sectors[i].fakegroup[ceiling] == -1))
317 {
318 gld_PreprocessFakeSector(ceiling, &sectors[i], groupid);
319 fakeplanes[groupid].ceiling = ceiling;
320 fakeplanes[groupid].list = Z_Malloc(fakeplanes[groupid].count * sizeof(sector_t*));
321 for (j = 0, k = 0; k < fakeplanes[groupid].count; k++)
322 {
323 if (!(sectors2[k]->flags & no_texture_flag))
324 {
325 fakeplanes[groupid].list[j++] = sectors2[k];
326 }
327 }
328 fakeplanes[groupid].count = j;
329 groupid++;
330 break;
331 }
332 }
333 }
334 while (i < numsectors);
335 }
336 }
337
338 //
339 // Get highest surounding floorheight for flors and
340 // lowest surounding ceilingheight for ceilings
341 //
342
343 sector_t* GetBestFake(sector_t *sector, int ceiling, int validcount)
344 {
345 int i;
346 int groupid = sector->fakegroup[ceiling];
347
348 if (groupid == -1)
349 return NULL;
350
351 if (fakeplanes[groupid].validcount != validcount)
352 {
353 fakeplanes[groupid].validcount = validcount;
354 fakeplanes[groupid].sector = NULL;
355
356 if (fakeplanes[groupid].ceiling)
357 {
358 fixed_t min_height = INT_MAX;
359 for (i = 0; i < fakeplanes[groupid].count; i++)
360 {
361 if (!(fakeplanes[groupid].list[i]->flags & NO_TOPTEXTURES) &&
362 fakeplanes[groupid].list[i]->ceilingheight < min_height)
363 {
364 min_height = fakeplanes[groupid].list[i]->ceilingheight;
365 fakeplanes[groupid].sector = fakeplanes[groupid].list[i];
366 }
367 }
368 }
369 else
370 {
371 fixed_t max_height = INT_MIN;
372 for (i = 0; i < fakeplanes[groupid].count; i++)
373 {
374 if (!(fakeplanes[groupid].list[i]->flags & NO_BOTTOMTEXTURES) &&
375 fakeplanes[groupid].list[i]->floorheight > max_height)
376 {
377 max_height = fakeplanes[groupid].list[i]->floorheight;
378 fakeplanes[groupid].sector = fakeplanes[groupid].list[i];
379 }
380 }
381 }
382 }
383
384 if (fakeplanes[groupid].sector)
385 {
386 if (fakeplanes[groupid].ceiling)
387 {
388 if (sector->ceilingheight < fakeplanes[groupid].sector->ceilingheight)
389 {
390 return sector;
391 }
392 }
393 else
394 {
395 if (sector->floorheight > fakeplanes[groupid].sector->floorheight)
396 {
397 return sector;
398 }
399 }
400 }
401
402 return fakeplanes[groupid].sector;
403 }
404
405 //==========================================================================
406 //
407 // Flood gaps with the back side's ceiling/floor texture
408 // This requires a stencil because the projected plane interferes with
409 // the depth buffer
410 //
411 //==========================================================================
412
413 void gld_SetupFloodStencil(GLWall *wall)
414 {
415 int recursion = 0;
416
417 // Create stencil
418 glStencilFunc(GL_EQUAL, recursion, ~0); // create stencil
419 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR); // increment stencil of valid pixels
420 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // don't write to the graphics buffer
421 gld_EnableTexture2D(GL_TEXTURE0_ARB, false);
422 glColor3f(1, 1, 1);
423 glEnable(GL_DEPTH_TEST);
424 glDepthMask(true);
425
426 glBegin(GL_TRIANGLE_FAN);
427 glVertex3f(wall->glseg->x1, wall->ytop, wall->glseg->z1);
428 glVertex3f(wall->glseg->x1, wall->ybottom, wall->glseg->z1);
429 glVertex3f(wall->glseg->x2, wall->ybottom, wall->glseg->z2);
430 glVertex3f(wall->glseg->x2, wall->ytop, wall->glseg->z2);
431 glEnd();
432
433 glStencilFunc(GL_EQUAL, recursion+1, ~0); // draw sky into stencil
434 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); // this stage doesn't modify the stencil
435
436 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // don't write to the graphics buffer
437 gld_EnableTexture2D(GL_TEXTURE0_ARB, true);
438 glDisable(GL_DEPTH_TEST);
439 glDepthMask(false);
440 }
441
442 void gld_ClearFloodStencil(GLWall *wall)
443 {
444 int recursion = 0;
445
446 glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
447 gld_EnableTexture2D(GL_TEXTURE0_ARB, false);
448 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // don't write to the graphics buffer
449 glColor3f(1, 1, 1);
450
451 glBegin(GL_TRIANGLE_FAN);
452 glVertex3f(wall->glseg->x1, wall->ytop, wall->glseg->z1);
453 glVertex3f(wall->glseg->x1, wall->ybottom, wall->glseg->z1);
454 glVertex3f(wall->glseg->x2, wall->ybottom, wall->glseg->z2);
455 glVertex3f(wall->glseg->x2, wall->ytop, wall->glseg->z2);
456 glEnd();
457
458 // restore old stencil op.
459 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
460 glStencilFunc(GL_EQUAL, recursion, ~0);
461 gld_EnableTexture2D(GL_TEXTURE0_ARB, true);
462 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
463 glEnable(GL_DEPTH_TEST);
464 glDepthMask(true);
465 }
466
467 //
468 // Calculation of the coordinates of the gap
469 //
470 void gld_SetupFloodedPlaneCoords(GLWall *wall, gl_strip_coords_t *c)
471 {
472 float prj_fac1, prj_fac2;
473 float k = 0.5f;
474 float ytop, ybottom, planez;
475
476 if (wall->flag == GLDWF_TOPFLUD)
477 {
478 ytop = wall->ybottom;
479 ybottom = wall->ytop;
480 planez = wall->ybottom;
481 }
482 else
483 {
484 ytop = wall->ytop;
485 ybottom = wall->ybottom;
486 planez = wall->ytop;
487 }
488
489 prj_fac1 = (ytop - zCamera) / (ytop - zCamera);
490 prj_fac2 = (ytop - zCamera) / (ybottom - zCamera);
491
492 c->v[0][0] = xCamera + prj_fac1 * (wall->glseg->x1 - xCamera);
493 c->v[0][1] = planez;
494 c->v[0][2] = yCamera + prj_fac1 * (wall->glseg->z1 - yCamera);
495
496 c->v[1][0] = xCamera + prj_fac2 * (wall->glseg->x1 - xCamera);
497 c->v[1][1] = planez;
498 c->v[1][2] = yCamera + prj_fac2 * (wall->glseg->z1 - yCamera);
499
500 c->v[2][0] = xCamera + prj_fac1 * (wall->glseg->x2 - xCamera);
501 c->v[2][1] = planez;
502 c->v[2][2] = yCamera + prj_fac1 * (wall->glseg->z2 - yCamera);
503
504 c->v[3][0] = xCamera + prj_fac2 * (wall->glseg->x2 - xCamera);
505 c->v[3][1] = planez;
506 c->v[3][2] = yCamera + prj_fac2 * (wall->glseg->z2 - yCamera);
507
508 c->t[0][0] = -c->v[0][0] / k;
509 c->t[0][1] = -c->v[0][2] / k;
510
511 c->t[1][0] = -c->v[1][0] / k;
512 c->t[1][1] = -c->v[1][2] / k;
513
514 c->t[2][0] = -c->v[2][0] / k;
515 c->t[2][1] = -c->v[2][2] / k;
516
517 c->t[3][0] = -c->v[3][0] / k;
518 c->t[3][1] = -c->v[3][2] / k;
519 }
520
521 void gld_SetupFloodedPlaneLight(GLWall *wall)
522 {
523 if (wall->seg->backsector)
524 {
525 float light;
526 light = gld_CalcLightLevel(wall->seg->backsector->lightlevel+(extralight<<5));
527 gld_StaticLightAlpha(light, wall->alpha);
528 }
529 else
530 {
531 gld_StaticLightAlpha(wall->light, wall->alpha);
532 }
533 }