Fix rendering buffer handling
[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 <math.h>
24 #include <errno.h>
25 #include "print.h"
26 #include "opengl.h"
27 #include <GL/glew.h>
28
29 #define MAX_OSD 2
30 static uint8_t *legacy_rgb = NULL;
31 static uint8_t *benson_rgb = NULL;
32 static uint8_t *osd_rgba[MAX_OSD] = { NULL, NULL };
33 static GLuint legacy_name;
34 static GLuint benson_name;
35 static GLuint osd_name[MAX_OSD];
36 static int screen_width, screen_height;
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
42 /* alloc and init an image texture with size that is greater than the power of two */
43 int init_opengl(int _image_width, int _image_height)
44 {
45         int rc;
46
47         image_width = _image_width;
48         image_height = _image_height;
49
50         /* generate texture */
51         for (texture_size = 1; texture_size <= image_width || texture_size <= image_height; texture_size *= 2)
52                 ;
53
54         legacy_rgb = calloc(texture_size * texture_size, 3);
55         if (!legacy_rgb) {
56                 print_error("Failed to allocate texture\n");
57                 rc = -ENOMEM;
58                 goto error;
59         }
60         glShadeModel(GL_FLAT);
61         glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* bytes */
62         glGenTextures(1, &legacy_name);
63         glBindTexture(GL_TEXTURE_2D, legacy_name);
64         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
65         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
66         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_size, texture_size, 0, GL_RGB, GL_UNSIGNED_BYTE, legacy_rgb);
67
68         benson_rgb = calloc(texture_size * texture_size, 3);
69         if (!benson_rgb) {
70                 print_error("Failed to allocate texture\n");
71                 rc = -ENOMEM;
72                 goto error;
73         }
74
75         glShadeModel(GL_FLAT);
76         glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* bytes */
77         glGenTextures(1, &benson_name);
78         glBindTexture(GL_TEXTURE_2D, benson_name);
79         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
80         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
81         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_size, texture_size, 0, GL_RGB, GL_UNSIGNED_BYTE, benson_rgb);
82
83         return 0;
84
85 error:
86         exit_opengl();
87         return rc;
88 }
89
90 /* alloc and init an osd texture with size that is greater than the power of two */
91 int init_osd(int num, int _osd_width, int _osd_height)
92 {
93         int rc;
94
95         if (num < 0 || num >= MAX_OSD) {
96                 print_error("given OSD number out of range");
97                 rc = -ENOMEM;
98                 goto error;
99         }
100
101         osd_width[num] = _osd_width;
102         osd_height[num] = _osd_height;
103
104         /* generate texture */
105         for (osd_size[num] = 1; osd_size[num] <= osd_width[num] || osd_size[num] <= osd_height[num]; osd_size[num] *= 2)
106                 ;
107         osd_rgba[num] = calloc(osd_size[num] * osd_size[num], 4);
108         if (!osd_rgba[num]) {
109                 print_error("Failed to allocate texture\n");
110                 rc = -ENOMEM;
111                 goto error;
112         }
113         glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* bytes */
114         glGenTextures(1, &osd_name[num]);
115         glBindTexture(GL_TEXTURE_2D, osd_name[num]);
116         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
117         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
118         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, osd_size[num], osd_size[num], 0, GL_RGBA, GL_UNSIGNED_BYTE, osd_rgba[num]);
119
120         return 0;
121
122 error:
123         return rc;
124 }
125
126 /* set clip planes so that the image portion of the image texture is centered and pixels are rectengular */
127 void resize_opengl(int _screen_width, int _screen_height)
128 {
129         screen_width = _screen_width;
130         screen_height = _screen_height;
131 }
132
133 void opengl_clear(void)
134 {
135         /* clear screen */
136         glClearColor(0.0, 0.0, 0.0, 1.0);
137         glClear(GL_COLOR_BUFFER_BIT);
138 }
139
140 /* set viewport for legacy image */
141 void opengl_viewport_legacy(int split)
142 {
143         int view_x, view_y;
144         int view_width, view_height;
145         int new_width, new_height;
146
147         if (!split) {
148                 view_x = 0;
149                 view_y = 0;
150                 view_width = screen_width;
151                 view_height = screen_height;
152         } else {
153                 view_x = 0;
154                 view_y = screen_height / 2;
155                 view_width = screen_width;
156                 view_height = screen_height / 2;
157         }
158
159         /* avoid division by zero, if one dimension is too small */
160         if (view_width < 1 || view_height < 1)
161                 return;
162
163         /* calculate a viewport that has apect of image_width:image_height */
164         if (view_height * image_width > view_width * image_height) {
165                 new_height = view_width * image_height / image_width;
166                 view_y = view_y + view_height / 2 - new_height / 2;
167                 view_height = new_height;
168         } else if (view_height * image_width < view_width * image_height) {
169                 new_width = view_height * image_width / image_height;
170                 view_x = view_x + view_width / 2 - new_width / 2;
171                 view_width = new_width;
172         }
173
174         /* avoid views that are too small */
175         if (view_width < 1 || view_height < 1)
176                 return;
177
178         /* viewport and projection matrix to view rectangle */
179         glViewport((GLsizei)view_x, (GLsizei)view_y, (GLsizei)view_width, (GLsizei)view_height);
180         glMatrixMode(GL_PROJECTION);
181         glLoadIdentity();
182         glOrtho(0.0, 1.0, 1.0, 0.0, -1.0, 1.0);
183         glMatrixMode(GL_MODELVIEW);
184 }
185
186 /* render legacy image texture */
187 void opengl_blit_legacy(uint8_t *rgb, int filter)
188 {
189         double width = (double)image_width / (double)texture_size;
190         double height = (double)image_height / (double)texture_size;
191
192         glEnable(GL_TEXTURE_2D);
193         glBindTexture(GL_TEXTURE_2D, legacy_name);
194         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);  /* no modulation with color */
195         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (filter) ? GL_LINEAR : GL_NEAREST);
196         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (filter) ? GL_LINEAR : GL_NEAREST);
197         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image_width, image_height, GL_RGB, GL_UNSIGNED_BYTE, rgb);
198         glBegin(GL_QUADS);
199         glTexCoord2f(0.0, 0.0);
200         glVertex3f(0.0, 0.0, 0.0);
201         glTexCoord2f(width, 0.0);
202         glVertex3f(1.0, 0.0, 0.0);
203         glTexCoord2f(width, height);
204         glVertex3f(1.0, 1.0, 0.0);
205         glTexCoord2f(0.0, height);
206         glVertex3f(0.0, 1.0, 0.0);
207         glEnd();
208         glDisable(GL_TEXTURE_2D);
209 }
210
211 /* set viewport for improved rendering */
212 void opengl_viewport_improved(int split, int benson_at_line, double fov, double benson_size)
213 {
214         int view_x, view_y;
215         int view_width, view_height;
216         int new_width, new_height;
217
218         /* center view, or put it in the top half of the window */
219         if (!split) {
220                 view_x = 0;
221                 view_y = 0;
222                 view_width = screen_width;
223                 view_height = screen_height;
224         } else {
225                 view_x = 0;
226                 view_y = 0;
227                 view_width = screen_width;
228                 view_height = screen_height / 2;
229         }
230
231         /* avoid division by zero, if one dimension is too small */
232         if (view_width < 1 || view_height < 1)
233                 return;
234
235         /* calculate a viewport that has apect of image_width:image_height */
236         if (view_height * image_width > view_width * image_height) {
237                 new_height = view_width * image_height / image_width;
238                 view_y = view_y + view_height / 2 - new_height / 2;
239                 view_height = new_height;
240         } else if (view_height * image_width < view_width * image_height) {
241                 new_width = view_height * image_width / image_height;
242                 view_x = view_x + view_width / 2 - new_width / 2;
243                 view_width = new_width;
244         }
245
246         /* avoid views that are too small */
247         if (view_width < 1 || view_height < 1)
248                 return;
249
250         /* viewport and projection matrix */
251         glViewport((GLsizei)view_x, (GLsizei)view_y, (GLsizei)view_width, (GLsizei)view_height);
252         glMatrixMode(GL_PROJECTION);
253         glLoadIdentity();
254
255         /* calculate field-of-view */
256         double slope = tan(fov / 360 * M_PI);
257         /* make frustum to center the view in the game view above benson */
258         double left = -slope;
259         double right = slope;
260         double benson_start_at_position = ((double)image_height - (double)benson_at_line) * benson_size;
261         double top = slope * ((double)image_height - benson_start_at_position) / (double)image_width;
262         double bottom = -slope * ((double)image_height * 2.0 - ((double)image_height - benson_start_at_position)) / (double)image_width;
263         glFrustum(left, right, bottom, top, 1.0, 5000000000.0);
264         glMatrixMode(GL_MODELVIEW);
265 }
266
267 /* render only benson */
268 void opengl_blit_benson(uint8_t *rgb, int filter, int benson_at_line, double fov, double benson_size, int pixel_size)
269 {
270         double border = (benson_size != 1.0) ? (double)pixel_size : 0.0;
271         double texture_left = border / (double)texture_size;
272         double texture_right = ((double)image_width - border) / (double)texture_size;
273         double texture_top = ((double)benson_at_line + border) / (double)texture_size;
274         double texture_bottom = ((double)image_height -border) / (double)texture_size;
275         double benson_start_at_position = ((double)image_height - (double)benson_at_line) * benson_size;
276         double benson_top = -((double)image_height - benson_start_at_position) / (double)image_width;
277         double benson_bottom = -((double)image_height * 2.0 - ((double)image_height - benson_start_at_position)) / (double)image_width;
278
279         /* calculate field-of-view */
280         double slope = tan(fov / 360 * M_PI);
281
282         /* render benson */
283         rgb += image_width * benson_at_line * 3;
284         glEnable(GL_TEXTURE_2D);
285         glBindTexture(GL_TEXTURE_2D, benson_name);
286         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);  /* no modulation with color */
287         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (filter) ? GL_LINEAR : GL_NEAREST);
288         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (filter) ? GL_LINEAR : GL_NEAREST);
289         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, benson_at_line, image_width, image_height - benson_at_line, GL_RGB, GL_UNSIGNED_BYTE, rgb);
290         glBegin(GL_QUADS);
291         glTexCoord2f(texture_left, texture_bottom);
292         glVertex3f(-100.0 * benson_size, 100.0 * benson_bottom, -100.0 / slope);
293         glTexCoord2f(texture_right, texture_bottom);
294         glVertex3f(100.0 * benson_size, 100.0 * benson_bottom, -100.0 / slope);
295         glTexCoord2f(texture_right, texture_top);
296         glVertex3f(100.0 * benson_size, 100.0 * benson_top, -100.0 / slope);
297         glTexCoord2f(texture_left, texture_top);
298         glVertex3f(-100.0 * benson_size, 100.0 * benson_top, -100.0 / slope);
299         glEnd();
300         glDisable(GL_TEXTURE_2D);
301 }
302
303 /* render osd texture */
304 void opengl_blit_osd(int num, uint8_t *rgba, int filter, int benson_at_line, double fov, double benson_size, double scale_x, double scale_y, double offset_x, double offset_y)
305 {
306         double texture_left = 0.0;
307         double texture_right = (double)osd_width[num] / (double)osd_size[num];
308         double texture_top = 0.0;
309         double texture_bottom = (double)osd_height[num] / (double)osd_size[num];
310         double benson_start_at_position;
311         double osd_left = 0.0;
312         double osd_right = 1.0;
313         double osd_top = 0.0;
314         double osd_bottom = 1.0;
315         double slope, range, center;
316         if (fov) {
317                 osd_left = -1.0;
318                 osd_right = 1.0;
319                 benson_start_at_position = ((double)image_height - (double)benson_at_line) * benson_size;
320                 osd_top = ((double)image_height - benson_start_at_position) / (double)image_width;
321                 osd_bottom = -((double)image_height * 2.0 - ((double)image_height - benson_start_at_position)) / (double)image_width;
322                 /* calculate field-of-view */
323                 slope = tan(fov / 360 * M_PI);
324         }
325         range = (osd_right - osd_left) / 2.0;
326         center = (osd_right + osd_left) / 2.0;
327         osd_left = (osd_left - center) * scale_x + center + range * offset_x;
328         osd_right = (osd_right - center) * scale_x + center + range * offset_x;
329         range = (osd_bottom - osd_top) / 2.0;
330         center = (osd_bottom + osd_top) / 2.0;
331         osd_top = (osd_top - center) * scale_y + center + range * offset_y;
332         osd_bottom = (osd_bottom - center) * scale_y + center + range * offset_y;
333
334         glEnable(GL_BLEND);
335         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
336         glEnable(GL_TEXTURE_2D);
337         glBindTexture(GL_TEXTURE_2D, osd_name[num]);
338         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);  /* no modulation with color */
339         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (filter) ? GL_LINEAR : GL_NEAREST);
340         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (filter) ? GL_LINEAR : GL_NEAREST);
341         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, osd_width[num], osd_height[num], GL_RGBA, GL_UNSIGNED_BYTE, rgba);
342         glBegin(GL_QUADS);
343         if (fov) {
344                 /* perspective viewport */
345                 glTexCoord2f(texture_left, texture_bottom);
346                 glVertex3f(100.0 * osd_left, 100.0 * osd_bottom, -100.0 / slope);
347                 glTexCoord2f(texture_right, texture_bottom);
348                 glVertex3f(100.0 * osd_right, 100.0 * osd_bottom, -100.0 / slope);
349                 glTexCoord2f(texture_right, texture_top);
350                 glVertex3f(100.0 * osd_right, 100.0 * osd_top, -100.0 / slope);
351                 glTexCoord2f(texture_left, texture_top);
352                 glVertex3f(100.0 * osd_left, 100.0 * osd_top, -100.0 / slope);
353         } else {
354                 /* orthogonal viewport */
355                 glTexCoord2f(texture_left, texture_top);
356                 glVertex3f(osd_left, osd_top, 0.0);
357                 glTexCoord2f(texture_right, texture_top);
358                 glVertex3f(osd_right, osd_top, 0.0);
359                 glTexCoord2f(texture_right, texture_bottom);
360                 glVertex3f(osd_right, osd_bottom, 0.0);
361                 glTexCoord2f(texture_left, texture_bottom);
362                 glVertex3f(osd_left, osd_bottom, 0.0);
363         }
364         glEnd();
365         glDisable(GL_TEXTURE_2D);
366         glDisable(GL_BLEND);
367 }
368
369 /* set color and opacity */
370 void opengl_render_color(double r, double g, double b, double a)
371 {
372         if (a < 1.0) {
373                 glEnable(GL_BLEND);
374                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
375                 glColor4d(r, g, b, a);
376         } else {
377                 glDisable(GL_BLEND);
378                 glColor3d(r, g, b);
379         }
380 }
381
382 /* render polygon */
383 void opengl_render_polygon(double *x, double *y, double *z, int count, int cull_face)
384 {
385         int i;
386
387         if (cull_face) {
388                 glEnable(GL_CULL_FACE);
389                 glFrontFace(GL_CW);
390                 glCullFace(GL_BACK);
391         }
392         glBegin(GL_POLYGON);
393         for (i = 0; i < count; i++)
394                 glVertex3d(x[i], y[i], -z[i]);
395         glEnd();
396         if (cull_face)
397                 glDisable(GL_CULL_FACE);
398 }
399
400 /* render polygon, but make sure any size of it is visible */
401 void opengl_render_polygon_and_line(double *x, double *y, double *z, int count)
402 {
403         int i;
404
405         glBegin(GL_POLYGON);
406         for (i = 0; i < count; i++)
407                 glVertex3d(x[i], y[i], -z[i]);
408         glEnd();
409         glBegin(GL_LINE_LOOP);
410         for (i = 0; i < count; i++)
411                 glVertex3d(x[i], y[i], -z[i]);
412         glEnd();
413 }
414
415 /* render line */
416 void opengl_render_line(double x1, double y1, double z1, double x2, double y2, double z2, double size)
417 {
418         if (size == 0.0) {
419                 glBegin(GL_LINES);
420                 glVertex3f(x1, y1, -z1);
421                 glVertex3f(x2, y2, -z2);
422                 glEnd();
423                 return;
424         }
425 }
426
427 /* render point */
428 void opengl_render_point(double x, double y, double z, double size)
429 {
430         if (size == 0.0) {
431                 glBegin(GL_POINTS);
432                 glVertex3f(x, y, -z);
433                 glEnd();
434                 return;
435         }
436 }
437
438 /* free image texture */
439 void exit_opengl(void)
440 {
441         int i;
442
443         if (legacy_rgb) {
444                 free(legacy_rgb);
445                 legacy_rgb = NULL;
446         }
447         if (benson_rgb) {
448                 free(benson_rgb);
449                 benson_rgb = NULL;
450         }
451         for (i = 0; i < MAX_OSD; i++) {
452                 if (osd_rgba[i]) {
453                         free(osd_rgba[i]);
454                         osd_rgba[i] = NULL;
455                 }
456         }
457 }
458