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/>.
28 static uint8_t *text_rgb = NULL;
29 static GLuint text_name;
30 static int screen_width, screen_height;
31 static int image_width, image_height;
32 static int texture_size;
34 /* alloc and init an image texture with size that is greater than the power of two */
35 int init_opengl(int _image_width, int _image_height)
39 image_width = _image_width;
40 image_height = _image_height;
42 /* generate texture */
43 for (texture_size = 1; texture_size <= image_width || texture_size <= image_height; texture_size *= 2)
45 text_rgb = calloc(texture_size * texture_size * 10, 3);
47 print_error("Failed to allocate texture\n");
51 glClearColor(0.0, 0.0, 0.0, 1.0);
52 glShadeModel(GL_FLAT);
53 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* bytes */
54 glGenTextures(1, &text_name);
55 glBindTexture(GL_TEXTURE_2D, text_name);
56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
58 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_size, texture_size, 0, GL_RGB, GL_UNSIGNED_BYTE, text_rgb);
66 /* set clip planes so that the image portion of the image texture is centered and pixels are rectengular */
67 void resize_opengl(int _screen_width, int _screen_height)
69 double width_border = 1.0;
70 double height_border = 1.0;
72 if (_screen_width < 1 || _screen_height < 1)
74 screen_width = _screen_width;
75 screen_height = _screen_height;
77 if (image_width * screen_height > screen_width * image_height) {
78 height_border = (double)(image_width * screen_height) / (double)(screen_width * image_height);
80 if (image_width * screen_height < screen_width * image_height) {
81 width_border = (double)(screen_width * image_height / (double)(image_width * screen_height));
84 /* viewport and projection matrix */
85 glViewport(0, 0, (GLsizei)screen_width, (GLsizei)screen_height);
86 glMatrixMode(GL_PROJECTION);
89 double width = (double)image_width / (double)texture_size;
90 double height = (double)image_height / (double)texture_size;
92 -width * (width_border - 1.0) / 2,
93 width / 2 + width * width_border / 2,
94 height / 2 + height * height_border / 2,
95 -height * (height_border - 1.0) / 2,
97 glMatrixMode(GL_MODELVIEW);
100 /* render image texture */
101 void render_opengl(uint8_t *rgb, int filter)
103 glClear(GL_COLOR_BUFFER_BIT);
104 glEnable(GL_TEXTURE_2D);
105 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); /* no modulation with color */
106 glBindTexture(GL_TEXTURE_2D, text_name);
107 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (filter) ? GL_LINEAR : GL_NEAREST);
108 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (filter) ? GL_LINEAR : GL_NEAREST);
109 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image_width, image_height, GL_RGB, GL_UNSIGNED_BYTE, rgb);
111 glTexCoord2f(0.0, 0.0);
112 glVertex3f(0.0, 0.0, 0.0);
113 glTexCoord2f(1.0, 0.0);
114 glVertex3f(1.0, 0.0, 0.0);
115 glTexCoord2f(1.0, 1.0);
116 glVertex3f(1.0, 1.0, 0.0);
117 glTexCoord2f(0.0, 1.0);
118 glVertex3f(0.0, 1.0, 0.0);
120 glDisable(GL_TEXTURE_2D);
123 /* free image texture */
124 void exit_opengl(void)