Debian Codegraph @

prboom2/src/gl_main.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 * Copyright 2007 by
14 * Andrey Budko, Roman Marchenko
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 * 02111-1307, USA.
30 *
31 * DESCRIPTION:
32 *
33 *---------------------------------------------------------------------
34 */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "gl_opengl.h"
41
42 #include "z_zone.h"
43 #include <math.h>
44 #include <SDL.h>
45 #include "doomtype.h"
46 #include "w_wad.h"
47 #include "d_event.h"
48 #include "v_video.h"
49 #include "doomstat.h"
50 #include "r_bsp.h"
51 #include "r_main.h"
52 #include "r_draw.h"
53 #include "r_sky.h"
54 #include "r_plane.h"
55 #include "r_data.h"
56 #include "r_segs.h"
57 #include "r_things.h"
58 #include "r_fps.h"
59 #include "p_maputl.h"
60 #include "m_bbox.h"
61 #include "lprintf.h"
62 #include "gl_intern.h"
63 #include "gl_struct.h"
64 #include "p_spec.h"
65 #include "i_system.h"
66 #include "i_video.h"
67 #include "i_main.h"
68 #include "am_map.h"
69 #include "sc_man.h"
70 #include "st_stuff.h"
71 #include "hu_stuff.h"
72 #include "e6y.h"//e6y
73
74 #include "dsda/configuration.h"
75 #include "dsda/map_format.h"
76 #include "dsda/render_stats.h"
77 #include "dsda/settings.h"
78 #include "dsda/stretch.h"
79 #include "dsda/gl/render_scale.h"
80
81 int gl_preprocessed = false;
82
83 int gl_spriteindex;
84 int scene_has_overlapped_sprites;
85
86 float gldepthmin, gldepthmax;
87
88 dboolean invul_cm;
89 float bw_red = 0.3f;
90 float bw_green = 0.59f;
91 float bw_blue = 0.11f;
92
93 const int tran_filter_pct = 66;
94
95 GLfloat gl_texture_filter_anisotropic;
96
97 extern int gld_paletteIndex;
98
99 //sprites
100 const float gl_spriteclip_threshold_f = 10.f / MAP_COEFF;
101
102 GLfloat gl_whitecolor[4]={1.0f,1.0f,1.0f,1.0f};
103
104 float xCamera,yCamera,zCamera;
105 TAnimItemParam *anim_flats = NULL;
106 TAnimItemParam *anim_textures = NULL;
107
108 void SetFrameTextureMode(void)
109 {
110 if (SceneInTexture)
111 {
112 glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
113 }
114
115 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE);
116 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
117 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
118 }
119
120 const int gl_colorbuffer_bits = 32;
121 const int gl_depthbuffer_bits = 24;
122 int gl_render_multisampling;
123
124 void gld_MultisamplingInit(void)
125 {
126 gl_render_multisampling = dsda_IntConfig(dsda_config_gl_render_multisampling);
127 gl_render_multisampling -= (gl_render_multisampling % 2);
128
129 if (gl_render_multisampling)
130 {
131 SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, gl_colorbuffer_bits );
132 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, gl_depthbuffer_bits );
133 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, gl_render_multisampling );
134 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
135 }
136 else
137 {
138 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 0 );
139 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 0 );
140 }
141 }
142
143 void gld_MultisamplingSet(void)
144 {
145 if (gl_render_multisampling)
146 {
147 extern int map_use_multisampling;
148
149 int use_multisampling = map_use_multisampling || automap_off;
150
151 gld_EnableMultisample(use_multisampling);
152 }
153 }
154
155 void gld_Init(int width, int height)
156 {
157 GLfloat params[4]={0.0f,0.0f,1.0f,0.0f};
158
159 lprintf(LO_DEBUG, "GL_VENDOR: %s\n",glGetString(GL_VENDOR));
160 lprintf(LO_DEBUG, "GL_RENDERER: %s\n",glGetString(GL_RENDERER));
161 lprintf(LO_DEBUG, "GL_VERSION: %s\n",glGetString(GL_VERSION));
162 lprintf(LO_DEBUG, "GL_EXTENSIONS:\n");
163 {
164 char ext_name[256];
165 const char *extensions = (const char*)glGetString(GL_EXTENSIONS);
166 const char *rover = extensions;
167 const char *p = rover;
168
169 while (*rover)
170 {
171 size_t len = 0;
172 p = rover;
173 while (*p && *p != ' ')
174 {
175 p++;
176 len++;
177 }
178 if (*p)
179 {
180 len = MIN(len, sizeof(ext_name)-1);
181 memset(ext_name, 0, sizeof(ext_name));
182 strncpy(ext_name, rover, len);
183 lprintf(LO_DEBUG,"\t%s\n", ext_name);
184 }
185 rover = p;
186 while (*rover && *rover == ' ')
187 rover++;
188 }
189 }
190
191 gld_InitOpenGL();
192
193 dsda_GLSetRenderViewport();
194 dsda_GLSetRenderViewportScissor();
195
196 glClearColor(0.0f, 0.5f, 0.5f, 1.0f);
197 glClearDepth(1.0f);
198
199 glEnable(GL_BLEND);
200 glEnable(GL_DEPTH_CLAMP_NV);
201 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
202 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
203 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // proff_dis
204 glShadeModel(GL_FLAT);
205 gld_EnableTexture2D(GL_TEXTURE0_ARB, true);
206 glDepthFunc(GL_LEQUAL);
207 glEnable(GL_ALPHA_TEST);
208 glAlphaFunc(GL_GREATER, 0.0f);
209 glDisable(GL_CULL_FACE);
210 glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
211
212 glTexGenfv(GL_Q,GL_EYE_PLANE,params);
213 glTexGenf(GL_S,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
214 glTexGenf(GL_T,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
215 glTexGenf(GL_Q,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
216
217 //e6y
218 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
219 glClear(GL_COLOR_BUFFER_BIT);
220 gld_Finish();
221 glClear(GL_COLOR_BUFFER_BIT);
222 glClearColor(0.0f, 0.5f, 0.5f, 1.0f);
223
224 gld_InitLightTable();
225 gld_InitSky();
226 glsl_Init();
227 gld_FlushTextures(); // TODO: should this be here?
228 M_ChangeSkyMode();
229
230 #ifdef HAVE_LIBSDL2_IMAGE
231 gld_InitMapPics();
232 #endif
233
234 // Create FBO object and associated render targets
235 gld_InitFBO();
236 I_AtExit(gld_FreeScreenSizeFBO, true, "gld_FreeScreenSizeFBO", exit_priority_normal);
237
238 gld_ResetLastTexture();
239
240 I_AtExit(gld_CleanMemory, true, "gld_CleanMemory", exit_priority_normal); //e6y
241 }
242
243 void gld_InitCommandLine(void)
244 {
245 }
246
247 //
248 // Textured automap
249 //
250
251 static int C_DECL dicmp_visible_subsectors_by_pic(const void *a, const void *b)
252 {
253 return (*((const subsector_t *const *)b))->sector->floorpic -
254 (*((const subsector_t *const *)a))->sector->floorpic;
255 }
256
257 static int visible_subsectors_count_prev = -1;
258 void gld_ResetTexturedAutomap(void)
259 {
260 visible_subsectors_count_prev = -1;
261 }
262
263 static int map_textured_trans;
264 static int map_textured_overlay_trans;
265 static int map_lines_overlay_trans;
266
267 void gld_ResetAutomapTransparency(void)
268 {
269 map_textured_trans = dsda_IntConfig(dsda_config_map_textured_trans);
270 map_textured_overlay_trans = dsda_IntConfig(dsda_config_map_textured_overlay_trans);
271 map_lines_overlay_trans = dsda_IntConfig(dsda_config_map_lines_overlay_trans);
272 }
273
274 void gld_MapDrawSubsectors(player_t *plr, int fx, int fy, fixed_t mx, fixed_t my, int fw, int fh, fixed_t scale)
275 {
276 static subsector_t **visible_subsectors = NULL;
277 static int visible_subsectors_size = 0;
278 int visible_subsectors_count;
279
280 int i;
281 float alpha;
282 float coord_scale;
283 GLTexture *gltexture;
284
285 alpha = (float)((automap_overlay > 0) ? map_textured_overlay_trans : map_textured_trans) / 100.0f;
286 if (alpha == 0)
287 return;
288
289 if (numsubsectors > visible_subsectors_size)
290 {
291 visible_subsectors_size = numsubsectors;
292 visible_subsectors = Z_Realloc(visible_subsectors, visible_subsectors_size * sizeof(visible_subsectors[0]));
293 }
294
295 visible_subsectors_count = 0;
296 if (dsda_RevealAutomap())
297 {
298 visible_subsectors_count = numsubsectors;
299 }
300 else
301 {
302 for (i = 0; i < numsubsectors; i++)
303 {
304 visible_subsectors_count += map_subsectors[i];
305 }
306 }
307
308 // Do not sort static visible_subsectors array at all
309 // if there are no new visible subsectors.
310 if (visible_subsectors_count != visible_subsectors_count_prev)
311 {
312 visible_subsectors_count_prev = visible_subsectors_count;
313
314 visible_subsectors_count = 0;
315 for (i = 0; i < numsubsectors; i++)
316 {
317 if (map_subsectors[i] || dsda_RevealAutomap())
318 {
319 visible_subsectors[visible_subsectors_count++] = &subsectors[i];
320 }
321 }
322
323 // sort subsectors by texture
324 qsort(visible_subsectors, visible_subsectors_count,
325 sizeof(visible_subsectors[0]), dicmp_visible_subsectors_by_pic);
326 }
327
328 glMatrixMode(GL_MODELVIEW);
329 glPushMatrix();
330 dsda_GLSetScreenSpaceScissor(fx, fy, fw, fh);
331 glEnable(GL_SCISSOR_TEST);
332
333 // Look "down" at the floor, since subsector flats extend along x and z coordinates
334 glRotatef(-90.0, 1.0f, 0.0f, 0.0f);
335
336 if (automap_rotate)
337 {
338 float pivotx = (float)(fx + fw / 2);
339 float pivoty = (float)(fy + fh / 2);
340
341 float rot = (float)(ANG90 - viewangle) / (float)(1u << 31) * 180.0f;
342
343 // Apply the automap's rotation to the vertexes.
344 glTranslatef(pivotx, 0.0f, pivoty);
345 glRotatef(rot, 0.0f, 1.0f, 0.0f);
346 glTranslatef(-pivotx, 0.0f, -pivoty);
347 }
348
349 glTranslatef(
350 (float)fx - (float)mx / (float)FRACUNIT * (float)scale / (float)FRACUNIT,
351 0,
352 (float)fy + (float)fh + (float)my / (float)FRACUNIT * (float)scale / (float)FRACUNIT);
353 coord_scale = (float)scale / (float)(1<<FRACTOMAPBITS) / (float)FRACUNIT * MAP_COEFF;
354 glScalef(-coord_scale, 1.0, -coord_scale);
355
356 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
357 glEnableClientState(GL_VERTEX_ARRAY);
358 glDisableClientState(GL_COLOR_ARRAY);
359
360 if (gl_ext_arb_vertex_buffer_object)
361 GLEXT_glBindBufferARB(GL_ARRAY_BUFFER, flats_vbo_id);
362 glVertexPointer(3, GL_FLOAT, sizeof(flats_vbo[0]), flats_vbo_x);
363 glTexCoordPointer(2, GL_FLOAT, sizeof(flats_vbo[0]), flats_vbo_u);
364
365 for (i = 0; i < visible_subsectors_count; i++)
366 {
367 subsector_t *sub = visible_subsectors[i];
368 int ssidx = sub - subsectors;
369
370 if (sub->sector->bbox[BOXLEFT] > am_frame.bbox[BOXRIGHT] ||
371 sub->sector->bbox[BOXRIGHT] < am_frame.bbox[BOXLEFT] ||
372 sub->sector->bbox[BOXBOTTOM] > am_frame.bbox[BOXTOP] ||
373 sub->sector->bbox[BOXTOP] < am_frame.bbox[BOXBOTTOM] ||
374 sub->sector->flags & SECF_HIDDEN)
375 {
376 continue;
377 }
378
379 gltexture = gld_RegisterFlat(flattranslation[sub->sector->floorpic], true, V_IsUILightmodeIndexed());
380 if (gltexture)
381 {
382 sector_t tempsec;
383 int floorlight;
384 float light;
385 dboolean transform;
386 int loopnum;
387
388 // For lighting and texture determination
389 sector_t *sec = R_FakeFlat(sub->sector, &tempsec, &floorlight, NULL, false);
390
391 gld_BindFlat(gltexture, 0);
392 light = gld_Calc2DLightLevel(floorlight);
393 gld_StaticLightAlpha(light, alpha);
394
395 transform = sec->floor_xoffs ||
396 sec->floor_yoffs ||
397 sec->floor_rotation ||
398 sec->floor_xscale != FRACUNIT ||
399 sec->floor_yscale != FRACUNIT;
400
401 if (transform)
402 {
403 float uoffs;
404 float voffs;
405 float rotation;
406 float xscale;
407 float yscale;
408
409 uoffs = (float) sec->floor_xoffs / (64 * FRACUNIT);
410 voffs = (float) sec->floor_yoffs / (64 * FRACUNIT);
411 rotation = ((float) sec->floor_rotation / ANG45) * 45;
412 xscale = (float) sec->floor_xscale / FRACUNIT;
413 yscale = (float) sec->floor_yscale / FRACUNIT;
414
415 glMatrixMode(GL_TEXTURE);
416 glPushMatrix();
417 glScalef(xscale, yscale, 1.f);
418 glTranslatef(uoffs, voffs, 0.f);
419 glRotatef(-rotation, 0.f, 0.f, 1.f);
420 }
421
422 for (loopnum = 0; loopnum < subsectorloops[ssidx].loopcount; loopnum++)
423 {
424 GLLoopDef *currentloop = &subsectorloops[ssidx].loops[loopnum];
425
426 glDrawArrays(currentloop->mode,currentloop->vertexindex,currentloop->vertexcount);
427 }
428
429 if (transform)
430 {
431 glPopMatrix();
432 }
433 }
434 }
435
436 glMatrixMode(GL_MODELVIEW);
437 glPopMatrix();
438 glDisable(GL_SCISSOR_TEST);
439
440 if (gl_ext_arb_vertex_buffer_object)
441 GLEXT_glBindBufferARB(GL_ARRAY_BUFFER, 0);
442
443 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
444 glDisableClientState(GL_VERTEX_ARRAY);
445 glDisableClientState(GL_COLOR_ARRAY);
446 }
447
448 void gld_DrawTriangleStrip(GLWall *wall, gl_strip_coords_t *c)
449 {
450 glBegin(GL_TRIANGLE_STRIP);
451
452 glTexCoord2fv((const GLfloat*)&c->t[0]);
453 glVertex3fv((const GLfloat*)&c->v[0]);
454
455 glTexCoord2fv((const GLfloat*)&c->t[1]);
456 glVertex3fv((const GLfloat*)&c->v[1]);
457
458 glTexCoord2fv((const GLfloat*)&c->t[2]);
459 glVertex3fv((const GLfloat*)&c->v[2]);
460
461 glTexCoord2fv((const GLfloat*)&c->t[3]);
462 glVertex3fv((const GLfloat*)&c->v[3]);
463
464 glEnd();
465 }
466
467 void gld_BeginUIDraw(void)
468 {
469 gld_InitColormapTextures(true);
470 glsl_PushMainShader();
471 gl_ui_lightmode_indexed = true;
472 }
473
474 void gld_EndUIDraw(void)
475 {
476 gl_ui_lightmode_indexed = false;
477 glsl_PopMainShader();
478 }
479
480 void gld_BeginAutomapDraw(void)
481 {
482 gld_InitColormapTextures(true);
483 glsl_PushNullShader();
484 gl_automap_lightmode_indexed = true;
485 }
486
487 void gld_EndAutomapDraw(void)
488 {
489 gl_automap_lightmode_indexed = false;
490 glsl_PopNullShader();
491 }
492
493 void gld_BeginMenuDraw(void)
494 {
495 gld_InitColormapTextures(true);
496 glsl_PushNullShader();
497 gl_menu_lightmode_indexed = true;
498 }
499
500 void gld_EndMenuDraw(void)
501 {
502 gl_menu_lightmode_indexed = false;
503 glsl_PopNullShader();
504 }
505
506 void gld_DrawNumPatch_f(float x, float y, int lump, dboolean center, int cm, enum patch_translation_e flags)
507 {
508 GLTexture *gltexture;
509 float fU1,fU2,fV1,fV2;
510 float width,height;
511 float xpos, ypos;
512 int cmap;
513 int leftoffset, topoffset;
514
515 cmap = ((flags & VPT_TRANS) ? cm : CR_DEFAULT);
516 gltexture=gld_RegisterPatch(lump, cmap, false, V_IsUILightmodeIndexed());
517 gld_BindPatch(gltexture, cmap);
518
519 if (!gltexture)
520 return;
521 fV1=0.0f;
522 fV2=gltexture->scaleyfac;
523 if (flags & VPT_FLIP)
524 {
525 fU1=gltexture->scalexfac;
526 fU2=0.0f;
527 }
528 else
529 {
530 fU1=0.0f;
531 fU2=gltexture->scalexfac;
532 }
533
534 if (flags & VPT_NOOFFSET)
535 {
536 leftoffset = 0;
537 topoffset = 0;
538 }
539 else
540 {
541 leftoffset = gltexture->leftoffset;
542 topoffset = gltexture->topoffset;
543 }
544
545 // [FG] automatically center wide patches without horizontal offset
546 if (center)
547 {
548 if (gltexture->width > 320 && leftoffset == 0)
549 x -= (float)(gltexture->width - 320) / 2;
550 }
551
552 if (flags & VPT_STRETCH_MASK)
553 {
554 stretch_param_t *params = dsda_StretchParams(flags);
555
556 xpos = (float)((x - leftoffset) * params->video->width) / 320.0f + params->deltax1;
557 ypos = (float)((y - topoffset) * params->video->height) / 200.0f + params->deltay1;
558 width = (float)(gltexture->realtexwidth * params->video->width) / 320.0f;
559 height = (float)(gltexture->realtexheight * params->video->height) / 200.0f;
560
561 if (TOP_ALIGNMENT(flags & VPT_STRETCH_MASK))
562 ypos += global_patch_top_offset;
563 }
564 else
565 {
566 xpos = (float)(x - leftoffset);
567 ypos = (float)(y - topoffset);
568 width = (float)(gltexture->realtexwidth);
569 height = (float)(gltexture->realtexheight);
570 }
571
572 // e6y
573 // This is a workaround for some on-board Intel video cards.
574 // Do you know more elegant solution?
575 glColor3f(1.0f, 1.0f, 1.0f);
576
577 glBegin(GL_TRIANGLE_STRIP);
578 glTexCoord2f(fU1, fV1); glVertex2f((xpos),(ypos));
579 glTexCoord2f(fU1, fV2); glVertex2f((xpos),(ypos+height));
580 glTexCoord2f(fU2, fV1); glVertex2f((xpos+width),(ypos));
581 glTexCoord2f(fU2, fV2); glVertex2f((xpos+width),(ypos+height));
582 glEnd();
583 }
584
585 void gld_DrawNumPatch(int x, int y, int lump, dboolean center, int cm, enum patch_translation_e flags)
586 {
587 gld_DrawNumPatch_f((float)x, (float)y, lump, center, cm, flags);
588 }
589
590 void gld_FillRaw(int lump, int x, int y, int src_width, int src_height, int dst_width, int dst_height, enum patch_translation_e flags)
591 {
592 GLTexture *gltexture;
593 float fU1, fU2, fV1, fV2;
594
595 //e6y: Boom colormap should not be applied for background
596 int saved_boom_cm = boom_cm;
597 boom_cm = 0;
598
599 gltexture = gld_RegisterRaw(lump, src_width, src_height, false, V_IsUILightmodeIndexed());
600 gld_BindRaw(gltexture, 0);
601
602 //e6y
603 boom_cm = saved_boom_cm;
604
605 if (!gltexture)
606 return;
607
608 fU1 = 0;
609 fV1 = 0;
610
611 // [XA] ...this flag means "stretch". welp.
612 if (flags & VPT_STRETCH_REAL)
613 {
614 fU2 = 1.0f;
615 fV2 = 1.0f;
616 }
617 else if (flags & VPT_STRETCH)
618 {
619 stretch_param_t *params = dsda_StretchParams(flags);
620
621 fU2 = (float)dst_width / (float)gltexture->realtexwidth / (params->video->width / 320.f);
622 fV2 = (float)dst_height / (float)gltexture->realtexheight / (params->video->height / 200.f);
623 }
624 else
625 {
626 fU2 = (float)dst_width / (float)gltexture->realtexwidth;
627 fV2 = (float)dst_height / (float)gltexture->realtexheight;
628 }
629
630 glBegin(GL_TRIANGLE_STRIP);
631 glTexCoord2f(fU1, fV1); glVertex2f((float)(x),(float)(y));
632 glTexCoord2f(fU1, fV2); glVertex2f((float)(x),(float)(y + dst_height));
633 glTexCoord2f(fU2, fV1); glVertex2f((float)(x + dst_width),(float)(y));
634 glTexCoord2f(fU2, fV2); glVertex2f((float)(x + dst_width),(float)(y + dst_height));
635 glEnd();
636 }
637
638 void gld_FillPatch(int lump, int x, int y, int width, int height, enum patch_translation_e flags)
639 {
640 GLTexture *gltexture;
641 float fU1, fU2, fV1, fV2;
642
643 //e6y: Boom colormap should not be applied for background
644 int saved_boom_cm = boom_cm;
645 boom_cm = 0;
646
647 gltexture = gld_RegisterPatch(lump, CR_DEFAULT, false, V_IsUILightmodeIndexed());
648 gld_BindPatch(gltexture, CR_DEFAULT);
649
650 if (!gltexture)
651 return;
652
653 x = x - gltexture->leftoffset;
654 y = y - gltexture->topoffset;
655
656 //e6y
657 boom_cm = saved_boom_cm;
658
659 if (flags & VPT_STRETCH)
660 {
661 x = x * SCREENWIDTH / 320;
662 y = y * SCREENHEIGHT / 200;
663 width = width * SCREENWIDTH / 320;
664 height = height * SCREENHEIGHT / 200;
665 }
666
667 fU1 = 0;
668 fV1 = 0;
669 fU2 = (float)width / (float)gltexture->realtexwidth;
670 fV2 = (float)height / (float)gltexture->realtexheight;
671
672 glBegin(GL_TRIANGLE_STRIP);
673 glTexCoord2f(fU1, fV1); glVertex2f((float)(x),(float)(y));
674 glTexCoord2f(fU1, fV2); glVertex2f((float)(x),(float)(y + height));
675 glTexCoord2f(fU2, fV1); glVertex2f((float)(x + width),(float)(y));
676 glTexCoord2f(fU2, fV2); glVertex2f((float)(x + width),(float)(y + height));
677 glEnd();
678 }
679
680 // [XA] some UI functions may run before fullcolormap has
681 // been initialized, since that's is done in SetupFrame;
682 // use colormaps[0] as a fallback in such a case.
683 const lighttable_t *gld_GetActiveColormap()
684 {
685 if (V_IsAutomapLightmodeIndexed() || V_IsMenuLightmodeIndexed())
686 return colormaps[0];
687 else if (fixedcolormap)
688 return fixedcolormap;
689 else if (fullcolormap)
690 return fullcolormap;
691 else
692 return colormaps[0];
693 }
694
695 // [XA] quicky indexed color-lookup function for a couple
696 // of things that aren't done in the shader for the indexed
697 // lightmode. ideally this will go away in the future.
698 color_rgb_t gld_LookupIndexedColor(int index, dboolean usecolormap)
699 {
700 color_rgb_t color;
701 const unsigned char *playpal;
702
703 if (usecolormap)
704 {
705 int gtlump = W_CheckNumForName2("GAMMATBL", ns_prboom);
706 const byte * gtable = (const byte*)W_LumpByNum(gtlump) + 256 * usegamma;
707 const lighttable_t *colormap = gld_GetActiveColormap();
708
709 playpal = V_GetPlaypal() + (gld_paletteIndex*PALETTE_SIZE);
710
711 color.r = gtable[playpal[colormap[index] * 3 + 0]];
712 color.g = gtable[playpal[colormap[index] * 3 + 1]];
713 color.b = gtable[playpal[colormap[index] * 3 + 2]];
714 }
715 else
716 {
717 playpal = V_GetPlaypal();
718
719 color.r = playpal[3 * index + 0];
720 color.g = playpal[3 * index + 1];
721 color.b = playpal[3 * index + 2];
722 }
723
724 return color;
725 }
726
727 void gld_DrawLine_f(float x0, float y0, float x1, float y1, int BaseColor)
728 {
729 color_rgb_t color;
730 unsigned char a;
731 map_line_t *line;
732
733 a = ((automap_overlay == 1) ? map_lines_overlay_trans * 255 / 100 : 255);
734 if (a == 0)
735 return;
736
737 color = gld_LookupIndexedColor(BaseColor, V_IsUILightmodeIndexed() || V_IsAutomapLightmodeIndexed() || V_IsMenuLightmodeIndexed());
738
739 line = M_ArrayGetNewItem(&map_lines, sizeof(line[0]));
740
741 line->point[0].x = x0;
742 line->point[0].y = y0;
743 line->point[0].r = color.r;
744 line->point[0].g = color.g;
745 line->point[0].b = color.b;
746 line->point[0].a = a;
747
748 line->point[1].x = x1;
749 line->point[1].y = y1;
750 line->point[1].r = color.r;
751 line->point[1].g = color.g;
752 line->point[1].b = color.b;
753 line->point[1].a = a;
754 }
755
756 void gld_DrawLine(int x0, int y0, int x1, int y1, int BaseColor)
757 {
758 gld_DrawLine_f((float)x0, (float)y0, (float)x1, (float)y1, BaseColor);
759 }
760
761
762 void gld_StartFuzz(int sprite, float ratio)
763 {
764 color_rgb_t color;
765
766 // shader init
767 glsl_PushFuzzShader(gametic, sprite, ratio);
768 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
769
770 // for indexed lightmode, the fuzz color needs to take
771 // pain/item fades and gamma into account, so do a color
772 // lookup based on the closest-to-black color index.
773 color = gld_LookupIndexedColor(invul_cm ? playpal_lightest : playpal_darkest, true);
774 glColor3f((float)color.r/255.0f,
775 (float)color.g/255.0f,
776 (float)color.b/255.0f);
777 }
778
779 void gld_EndFuzz()
780 {
781 glsl_PopFuzzShader();
782 }
783
784 void gld_DrawWeapon(int weaponlump, vissprite_t *vis, int lightlevel)
785 {
786 GLTexture *gltexture;
787 float fU1,fU2,fV1,fV2;
788 float x1,y1,x2,y2;
789 float light;
790
791 gltexture=gld_RegisterPatch(firstspritelump+weaponlump, CR_DEFAULT, false, true);
792 if (!gltexture)
793 return;
794 gld_BindPatch(gltexture, CR_DEFAULT);
795 fU1=0;
796 fV1=0;
797 fU2=gltexture->scalexfac;
798 fV2=gltexture->scaleyfac;
799 // e6y
800 // More precise weapon drawing:
801 // Shotgun from DSV3_War looks correctly now. Especially during movement.
802 // There is no more line of graphics under certain weapons.
803 x1 = vis->x1;
804 x2 = roundf(x1 + gltexture->realtexwidth * pspritexscale_f);
805 y1 = roundf(viewheight / 2.0 - vis->texturemid * pspriteyscale_f / FRACUNIT);
806 y2 = roundf(y1 + gltexture->realtexheight * pspriteyscale_f);
807 // e6y: don't do the gamma table correction on the lighting
808 light = (float)lightlevel / 255.0f;
809
810 // e6y
811 // Fix of no warning (flashes between shadowed and solid)
812 // when invisibility is about to go
813 if (/*(viewplayer->mo->flags & MF_SHADOW) && */!vis->colormap)
814 {
815 gld_StartFuzz(-1, 0);
816 }
817 else
818 {
819 if (viewplayer->mo->flags & g_mf_translucent)
820 gld_StaticLightAlpha(light,(float)tran_filter_pct/100.0f);
821 else
822 gld_StaticLight(light);
823 }
824 glBegin(GL_TRIANGLE_STRIP);
825 glTexCoord2f(fU1, fV1);
826 glVertex2f(x1, y1);
827 glTexCoord2f(fU1, fV2);
828 glVertex2f(x1, y2);
829 glTexCoord2f(fU2, fV1);
830 glVertex2f(x2, y1);
831 glTexCoord2f(fU2, fV2);
832 glVertex2f(x2, y2);
833 glEnd();
834 if(!vis->colormap)
835 {
836 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
837 }
838 glColor3f(1.0f,1.0f,1.0f);
839 if (/*(viewplayer->mo->flags & MF_SHADOW) && */!vis->colormap)
840 gld_EndFuzz();
841 }
842
843 void gld_FillBlock(int x, int y, int width, int height, int col)
844 {
845 color_rgb_t color = gld_LookupIndexedColor(col, V_IsUILightmodeIndexed() || V_IsAutomapLightmodeIndexed() || V_IsMenuLightmodeIndexed());
846
847 glsl_PushNullShader();
848
849 gld_EnableTexture2D(GL_TEXTURE0_ARB, false);
850
851 glColor3f((float)color.r/255.0f,
852 (float)color.g/255.0f,
853 (float)color.b/255.0f);
854
855 glBegin(GL_TRIANGLE_STRIP);
856 glVertex2i( x, y );
857 glVertex2i( x, y+height );
858 glVertex2i( x+width, y );
859 glVertex2i( x+width, y+height );
860 glEnd();
861 glColor3f(1.0f,1.0f,1.0f);
862 gld_EnableTexture2D(GL_TEXTURE0_ARB, true);
863
864 glsl_PopNullShader();
865 }
866
867 void gld_DrawShaded(int x, int y, int width, int height, int shade)
868 {
869 color_rgb_t color = gld_LookupIndexedColor(playpal_darkest, V_IsAutomapLightmodeIndexed() || V_IsMenuLightmodeIndexed());
870
871 glsl_PushNullShader();
872
873 gld_EnableTexture2D(GL_TEXTURE0_ARB, false);
874
875 glColor4f((float)color.r/255.0f,
876 (float)color.g/255.0f,
877 (float)color.b/255.0f,
878 (float)shade/30);
879
880 glBegin(GL_TRIANGLE_STRIP);
881 glVertex2i( x, y );
882 glVertex2i( x, y+height );
883 glVertex2i( x+width, y );
884 glVertex2i( x+width, y+height );
885 glEnd();
886 glColor4f(1.0f,1.0f,1.0f,1.0f);
887 gld_EnableTexture2D(GL_TEXTURE0_ARB, true);
888
889 glsl_PopNullShader();
890 }
891
892 void gld_SetPalette(int palette)
893 {
894 static int last_palette = 0;
895
896 if (palette < 0)
897 palette = last_palette;
898 last_palette = palette;
899
900 // [XA] store the current palette so
901 // the indexed lightmode can use it.
902 gld_SetIndexedPalette(palette);
903 }
904
905 unsigned char *gld_ReadScreen(void)
906 { // NSM convert to static
907 static unsigned char *scr = NULL;
908 static unsigned char *buffer = NULL;
909 static int scr_size = 0;
910
911 int src_row, dest_row, size, pixels_per_row;
912
913 pixels_per_row = renderer_rect.w * 3;
914 size = pixels_per_row * renderer_rect.h;
915 if (!scr || size > scr_size)
916 {
917 scr_size = size;
918 scr = Z_Realloc(scr, size);
919 buffer = Z_Realloc(buffer, size);
920 }
921
922 if (buffer && scr)
923 {
924 GLint pack_aligment;
925 glGetIntegerv(GL_PACK_ALIGNMENT, &pack_aligment);
926 glPixelStorei(GL_PACK_ALIGNMENT, 1);
927
928 glFlush();
929 glReadPixels(0, 0, renderer_rect.w, renderer_rect.h, GL_RGB, GL_UNSIGNED_BYTE, scr);
930
931 glPixelStorei(GL_PACK_ALIGNMENT, pack_aligment);
932
933 // GL textures are bottom up, so copy the rows in reverse to flip vertically
934 for (src_row = renderer_rect.h - 1, dest_row = 0; src_row >= 0; --src_row, ++dest_row)
935 {
936 memcpy(&buffer[dest_row * pixels_per_row],
937 &scr[src_row * pixels_per_row],
938 pixels_per_row);
939 }
940 }
941
942 return buffer;
943 }
944
945 GLvoid gld_Set2DMode(void)
946 {
947 glMatrixMode(GL_MODELVIEW);
948 glLoadIdentity();
949 glMatrixMode(GL_PROJECTION);
950 glLoadIdentity();
951 glOrtho(
952 (GLdouble) 0,
953 (GLdouble) SCREENWIDTH,
954 (GLdouble) SCREENHEIGHT,
955 (GLdouble) 0,
956 (GLdouble) -1.0,
957 (GLdouble) 1.0
958 );
959 glDisable(GL_DEPTH_TEST);
960 }
961
962 void gld_InitDrawScene(void)
963 {
964 gld_ResetDrawInfo();
965 }
966
967 void gld_Finish(void)
968 {
969 gld_Set2DMode();
970 I_HandleCapture();
971 SDL_GL_SwapWindow(sdl_window);
972 }
973
974 GLuint flats_vbo_id = 0; // ID of VBO
975
976 vbo_xyz_uv_t *flats_vbo = NULL;
977
978 GLSeg *gl_segs=NULL;
979 GLSeg *gl_lines=NULL;
980
981 int rendermarker=0;
982 byte *segrendered; // true if sector rendered (only here for malloc)
983 int *linerendered[2]; // true if linedef rendered (only here for malloc)
984
985 float roll = 0.0f;
986 float yaw = 0.0f;
987 float inv_yaw = 0.0f;
988 float pitch = 0.0f;
989 float cos_inv_yaw, sin_inv_yaw;
990 float paperitems_pitch;
991 float cos_paperitems_pitch, sin_paperitems_pitch;
992
993 #define __glPi 3.14159265358979323846
994
995 void gld_Clear(void)
996 {
997 int clearbits = 0;
998
999 // flashing red HOM indicators
1000 if (dsda_IntConfig(dsda_config_flashing_hom))
1001 {
1002 clearbits |= GL_COLOR_BUFFER_BIT;
1003 glClearColor (gametic % 20 < 9 ? 1.0f : 0.0f, 0.0f, 0.0f, 1.0f);
1004 }
1005
1006 if (gl_use_stencil)
1007 clearbits |= GL_STENCIL_BUFFER_BIT;
1008
1009 clearbits |= GL_DEPTH_BUFFER_BIT;
1010
1011 if (clearbits)
1012 glClear(clearbits);
1013 }
1014
1015 void gld_StartDrawScene(void)
1016 {
1017 gld_MultisamplingSet();
1018
1019 gld_SetPalette(-1);
1020
1021 glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
1022 glScissor(0, SCREENHEIGHT - viewheight, viewwidth, viewheight);
1023 glEnable(GL_SCISSOR_TEST);
1024 // Player coordinates
1025 xCamera=-(float)viewx/MAP_SCALE;
1026 yCamera=(float)viewy/MAP_SCALE;
1027 zCamera=(float)viewz/MAP_SCALE;
1028
1029 yaw=270.0f-(float)(viewangle>>ANGLETOFINESHIFT)*360.0f/FINEANGLES;
1030 inv_yaw=180.0f-yaw;
1031 cos_inv_yaw = (float)cos(inv_yaw * M_PI / 180.f);
1032 sin_inv_yaw = (float)sin(inv_yaw * M_PI / 180.f);
1033
1034 gl_spriteindex = 0;
1035
1036 pitch = ANGLE_T_TO_PITCH_F(viewpitch);
1037 viewPitch = (pitch > 180 ? pitch - 360 : pitch);
1038 paperitems_pitch = ((pitch > 87.0f && pitch <= 90.0f) ? 87.0f : pitch);
1039
1040 skyXShift = (double) viewangle / (double) ANGLE_MAX;
1041
1042 if (skystretch)
1043 skyYShift = BETWEEN(-25 / skyscale, 180, viewPitch) / 360.0;
1044 else
1045 skyYShift = viewPitch / 360.0;
1046
1047 cos_paperitems_pitch = (float)cos(paperitems_pitch * M_PI / 180.f);
1048 sin_paperitems_pitch = (float)sin(paperitems_pitch * M_PI / 180.f);
1049
1050 gld_InitFrameSky();
1051
1052 invul_cm = (players[displayplayer].fixedcolormap == 32);
1053
1054 // elim - Always enabled (when supported) for upscaling with GL exclusive disabled
1055 SceneInTexture = gl_ext_framebuffer_object;
1056
1057 // Vortex: Set FBO object
1058 if (SceneInTexture)
1059 {
1060 GLEXT_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, glSceneImageFBOTexID);
1061 }
1062
1063 SetFrameTextureMode();
1064
1065 gld_Clear();
1066
1067 glEnable(GL_DEPTH_TEST);
1068
1069 glMatrixMode(GL_PROJECTION);
1070 glLoadMatrixf(projMatrix);
1071
1072 glMatrixMode(GL_MODELVIEW);
1073 glLoadMatrixf(modelMatrix);
1074
1075 gld_InitColormapTextures(false);
1076 gld_InitFuzzTexture();
1077
1078 rendermarker++;
1079 scene_has_overlapped_sprites = false;
1080 }
1081
1082 void gld_EndDrawScene(void)
1083 {
1084 glDisable(GL_POLYGON_SMOOTH);
1085
1086 glViewport(0, 0, SCREENWIDTH, SCREENHEIGHT);
1087 gld_Set2DMode();
1088
1089 glsl_PushMainShader();
1090 R_DrawPlayerSprites();
1091 glsl_PopMainShader();
1092
1093 // e6y
1094 // Effect of invulnerability uses a colormap instead of hard-coding now
1095 // See nuts.wad
1096 // http://www.doomworld.com/idgames/index.php?id=11402
1097
1098 // Vortex: Black and white effect
1099 if (SceneInTexture)
1100 {
1101 // Vortex: Restore original RT
1102 GLEXT_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1103
1104 glBindTexture(GL_TEXTURE_2D, glSceneImageTextureFBOTexID);
1105
1106 // Setup blender
1107 glColor3f(1.0f, 1.0f, 1.0f);
1108
1109 // Setup GL camera for drawing the render texture
1110 dsda_GLFullscreenOrtho2D();
1111 dsda_GLSetRenderViewport();
1112 // elim - Prevent undrawn parts of game scene texture being rendered into the viewport
1113 dsda_GLSetRenderSceneScissor();
1114 glBegin(GL_TRIANGLE_STRIP);
1115 {
1116 glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, 0.0f);
1117 glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, renderer_rect.h);
1118 glTexCoord2f(1.0f, 1.0f); glVertex2f((float)renderer_rect.w, 0.0f);
1119 glTexCoord2f(1.0f, 0.0f); glVertex2f((float)renderer_rect.w, (float)renderer_rect.h);
1120 }
1121 glEnd();
1122
1123 // elim - Set the scissor back to the full viewport so post-scene draws can happen (ie StatusBar)
1124 dsda_GLSetRenderViewportScissor();
1125
1126 gld_Set2DMode();
1127
1128 glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
1129 }
1130
1131 glColor3f(1.0f,1.0f,1.0f);
1132 glDisable(GL_SCISSOR_TEST);
1133 glDisable(GL_ALPHA_TEST);
1134 }
1135
1136 static void gld_AddDrawWallItem(GLDrawItemType itemtype, void *itemdata)
1137 {
1138 gld_AddDrawItem(itemtype, itemdata);
1139 }
1140
1141 /*****************
1142 * *
1143 * Walls *
1144 * *
1145 *****************/
1146
1147 static void gld_DrawWall(GLWall *wall)
1148 {
1149 unsigned int flags;
1150
1151 dsda_RecordDrawSeg();
1152
1153 // Do not repeat middle texture vertically
1154 // to avoid visual glitches for textures with holes
1155 if ((wall->flag == GLDWF_M2S) && (wall->flag < GLDWF_SKY))
1156 flags = GLTEXTURE_CLAMPY;
1157 else
1158 flags = 0;
1159
1160 gld_BindTexture(wall->gltexture, flags, false);
1161
1162 if (!wall->gltexture)
1163 {
1164 glColor4f(1.0f,0.0f,0.0f,1.0f);
1165 }
1166
1167 if ((wall->flag == GLDWF_TOPFLUD) || (wall->flag == GLDWF_BOTFLUD))
1168 {
1169 gl_strip_coords_t c;
1170
1171 gld_BindFlat(wall->gltexture, 0);
1172
1173 gld_SetupFloodStencil(wall);
1174 gld_SetupFloodedPlaneCoords(wall, &c);
1175 gld_SetupFloodedPlaneLight(wall);
1176 gld_DrawTriangleStrip(wall, &c);
1177
1178 gld_ClearFloodStencil(wall);
1179 }
1180 else
1181 {
1182 gld_StaticLightAlpha(wall->light, wall->alpha);
1183
1184 glBegin(GL_TRIANGLE_FAN);
1185
1186 // lower left corner
1187 glTexCoord2f(wall->ul,wall->vb);
1188 glVertex3f(wall->glseg->x1,wall->ybottom,wall->glseg->z1);
1189
1190 // split left edge of wall
1191 if (!wall->glseg->fracleft)
1192 gld_SplitLeftEdge(wall);
1193
1194 // upper left corner
1195 glTexCoord2f(wall->ul,wall->vt);
1196 glVertex3f(wall->glseg->x1,wall->ytop,wall->glseg->z1);
1197
1198 // upper right corner
1199 glTexCoord2f(wall->ur,wall->vt);
1200 glVertex3f(wall->glseg->x2,wall->ytop,wall->glseg->z2);
1201
1202 // split right edge of wall
1203 if (!wall->glseg->fracright)
1204 gld_SplitRightEdge(wall);
1205
1206 // lower right corner
1207 glTexCoord2f(wall->ur,wall->vb);
1208 glVertex3f(wall->glseg->x2,wall->ybottom,wall->glseg->z2);
1209
1210 glEnd();
1211 }
1212 }
1213
1214 static void gld_CalculateWallY(GLWall *wall, float *lineheight,
1215 fixed_t floor_height, fixed_t ceiling_height)
1216 {
1217 wall->ytop = (float) ceiling_height / (float) MAP_SCALE;
1218 wall->ybottom = (float) floor_height / (float) MAP_SCALE;
1219 *lineheight = (float) fabs((float) (ceiling_height - floor_height) / FRACUNIT);
1220 }
1221
1222 static void gld_CalculateWallU(GLWall *wall, seg_t *seg, int backseg,
1223 float linelength, fixed_t specific_offset)
1224 {
1225 float scaled_texwidth;
1226 float scaled_textureoffset;
1227
1228 scaled_texwidth = (float) wall->gltexture->buffer_width / wall->xscale;
1229 scaled_textureoffset =
1230 (float) (seg->sidedef->textureoffset + specific_offset) / wall->xscale / FRACUNIT;
1231
1232 if (backseg)
1233 {
1234 wall->ur = scaled_textureoffset / scaled_texwidth;
1235 wall->ul = wall->ur + linelength / scaled_texwidth;
1236 }
1237 else
1238 {
1239 wall->ul = scaled_textureoffset / scaled_texwidth;
1240 wall->ur = wall->ul + linelength / scaled_texwidth;
1241 }
1242 }
1243
1244 static void gld_CalculateWallV(GLWall *wall, seg_t *seg, int peg,
1245 float lineheight, fixed_t specific_offset)
1246 {
1247 float scaled_texheight;
1248 float scaled_rowoffset;
1249
1250 scaled_texheight = (float) wall->gltexture->buffer_height / wall->yscale;
1251 scaled_rowoffset =
1252 (float) (seg->sidedef->rowoffset + specific_offset) / wall->yscale / FRACUNIT;
1253
1254 if (peg)
1255 {
1256 wall->vb = scaled_rowoffset / scaled_texheight + wall->gltexture->scaleyfac;
1257 wall->vt = wall->vb - lineheight / scaled_texheight;
1258 }
1259 else
1260 {
1261 wall->vt = scaled_rowoffset / scaled_texheight + wall->gltexture->scaleyfac;
1262 wall->vb = wall->vt + lineheight / scaled_texheight;
1263 }
1264 }
1265
1266 void gld_AddWall(seg_t *seg)
1267 {
1268 extern sector_t *poly_frontsector;
1269 extern dboolean poly_add_line;
1270 GLWall wall;
1271 GLTexture *temptex;
1272 sector_t *frontsector;
1273 sector_t *backsector;
1274 sector_t ftempsec; // needed for R_FakeFlat
1275 sector_t btempsec; // needed for R_FakeFlat
1276 float lineheight, linelength;
1277 int base_lightlevel;
1278 int backseg;
1279 dboolean fix_sky_bleed = false;
1280
1281 int side = (seg->sidedef == &sides[seg->linedef->sidenum[0]] ? 0 : 1);
1282 if (linerendered[side][seg->linedef->iLineID] == rendermarker)
1283 return;
1284 linerendered[side][seg->linedef->iLineID] = rendermarker;
1285 linelength = lines[seg->linedef->iLineID].texel_length;
1286 wall.glseg=&gl_lines[seg->linedef->iLineID];
1287 backseg = seg->sidedef != &sides[seg->linedef->sidenum[0]];
1288
1289 if (poly_add_line)
1290 {
1291 int i = seg->linedef->iLineID;
1292 // hexen_note: find some way to do this only on update?
1293 gl_lines[i].x1=-(float)lines[i].v1->x/(float)MAP_SCALE;
1294 gl_lines[i].z1= (float)lines[i].v1->y/(float)MAP_SCALE;
1295 gl_lines[i].x2=-(float)lines[i].v2->x/(float)MAP_SCALE;
1296 gl_lines[i].z2= (float)lines[i].v2->y/(float)MAP_SCALE;
1297
1298 if (!poly_frontsector)
1299 return;
1300 frontsector=R_FakeFlat(poly_frontsector, &ftempsec, NULL, NULL, false); // for boom effects
1301 }
1302 else
1303 {
1304 if (!seg->frontsector)
1305 return;
1306 frontsector=R_FakeFlat(seg->frontsector, &ftempsec, NULL, NULL, false); // for boom effects
1307 }
1308
1309 if (!frontsector)
1310 return;
1311
1312 base_lightlevel = frontsector->lightlevel + gld_GetGunFlashLight();
1313
1314 R_AddContrast(seg, &base_lightlevel);
1315
1316 wall.alpha=1.0f;
1317 wall.gltexture=NULL;
1318 wall.seg = seg; //e6y
1319
1320 if (!seg->backsector) /* onesided */
1321 {
1322 wall.light = gld_CalcLightLevel(R_MidLightLevel(seg->sidedef, base_lightlevel));
1323 wall.xscale = (float) seg->sidedef->scalex_mid / FRACUNIT;
1324 wall.yscale = (float) seg->sidedef->scaley_mid / FRACUNIT;
1325
1326 if (frontsector->ceilingpic==skyflatnum)
1327 {
1328 wall.ytop=MAXCOORD*2; // Simply using MAXCOORD would result in HOM when the floor is at a height close to the limit
1329 wall.ybottom=(float)frontsector->ceilingheight/MAP_SCALE;
1330 gld_AddSkyTexture(&wall, frontsector->ceilingsky, frontsector->ceilingsky, SKY_CEILING);
1331 }
1332 if (frontsector->floorpic==skyflatnum)
1333 {
1334 wall.ytop=(float)frontsector->floorheight/MAP_SCALE;
1335 wall.ybottom=-MAXCOORD*2; // Simply using MAXCOORD would result in HOM when the ceiling is at a height close to the limit
1336 gld_AddSkyTexture(&wall, frontsector->floorsky, frontsector->floorsky, SKY_FLOOR);
1337 }
1338 temptex=gld_RegisterTexture(texturetranslation[seg->sidedef->midtexture], true, false, true, false);
1339 if (temptex && frontsector->ceilingheight > frontsector->floorheight)
1340 {
1341 wall.gltexture=temptex;
1342 wall.flag = GLDWF_M1S;
1343 gld_CalculateWallY(&wall, &lineheight, frontsector->floorheight, frontsector->ceilingheight);
1344 gld_CalculateWallU(&wall, seg, backseg, linelength, seg->sidedef->textureoffset_mid);
1345 gld_CalculateWallV(&wall, seg, seg->linedef->flags & ML_DONTPEGBOTTOM, lineheight,
1346 seg->sidedef->rowoffset_mid);
1347 gld_AddDrawWallItem(GLDIT_WALL, &wall);
1348 }
1349 }
1350 else /* twosided */
1351 {
1352 sector_t *fs, *bs;
1353 int toptexture, midtexture, bottomtexture;
1354 fixed_t floor_height,ceiling_height;
1355 fixed_t max_floor, min_floor;
1356 fixed_t max_ceiling, min_ceiling;
1357 //fixed_t max_floor_tex, min_ceiling_tex;
1358
1359 backsector=R_FakeFlat(seg->backsector, &btempsec, NULL, NULL, true); // for boom effects
1360 if (!backsector)
1361 return;
1362
1363 if (frontsector->floorheight > backsector->floorheight)
1364 {
1365 max_floor = frontsector->floorheight;
1366 min_floor = backsector->floorheight;
1367 }
1368 else
1369 {
1370 max_floor = backsector->floorheight;
1371 min_floor = frontsector->floorheight;
1372 }
1373
1374 if (frontsector->ceilingheight > backsector->ceilingheight)
1375 {
1376 max_ceiling = frontsector->ceilingheight;
1377 min_ceiling = backsector->ceilingheight;
1378 }
1379 else
1380 {
1381 max_ceiling = backsector->ceilingheight;
1382 min_ceiling = frontsector->ceilingheight;
1383 }
1384
1385 if (backseg)
1386 {
1387 fs = backsector;
1388 bs = frontsector;
1389 }
1390 else
1391 {
1392 fs = frontsector;
1393 bs = backsector;
1394 }
1395
1396 toptexture = texturetranslation[seg->sidedef->toptexture];
1397 midtexture = texturetranslation[seg->sidedef->midtexture];
1398 bottomtexture = texturetranslation[seg->sidedef->bottomtexture];
1399
1400 /* toptexture */
1401 ceiling_height=frontsector->ceilingheight;
1402 floor_height=backsector->ceilingheight;
1403 wall.light = gld_CalcLightLevel(R_TopLightLevel(seg->sidedef, base_lightlevel));
1404 wall.xscale = (float) seg->sidedef->scalex_top / FRACUNIT;
1405 wall.yscale = (float) seg->sidedef->scaley_top / FRACUNIT;
1406 if (frontsector->ceilingpic==skyflatnum)// || backsector->ceilingpic==skyflatnum)
1407 {
1408 wall.ytop= MAXCOORD*2;
1409 if (
1410 // e6y
1411 // There is no more HOM in the starting area on Memento Mori map29 and on map30.
1412 // Old code:
1413 // (backsector->ceilingheight==backsector->floorheight) &&
1414 // (backsector->ceilingpic==skyflatnum)
1415 (backsector->ceilingpic==skyflatnum) &&
1416 (backsector->ceilingheight<=backsector->floorheight)
1417 )
1418 {
1419 fixed_t specific_rowoffset;
1420
1421 specific_rowoffset = seg->sidedef->rowoffset + seg->sidedef->rowoffset_top;
1422
1423 // e6y
1424 // There is no more visual glitches with sky on Icarus map14 sector 187
1425 // Old code: wall.ybottom=(float)backsector->floorheight/MAP_SCALE;
1426 wall.ybottom=((float)(backsector->floorheight +
1427 (specific_rowoffset > 0 ? specific_rowoffset : 0)))/MAP_SCALE;
1428 gld_AddSkyTexture(&wall, frontsector->ceilingsky, backsector->ceilingsky, SKY_CEILING);
1429 }
1430 else
1431 {
1432 if (bs->ceilingpic == skyflatnum && fs->ceilingpic != skyflatnum &&
1433 toptexture == NO_TEXTURE && midtexture == NO_TEXTURE)
1434 {
1435 wall.ybottom=(float)min_ceiling/MAP_SCALE;
1436 if (bs->ceilingheight < fs->floorheight)
1437 {
1438 fix_sky_bleed = true;
1439 }
1440 gld_AddSkyTexture(&wall, frontsector->ceilingsky, backsector->ceilingsky, SKY_CEILING);
1441 }
1442 else
1443 {
1444 if (((backsector->ceilingpic != skyflatnum && toptexture != NO_TEXTURE) && midtexture == NO_TEXTURE) ||
1445 backsector->ceilingpic != skyflatnum ||
1446 backsector->ceilingheight <= frontsector->floorheight)
1447 {
1448 if (frontsector->ceilingpic == skyflatnum && frontsector->ceilingheight < backsector->floorheight)
1449 {
1450 wall.ybottom=(float)min_ceiling/MAP_SCALE;
1451 fix_sky_bleed = true;
1452 }
1453 else
1454 {
1455 wall.ybottom=(float)max_ceiling/MAP_SCALE;
1456 }
1457 gld_AddSkyTexture(&wall, frontsector->ceilingsky, backsector->ceilingsky, SKY_CEILING);
1458 }
1459 }
1460 }
1461 }
1462 if (floor_height<ceiling_height)
1463 {
1464 if (!((frontsector->ceilingpic==skyflatnum) && (backsector->ceilingpic==skyflatnum)))
1465 {
1466 temptex=gld_RegisterTexture(toptexture, true, false, true, false);
1467 if (!temptex && gl_use_stencil && backsector &&
1468 !(seg->linedef->r_flags & RF_ISOLATED) &&
1469 /*frontsector->ceilingpic != skyflatnum && */backsector->ceilingpic != skyflatnum &&
1470 !(backsector->flags & NULL_SECTOR) &&
1471 backsector->floorheight < backsector->ceilingheight)
1472 {
1473 wall.ytop=((float)(ceiling_height)/(float)MAP_SCALE);
1474 wall.ybottom=((float)(floor_height)/(float)MAP_SCALE);
1475 if (wall.ybottom >= zCamera)
1476 {
1477 wall.flag=GLDWF_TOPFLUD;
1478 temptex=gld_RegisterFlat(flattranslation[seg->backsector->ceilingpic], true, true);
1479 if (temptex)
1480 {
1481 wall.gltexture=temptex;
1482 gld_AddDrawWallItem(GLDIT_FWALL, &wall);
1483 }
1484 }
1485 }
1486 else
1487 if (temptex)
1488 {
1489 wall.gltexture=temptex;
1490 wall.flag = GLDWF_TOP;
1491 gld_CalculateWallY(&wall, &lineheight, floor_height, ceiling_height);
1492 gld_CalculateWallU(&wall, seg, backseg, linelength, seg->sidedef->textureoffset_top);
1493 gld_CalculateWallV(&wall, seg, !(seg->linedef->flags & ML_DONTPEGTOP), lineheight,
1494 seg->sidedef->rowoffset_top);
1495 gld_AddDrawWallItem(GLDIT_WALL, &wall);
1496 }
1497 }
1498 }
1499
1500 /* midtexture */
1501 //e6y
1502 if (!raven && comp[comp_maskedanim])
1503 temptex=gld_RegisterTexture(seg->sidedef->midtexture, true, false, true, false);
1504 else
1505 // e6y
1506 // Animated middle textures with a zero index should be forced
1507 // See spacelab.wad (http://www.doomworld.com/idgames/index.php?id=6826)
1508 temptex=gld_RegisterTexture(midtexture, true, true, true, false);
1509
1510 if (temptex && seg->sidedef->midtexture != NO_TEXTURE && backsector->ceilingheight>frontsector->floorheight)
1511 {
1512 int top, bottom;
1513 fixed_t scaled_texheight;
1514 fixed_t scaled_rowoffset;
1515 dboolean wrapmidtex;
1516
1517 wall.light = gld_CalcLightLevel(R_MidLightLevel(seg->sidedef, base_lightlevel));
1518 wall.xscale = (float) seg->sidedef->scalex_mid / FRACUNIT;
1519 wall.yscale = (float) seg->sidedef->scaley_mid / FRACUNIT;
1520 wall.gltexture=temptex;
1521
1522 scaled_texheight = (fixed_t) ((wall.gltexture->realtexheight << FRACBITS) / wall.yscale);
1523 scaled_rowoffset = (fixed_t) ((seg->sidedef->rowoffset + seg->sidedef->rowoffset_mid) / wall.yscale);
1524
1525 wrapmidtex = seg->sidedef->flags & SF_WRAPMIDTEX || seg->linedef->flags & ML_WRAPMIDTEX;
1526
1527 if (wrapmidtex)
1528 {
1529 fixed_t real_ceiling_height;
1530
1531 floor_height = MAX(seg->frontsector->floorheight, seg->backsector->floorheight);
1532 real_ceiling_height = MIN(seg->frontsector->ceilingheight, seg->backsector->ceilingheight);
1533
1534 if (seg->linedef->flags & ML_DONTPEGBOTTOM)
1535 {
1536 ceiling_height = floor_height + scaled_rowoffset;
1537 }
1538 else
1539 {
1540 ceiling_height = real_ceiling_height + scaled_rowoffset;
1541 }
1542
1543 while (ceiling_height < real_ceiling_height)
1544 ceiling_height += scaled_texheight;
1545 }
1546 else
1547 {
1548 if (seg->linedef->flags & ML_DONTPEGBOTTOM)
1549 {
1550 floor_height = MAX(seg->frontsector->floorheight, seg->backsector->floorheight) +
1551 scaled_rowoffset;
1552 ceiling_height = floor_height + scaled_texheight;
1553 }
1554 else
1555 {
1556 ceiling_height = MIN(seg->frontsector->ceilingheight, seg->backsector->ceilingheight) +
1557 scaled_rowoffset;
1558 floor_height = ceiling_height - scaled_texheight;
1559 }
1560 }
1561
1562 // Depending on missing textures and possible plane intersections
1563 // decide which planes to use for the polygon
1564 if (seg->frontsector != seg->backsector ||
1565 seg->frontsector->heightsec != -1)
1566 {
1567 sector_t *f, *b;
1568
1569 f = (seg->frontsector->heightsec == -1 ? seg->frontsector : &ftempsec);
1570 b = (seg->backsector->heightsec == -1 ? seg->backsector : &btempsec);
1571
1572 // Set up the top
1573 if (frontsector->ceilingpic != skyflatnum ||
1574 backsector->ceilingpic != skyflatnum)
1575 {
1576 if (toptexture == NO_TEXTURE)
1577 // texture is missing - use the higher plane
1578 top = MAX(f->ceilingheight, b->ceilingheight);
1579 else
1580 top = MIN(f->ceilingheight, b->ceilingheight);
1581 }
1582 else
1583 top = ceiling_height;
1584
1585 // Set up the bottom
1586 if (frontsector->floorpic != skyflatnum ||
1587 backsector->floorpic != skyflatnum ||
1588 frontsector->floorheight != backsector->floorheight)
1589 {
1590 if (seg->sidedef->bottomtexture == NO_TEXTURE)
1591 // texture is missing - use the lower plane
1592 bottom = MIN(f->floorheight, b->floorheight);
1593 else
1594 // normal case - use the higher plane
1595 bottom = MAX(f->floorheight, b->floorheight);
1596 }
1597 else
1598 {
1599 bottom = floor_height;
1600 }
1601
1602 //let's clip away some unnecessary parts of the polygon
1603 if (ceiling_height < top)
1604 top = ceiling_height;
1605 if (floor_height > bottom)
1606 bottom = floor_height;
1607 }
1608 else
1609 {
1610 // both sides of the line are in the same sector
1611 top = ceiling_height;
1612 bottom = floor_height;
1613 }
1614
1615 if (top <= bottom)
1616 goto bottomtexture;
1617
1618 wall.ytop = (float)top/(float)MAP_SCALE;
1619 wall.ybottom = (float)bottom/(float)MAP_SCALE;
1620
1621 if (!wrapmidtex)
1622 wall.flag = GLDWF_M2S;
1623
1624 gld_CalculateWallU(&wall, seg, backseg, linelength, seg->sidedef->textureoffset_mid);
1625
1626 wall.vt = (float) (-top + ceiling_height) / (float) scaled_texheight;
1627 wall.vb = (float) (-bottom + ceiling_height) / (float) scaled_texheight;
1628
1629 wall.alpha = seg->linedef->alpha;
1630 gld_AddDrawWallItem((wall.alpha == 1.0f ? GLDIT_MWALL : GLDIT_TWALL), &wall);
1631 wall.alpha = 1.0f;
1632 }
1633 bottomtexture:
1634 /* bottomtexture */
1635 ceiling_height=backsector->floorheight;
1636 floor_height=frontsector->floorheight;
1637 wall.light = gld_CalcLightLevel(R_BottomLightLevel(seg->sidedef, base_lightlevel));
1638 wall.xscale = (float) seg->sidedef->scalex_bottom / FRACUNIT;
1639 wall.yscale = (float) seg->sidedef->scaley_bottom / FRACUNIT;
1640 if (frontsector->floorpic==skyflatnum)
1641 {
1642 wall.ybottom=-MAXCOORD*2;
1643 if (
1644 (backsector->ceilingheight==backsector->floorheight) &&
1645 (backsector->floorpic==skyflatnum) &&
1646 (bottomtexture == NO_TEXTURE)
1647 )
1648 {
1649 wall.ytop=(float)backsector->floorheight/MAP_SCALE;
1650 gld_AddSkyTexture(&wall, frontsector->floorsky, backsector->floorsky, SKY_FLOOR);
1651 }
1652 else
1653 {
1654 if (bottomtexture == NO_TEXTURE && midtexture == NO_TEXTURE)
1655 {
1656 wall.ytop=(float)max_floor/MAP_SCALE;
1657 gld_AddSkyTexture(&wall, frontsector->floorsky, backsector->floorsky, SKY_CEILING);
1658 }
1659 else
1660 {
1661 if ((bottomtexture != NO_TEXTURE && midtexture == NO_TEXTURE) ||
1662 backsector->floorpic != skyflatnum ||
1663 backsector->floorheight >= frontsector->ceilingheight)
1664 {
1665 wall.ytop=(float)min_floor/MAP_SCALE;
1666 gld_AddSkyTexture(&wall, frontsector->floorsky, backsector->floorsky, SKY_FLOOR);
1667 }
1668 }
1669 }
1670 }
1671 if (floor_height<ceiling_height)
1672 {
1673 temptex=gld_RegisterTexture(bottomtexture, true, false, true, false);
1674 if (!temptex && gl_use_stencil && backsector &&
1675 !(seg->linedef->r_flags & RF_ISOLATED) &&
1676 /*frontsector->floorpic != skyflatnum && */backsector->floorpic != skyflatnum &&
1677 !(backsector->flags & NULL_SECTOR) &&
1678 backsector->floorheight < backsector->ceilingheight)
1679 {
1680 wall.ytop=((float)(ceiling_height)/(float)MAP_SCALE);
1681 wall.ybottom=((float)(floor_height)/(float)MAP_SCALE);
1682 if (wall.ytop <= zCamera)
1683 {
1684 wall.flag = GLDWF_BOTFLUD;
1685 temptex=gld_RegisterFlat(flattranslation[seg->backsector->floorpic], true, true);
1686 if (temptex)
1687 {
1688 wall.gltexture=temptex;
1689 gld_AddDrawWallItem(GLDIT_FWALL, &wall);
1690 }
1691 }
1692 }
1693 else
1694 if (temptex)
1695 {
1696 fixed_t rowoffset = seg->sidedef->rowoffset;
1697 fixed_t specific_rowoffset;
1698 wall.gltexture=temptex;
1699 wall.flag = GLDWF_BOT;
1700 if (fix_sky_bleed)
1701 {
1702 ceiling_height = MIN(frontsector->ceilingheight, backsector->ceilingheight);
1703 seg->sidedef->rowoffset += (MAX(frontsector->floorheight, backsector->floorheight) - min_ceiling);
1704 }
1705
1706 specific_rowoffset = seg->sidedef->rowoffset_bottom;
1707 if (seg->linedef->flags & ML_DONTPEGBOTTOM)
1708 specific_rowoffset += frontsector->ceilingheight - floor_height;
1709
1710 if (ceiling_height > backsector->ceilingheight)
1711 {
1712 if (!(seg->linedef->flags & ML_DONTPEGBOTTOM))
1713 specific_rowoffset -= backsector->ceilingheight - ceiling_height;
1714 ceiling_height = backsector->ceilingheight;
1715 }
1716
1717 gld_CalculateWallY(&wall, &lineheight, floor_height, ceiling_height);
1718 gld_CalculateWallU(&wall, seg, backseg, linelength, seg->sidedef->textureoffset_bottom);
1719 gld_CalculateWallV(&wall, seg, seg->linedef->flags & ML_DONTPEGBOTTOM, lineheight,
1720 specific_rowoffset);
1721 gld_AddDrawWallItem(GLDIT_WALL, &wall);
1722 seg->sidedef->rowoffset = rowoffset;
1723 }
1724 }
1725 }
1726 }
1727
1728 /*****************
1729 * *
1730 * Flats *
1731 * *
1732 *****************/
1733
1734 static void gld_DrawFlat(GLFlat *flat)
1735 {
1736 int loopnum; // current loop number
1737 GLLoopDef *currentloop; // the current loop
1738 int has_offset;
1739 unsigned int flags = 0;
1740
1741 dsda_RecordVisPlane();
1742
1743 has_offset = (flat->flags & GLFLAT_HAVE_TRANSFORM);
1744
1745 gld_BindFlat(flat->gltexture, flags);
1746 gld_StaticLightAlpha(flat->light, flat->alpha);
1747
1748 glMatrixMode(GL_MODELVIEW);
1749 glPushMatrix();
1750 glTranslatef(0.0f,flat->z,0.0f);
1751
1752 if (has_offset)
1753 {
1754 glMatrixMode(GL_TEXTURE);
1755 glPushMatrix();
1756 glScalef(flat->xscale, flat->yscale, 1.f);
1757 glTranslatef(flat->uoffs, flat->voffs, 0.f);
1758 glRotatef(-flat->rotation, 0.f, 0.f, 1.f);
1759 }
1760
1761 if (flat->sectornum>=0)
1762 {
1763 // go through all loops of this sector
1764 for (loopnum=0; loopnum<sectorloops[flat->sectornum].loopcount; loopnum++)
1765 {
1766 // set the current loop
1767 currentloop=&sectorloops[flat->sectornum].loops[loopnum];
1768 glDrawArrays(currentloop->mode,currentloop->vertexindex,currentloop->vertexcount);
1769 }
1770 }
1771
1772 if (has_offset)
1773 {
1774 glPopMatrix();
1775 }
1776
1777 glMatrixMode(GL_MODELVIEW);
1778 glPopMatrix();
1779 }
1780
1781 // gld_AddFlat
1782 //
1783 // This draws on flat for the sector "num"
1784 // The ceiling boolean indicates if the flat is a floor(false) or a ceiling(true)
1785
1786 static void gld_AddFlat(int sectornum, dboolean ceiling, visplane_t *plane)
1787 {
1788 sector_t *sector; // the sector we want to draw
1789 sector_t tempsec; // needed for R_FakeFlat
1790 int floorlightlevel; // killough 3/16/98: set floor lightlevel
1791 int ceilinglightlevel; // killough 4/11/98
1792 GLFlat flat;
1793
1794 if (sectornum<0)
1795 return;
1796 flat.sectornum=sectornum;
1797 sector=&sectors[sectornum]; // get the sector
1798 sector=R_FakeFlat(sector, &tempsec, &floorlightlevel, &ceilinglightlevel, false); // for boom effects
1799 flat.flags = (ceiling ? GLFLAT_CEILING : 0);
1800
1801 if (plane->picnum & PL_SKYFLAT || plane->picnum == skyflatnum) // don't draw if sky
1802 return;
1803
1804 if (!ceiling) // if it is a floor ...
1805 {
1806 // get the texture. flattranslation is maintained by doom and
1807 // contains the number of the current animation frame
1808 flat.gltexture=gld_RegisterFlat(flattranslation[plane->picnum], true, true);
1809 if (!flat.gltexture)
1810 return;
1811 // get the lightlevel from floorlightlevel
1812 flat.light=gld_CalcLightLevel(plane->lightlevel+gld_GetGunFlashLight());
1813 // calculate texture offsets
1814 if (sector->floor_xoffs | sector->floor_yoffs)
1815 {
1816 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1817 flat.uoffs=(float)sector->floor_xoffs/(float)(FRACUNIT*64);
1818 flat.voffs=(float)sector->floor_yoffs/(float)(FRACUNIT*64);
1819 }
1820 else
1821 {
1822 flat.uoffs=0.0f;
1823 flat.voffs=0.0f;
1824
1825 if (map_format.hexen)
1826 {
1827 int scrollOffset = leveltime >> 1 & 63;
1828
1829 switch (plane->special)
1830 { // Handle scrolling flats
1831 case 201:
1832 case 202:
1833 case 203: // Scroll_North_xxx
1834 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1835 flat.voffs = (float) (scrollOffset << (plane->special - 201) & 63) / 64;
1836 break;
1837 case 204:
1838 case 205:
1839 case 206: // Scroll_East_xxx
1840 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1841 flat.uoffs = (float) ((63 - scrollOffset) << (plane->special - 204) & 63) / 64;
1842 break;
1843 case 207:
1844 case 208:
1845 case 209: // Scroll_South_xxx
1846 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1847 flat.voffs = (float) ((63 - scrollOffset) << (plane->special - 207) & 63) / 64;
1848 break;
1849 case 210:
1850 case 211:
1851 case 212: // Scroll_West_xxx
1852 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1853 flat.uoffs = (float) (scrollOffset << (plane->special - 210) & 63) / 64;
1854 break;
1855 case 213:
1856 case 214:
1857 case 215: // Scroll_NorthWest_xxx
1858 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1859 flat.voffs = (float) (scrollOffset << (plane->special - 213) & 63) / 64;
1860 flat.uoffs = (float) (scrollOffset << (plane->special - 213) & 63) / 64;
1861 break;
1862 case 216:
1863 case 217:
1864 case 218: // Scroll_NorthEast_xxx
1865 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1866 flat.voffs = (float) (scrollOffset << (plane->special - 216) & 63) / 64;
1867 flat.uoffs = (float) ((63 - scrollOffset) << (plane->special - 216) & 63) / 64;
1868 break;
1869 case 219:
1870 case 220:
1871 case 221: // Scroll_SouthEast_xxx
1872 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1873 flat.voffs = (float) ((63 - scrollOffset) << (plane->special - 219) & 63) / 64;
1874 flat.uoffs = (float) ((63 - scrollOffset) << (plane->special - 219) & 63) / 64;
1875 break;
1876 case 222:
1877 case 223:
1878 case 224: // Scroll_SouthWest_xxx
1879 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1880 flat.voffs = (float) ((63 - scrollOffset) << (plane->special - 222) & 63) / 64;
1881 flat.uoffs = (float) (scrollOffset << (plane->special - 222) & 63) / 64;
1882 break;
1883 default:
1884 break;
1885 }
1886 }
1887 else if (heretic)
1888 {
1889 switch (plane->special)
1890 {
1891 case 20:
1892 case 21:
1893 case 22:
1894 case 23:
1895 case 24: // Scroll_East
1896 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1897 flat.uoffs = (float) ((63 - ((leveltime >> 1) & 63)) << (plane->special - 20) & 63) / 64;
1898 break;
1899 case 4: // Scroll_EastLavaDamage
1900 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1901 flat.uoffs = (float) (((63 - ((leveltime >> 1) & 63)) << 3) & 63) / 64;
1902 break;
1903 }
1904 }
1905 }
1906 }
1907 else // if it is a ceiling ...
1908 {
1909 // get the texture. flattranslation is maintained by doom and
1910 // contains the number of the current animation frame
1911 flat.gltexture=gld_RegisterFlat(flattranslation[plane->picnum], true, true);
1912 if (!flat.gltexture)
1913 return;
1914 // get the lightlevel from ceilinglightlevel
1915 flat.light=gld_CalcLightLevel(plane->lightlevel+gld_GetGunFlashLight());
1916 // calculate texture offsets
1917 if (sector->ceiling_xoffs | sector->ceiling_yoffs)
1918 {
1919 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1920 flat.uoffs=(float)sector->ceiling_xoffs/(float)(FRACUNIT*64);
1921 flat.voffs=(float)sector->ceiling_yoffs/(float)(FRACUNIT*64);
1922 }
1923 else
1924 {
1925 flat.uoffs=0.0f;
1926 flat.voffs=0.0f;
1927 }
1928 }
1929
1930 // get height from plane
1931 flat.z=(float)plane->height/MAP_SCALE;
1932
1933 if (plane->rotation)
1934 {
1935 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1936 flat.rotation = ((float) plane->rotation / ANG45) * 45;
1937 }
1938 else
1939 {
1940 flat.rotation = 0;
1941 }
1942
1943 if (plane->xscale != FRACUNIT || plane->yscale != FRACUNIT)
1944 {
1945 flat.flags |= GLFLAT_HAVE_TRANSFORM;
1946 flat.xscale = (float) plane->xscale / FRACUNIT;
1947 flat.yscale = (float) plane->yscale / FRACUNIT;
1948 }
1949 else
1950 {
1951 flat.xscale = 1.f;
1952 flat.yscale = 1.f;
1953 }
1954
1955 flat.alpha = 1.0;
1956
1957 gld_AddDrawItem(((flat.flags & GLFLAT_CEILING) ? GLDIT_CEILING : GLDIT_FLOOR), &flat);
1958 }
1959
1960 void gld_AddPlane(int subsectornum, visplane_t *floor, visplane_t *ceiling)
1961 {
1962 subsector_t *subsector;
1963
1964 subsector = &subsectors[subsectornum];
1965 if (!subsector)
1966 return;
1967
1968 // render the floor
1969 if (floor && floor->height < viewz)
1970 gld_AddFlat(subsector->sector->iSectorID, false, floor);
1971 // render the ceiling
1972 if (ceiling && ceiling->height > viewz)
1973 gld_AddFlat(subsector->sector->iSectorID, true, ceiling);
1974 }
1975
1976 /*****************
1977 * *
1978 * Sprites *
1979 * *
1980 *****************/
1981
1982 static void gld_DrawSprite(GLSprite *sprite)
1983 {
1984 GLint blend_src, blend_dst;
1985 int restore = 0;
1986
1987 dsda_RecordVisSprite();
1988
1989 gld_BindPatch(sprite->gltexture,sprite->cm);
1990
1991 if (!(sprite->flags & MF_NO_DEPTH_TEST))
1992 {
1993 if(sprite->flags & g_mf_shadow)
1994 {
1995 // Fuzz has less aliasing if ratio is an integer
1996 float ratio = floor((SCREENWIDTH > SCREENHEIGHT ? SCREENHEIGHT : SCREENWIDTH) / 200.0);
1997 glGetIntegerv(GL_BLEND_SRC, &blend_src);
1998 glGetIntegerv(GL_BLEND_DST, &blend_dst);
1999 restore = 1;
2000 gld_StartFuzz(sprite->id, ratio);
2001 }
2002 else
2003 {
2004 if (sprite->alpha != 1.f)
2005 gld_StaticLightAlpha(sprite->light, sprite->alpha);
2006 else
2007 gld_StaticLight(sprite->light);
2008 }
2009 }
2010
2011 if (!(sprite->flags & (MF_SOLID | MF_SPAWNCEILING)))
2012 {
2013 float x1, x2, x3, x4, z1, z2, z3, z4;
2014 float y1, y2, cy, ycenter, y1c, y2c;
2015 float y1z2_y, y2z2_y;
2016
2017 ycenter = (float)fabs(sprite->y1 - sprite->y2) * 0.5f;
2018 y1c = sprite->y1 - ycenter;
2019 y2c = sprite->y2 - ycenter;
2020 cy = sprite->y + ycenter;
2021
2022 y1z2_y = -(y1c * sin_paperitems_pitch);
2023 y2z2_y = -(y2c * sin_paperitems_pitch);
2024
2025 x1 = +(sprite->x1 * cos_inv_yaw - y1z2_y * sin_inv_yaw) + sprite->x;
2026 x2 = +(sprite->x2 * cos_inv_yaw - y1z2_y * sin_inv_yaw) + sprite->x;
2027 x3 = +(sprite->x1 * cos_inv_yaw - y2z2_y * sin_inv_yaw) + sprite->x;
2028 x4 = +(sprite->x2 * cos_inv_yaw - y2z2_y * sin_inv_yaw) + sprite->x;
2029
2030 y1 = +(y1c * cos_paperitems_pitch) + cy;
2031 y2 = +(y2c * cos_paperitems_pitch) + cy;
2032
2033 z1 = -(sprite->x1 * sin_inv_yaw + y1z2_y * cos_inv_yaw) + sprite->z;
2034 z2 = -(sprite->x2 * sin_inv_yaw + y1z2_y * cos_inv_yaw) + sprite->z;
2035 z3 = -(sprite->x1 * sin_inv_yaw + y2z2_y * cos_inv_yaw) + sprite->z;
2036 z4 = -(sprite->x2 * sin_inv_yaw + y2z2_y * cos_inv_yaw) + sprite->z;
2037
2038 glBegin(GL_TRIANGLE_STRIP);
2039 glTexCoord2f(sprite->ul, sprite->vt); glVertex3f(x1, y1, z1);
2040 glTexCoord2f(sprite->ur, sprite->vt); glVertex3f(x2, y1, z2);
2041 glTexCoord2f(sprite->ul, sprite->vb); glVertex3f(x3, y2, z3);
2042 glTexCoord2f(sprite->ur, sprite->vb); glVertex3f(x4, y2, z4);
2043 glEnd();
2044 }
2045 else
2046 {
2047 float x1, x2, y1, y2, z1, z2;
2048
2049 x1 = +(sprite->x1 * cos_inv_yaw) + sprite->x;
2050 x2 = +(sprite->x2 * cos_inv_yaw) + sprite->x;
2051
2052 y1 = sprite->y + sprite->y1;
2053 y2 = sprite->y + sprite->y2;
2054
2055 z2 = -(sprite->x1 * sin_inv_yaw) + sprite->z;
2056 z1 = -(sprite->x2 * sin_inv_yaw) + sprite->z;
2057
2058 glBegin(GL_TRIANGLE_STRIP);
2059 glTexCoord2f(sprite->ul, sprite->vt); glVertex3f(x1, y1, z2);
2060 glTexCoord2f(sprite->ur, sprite->vt); glVertex3f(x2, y1, z1);
2061 glTexCoord2f(sprite->ul, sprite->vb); glVertex3f(x1, y2, z2);
2062 glTexCoord2f(sprite->ur, sprite->vb); glVertex3f(x2, y2, z1);
2063 glEnd();
2064 }
2065
2066 if (restore)
2067 {
2068 glBlendFunc(blend_src, blend_dst);
2069 gld_EndFuzz();
2070 }
2071 }
2072
2073 static void gld_AddHealthBar(mobj_t* thing, GLSprite *sprite)
2074 {
2075 if (((thing->flags & (MF_COUNTKILL | MF_CORPSE)) == MF_COUNTKILL) && (thing->health > 0))
2076 {
2077 GLHealthBar hbar;
2078 int health_percent = thing->health * 100 / P_MobjSpawnHealth(thing);
2079
2080 hbar.color = health_bar_null;
2081 if (health_percent <= 50)
2082 hbar.color = health_bar_red;
2083 else if (health_percent <= 99)
2084 hbar.color = health_bar_yellow;
2085
2086 if (hbar.color != health_bar_null)
2087 {
2088 float sx2 = (float)thing->radius / 2.0f / MAP_SCALE;
2089 float sx1 = sx2 - (float)health_percent * (float)thing->radius / 100.0f / MAP_SCALE;
2090 float sx3 = -sx2;
2091
2092 hbar.x1 = +(sx1 * cos_inv_yaw) + sprite->x;
2093 hbar.x2 = +(sx2 * cos_inv_yaw) + sprite->x;
2094 hbar.x3 = +(sx3 * cos_inv_yaw) + sprite->x;
2095
2096 hbar.z1 = -(sx1 * sin_inv_yaw) + sprite->z;
2097 hbar.z2 = -(sx2 * sin_inv_yaw) + sprite->z;
2098 hbar.z3 = -(sx3 * sin_inv_yaw) + sprite->z;
2099
2100 hbar.y = sprite->y + sprite->y1 + 2.0f / MAP_COEFF;
2101
2102 gld_AddDrawItem(GLDIT_HBAR, &hbar);
2103 }
2104 }
2105 }
2106
2107 static GLfloat health_bar_rgb[3][3] = {
2108 [health_bar_null] = { 0.0f, 0.0f, 0.0f },
2109 [health_bar_red] = { 1.0f, 0.0f, 0.0f },
2110 [health_bar_yellow] = { 1.0f, 1.0f, 0.0f },
2111 };
2112
2113 static void gld_DrawHealthBars(void)
2114 {
2115 int i, count;
2116 int color = health_bar_null;
2117
2118 count = gld_drawinfo.num_items[GLDIT_HBAR];
2119 if (count > 0)
2120 {
2121 gld_EnableTexture2D(GL_TEXTURE0_ARB, false);
2122
2123 glBegin(GL_LINES);
2124 for (i = count - 1; i >= 0; i--)
2125 {
2126 GLHealthBar *hbar = gld_drawinfo.items[GLDIT_HBAR][i].item.hbar;
2127 if (hbar->color != color)
2128 {
2129 color = hbar->color;
2130 glColor4f(health_bar_rgb[color][0],
2131 health_bar_rgb[color][1],
2132 health_bar_rgb[color][2], 1.0f);
2133 }
2134
2135 glVertex3f(hbar->x1, hbar->y, hbar->z1);
2136 glVertex3f(hbar->x2, hbar->y, hbar->z2);
2137 }
2138 glEnd();
2139
2140 glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
2141 glBegin(GL_LINES);
2142 for (i = count - 1; i >= 0; i--)
2143 {
2144 GLHealthBar *hbar = gld_drawinfo.items[GLDIT_HBAR][i].item.hbar;
2145
2146 glVertex3f(hbar->x1, hbar->y, hbar->z1);
2147 glVertex3f(hbar->x3, hbar->y, hbar->z3);
2148 }
2149 glEnd();
2150
2151 gld_EnableTexture2D(GL_TEXTURE0_ARB, true);
2152 }
2153 }
2154
2155 void gld_ProjectSprite(mobj_t* thing, int lightlevel)
2156 {
2157 // Stupid C trick to get alignment without C11 alignof
2158 static const size_t align = offsetof(struct { char a; mobj_t m; }, m);
2159 fixed_t tx;
2160 spritedef_t *sprdef;
2161 spriteframe_t *sprframe;
2162 int lump;
2163 dboolean flip;
2164 int heightsec; // killough 3/27/98
2165
2166 // transform the origin point
2167 //e6y
2168 fixed_t tr_x, tr_y;
2169 fixed_t fx, fy, fz;
2170 fixed_t gxt, gyt;
2171 fixed_t tz;
2172
2173 GLSprite sprite;
2174 const rpatch_t* patch;
2175
2176 int frustum_culling = HaveMouseLook();
2177 int mlook = HaveMouseLook() || (gl_render_fov > FOV90);
2178
2179 if (R_ViewInterpolation())
2180 {
2181 fx = thing->PrevX + FixedMul (tic_vars.frac, thing->x - thing->PrevX);
2182 fy = thing->PrevY + FixedMul (tic_vars.frac, thing->y - thing->PrevY);
2183 fz = thing->PrevZ + FixedMul (tic_vars.frac, thing->z - thing->PrevZ);
2184 }
2185 else
2186 {
2187 fx = thing->x;
2188 fy = thing->y;
2189 fz = thing->z;
2190 }
2191
2192 tr_x = fx - viewx;
2193 tr_y = fy - viewy;
2194
2195 gxt = FixedMul(tr_x, viewcos);
2196 gyt = -FixedMul(tr_y, viewsin);
2197
2198 tz = gxt - gyt;
2199
2200 // thing is behind view plane?
2201 if (tz < r_near_clip_plane)
2202 return;
2203
2204 gxt = -FixedMul(tr_x, viewsin);
2205 gyt = FixedMul(tr_y, viewcos);
2206 tx = -(gyt + gxt);
2207
2208 //e6y
2209 if (mlook)
2210 {
2211 if (tz >= MINZ && (D_abs(tx) >> 5) > tz)
2212 return;
2213 }
2214 else
2215 {
2216 // too far off the side?
2217 if (D_abs(tx) > ((int64_t) tz << 2))
2218 return;
2219 }
2220
2221 // decide which patch to use for sprite relative to player
2222 #ifdef RANGECHECK
2223 if ((unsigned) thing->sprite >= (unsigned)num_sprites)
2224 I_Error ("R_ProjectSprite: Invalid sprite number %i", thing->sprite);
2225 #endif
2226
2227 sprdef = &sprites[thing->sprite];
2228
2229 #ifdef RANGECHECK
2230 if ((thing->frame&FF_FRAMEMASK) >= sprdef->numframes)
2231 I_Error ("R_ProjectSprite: Invalid sprite frame %i : %i", thing->sprite, thing->frame);
2232 #endif
2233
2234 if (!sprdef->spriteframes)
2235 I_Error("R_ProjectSprite: Missing spriteframes %i : %i", thing->sprite, thing->frame);
2236
2237 sprframe = &sprdef->spriteframes[thing->frame & FF_FRAMEMASK];
2238
2239 if (sprframe->rotate)
2240 {
2241 // choose a different rotation based on player view
2242 angle_t rot;
2243 angle_t ang = R_PointToAngle2(viewx, viewy, fx, fy);
2244 if (sprframe->lump[0] == sprframe->lump[1])
2245 {
2246 rot = (ang - thing->angle + (angle_t)(ANG45/2)*9) >> 28;
2247 }
2248 else
2249 {
2250 rot = (ang - thing->angle + (angle_t)(ANG45 / 2) * 9 -
2251 (angle_t)(ANG180 / 16)) >> 28;
2252 }
2253 lump = sprframe->lump[rot];
2254 flip = (dboolean)(sprframe->flip & (1 << rot));
2255 }
2256 else
2257 {
2258 // use single rotation for all views
2259 lump = sprframe->lump[0];
2260 flip = (dboolean)(sprframe->flip & 1);
2261 }
2262 lump += firstspritelump;
2263
2264 patch = R_PatchByNum(lump);
2265 thing->patch_width = patch->width;
2266
2267 // killough 4/9/98: clip things which are out of view due to height
2268 if(!mlook)
2269 {
2270 int x1, x2;
2271 fixed_t xscale = FixedDiv(projection, tz);
2272 /* calculate edges of the shape
2273 * cph 2003/08/1 - fraggle points out that this offset must be flipped
2274 * if the sprite is flipped; e.g. FreeDoom imp is messed up by this. */
2275 if (flip)
2276 tx -= (patch->width - patch->leftoffset) << FRACBITS;
2277 else
2278 tx -= patch->leftoffset << FRACBITS;
2279
2280 x1 = (centerxfrac + FixedMul(tx, xscale)) >> FRACBITS;
2281 tx += patch->width << FRACBITS;
2282 x2 = ((centerxfrac + FixedMul (tx, xscale) - FRACUNIT/2) >> FRACBITS);
2283
2284 // off the side?
2285 if (x1 > viewwidth || x2 < 0)
2286 return;
2287 }
2288
2289 // killough 3/27/98: exclude things totally separated
2290 // from the viewer, by either water or fake ceilings
2291 // killough 4/11/98: improve sprite clipping for underwater/fake ceilings
2292
2293 heightsec = thing->subsector->sector->heightsec;
2294 if (heightsec != -1) // only clip things which are in special sectors
2295 {
2296 int phs = viewplayer->mo->subsector->sector->heightsec;
2297 fixed_t gzt = fz + (patch->topoffset << FRACBITS);
2298 if (phs != -1 && viewz < sectors[phs].floorheight ?
2299 fz >= sectors[heightsec].floorheight :
2300 gzt < sectors[heightsec].floorheight)
2301 return;
2302 if (phs != -1 && viewz > sectors[phs].ceilingheight ?
2303 gzt < sectors[heightsec].ceilingheight && viewz >= sectors[heightsec].ceilingheight :
2304 fz >= sectors[heightsec].ceilingheight)
2305 return;
2306 }
2307
2308 //e6y FIXME!!!
2309 if (thing == players[displayplayer].mo && walkcamera.type != 2)
2310 return;
2311
2312 sprite.x =-(float)fx / MAP_SCALE;
2313 sprite.y = (float)fz / MAP_SCALE;
2314 sprite.z = (float)fy / MAP_SCALE;
2315
2316 sprite.x2 = (float)patch->leftoffset / MAP_COEFF;
2317 sprite.x1 = sprite.x2 - ((float)patch->width / MAP_COEFF);
2318 sprite.y1 = (float)patch->topoffset / MAP_COEFF;
2319 sprite.y2 = sprite.y1 - ((float)patch->height / MAP_COEFF);
2320
2321 // e6y
2322 // if the sprite is below the floor, and it's not a hanger/floater/missile,
2323 // and it's not a fully dead corpse, move it up
2324 if (
2325 sprite.y2 < 0 &&
2326 sprite.y2 >= -gl_spriteclip_threshold_f &&
2327 !(thing->flags & (MF_SPAWNCEILING|MF_FLOAT|MF_MISSILE|MF_NOGRAVITY)) &&
2328 !(thing->flags & MF_CORPSE && thing->tics == -1)
2329 )
2330 {
2331 sprite.y1 -= sprite.y2;
2332 sprite.y2 = 0.0f;
2333 }
2334
2335 if (frustum_culling)
2336 {
2337 if (!gld_SphereInFrustum(
2338 sprite.x + cos_inv_yaw * (sprite.x1 + sprite.x2) / 2.0f,
2339 sprite.y + (sprite.y1 + sprite.y2) / 2.0f,
2340 sprite.z - sin_inv_yaw * (sprite.x1 + sprite.x2) / 2.0f,
2341 //1.5 == sqrt(2) + small delta for MF_FOREGROUND
2342 (float)(MAX(patch->width, patch->height)) / MAP_COEFF / 2.0f * 1.5f))
2343 {
2344 return;
2345 }
2346 }
2347
2348 sprite.scale = FixedDiv(projectiony, tz);;
2349 if ((thing->frame & FF_FULLBRIGHT) || dsda_ShowAliveMonsters())
2350 {
2351 sprite.light = 1.0f;
2352 }
2353 else
2354 {
2355 sprite.light = gld_CalcLightLevel(lightlevel+gld_GetGunFlashLight());
2356 }
2357 if (thing->color)
2358 sprite.cm = thing->color;
2359 else
2360 sprite.cm = CR_LIMIT + (int)((thing->flags & MF_TRANSLATION) >> (MF_TRANSSHIFT));
2361 sprite.gltexture = gld_RegisterPatch(lump, sprite.cm, true, true);
2362 if (!sprite.gltexture)
2363 return;
2364 sprite.flags = thing->flags;
2365
2366 if (thing->flags & MF_FOREGROUND)
2367 scene_has_overlapped_sprites = true;
2368
2369 sprite.index = gl_spriteindex++;
2370 // ID for rendering uniqueness purposes, collisions are OK
2371 sprite.id = (int) ((uintptr_t) thing / align);
2372 sprite.xy = thing->x + (thing->y >> 16);
2373 sprite.fx = thing->x;
2374 sprite.fy = thing->y;
2375
2376 sprite.vt = 0.0f;
2377 sprite.vb = sprite.gltexture->scaleyfac;
2378 if (flip)
2379 {
2380 sprite.ul = 0.0f;
2381 sprite.ur = sprite.gltexture->scalexfac;
2382 }
2383 else
2384 {
2385 sprite.ul = sprite.gltexture->scalexfac;
2386 sprite.ur = 0.0f;
2387 }
2388
2389 if (thing->alpha != 1.f)
2390 sprite.alpha = thing->alpha;
2391 else if (sprite.flags & g_mf_translucent)
2392 sprite.alpha = 0.66f;
2393 else
2394 sprite.alpha = 1.f;
2395
2396 //e6y: support for transparent sprites
2397 if (sprite.flags & MF_NO_DEPTH_TEST)
2398 {
2399 gld_AddDrawItem(GLDIT_ASPRITE, &sprite);
2400 }
2401 else if (sprite.alpha != 1.f || sprite.flags & (MF_SHADOW | MF_TRANSLUCENT))
2402 {
2403 gld_AddDrawItem(GLDIT_TSPRITE, &sprite);
2404 }
2405 else
2406 {
2407 gld_AddDrawItem(GLDIT_SPRITE, &sprite);
2408 }
2409
2410 if (dsda_ShowHealthBars())
2411 {
2412 gld_AddHealthBar(thing, &sprite);
2413 }
2414 }
2415
2416 /*****************
2417 * *
2418 * Draw *
2419 * *
2420 *****************/
2421
2422 //e6y
2423 void gld_ProcessWall(GLWall *wall)
2424 {
2425 seg_t *seg = wall->seg;
2426
2427 // The following code decides where to insert vertices in the left and right
2428 // sides of a wall polygon so that it precisely shares vertices with adjacent
2429 // ceilings/floors/walls. This prevents gaps between polygons from the
2430 // T-junction problem.
2431 //
2432 // See: https://computergraphics.stackexchange.com/questions/1461/why-do-t-junctions-in-meshes-result-in-cracks
2433 wall->glseg->fracleft = 0;
2434 wall->glseg->fracright = 0;
2435
2436 gld_RecalcVertexHeights(seg->linedef->v1);
2437 gld_RecalcVertexHeights(seg->linedef->v2);
2438
2439 gld_DrawWall(wall);
2440 }
2441
2442 static int C_DECL dicmp_wall(const void *a, const void *b)
2443 {
2444 GLTexture *tx1 = ((const GLDrawItem *)a)->item.wall->gltexture;
2445 GLTexture *tx2 = ((const GLDrawItem *)b)->item.wall->gltexture;
2446 return tx1 - tx2;
2447 }
2448 static int C_DECL dicmp_flat(const void *a, const void *b)
2449 {
2450 GLTexture *tx1 = ((const GLDrawItem *)a)->item.flat->gltexture;
2451 GLTexture *tx2 = ((const GLDrawItem *)b)->item.flat->gltexture;
2452 return tx1 - tx2;
2453 }
2454 static int C_DECL dicmp_sprite(const void *a, const void *b)
2455 {
2456 GLTexture *tx1 = ((const GLDrawItem *)a)->item.sprite->gltexture;
2457 GLTexture *tx2 = ((const GLDrawItem *)b)->item.sprite->gltexture;
2458 return tx1 - tx2;
2459 }
2460
2461 static int C_DECL dicmp_sprite_scale(const void *a, const void *b)
2462 {
2463 GLSprite *sprite1 = ((const GLDrawItem *)a)->item.sprite;
2464 GLSprite *sprite2 = ((const GLDrawItem *)b)->item.sprite;
2465
2466 if (sprite1->scale != sprite2->scale)
2467 {
2468 return sprite2->scale - sprite1->scale;
2469 }
2470 else
2471 {
2472 return sprite1->gltexture - sprite2->gltexture;
2473 }
2474 }
2475
2476 static void gld_DrawItemsSortByTexture(GLDrawItemType itemtype)
2477 {
2478 typedef int(C_DECL *DICMP_ITEM)(const void *a, const void *b);
2479
2480 static DICMP_ITEM itemfuncs[GLDIT_TYPES] = {
2481 0,
2482 dicmp_wall, dicmp_wall, dicmp_wall, dicmp_wall, dicmp_wall,
2483 dicmp_wall, dicmp_wall,
2484 dicmp_flat, dicmp_flat,
2485 dicmp_flat, dicmp_flat,
2486 dicmp_sprite, dicmp_sprite_scale, dicmp_sprite,
2487 0,
2488 0,
2489 };
2490
2491 if (itemfuncs[itemtype] && gld_drawinfo.num_items[itemtype] > 1)
2492 {
2493 qsort(gld_drawinfo.items[itemtype], gld_drawinfo.num_items[itemtype],
2494 sizeof(gld_drawinfo.items[itemtype][0]), itemfuncs[itemtype]);
2495 }
2496 }
2497
2498 static void gld_DrawItemsSortSprites(GLDrawItemType itemtype)
2499 {
2500 static const float delta = 0.2f / MAP_COEFF;
2501 int i;
2502
2503 if (scene_has_overlapped_sprites)
2504 {
2505 for (i = 0; i < gld_drawinfo.num_items[itemtype]; i++)
2506 {
2507 GLSprite *sprite = gld_drawinfo.items[itemtype][i].item.sprite;
2508 if (sprite->flags & MF_FOREGROUND)
2509 {
2510 sprite->index = gl_spriteindex;
2511 sprite->x -= delta * sin_inv_yaw;
2512 sprite->z -= delta * cos_inv_yaw;
2513 }
2514 }
2515 }
2516
2517 gld_DrawItemsSortByTexture(itemtype);
2518 }
2519
2520 //
2521 // projected walls
2522 //
2523 void gld_DrawProjectedWalls(GLDrawItemType itemtype)
2524 {
2525 int i;
2526
2527 if (gl_use_stencil && gld_drawinfo.num_items[itemtype] > 0)
2528 {
2529 // Push bleeding floor/ceiling textures back a little in the z-buffer
2530 // so they don't interfere with overlapping mid textures.
2531 glPolygonOffset(1.0f, 128.0f);
2532 glEnable(GL_POLYGON_OFFSET_FILL);
2533
2534 glEnable(GL_STENCIL_TEST);
2535 gld_DrawItemsSortByTexture(itemtype);
2536 for (i = gld_drawinfo.num_items[itemtype] - 1; i >= 0; i--)
2537 {
2538 GLWall *wall = gld_drawinfo.items[itemtype][i].item.wall;
2539
2540 gld_ProcessWall(wall);
2541 }
2542 glDisable(GL_STENCIL_TEST);
2543
2544 glPolygonOffset(0.0f, 0.0f);
2545 glDisable(GL_POLYGON_OFFSET_FILL);
2546 }
2547 }
2548
2549 void gld_DrawScene(player_t *player)
2550 {
2551 int i;
2552
2553 glEnable(GL_ALPHA_TEST);
2554
2555 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2556 glEnableClientState(GL_VERTEX_ARRAY);
2557 glDisableClientState(GL_COLOR_ARRAY);
2558
2559 if (gl_drawskys == skytype_skydome)
2560 {
2561 gld_DrawDomeSkyBox();
2562 }
2563
2564 if (gl_ext_arb_vertex_buffer_object)
2565 {
2566 GLEXT_glBindBufferARB(GL_ARRAY_BUFFER, flats_vbo_id);
2567 }
2568 glVertexPointer(3, GL_FLOAT, sizeof(flats_vbo[0]), flats_vbo_x);
2569 glTexCoordPointer(2, GL_FLOAT, sizeof(flats_vbo[0]), flats_vbo_u);
2570
2571 glsl_PushMainShader();
2572
2573 //
2574 // opaque stuff
2575 //
2576
2577 glBlendFunc(GL_ONE, GL_ZERO);
2578
2579 // solid geometry
2580 glDisable(GL_ALPHA_TEST);
2581
2582 // enable backside removing
2583 glEnable(GL_CULL_FACE);
2584
2585 // floors
2586 glCullFace(GL_FRONT);
2587 gld_DrawItemsSortByTexture(GLDIT_FLOOR);
2588 for (i = gld_drawinfo.num_items[GLDIT_FLOOR] - 1; i >= 0; i--)
2589 {
2590 gld_DrawFlat(gld_drawinfo.items[GLDIT_FLOOR][i].item.flat);
2591 }
2592
2593 // ceilings
2594 glCullFace(GL_BACK);
2595 gld_DrawItemsSortByTexture(GLDIT_CEILING);
2596 for (i = gld_drawinfo.num_items[GLDIT_CEILING] - 1; i >= 0; i--)
2597 {
2598 gld_DrawFlat(gld_drawinfo.items[GLDIT_CEILING][i].item.flat);
2599 }
2600
2601 // disable backside removing
2602 glDisable(GL_CULL_FACE);
2603
2604 // top, bottom, one-sided walls
2605 gld_DrawItemsSortByTexture(GLDIT_WALL);
2606 for (i = gld_drawinfo.num_items[GLDIT_WALL] - 1; i >= 0; i--)
2607 {
2608 gld_ProcessWall(gld_drawinfo.items[GLDIT_WALL][i].item.wall);
2609 }
2610
2611 // masked geometry
2612 glEnable(GL_ALPHA_TEST);
2613
2614 gld_DrawItemsSortByTexture(GLDIT_MWALL);
2615
2616 if (gl_use_stencil && gld_drawinfo.num_items[GLDIT_MWALL] > 0)
2617 {
2618 // opaque mid walls without holes
2619 for (i = gld_drawinfo.num_items[GLDIT_MWALL] - 1; i >= 0; i--)
2620 {
2621 GLWall *wall = gld_drawinfo.items[GLDIT_MWALL][i].item.wall;
2622 if (!(wall->gltexture->flags & GLTEXTURE_HASHOLES))
2623 {
2624 gld_ProcessWall(wall);
2625 }
2626 }
2627
2628 // opaque mid walls with holes
2629
2630 glEnable(GL_STENCIL_TEST);
2631 glStencilFunc(GL_ALWAYS, 1, ~0);
2632 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
2633
2634 for (i = gld_drawinfo.num_items[GLDIT_MWALL] - 1; i >= 0; i--)
2635 {
2636 GLWall *wall = gld_drawinfo.items[GLDIT_MWALL][i].item.wall;
2637 if (wall->gltexture->flags & GLTEXTURE_HASHOLES)
2638 {
2639 gld_ProcessWall(wall);
2640 }
2641 }
2642
2643 glStencilFunc(GL_EQUAL, 1, ~0);
2644 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
2645
2646 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
2647 glBlendFunc (GL_DST_COLOR, GL_SRC_COLOR);
2648
2649 //restoring
2650 SetFrameTextureMode();
2651 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2652 glClear(GL_STENCIL_BUFFER_BIT);
2653 glDisable(GL_STENCIL_TEST);
2654 }
2655 else
2656 {
2657 // opaque mid walls
2658 for (i = gld_drawinfo.num_items[GLDIT_MWALL] - 1; i >= 0; i--)
2659 {
2660 gld_ProcessWall(gld_drawinfo.items[GLDIT_MWALL][i].item.wall);
2661 }
2662 }
2663
2664 // projected walls
2665 gld_DrawProjectedWalls(GLDIT_FWALL);
2666
2667 glEnable(GL_ALPHA_TEST);
2668
2669 // normal sky (not a skybox)
2670 if (gl_drawskys == skytype_none || gl_drawskys == skytype_standard)
2671 {
2672 dsda_RecordDrawSegs(gld_drawinfo.num_items[GLDIT_SWALL]);
2673 // fake strips of sky
2674 glsl_PushNullShader();
2675 gld_DrawStripsSky();
2676 glsl_PopNullShader();
2677 }
2678
2679 // opaque sprites
2680 gld_DrawItemsSortSprites(GLDIT_SPRITE);
2681 for (i = gld_drawinfo.num_items[GLDIT_SPRITE] - 1; i >= 0; i--)
2682 {
2683 gld_DrawSprite(gld_drawinfo.items[GLDIT_SPRITE][i].item.sprite);
2684 }
2685
2686 // mode for viewing all the alive monsters
2687 if (dsda_ShowAliveMonsters())
2688 {
2689 const int period = 250;
2690 float color;
2691 int step = (SDL_GetTicks() % (period * 2)) + 1;
2692 if (step > period)
2693 {
2694 step = period * 2 - step;
2695 }
2696 color = 0.1f + 0.9f * (float)step / (float)period;
2697
2698 R_AddAllAliveMonstersSprites();
2699 glDisable(GL_DEPTH_TEST);
2700 gld_DrawItemsSortByTexture(GLDIT_ASPRITE);
2701 glColor4f(1.0f, color, color, 1.0f);
2702 for (i = gld_drawinfo.num_items[GLDIT_ASPRITE] - 1; i >= 0; i--)
2703 {
2704 gld_DrawSprite(gld_drawinfo.items[GLDIT_ASPRITE][i].item.sprite);
2705 }
2706 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
2707 glEnable(GL_DEPTH_TEST);
2708 }
2709
2710 if (dsda_ShowHealthBars())
2711 {
2712 glsl_PushNullShader();
2713 gld_DrawHealthBars();
2714 glsl_PopNullShader();
2715 }
2716
2717 //
2718 // transparent stuff
2719 //
2720
2721 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2722
2723 /* Transparent sprites and transparent things must be rendered
2724 * in far-to-near order. The approach used here is to sort in-
2725 * place by comparing the next farthest items in the queue.
2726 * There are known limitations to this approach, but it is
2727 * a trade-off of accuracy for speed.
2728 * Refer to the discussion below for more detail.
2729 * https://github.com/coelckers/prboom-plus/pull/262
2730 */
2731 if (gld_drawinfo.num_items[GLDIT_TWALL] > 0 || gld_drawinfo.num_items[GLDIT_TSPRITE] > 0)
2732 {
2733 int twall_idx = gld_drawinfo.num_items[GLDIT_TWALL] - 1;
2734 int tsprite_idx = gld_drawinfo.num_items[GLDIT_TSPRITE] - 1;
2735
2736 if (tsprite_idx > 0)
2737 gld_DrawItemsSortSprites(GLDIT_TSPRITE);
2738
2739 while (twall_idx >= 0 || tsprite_idx >= 0 )
2740 {
2741 dboolean draw_tsprite = false;
2742
2743 /* find out what is next to draw */
2744 if (twall_idx >= 0 && tsprite_idx >= 0)
2745 {
2746 /* both are left to draw, determine
2747 * which is farther */
2748 seg_t *twseg = gld_drawinfo.items[GLDIT_TWALL][twall_idx].item.wall->seg;
2749 int ti;
2750 for (ti = tsprite_idx; ti >= 0; ti--) {
2751 /* reconstruct the sprite xy */
2752 fixed_t tsx = gld_drawinfo.items[GLDIT_TSPRITE][ti].item.sprite->fx;
2753 fixed_t tsy = gld_drawinfo.items[GLDIT_TSPRITE][ti].item.sprite->fy;
2754
2755 if (R_PointOnSegSide(tsx, tsy, twseg))
2756 {
2757 /* a thing is behind the seg */
2758 /* do not draw the seg yet */
2759 draw_tsprite = true;
2760 break;
2761 }
2762 }
2763 }
2764 else if (tsprite_idx >= 0)
2765 {
2766 /* no transparent walls left, draw a sprite */
2767 draw_tsprite = true;
2768 }
2769 /* fall-through case is draw wall */
2770
2771 if (draw_tsprite)
2772 {
2773 /* transparent sprite is farther, draw it */
2774 gld_DrawSprite(gld_drawinfo.items[GLDIT_TSPRITE][tsprite_idx].item.sprite);
2775 tsprite_idx--;
2776 }
2777 else
2778 {
2779 glDepthMask(GL_FALSE);
2780 /* transparent wall is farther, draw it */
2781 gld_ProcessWall(gld_drawinfo.items[GLDIT_TWALL][twall_idx].item.wall);
2782 glDepthMask(GL_TRUE);
2783 twall_idx--;
2784 }
2785 }
2786 glEnable(GL_ALPHA_TEST);
2787 }
2788
2789 if (gl_ext_arb_vertex_buffer_object)
2790 {
2791 // bind with 0, so, switch back to normal pointer operation
2792 GLEXT_glBindBufferARB(GL_ARRAY_BUFFER, 0);
2793 }
2794 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2795 glDisableClientState(GL_VERTEX_ARRAY);
2796 glDisableClientState(GL_COLOR_ARRAY);
2797
2798 glsl_PopMainShader();
2799 }