Debian Codegraph @

prboom2/src/dsda/gl/render_scale.c

raw
1 //
2 // Copyright(C) 2022 by Ryan Krafnick
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 // Data for rendering non-exclusive fullscreen in OpenGL
16 // Original Author: elim
17 //
18
19 #include "render_scale.h"
20
21 #include "gl_opengl.h"
22 #include "gl_intern.h"
23
24 #include "i_video.h"
25 #include "r_main.h"
26 #include "st_stuff.h"
27
28 #include <math.h>
29
30 int gl_statusbar_height;
31 int gl_scene_width;
32 int gl_scene_height;
33 float gl_scale_x;
34 float gl_scale_y;
35 int gl_letterbox_clear_required = 0;
36
37 static int gl_clear_box_width;
38 static int gl_clear_box_height;
39
40
41 void dsda_GLSetRenderViewportParams() {
42 extern SDL_Rect viewport_rect;
43 gl_scale_x = (float)viewport_rect.w / (float)SCREENWIDTH;
44 gl_scale_y = (float)viewport_rect.h / (float)SCREENHEIGHT;
45
46 // elim - This will be zero if no statusbar is being drawn
47 gl_statusbar_height = ceilf(gl_scale_y * (float)ST_SCALED_HEIGHT) * R_PartialView();
48
49 gl_scene_width = viewport_rect.w;
50 gl_scene_height = viewport_rect.h - gl_statusbar_height;
51
52 // elim - If the viewport's x or y coordinate is indented from the window, we need to call glClear
53 // each frame to prevent artifacts smearing on undrawn framebuffer area
54 gl_letterbox_clear_required = viewport_rect.x + viewport_rect.y;
55 if (gl_letterbox_clear_required) {
56 gl_clear_box_width = ((viewport_rect.y != 0) * renderer_rect.w) + viewport_rect.x;
57 gl_clear_box_height = ((viewport_rect.y == 0) * renderer_rect.h) + viewport_rect.y;
58 }
59 }
60
61 void dsda_GLSetRenderViewport() {
62 glViewport(viewport_rect.x, viewport_rect.y, viewport_rect.w, viewport_rect.h);
63 }
64
65 void dsda_GLSetRenderViewportScissor() {
66 glScissor(viewport_rect.x, viewport_rect.y, viewport_rect.w, viewport_rect.h);
67 }
68
69 void dsda_GLSetRenderSceneScissor() {
70 glScissor(viewport_rect.x,
71 viewport_rect.y + gl_statusbar_height,
72 gl_scene_width, gl_scene_height);
73 }
74
75 void dsda_GLSetScreenSpaceScissor(int x, int y, int w, int h)
76 {
77 glScissor(viewport_rect.x + x * gl_scale_x,
78 viewport_rect.y + (SCREENHEIGHT - (y + h)) * gl_scale_y,
79 w * gl_scale_x,
80 h * gl_scale_y);
81 }
82
83 void dsda_GLUpdateStatusBarVisible() {
84 int saved_visible;
85 int current_visible;
86
87 saved_visible = (gl_statusbar_height > 0);
88 current_visible = R_PartialView();
89
90 if (saved_visible != current_visible) {
91 gl_statusbar_height = (int)(gl_scale_y * (float)ST_SCALED_HEIGHT) * R_PartialView();
92 gl_scene_height = viewport_rect.h - gl_statusbar_height;
93 }
94 }
95
96 void dsda_GLLetterboxClear() {
97 if (!gl_letterbox_clear_required)
98 return;
99
100 glViewport(0, 0, renderer_rect.w, renderer_rect.h);
101 glEnable(GL_SCISSOR_TEST);
102 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
103
104 // Bottom or left box
105 glScissor(0, 0, gl_clear_box_width, gl_clear_box_height);
106 glClear(GL_COLOR_BUFFER_BIT);
107
108 // Top or right box
109 glScissor(renderer_rect.w - gl_clear_box_width,
110 renderer_rect.h - gl_clear_box_height,
111 gl_clear_box_width, gl_clear_box_height);
112 glClear(GL_COLOR_BUFFER_BIT);
113
114 // Reset to expected state before rendering the actual frame starts
115 dsda_GLSetRenderViewport();
116 dsda_GLSetRenderViewportScissor();
117 }
118
119 void dsda_GLStartMeltRenderTexture() {
120 if (!SceneInTexture)
121 return;
122
123 gld_InitDrawScene();
124 gld_StartDrawScene();
125 gld_Set2DMode();
126 glViewport(0, 0, SCREENWIDTH, SCREENHEIGHT);
127 glScissor(0, 0, SCREENWIDTH, SCREENHEIGHT);
128 }
129
130 void dsda_GLEndMeltRenderTexture() {
131 if (!SceneInTexture)
132 return;
133
134 GLEXT_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
135 glBindTexture(GL_TEXTURE_2D, glSceneImageTextureFBOTexID);
136
137 dsda_GLFullscreenOrtho2D();
138 dsda_GLSetRenderViewport();
139 dsda_GLSetRenderViewportScissor();
140
141 glBegin(GL_TRIANGLE_STRIP);
142 {
143 glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, 0.0f);
144 glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, renderer_rect.h);
145 glTexCoord2f(1.0f, 1.0f); glVertex2f((float)renderer_rect.w, 0.0f);
146 glTexCoord2f(1.0f, 0.0f); glVertex2f((float)renderer_rect.w, (float)renderer_rect.h);
147 }
148 glEnd();
149
150 gld_Set2DMode();
151 glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
152 }
153
154 void dsda_GLFullscreenOrtho2D() {
155 glMatrixMode(GL_MODELVIEW);
156 glLoadIdentity();
157 glMatrixMode(GL_PROJECTION);
158 glLoadIdentity();
159 glOrtho(
160 (GLdouble) 0,
161 (GLdouble) renderer_rect.w,
162 (GLdouble) renderer_rect.h,
163 (GLdouble) 0,
164 (GLdouble) -1.0,
165 (GLdouble) 1.0
166 );
167 glDisable(GL_DEPTH_TEST);
168 }