Fix viewport resize and cleanup code structure of viewport width and height
[mercenary-reloaded.git] / src / libsdl / opengl.c
1 /* OpenGL rendering
2  *
3  * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
4  * All Rights Reserved
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25 #include <errno.h>
26 #include "print.h"
27 #include "opengl.h"
28 #define GL3_PROTOTYPES 1
29 #include <GL/glew.h>
30
31 #define MAX_OSD 2
32 #define MONITOR_DISTANCE 35.0
33 static uint8_t *image_rgb = NULL;
34 static uint8_t *osd_rgba[MAX_OSD] = { NULL, NULL };
35 static GLuint image_name;
36 static GLuint osd_name[MAX_OSD];
37 static int image_width, image_height;
38 static int osd_width[MAX_OSD], osd_height[MAX_OSD];
39 static int texture_size;
40 static int osd_size[MAX_OSD];
41 static int flip_y;
42
43 /* alloc and init an image texture with size that is greater than the power of two */
44 int init_opengl_image(int _image_width, int _image_height)
45 {
46         int rc;
47
48         image_width = _image_width;
49         image_height = _image_height;
50
51         /* generate texture */
52         for (texture_size = 1; texture_size <= image_width || texture_size <= image_height; texture_size *= 2)
53                 ;
54
55         image_rgb = calloc(texture_size * texture_size, 3);
56         if (!image_rgb) {
57                 print_error("Failed to allocate texture\n");
58                 rc = -ENOMEM;
59                 goto error;
60         }
61         glShadeModel(GL_FLAT);
62         glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* bytes */
63         glGenTextures(1, &image_name);
64         glBindTexture(GL_TEXTURE_2D, image_name);
65         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
66         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
67         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_size, texture_size, 0, GL_RGB, GL_UNSIGNED_BYTE, image_rgb);
68
69
70         return 0;
71
72 error:
73         exit_opengl();
74         return rc;
75 }
76
77 /* alloc and init an osd texture with size that is greater than the power of two */
78 int init_opengl_osd(int num, int _osd_width, int _osd_height)
79 {
80         int rc;
81
82         if (num < 0 || num >= MAX_OSD) {
83                 print_error("given OSD number out of range");
84                 rc = -ENOMEM;
85                 goto error;
86         }
87
88         osd_width[num] = _osd_width;
89         osd_height[num] = _osd_height;
90
91         /* generate texture */
92         for (osd_size[num] = 1; osd_size[num] <= osd_width[num] || osd_size[num] <= osd_height[num]; osd_size[num] *= 2)
93                 ;
94         osd_rgba[num] = calloc(osd_size[num] * osd_size[num], 4);
95         if (!osd_rgba[num]) {
96                 print_error("Failed to allocate texture\n");
97                 rc = -ENOMEM;
98                 goto error;
99         }
100         glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* bytes */
101         glGenTextures(1, &osd_name[num]);
102         glBindTexture(GL_TEXTURE_2D, osd_name[num]);
103         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
104         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
105         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, osd_size[num], osd_size[num], 0, GL_RGBA, GL_UNSIGNED_BYTE, osd_rgba[num]);
106
107         return 0;
108
109 error:
110         exit_opengl();
111         return rc;
112 }
113
114 void opengl_clear(int _flip_y)
115 {
116         flip_y = _flip_y;
117
118         /* clear screen */
119         glClearColor(0.0, 0.0, 0.0, 1.0);
120         glClear(GL_COLOR_BUFFER_BIT);
121 }
122
123 /* set viewport for improved rendering */
124 void opengl_viewport(int view_width, int view_height, int split, int benson_at_line, double fov, double benson_size)
125 {
126         int view_x = 0, view_y = 0;
127         int new_width, new_height;
128
129         /* center view, or put it in the top half of the window */
130         if (split == 1) {
131                 view_y = view_height / 2;
132                 view_height = view_height / 2;
133         } else if (split == 2) {
134                 view_height = view_height / 2;
135         }
136
137         /* avoid division by zero, if one dimension is too small */
138         if (view_width < 1 || view_height < 1)
139                 return;
140
141         /* calculate a viewport that has apect of image_width:image_height */
142         if (view_height * image_width > view_width * image_height) {
143                 new_height = view_width * image_height / image_width;
144                 view_y = view_y + view_height / 2 - new_height / 2;
145                 view_height = new_height;
146         } else if (view_height * image_width < view_width * image_height) {
147                 new_width = view_height * image_width / image_height;
148                 view_x = view_x + view_width / 2 - new_width / 2;
149                 view_width = new_width;
150         }
151
152         /* avoid views that are too small */
153         if (view_width < 1 || view_height < 1)
154                 return;
155
156         /* viewport and projection matrix */
157         glViewport((GLsizei)view_x, (GLsizei)view_y, (GLsizei)view_width, (GLsizei)view_height);
158         glMatrixMode(GL_PROJECTION);
159         glLoadIdentity();
160
161         /* calculate field-of-view */
162         double slope = tan(fov / 360 * M_PI);
163         /* make frustum to center the view in the game view above benson */
164         double left = -slope;
165         double right = slope;
166         double benson_start_at_position = ((double)image_height - (double)benson_at_line) * benson_size;
167         double top = slope * ((double)image_height - benson_start_at_position) / (double)image_width;
168         double bottom = -slope * ((double)image_height * 2.0 - ((double)image_height - benson_start_at_position)) / (double)image_width;
169         glFrustum(left, right, bottom, top, 1.0, 5000000000.0);
170
171         glMatrixMode(GL_MODELVIEW);
172 }
173
174 /* render image or only benson */
175 void opengl_blit_image(uint8_t *rgb, int filter, int benson_at_line, int render_benson_only, double fov, double monitor_distance, double benson_size)
176 {
177         double texture_left = 0;
178         double texture_right = ((double)image_width) / (double)texture_size;
179         double texture_top = ((double)benson_at_line) / (double)texture_size;
180         double texture_bottom = ((double)image_height) / (double)texture_size;
181         double image_start_at_position = ((double)image_height - (double)benson_at_line) * benson_size;
182         double image_top = -((double)image_height - image_start_at_position) / (double)image_width;
183         double image_bottom = -((double)image_height * 2.0 - ((double)image_height - image_start_at_position)) / (double)image_width;
184
185         /* calculate field-of-view */
186         double slope = tan(fov / 360 * M_PI) * monitor_distance;
187
188         /* render image */
189         glEnable(GL_TEXTURE_2D);
190         glBindTexture(GL_TEXTURE_2D, image_name);
191         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);  /* no modulation with color */
192         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (filter) ? GL_LINEAR : GL_NEAREST);
193         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (filter) ? GL_LINEAR : GL_NEAREST);
194         if (render_benson_only) {
195                 /* render benson only, and copy top line of benson to one line above, so the texture filter will not take false data above benson */
196                 rgb += image_width * (benson_at_line - 1) * 3;
197                 memcpy(rgb, rgb + image_width * 3, image_width * 3);
198                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, benson_at_line - 1, image_width, image_height - benson_at_line - 1, GL_RGB, GL_UNSIGNED_BYTE, rgb);
199         } else {
200                 image_top = -image_top;
201                 texture_top = 0.0;
202                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image_width, image_height, GL_RGB, GL_UNSIGNED_BYTE, rgb);
203         }
204         glBegin(GL_QUADS);
205         glTexCoord2f(texture_left, texture_bottom);
206         glVertex3f(-benson_size * slope, image_bottom * slope, -monitor_distance);
207         glTexCoord2f(texture_right, texture_bottom);
208         glVertex3f(benson_size * slope, image_bottom * slope, -monitor_distance);
209         glTexCoord2f(texture_right, texture_top);
210         glVertex3f(benson_size * slope, image_top * slope, -monitor_distance);
211         glTexCoord2f(texture_left, texture_top);
212         glVertex3f(-benson_size * slope, image_top * slope, -monitor_distance);
213         glEnd();
214         glDisable(GL_TEXTURE_2D);
215 }
216
217 /* render osd texture */
218 void opengl_blit_osd(int num, uint8_t *rgba, int filter, int benson_at_line, double fov, double monitor_distance, double benson_size, double scale_x, double scale_y, double offset_x, double offset_y)
219 {
220         double texture_left = 0.0;
221         double texture_right = (double)osd_width[num] / (double)osd_size[num];
222         double texture_top = 0.0;
223         double texture_bottom = (double)osd_height[num] / (double)osd_size[num];
224         double benson_start_at_position;
225         double osd_left = 0.0;
226         double osd_right = 1.0;
227         double osd_top = 0.0;
228         double osd_bottom = 1.0;
229         double slope, range, center;
230         if (fov) {
231                 osd_left = -1.0;
232                 osd_right = 1.0;
233                 benson_start_at_position = ((double)image_height - (double)benson_at_line) * benson_size;
234                 osd_top = ((double)image_height - benson_start_at_position) / (double)image_width;
235                 osd_bottom = -((double)image_height * 2.0 - ((double)image_height - benson_start_at_position)) / (double)image_width;
236                 /* calculate field-of-view */
237                 slope = tan(fov / 360 * M_PI) * monitor_distance;
238         }
239         range = (osd_right - osd_left) / 2.0;
240         center = (osd_right + osd_left) / 2.0;
241         osd_left = (osd_left - center) * scale_x + center + range * offset_x;
242         osd_right = (osd_right - center) * scale_x + center + range * offset_x;
243         range = (osd_bottom - osd_top) / 2.0;
244         center = (osd_bottom + osd_top) / 2.0;
245         osd_top = (osd_top - center) * scale_y + center + range * offset_y;
246         osd_bottom = (osd_bottom - center) * scale_y + center + range * offset_y;
247
248         glEnable(GL_BLEND);
249         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
250         glEnable(GL_TEXTURE_2D);
251         glBindTexture(GL_TEXTURE_2D, osd_name[num]);
252         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);  /* no modulation with color */
253         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (filter) ? GL_LINEAR : GL_NEAREST);
254         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (filter) ? GL_LINEAR : GL_NEAREST);
255         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, osd_width[num], osd_height[num], GL_RGBA, GL_UNSIGNED_BYTE, rgba);
256         glBegin(GL_QUADS);
257         if (fov) {
258                 /* perspective viewport */
259                 glTexCoord2f(texture_left, texture_bottom);
260                 glVertex3f(osd_left * slope, osd_bottom * slope, -monitor_distance);
261                 glTexCoord2f(texture_right, texture_bottom);
262                 glVertex3f(osd_right * slope, osd_bottom * slope, -monitor_distance);
263                 glTexCoord2f(texture_right, texture_top);
264                 glVertex3f(osd_right * slope, osd_top * slope, -monitor_distance);
265                 glTexCoord2f(texture_left, texture_top);
266                 glVertex3f(osd_left * slope, osd_top * slope, -monitor_distance);
267         } else {
268                 /* orthogonal viewport */
269                 glTexCoord2f(texture_left, texture_top);
270                 glVertex3f(osd_left, osd_top, 0.0);
271                 glTexCoord2f(texture_right, texture_top);
272                 glVertex3f(osd_right, osd_top, 0.0);
273                 glTexCoord2f(texture_right, texture_bottom);
274                 glVertex3f(osd_right, osd_bottom, 0.0);
275                 glTexCoord2f(texture_left, texture_bottom);
276                 glVertex3f(osd_left, osd_bottom, 0.0);
277         }
278         glEnd();
279         glDisable(GL_TEXTURE_2D);
280         glDisable(GL_BLEND);
281 }
282
283 /* set color and opacity */
284 void opengl_render_color(double r, double g, double b, double a)
285 {
286         if (a < 1.0) {
287                 glEnable(GL_BLEND);
288                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
289                 glColor4d(r, g, b, a);
290         } else {
291                 glDisable(GL_BLEND);
292                 glColor3d(r, g, b);
293         }
294 }
295
296 /* render polygon */
297 void opengl_render_polygon(double *x, double *y, double *z, int count, int cull_face)
298 {
299         int i;
300
301         if (cull_face) {
302                 glEnable(GL_CULL_FACE);
303                 glFrontFace((flip_y) ? GL_CCW : GL_CW);
304                 glCullFace(GL_BACK);
305         }
306         glBegin(GL_POLYGON);
307         for (i = 0; i < count; i++)
308                 glVertex3d(x[i], y[i], -z[i]);
309         glEnd();
310         if (cull_face)
311                 glDisable(GL_CULL_FACE);
312 }
313
314 /* render polygon, but make sure any size of it is visible */
315 void opengl_render_polygon_and_line(double *x, double *y, double *z, int count)
316 {
317         int i;
318
319         glBegin(GL_POLYGON);
320         for (i = 0; i < count; i++)
321                 glVertex3d(x[i], y[i], -z[i]);
322         glEnd();
323         glBegin(GL_LINE_LOOP);
324         for (i = 0; i < count; i++)
325                 glVertex3d(x[i], y[i], -z[i]);
326         glEnd();
327 }
328
329 /* render line */
330 void opengl_render_line(double x1, double y1, double z1, double x2, double y2, double z2, double size)
331 {
332         if (size == 0.0) {
333                 glBegin(GL_LINES);
334                 glVertex3f(x1, y1, -z1);
335                 glVertex3f(x2, y2, -z2);
336                 glEnd();
337                 return;
338         }
339 }
340
341 /* render point */
342 void opengl_render_point(double x, double y, double z, double size)
343 {
344         if (size == 0.0) {
345                 glBegin(GL_POINTS);
346                 glVertex3f(x, y, -z);
347                 glEnd();
348                 return;
349         }
350 }
351
352 /* free image texture */
353 void exit_opengl(void)
354 {
355         int i;
356
357         if (image_rgb) {
358                 free(image_rgb);
359                 image_rgb = NULL;
360         }
361         for (i = 0; i < MAX_OSD; i++) {
362                 if (osd_rgba[i]) {
363                         free(osd_rgba[i]);
364                         osd_rgba[i] = NULL;
365                 }
366         }
367 }
368