3 * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
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.
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.
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/>.
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];
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)
47 image_width = _image_width;
48 image_height = _image_height;
50 /* generate texture */
51 for (texture_size = 1; texture_size <= image_width || texture_size <= image_height; texture_size *= 2)
54 legacy_rgb = calloc(texture_size * texture_size, 3);
56 print_error("Failed to allocate texture\n");
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);
68 benson_rgb = calloc(texture_size * texture_size, 3);
70 print_error("Failed to allocate texture\n");
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);
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)
95 if (num < 0 || num >= MAX_OSD) {
96 print_error("given OSD number out of range");
101 osd_width[num] = _osd_width;
102 osd_height[num] = _osd_height;
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)
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");
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]);
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)
129 screen_width = _screen_width;
130 screen_height = _screen_height;
133 void opengl_clear(void)
136 glClearColor(0.0, 0.0, 0.0, 1.0);
137 glClear(GL_COLOR_BUFFER_BIT);
140 /* set viewport for legacy image */
141 void opengl_viewport_legacy(int split)
144 int view_width, view_height;
145 int new_width, new_height;
150 view_width = screen_width;
151 view_height = screen_height;
154 view_y = screen_height / 2;
155 view_width = screen_width;
156 view_height = screen_height / 2;
159 /* avoid division by zero, if one dimension is too small */
160 if (view_width < 1 || view_height < 1)
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;
174 /* avoid views that are too small */
175 if (view_width < 1 || view_height < 1)
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);
182 glOrtho(0.0, 1.0, 1.0, 0.0, -1.0, 1.0);
183 glMatrixMode(GL_MODELVIEW);
186 /* render legacy image texture */
187 void opengl_blit_legacy(uint8_t *rgb, int filter)
189 double width = (double)image_width / (double)texture_size;
190 double height = (double)image_height / (double)texture_size;
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);
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);
208 glDisable(GL_TEXTURE_2D);
211 /* set viewport for improved rendering */
212 void opengl_viewport_improved(int split, int benson_at_line, double fov, double benson_size)
215 int view_width, view_height;
216 int new_width, new_height;
218 /* center view, or put it in the top half of the window */
222 view_width = screen_width;
223 view_height = screen_height;
227 view_width = screen_width;
228 view_height = screen_height / 2;
231 /* avoid division by zero, if one dimension is too small */
232 if (view_width < 1 || view_height < 1)
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;
246 /* avoid views that are too small */
247 if (view_width < 1 || view_height < 1)
250 /* viewport and projection matrix */
251 glViewport((GLsizei)view_x, (GLsizei)view_y, (GLsizei)view_width, (GLsizei)view_height);
252 glMatrixMode(GL_PROJECTION);
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);
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)
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;
279 /* calculate field-of-view */
280 double slope = tan(fov / 360 * M_PI);
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);
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);
300 glDisable(GL_TEXTURE_2D);
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)
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;
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);
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;
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);
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);
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);
365 glDisable(GL_TEXTURE_2D);
369 /* set color and opacity */
370 void opengl_render_color(double r, double g, double b, double a)
374 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
375 glColor4d(r, g, b, a);
383 void opengl_render_polygon(double *x, double *y, double *z, int count, int cull_face)
388 glEnable(GL_CULL_FACE);
393 for (i = 0; i < count; i++)
394 glVertex3d(x[i], y[i], -z[i]);
397 glDisable(GL_CULL_FACE);
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)
406 for (i = 0; i < count; i++)
407 glVertex3d(x[i], y[i], -z[i]);
409 glBegin(GL_LINE_LOOP);
410 for (i = 0; i < count; i++)
411 glVertex3d(x[i], y[i], -z[i]);
416 void opengl_render_line(double x1, double y1, double z1, double x2, double y2, double z2, double size)
420 glVertex3f(x1, y1, -z1);
421 glVertex3f(x2, y2, -z2);
428 void opengl_render_point(double x, double y, double z, double size)
432 glVertex3f(x, y, -z);
438 /* free image texture */
439 void exit_opengl(void)
451 for (i = 0; i < MAX_OSD; i++) {