Debian Codegraph @

similar/arch/ogl/ogl.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 /*
8 *
9 * Graphics support functions for OpenGL.
10 *
11 */
12
13 #include "dxxsconf.h"
14 #include <bit>
15 #include <stdexcept>
16 #include <tuple>
17 #ifdef _WIN32
18 #include <windows.h>
19 #include <stddef.h>
20 #endif
21 #if defined(__APPLE__) && defined(__MACH__)
22 #include <OpenGL/gl.h>
23 #include <OpenGL/glu.h>
24 #else
25 #if DXX_USE_OGLES
26 #include <GLES/gl.h>
27 #else
28 #include <GL/gl.h>
29 #include <GL/glu.h>
30 #endif
31 #endif
32 #include <string.h>
33 #include <math.h>
34 #include <stdio.h>
35
36 #include "3d.h"
37 #include "piggy.h"
38 #include "common/3d/globvars.h"
39 #include "dxxerror.h"
40 #include "texmap.h"
41 #include "palette.h"
42 #include "rle.h"
43 #include "console.h"
44 #include "config.h"
45 #include "u_mem.h"
46
47 #include "segment.h"
48 #include "textures.h"
49 #include "texmerge.h"
50 #include "effects.h"
51 #include "weapon.h"
52 #include "powerup.h"
53 #include "laser.h"
54 #include "player.h"
55 #include "robot.h"
56 #include "gamefont.h"
57 #include "byteutil.h"
58 #include "internal.h"
59 #include "gauges.h"
60 #include "object.h"
61 #include "args.h"
62
63 #include "compiler-range_for.h"
64 #include "d_levelstate.h"
65 #include "d_zip.h"
66 #include "partial_range.h"
67
68 #include <algorithm>
69 #include <memory>
70 #include <utility>
71 using std::max;
72
73 //change to 1 for lots of spew.
74 #if 0
75 #define glmprintf(A) con_printf A
76 #else
77 #define glmprintf(A)
78 #endif
79
80 #ifndef M_PI
81 #define M_PI 3.14159
82 #endif
83
84 namespace {
85
86 template <unsigned G>
87 struct enable_ogl_client_state
88 {
89 enable_ogl_client_state() noexcept
90 {
91 glEnableClientState(G);
92 }
93 ~enable_ogl_client_state() noexcept
94 {
95 glDisableClientState(G);
96 }
97 };
98
99 template <typename T, unsigned... Gs>
100 using ogl_client_states = std::tuple<T, enable_ogl_client_state<Gs>...>;
101
102 template <typename T, std::size_t N1, std::size_t N2>
103 union flatten_array
104 {
105 std::array<T, N1 * N2> flat;
106 std::array<std::array<T, N1>, N2> nested;
107 static_assert(sizeof(flat) == sizeof(nested), "array padding error");
108 };
109
110 }
111
112 #if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__)) || defined(__sun__) || defined(macintosh)
113 #define cosf(a) cos(a)
114 #define sinf(a) sin(a)
115 #endif
116
117 namespace dcx {
118
119 static std::unique_ptr<GLubyte[]> texbuf;
120
121 unsigned last_width=~0u,last_height=~0u;
122 int GL_TEXTURE_2D_enabled=-1;
123
124 static int r_texcount = 0, r_cachedtexcount = 0;
125 #if DXX_USE_OGLES
126 static int ogl_rgba_internalformat = GL_RGBA;
127 static int ogl_rgb_internalformat = GL_RGB;
128 #else
129 static int ogl_rgba_internalformat = GL_RGBA8;
130 static int ogl_rgb_internalformat = GL_RGB8;
131 #endif
132 static std::unique_ptr<GLfloat[]> sphere_va, circle_va, disk_va;
133 static std::array<std::unique_ptr<GLfloat[]>, 3> secondary_lva;
134 static int r_polyc,r_tpolyc,r_bitmapc,r_ubitbltc;
135 #define f2glf(x) (f2fl(x))
136
137 #define OGL_BINDTEXTURE(a) glBindTexture(GL_TEXTURE_2D, a);
138
139 /* I assume this ought to be >= MAX_BITMAP_FILES in piggy.h? */
140 static std::array<ogl_texture, 20000> ogl_texture_list;
141 static int ogl_texture_list_cur;
142
143 /* some function prototypes */
144
145 #define GL_TEXTURE0_ARB 0x84C0
146 static int ogl_loadtexture(const palette_array_t &, const uint8_t *data, int dxo, int dyo, ogl_texture &tex, int bm_flags, int data_format, opengl_texture_filter texfilt, bool texanis, bool edgepad) dxx_compiler_attribute_nonnull();
147 static void ogl_freetexture(ogl_texture &gltexture);
148
149 static void ogl_loadbmtexture(grs_bitmap &bm, bool edgepad)
150 {
151 ogl_loadbmtexture_f(bm, CGameCfg.TexFilt, CGameCfg.TexAnisotropy, edgepad);
152 }
153
154 }
155
156 #if DXX_USE_OGLES
157 // Replacement for gluPerspective
158 static void perspective(double fovy, double aspect, double zNear, double zFar)
159 {
160 double xmin, xmax, ymin, ymax;
161
162 glMatrixMode(GL_PROJECTION);
163 glLoadIdentity();
164
165 ymax = zNear * tan(fovy * M_PI / 360.0);
166 ymin = -ymax;
167 xmin = ymin * aspect;
168 xmax = ymax * aspect;
169
170 glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
171 glMatrixMode(GL_MODELVIEW);
172 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
173 glDepthMask(GL_TRUE);
174 }
175 #endif
176
177 namespace dcx {
178
179 static void ogl_init_texture_stats(ogl_texture &t)
180 {
181 t.prio=0.3;//default prio
182 t.numrend=0;
183 }
184
185 void ogl_init_texture(ogl_texture &t, int w, int h, int flags)
186 {
187 t.handle = 0;
188 #if !DXX_USE_OGLES
189 if (flags & OGL_FLAG_NOCOLOR)
190 {
191 // use GL_INTENSITY instead of GL_RGB
192 if (flags & OGL_FLAG_ALPHA)
193 {
194 if (CGameArg.DbgGlIntensity4Ok)
195 {
196 t.internalformat = GL_INTENSITY4;
197 t.format = GL_LUMINANCE;
198 }
199 else if (CGameArg.DbgGlLuminance4Alpha4Ok)
200 {
201 t.internalformat = GL_LUMINANCE4_ALPHA4;
202 t.format = GL_LUMINANCE_ALPHA;
203 }
204 else if (CGameArg.DbgGlRGBA2Ok)
205 {
206 t.internalformat = GL_RGBA2;
207 t.format = GL_RGBA;
208 }
209 else
210 {
211 t.internalformat = ogl_rgba_internalformat;
212 t.format = GL_RGBA;
213 }
214 }
215 else
216 {
217 // there are certainly smaller formats we could use here, but nothing needs it ATM.
218 t.internalformat = ogl_rgb_internalformat;
219 t.format = GL_RGB;
220 }
221 }
222 else
223 {
224 #endif
225 if (flags & OGL_FLAG_ALPHA)
226 {
227 t.internalformat = ogl_rgba_internalformat;
228 t.format = GL_RGBA;
229 }
230 else
231 {
232 t.internalformat = ogl_rgb_internalformat;
233 t.format = GL_RGB;
234 }
235 #if !DXX_USE_OGLES
236 }
237 #endif
238 t.wrapstate = -1;
239 t.lw = t.w = w;
240 t.h = h;
241 ogl_init_texture_stats(t);
242 }
243
244 static void ogl_reset_texture(ogl_texture &t)
245 {
246 ogl_init_texture(t, 0, 0, 0);
247 }
248
249 static void ogl_reset_texture_stats_internal(void){
250 range_for (auto &i, ogl_texture_list)
251 if (i.handle>0)
252 ogl_init_texture_stats(i);
253 }
254
255 void ogl_init_texture_list_internal(void){
256 ogl_texture_list_cur=0;
257 range_for (auto &i, ogl_texture_list)
258 ogl_reset_texture(i);
259 }
260
261 void ogl_smash_texture_list_internal(void){
262 sphere_va.reset();
263 circle_va.reset();
264 disk_va.reset();
265 secondary_lva = {};
266 range_for (auto &i, ogl_texture_list)
267 {
268 if (i.handle>0){
269 glDeleteTextures( 1, &i.handle );
270 i.handle=0;
271 }
272 i.wrapstate = -1;
273 }
274 }
275
276 ogl_texture* ogl_get_free_texture(void){
277 for (unsigned i = ogl_texture_list.size(); i--;)
278 {
279 if (ogl_texture_list[ogl_texture_list_cur].handle<=0 && ogl_texture_list[ogl_texture_list_cur].w==0)
280 return &ogl_texture_list[ogl_texture_list_cur];
281 if (++ogl_texture_list_cur >= ogl_texture_list.size())
282 ogl_texture_list_cur=0;
283 }
284 throw std::runtime_error("OpenGL: texture list full");
285 }
286
287 static void ogl_texture_stats(grs_canvas &canvas)
288 {
289 int used{0}, usedother = 0, usedidx = 0, usedrgb = 0, usedrgba = 0;
290 int databytes{0}, truebytes = 0;
291 GLint idx, r, g, b, a, dbl, depth;
292 int res, colorsize, depthsize;
293 range_for (auto &i, ogl_texture_list)
294 {
295 if (i.handle>0){
296 used++;
297 databytes+=i.bytesu;
298 truebytes+=i.bytes;
299 if (i.format == GL_RGBA)
300 usedrgba++;
301 else if (i.format == GL_RGB)
302 usedrgb++;
303 #if !DXX_USE_OGLES
304 else if (i.format == GL_COLOR_INDEX)
305 usedidx++;
306 #endif
307 else
308 usedother++;
309 }
310 }
311
312 res = SWIDTH * SHEIGHT;
313 #if !DXX_USE_OGLES
314 glGetIntegerv(GL_INDEX_BITS, &idx);
315 #else
316 idx=16;
317 #endif
318 glGetIntegerv(GL_RED_BITS, &r);
319 glGetIntegerv(GL_GREEN_BITS, &g);
320 glGetIntegerv(GL_BLUE_BITS, &b);
321 glGetIntegerv(GL_ALPHA_BITS, &a);
322 #if !DXX_USE_OGLES
323 glGetIntegerv(GL_DOUBLEBUFFER, &dbl);
324 #else
325 dbl=1;
326 #endif
327 dbl += 1;
328 glGetIntegerv(GL_DEPTH_BITS, &depth);
329 const auto &game_font = *GAME_FONT;
330 gr_set_fontcolor(canvas, BM_XRGB(255, 255, 255), -1);
331 colorsize = (idx * res * dbl) / 8;
332 depthsize = res * depth / 8;
333 const auto &&fspacx2 = FSPACX(2);
334 const auto &&fspacy1 = FSPACY(1);
335 const auto &&line_spacing = LINE_SPACING(game_font, game_font);
336 gr_printf(canvas, game_font, fspacx2, fspacy1, "%i flat %i tex %i bitmaps", r_polyc, r_tpolyc, r_bitmapc);
337 gr_printf(canvas, game_font, fspacx2, fspacy1 + line_spacing, "%i(%i,%i,%i,%i) %iK(%iK wasted) (%i postcachedtex)", used, usedrgba, usedrgb, usedidx, usedother, truebytes / 1024, (truebytes - databytes) / 1024, r_texcount - r_cachedtexcount);
338 gr_printf(canvas, game_font, fspacx2, fspacy1 + (line_spacing * 2), "%ibpp(r%i,g%i,b%i,a%i)x%i=%iK depth%i=%iK", idx, r, g, b, a, dbl, colorsize / 1024, depth, depthsize / 1024);
339 gr_printf(canvas, game_font, fspacx2, fspacy1 + (line_spacing * 3), "total=%iK", (colorsize + depthsize + truebytes) / 1024);
340 }
341
342 }
343
344 static void ogl_bindbmtex(grs_bitmap &bm, bool edgepad){
345 if (bm.gltexture==NULL || bm.gltexture->handle<=0)
346 ogl_loadbmtexture(bm, edgepad);
347 OGL_BINDTEXTURE(bm.gltexture->handle);
348 bm.gltexture->numrend++;
349 }
350
351 //gltexture MUST be bound first
352 static void ogl_texwrap(ogl_texture *const gltexture, const int state)
353 {
354 if (gltexture->wrapstate != state || gltexture->numrend < 1)
355 {
356 gltexture->wrapstate = state;
357 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, state);
358 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, state);
359 }
360 }
361
362 //crude texture precaching
363 //handles: powerups, walls, weapons, polymodels, etc.
364 //it is done with the horrid do_special_effects kludge so that sides that have to be texmerged and have animated textures will be correctly cached.
365 //similarly, with the objects(esp weapons), we could just go through and cache em all instead, but that would get ones that might not even be on the level
366 //TODO: doors
367
368 namespace dsx {
369
370 void ogl_cache_polymodel_textures(const polygon_model_index model_num)
371 {
372 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
373 if (model_num == polygon_model_index::None)
374 return;
375 const auto &po = Polygon_models[model_num];
376 unsigned i = po.first_texture;
377 const unsigned last_texture = i + po.n_textures;
378 for (; i != last_texture; ++i)
379 {
380 const auto objbitmap = ObjBitmaps[ObjBitmapPtrs[i]];
381 PIGGY_PAGE_IN(objbitmap);
382 ogl_loadbmtexture(GameBitmaps[objbitmap], 1);
383 }
384 }
385
386 }
387
388 static void ogl_cache_vclip_textures(const vclip &vc)
389 {
390 for (const auto i : partial_const_range(vc.frames, vc.num_frames))
391 {
392 PIGGY_PAGE_IN(i);
393 ogl_loadbmtexture(GameBitmaps[i], 0);
394 }
395 }
396
397 static void ogl_cache_vclipn_textures(const d_vclip_array &Vclip, const vclip_index i)
398 {
399 if (Vclip.valid_index(i))
400 ogl_cache_vclip_textures(Vclip[i]);
401 }
402
403 static void ogl_cache_weapon_textures(const d_vclip_array &Vclip, const weapon_info_array &Weapon_info, const unsigned weapon_type)
404 {
405 if (weapon_type >= Weapon_info.size())
406 return;
407 const auto &w = Weapon_info[weapon_type];
408 ogl_cache_vclipn_textures(Vclip, w.flash_vclip);
409 ogl_cache_vclipn_textures(Vclip, w.robot_hit_vclip);
410 ogl_cache_vclipn_textures(Vclip, w.wall_hit_vclip);
411 if (w.render == WEAPON_RENDER_VCLIP)
412 ogl_cache_vclipn_textures(Vclip, w.weapon_vclip);
413 else if (w.render == WEAPON_RENDER_POLYMODEL)
414 {
415 ogl_cache_polymodel_textures(w.model_num);
416 ogl_cache_polymodel_textures(w.model_num_inner);
417 }
418 }
419
420 namespace dsx {
421
422 void ogl_cache_level_textures(void)
423 {
424 auto &Effects = LevelUniqueEffectsClipState.Effects;
425 auto &Objects = LevelUniqueObjectState.Objects;
426 auto &vcobjptridx = Objects.vcptridx;
427 int max_efx{0},ef;
428
429 ogl_reset_texture_stats_internal();//loading a new lev should reset textures
430
431 range_for (auto &ec, partial_const_range(Effects, Num_effects))
432 {
433 ogl_cache_vclipn_textures(Vclip, ec.dest_vclip);
434 if (ec.changing_wall_texture == -1 && ec.changing_object_texture == object_bitmap_index::None)
435 continue;
436 if (ec.vc.num_frames>max_efx)
437 max_efx=ec.vc.num_frames;
438 }
439 glmprintf((CON_DEBUG, "max_efx:%i", max_efx));
440 for (ef=0;ef<max_efx;ef++){
441 range_for (eclip &ec, partial_range(Effects, Num_effects))
442 {
443 if (ec.changing_wall_texture == -1 && ec.changing_object_texture == object_bitmap_index::None)
444 continue;
445 ec.time_left=-1;
446 }
447 do_special_effects();
448
449 range_for (const unique_segment &seg, vcsegptr)
450 {
451 range_for (auto &side, seg.sides)
452 {
453 const auto tmap1 = side.tmap_num;
454 const auto tmap2 = side.tmap_num2;
455 const auto tmap1idx = get_texture_index(tmap1);
456 if (tmap1idx >= NumTextures){
457 glmprintf((CON_DEBUG, "ogl_cache_level_textures %p %p %i %i", seg.get_unchecked_pointer(), &side, tmap1, NumTextures));
458 // tmap1=0;
459 continue;
460 }
461 const auto texture1 = Textures[tmap1idx];
462 PIGGY_PAGE_IN(texture1);
463 grs_bitmap *bm = &GameBitmaps[texture1];
464 if (tmap2 != texture2_value::None)
465 {
466 const auto texture2 = Textures[get_texture_index(tmap2)];
467 PIGGY_PAGE_IN(texture2);
468 auto &bm2 = GameBitmaps[texture2];
469 if (CGameArg.DbgUseOldTextureMerge || bm2.get_flag_mask(BM_FLAG_SUPER_TRANSPARENT))
470 bm = &texmerge_get_cached_bitmap( tmap1, tmap2 );
471 else {
472 ogl_loadbmtexture(bm2, 1);
473 }
474 }
475 ogl_loadbmtexture(*bm, 0);
476 }
477 }
478 glmprintf((CON_DEBUG, "finished ef:%i", ef));
479 }
480 reset_special_effects();
481 init_special_effects();
482 {
483 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
484 // always have lasers, concs, flares. Always shows player appearance, and at least concs are always available to disappear.
485 ogl_cache_weapon_textures(Vclip, Weapon_info, Primary_weapon_to_weapon_info[primary_weapon_index_t::LASER_INDEX]);
486 ogl_cache_weapon_textures(Vclip, Weapon_info, Secondary_weapon_to_weapon_info[secondary_weapon_index_t::CONCUSSION_INDEX]);
487 ogl_cache_weapon_textures(Vclip, Weapon_info, weapon_id_type::FLARE_ID);
488 ogl_cache_vclipn_textures(Vclip, vclip_index::player_appearance);
489 ogl_cache_vclipn_textures(Vclip, vclip_index::powerup_disappearance);
490 ogl_cache_polymodel_textures(Player_ship->model_num);
491 ogl_cache_vclipn_textures(Vclip, Player_ship->expl_vclip_num);
492
493 range_for (const auto &&objp, vcobjptridx)
494 {
495 if (objp->type == OBJ_POWERUP && objp->render_type == render_type::RT_POWERUP)
496 {
497 ogl_cache_vclipn_textures(Vclip, objp->rtype.vclip_info.vclip_num);
498 const auto id = get_powerup_id(objp);
499 primary_weapon_index_t p;
500 secondary_weapon_index_t s;
501 int w;
502 if (
503 (
504 (
505 (id == powerup_type_t::POW_VULCAN_WEAPON && (p = primary_weapon_index_t::VULCAN_INDEX, true)) ||
506 (id == powerup_type_t::POW_SPREADFIRE_WEAPON && (p = primary_weapon_index_t::SPREADFIRE_INDEX, true)) ||
507 (id == powerup_type_t::POW_PLASMA_WEAPON && (p = primary_weapon_index_t::PLASMA_INDEX, true)) ||
508 (id == powerup_type_t::POW_FUSION_WEAPON && (p = primary_weapon_index_t::FUSION_INDEX, true))
509 ) && (w = Primary_weapon_to_weapon_info[p], true)
510 ) ||
511 (
512 (
513 (id == powerup_type_t::POW_PROXIMITY_WEAPON && (s = secondary_weapon_index_t::PROXIMITY_INDEX, true)) ||
514 ((id == powerup_type_t::POW_HOMING_AMMO_1 || id == powerup_type_t::POW_HOMING_AMMO_4) && (s = secondary_weapon_index_t::HOMING_INDEX, true)) ||
515 (id == powerup_type_t::POW_SMARTBOMB_WEAPON && (s = secondary_weapon_index_t::SMART_INDEX, true)) ||
516 (id == powerup_type_t::POW_MEGA_WEAPON && (s = secondary_weapon_index_t::MEGA_INDEX, true))
517 ) && (w = Secondary_weapon_to_weapon_info[s], true)
518 )
519 )
520 {
521 ogl_cache_weapon_textures(Vclip, Weapon_info, w);
522 }
523 }
524 else if (objp->type != OBJ_NONE && objp->render_type == render_type::RT_POLYOBJ)
525 {
526 if (objp->type == OBJ_ROBOT)
527 {
528 auto &ri = Robot_info[get_robot_id(objp)];
529 ogl_cache_vclipn_textures(Vclip, ri.exp1_vclip_num);
530 ogl_cache_vclipn_textures(Vclip, ri.exp2_vclip_num);
531 ogl_cache_weapon_textures(Vclip, Weapon_info, ri.weapon_type);
532 }
533 if (objp->rtype.pobj_info.tmap_override != -1)
534 {
535 const auto t = Textures[objp->rtype.pobj_info.tmap_override];
536 PIGGY_PAGE_IN(t);
537 ogl_loadbmtexture(GameBitmaps[t], 1);
538 }
539 else
540 ogl_cache_polymodel_textures(objp->rtype.pobj_info.model_num);
541 }
542 }
543 }
544 glmprintf((CON_DEBUG, "finished caching"));
545 r_cachedtexcount = r_texcount;
546 }
547
548 }
549
550 namespace dcx {
551
552 void g3_draw_line(const g3_draw_line_context &context, const g3_draw_line_point &p0, const g3_draw_line_point &p1)
553 {
554 ogl_client_states<int, GL_VERTEX_ARRAY, GL_COLOR_ARRAY> cs;
555 OGL_DISABLE(TEXTURE_2D);
556 glDisable(GL_CULL_FACE);
557 std::array<GLfloat, 6> vertices = {{
558 f2glf(p0.p3_vec.x), f2glf(p0.p3_vec.y), -f2glf(p0.p3_vec.z),
559 f2glf(p1.p3_vec.x), f2glf(p1.p3_vec.y), -f2glf(p1.p3_vec.z)
560 }};
561 glVertexPointer(3, GL_FLOAT, 0, vertices.data());
562 glColorPointer(4, GL_FLOAT, 0, context.color_array.data());
563 glDrawArrays(GL_LINES, 0, 2);
564 }
565
566 namespace {
567
568 static void ogl_drawcircle(const unsigned nsides, const unsigned type, GLfloat *const vertices)
569 {
570 glEnableClientState(GL_VERTEX_ARRAY);
571 glVertexPointer(2, GL_FLOAT, 0, vertices);
572 glDrawArrays(type, 0, nsides);
573 glDisableClientState(GL_VERTEX_ARRAY);
574 }
575
576 static std::unique_ptr<GLfloat[]> circle_array_init(const unsigned nsides)
577 {
578 auto vertices = std::make_unique_for_overwrite<GLfloat[]>(nsides * 2);
579 for (unsigned i = 0; i < nsides; i++)
580 {
581 const float ang = 2.0 * M_PI * i / nsides;
582 vertices[i * 2] = cosf(ang);
583 vertices[i * 2 + 1] = sinf(ang);
584 }
585 return vertices;
586 }
587
588 static std::unique_ptr<GLfloat[]> circle_array_init_2(const unsigned nsides, const float xsc, const float xo, const float ysc, const float yo)
589 {
590 auto vertices = std::make_unique_for_overwrite<GLfloat[]>(nsides * 2);
591 for (unsigned i = 0; i < nsides; i++)
592 {
593 const float ang = 2.0 * M_PI * i / nsides;
594 vertices[i * 2] = cosf(ang) * xsc + xo;
595 vertices[i * 2 + 1] = sinf(ang) * ysc + yo;
596 }
597 return vertices;
598 }
599
600 }
601
602 void ogl_draw_vertex_reticle(grs_canvas &canvas, int cross, int primary, int secondary, int color, int alpha, int size_offs)
603 {
604 int size=270+(size_offs*20);
605 float scale = (static_cast<float>(SWIDTH)/SHEIGHT);
606 auto &&rgb{PAL2T(color)};
607 const std::array<float, 4> ret_rgba{{
608 rgb.r / 63.0f,
609 rgb.g / 63.0f,
610 rgb.b / 63.0f,
611 float{1.0f - (static_cast<float>(alpha) / (static_cast<float>(GR_FADE_LEVELS)))}
612 }}, ret_dark_rgba{{
613 ret_rgba[0] / 2,
614 ret_rgba[1] / 2,
615 ret_rgba[2] / 2,
616 ret_rgba[3] / 2
617 }};
618 std::array<GLfloat, 16 * 4> dark_lca, bright_lca;
619 for (uint_fast32_t i = 0; i != dark_lca.size(); i += 4)
620 {
621 bright_lca[i] = ret_rgba[0];
622 dark_lca[i] = ret_dark_rgba[0];
623 bright_lca[i+1] = ret_rgba[1];
624 dark_lca[i+1] = ret_dark_rgba[1];
625 bright_lca[i+2] = ret_rgba[2];
626 dark_lca[i+2] = ret_dark_rgba[2];
627 bright_lca[i+3] = ret_rgba[3];
628 dark_lca[i+3] = ret_dark_rgba[3];
629 }
630
631 glPushMatrix();
632 glTranslatef((canvas.cv_bitmap.bm_w / 2 + canvas.cv_bitmap.bm_x) / static_cast<float>(last_width), 1.0 - (canvas.cv_bitmap.bm_h / 2 + canvas.cv_bitmap.bm_y) / static_cast<float>(last_height), 0);
633
634 {
635 float gl1, gl2, gl3;
636 if (scale >= 1)
637 {
638 size/=scale;
639 gl2 = f2glf(size*scale);
640 gl1 = f2glf(size);
641 gl3 = gl1;
642 }
643 else
644 {
645 size*=scale;
646 gl1 = f2glf(size/scale);
647 gl2 = f2glf(size);
648 gl3 = gl2;
649 }
650 glScalef(gl1, gl2, gl3);
651 }
652
653 glLineWidth(linedotscale*2);
654 OGL_DISABLE(TEXTURE_2D);
655 glDisable(GL_CULL_FACE);
656 glEnableClientState(GL_VERTEX_ARRAY);
657 glEnableClientState(GL_COLOR_ARRAY);
658
659 //cross
660 std::array<GLfloat, 8 * 4> cross_lca;
661 GLfloat *cross_lca_ptr;
662 if (cross)
663 {
664 for (uint_fast32_t i = 0; i != cross_lca.size(); i += 8)
665 {
666 cross_lca[i] = ret_dark_rgba[0];
667 cross_lca[i+1] = ret_dark_rgba[1];
668 cross_lca[i+2] = ret_dark_rgba[2];
669 cross_lca[i+3] = ret_dark_rgba[3];
670 cross_lca[i+4] = ret_rgba[0];
671 cross_lca[i+5] = ret_rgba[1];
672 cross_lca[i+6] = ret_rgba[2];
673 cross_lca[i+7] = ret_rgba[3];
674 }
675 cross_lca_ptr = cross_lca.data();
676 }
677 else
678 {
679 cross_lca_ptr = dark_lca.data();
680 }
681 glColorPointer(4, GL_FLOAT, 0, cross_lca_ptr);
682
683 static const std::array<GLfloat, 8 * 2> cross_lva{{
684 -4.0, 2.0, -2.0, 0, -3.0, -4.0, -2.0, -3.0, 4.0, 2.0, 2.0, 0, 3.0, -4.0, 2.0, -3.0,
685 }};
686 glVertexPointer(2, GL_FLOAT, 0, cross_lva.data());
687 glDrawArrays(GL_LINES, 0, 8);
688
689 std::array<GLfloat, 4 * 4> primary_lca0;
690 GLfloat *lca0_data;
691 //left primary bar
692 if(primary == 0)
693 lca0_data = dark_lca.data();
694 else
695 {
696 primary_lca0[0] = primary_lca0[4] = ret_rgba[0];
697 primary_lca0[1] = primary_lca0[5] = ret_rgba[1];
698 primary_lca0[2] = primary_lca0[6] = ret_rgba[2];
699 primary_lca0[3] = primary_lca0[7] = ret_rgba[3];
700 primary_lca0[8] = primary_lca0[12] = ret_dark_rgba[0];
701 primary_lca0[9] = primary_lca0[13] = ret_dark_rgba[1];
702 primary_lca0[10] = primary_lca0[14] = ret_dark_rgba[2];
703 primary_lca0[11] = primary_lca0[15] = ret_dark_rgba[3];
704 lca0_data = primary_lca0.data();
705 }
706 glColorPointer(4, GL_FLOAT, 0, lca0_data);
707 static const std::array<GLfloat, 4 * 2> primary_lva0{{
708 -5.5, -5.0, -6.5, -7.5, -10.0, -7.0, -10.0, -8.7
709 }};
710 static const std::array<GLfloat, 4 * 2> primary_lva1{{
711 -10.0, -7.0, -10.0, -8.7, -15.0, -8.5, -15.0, -9.5
712 }};
713 static const std::array<GLfloat, 4 * 2> primary_lva2{{
714 5.5, -5.0, 6.5, -7.5, 10.0, -7.0, 10.0, -8.7
715 }};
716 static const std::array<GLfloat, 4 * 2> primary_lva3{{
717 10.0, -7.0, 10.0, -8.7, 15.0, -8.5, 15.0, -9.5
718 }};
719 glVertexPointer(2, GL_FLOAT, 0, primary_lva0.data());
720 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
721
722 std::array<GLfloat, 4 * 4> primary_lca1;
723 GLfloat *lca1_data;
724 if(primary != 2)
725 lca1_data = dark_lca.data();
726 else
727 {
728 primary_lca1[8] = primary_lca1[12] = ret_rgba[0];
729 primary_lca1[9] = primary_lca1[13] = ret_rgba[1];
730 primary_lca1[10] = primary_lca1[14] = ret_rgba[2];
731 primary_lca1[11] = primary_lca1[15] = ret_rgba[3];
732 primary_lca1[0] = primary_lca1[4] = ret_dark_rgba[0];
733 primary_lca1[1] = primary_lca1[5] = ret_dark_rgba[1];
734 primary_lca1[2] = primary_lca1[6] = ret_dark_rgba[2];
735 primary_lca1[3] = primary_lca1[7] = ret_dark_rgba[3];
736 lca1_data = primary_lca1.data();
737 }
738 glColorPointer(4, GL_FLOAT, 0, lca1_data);
739 glVertexPointer(2, GL_FLOAT, 0, primary_lva1.data());
740 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
741 //right primary bar
742 glColorPointer(4, GL_FLOAT, 0, lca0_data);
743 glVertexPointer(2, GL_FLOAT, 0, primary_lva2.data());
744 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
745 glColorPointer(4, GL_FLOAT, 0, lca1_data);
746 glVertexPointer(2, GL_FLOAT, 0, primary_lva3.data());
747 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
748
749 GLfloat *secondary_lva_ptr;
750 if (secondary<=2){
751 //left secondary
752 glColorPointer(4, GL_FLOAT, 0, (secondary != 1 ? dark_lca : bright_lca).data());
753 if(!secondary_lva[0])
754 secondary_lva[0] = circle_array_init_2(16, 2.0, -10.0, 2.0, -2.0);
755 ogl_drawcircle(16, GL_LINE_LOOP, secondary_lva[0].get());
756 //right secondary
757 glColorPointer(4, GL_FLOAT, 0, (secondary != 2 ? dark_lca : bright_lca).data());
758 if(!secondary_lva[1])
759 secondary_lva[1] = circle_array_init_2(16, 2.0, 10.0, 2.0, -2.0);
760 secondary_lva_ptr = secondary_lva[1].get();
761 }
762 else {
763 //bottom/middle secondary
764 glColorPointer(4, GL_FLOAT, 0, (secondary != 4 ? dark_lca : bright_lca).data());
765 if(!secondary_lva[2])
766 secondary_lva[2] = circle_array_init_2(16, 2.0, 0.0, 2.0, -8.0);
767 secondary_lva_ptr = secondary_lva[2].get();
768 }
769 ogl_drawcircle(16, GL_LINE_LOOP, secondary_lva_ptr);
770
771 //glDisableClientState(GL_VERTEX_ARRAY);
772 glDisableClientState(GL_COLOR_ARRAY);
773 glPopMatrix();
774 glLineWidth(linedotscale);
775 }
776
777 /*
778 * Stars on heaven in exit sequence, automap objects
779 */
780 void g3_draw_sphere(grs_canvas &canvas, const g3_draw_sphere_point &pnt, fix rad, const uint8_t c)
781 {
782 int i;
783 const float scale = (static_cast<float>(canvas.cv_bitmap.bm_w) / canvas.cv_bitmap.bm_h);
784 std::array<GLfloat, 20 * 4> color_array;
785 const auto color_r{CPAL2Tr(c)};
786 const auto color_g{CPAL2Tg(c)};
787 const auto color_b{CPAL2Tb(c)};
788 for (i = 0; i < 20*4; i += 4)
789 {
790 color_array[i] = color_r;
791 color_array[i+1] = color_g;
792 color_array[i+2] = color_b;
793 color_array[i+3] = 1.0;
794 }
795 OGL_DISABLE(TEXTURE_2D);
796 glDisable(GL_CULL_FACE);
797 glPushMatrix();
798 glTranslatef(f2glf(pnt.p3_vec.x),f2glf(pnt.p3_vec.y),-f2glf(pnt.p3_vec.z));
799 GLfloat gl1, gl2;
800 if (scale >= 1)
801 {
802 rad/=scale;
803 gl1 = f2glf(rad);
804 gl2 = f2glf(rad*scale);
805 }
806 else
807 {
808 rad*=scale;
809 gl1 = f2glf(rad/scale);
810 gl2 = f2glf(rad);
811 }
812 glScalef(gl1, gl2, f2glf(rad));
813 if(!sphere_va)
814 sphere_va = circle_array_init(20);
815 glEnableClientState(GL_COLOR_ARRAY);
816 glColorPointer(4, GL_FLOAT, 0, color_array.data());
817 ogl_drawcircle(20, GL_TRIANGLE_FAN, sphere_va.get());
818 glDisableClientState(GL_COLOR_ARRAY);
819 glPopMatrix();
820 }
821
822 int gr_ucircle(grs_canvas &canvas, const fix xc1, const fix yc1, const fix r1, const uint8_t c)
823 {
824 int nsides;
825 OGL_DISABLE(TEXTURE_2D);
826 glColor4f(CPAL2Tr(c), CPAL2Tg(c), CPAL2Tb(c), (canvas.cv_fade_level >= GR_FADE_OFF)?1.0:1.0 - static_cast<float>(canvas.cv_fade_level) / (static_cast<float>(GR_FADE_LEVELS) - 1.0));
827 glPushMatrix();
828 glTranslatef(
829 (f2fl(xc1) + canvas.cv_bitmap.bm_x + 0.5) / static_cast<float>(last_width),
830 1.0 - (f2fl(yc1) + canvas.cv_bitmap.bm_y + 0.5) / static_cast<float>(last_height),0);
831 glScalef(f2fl(r1) / last_width, f2fl(r1) / last_height, 1.0);
832 nsides = 10 + 2 * static_cast<int>(M_PI * f2fl(r1) / 19);
833 if(!circle_va)
834 circle_va = circle_array_init(nsides);
835 ogl_drawcircle(nsides, GL_LINE_LOOP, circle_va.get());
836 glPopMatrix();
837 return 0;
838 }
839
840 int gr_disk(grs_canvas &canvas, const fix x, const fix y, const fix r, const uint8_t c)
841 {
842 int nsides;
843 OGL_DISABLE(TEXTURE_2D);
844 glColor4f(CPAL2Tr(c), CPAL2Tg(c), CPAL2Tb(c), (canvas.cv_fade_level >= GR_FADE_OFF) ? 1.0 : 1.0 - static_cast<float>(canvas.cv_fade_level) / (static_cast<float>(GR_FADE_LEVELS) - 1.0));
845 glPushMatrix();
846 glTranslatef(
847 (f2fl(x) + canvas.cv_bitmap.bm_x + 0.5) / static_cast<float>(last_width),
848 1.0 - (f2fl(y) + canvas.cv_bitmap.bm_y + 0.5) / static_cast<float>(last_height),0);
849 glScalef(f2fl(r) / last_width, f2fl(r) / last_height, 1.0);
850 nsides = 10 + 2 * static_cast<int>(M_PI * f2fl(r) / 19);
851 if(!disk_va)
852 disk_va = circle_array_init(nsides);
853 ogl_drawcircle(nsides, GL_TRIANGLE_FAN, disk_va.get());
854 glPopMatrix();
855 return 0;
856 }
857
858 /*
859 * Draw flat-shaded Polygon (Lasers, Drone-arms, Driller-ears)
860 */
861 void _g3_draw_poly(grs_canvas &canvas, const std::span<g3_draw_tmap_point *const> pointlist, const uint8_t palette_color_index)
862 {
863 if (pointlist.size() > MAX_POINTS_PER_POLY)
864 return;
865 flatten_array<GLfloat, 4, MAX_POINTS_PER_POLY> color_array;
866 flatten_array<GLfloat, 3, MAX_POINTS_PER_POLY> vertices;
867
868 r_polyc++;
869 ogl_client_states<int, GL_VERTEX_ARRAY, GL_COLOR_ARRAY> cs;
870 OGL_DISABLE(TEXTURE_2D);
871 auto &&rgb{PAL2T(palette_color_index)};
872 const auto color_r{rgb.r / 63.0f};
873 const auto color_g{rgb.g / 63.0f};
874 const auto color_b{rgb.b / 63.0f};
875
876 const float color_a = (canvas.cv_fade_level >= GR_FADE_OFF)
877 ? 1.0
878 : 1.0 - static_cast<float>(canvas.cv_fade_level) / (static_cast<float>(GR_FADE_LEVELS) - 1.0);
879
880 for (auto &&[p, v, c] : zip(
881 pointlist,
882 unchecked_partial_range(vertices.nested, pointlist.size()),
883 unchecked_partial_range(color_array.nested, pointlist.size())
884 )
885 )
886 {
887 c[0] = color_r;
888 c[1] = color_g;
889 c[2] = color_b;
890 c[3] = color_a;
891 auto &pv = p->p3_vec;
892 v[0] = f2glf(pv.x);
893 v[1] = f2glf(pv.y);
894 v[2] = -f2glf(pv.z);
895 }
896
897 glVertexPointer(3, GL_FLOAT, 0, vertices.flat.data());
898 glColorPointer(4, GL_FLOAT, 0, color_array.flat.data());
899 glDrawArrays(GL_TRIANGLE_FAN, 0, pointlist.size());
900 }
901
902 /*
903 * Everything texturemapped (walls, robots, ship)
904 */
905 void _g3_draw_tmap(grs_canvas &canvas, const std::span<g3_draw_tmap_point *const> pointlist, const g3s_uvl *const uvl_list, const g3s_lrgb *const light_rgb, grs_bitmap &bm, const tmap_drawer_type tmap_drawer_ptr)
906 {
907 GLfloat color_alpha = 1.0;
908
909 ogl_client_states<int, GL_VERTEX_ARRAY, GL_COLOR_ARRAY> cs;
910 if (tmap_drawer_ptr == draw_tmap) {
911 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
912 OGL_ENABLE(TEXTURE_2D);
913 ogl_bindbmtex(bm, 0);
914 ogl_texwrap(bm.gltexture, GL_REPEAT);
915 r_tpolyc++;
916 color_alpha = (canvas.cv_fade_level >= GR_FADE_OFF) ? 1.0 : (1.0 - static_cast<float>(canvas.cv_fade_level) / (static_cast<float>(GR_FADE_LEVELS) - 1.0));
917 } else if (tmap_drawer_ptr == draw_tmap_flat) {
918 OGL_DISABLE(TEXTURE_2D);
919 /* for cloaked state faces */
920 color_alpha = 1.0 - (static_cast<GLfloat>(canvas.cv_fade_level) / static_cast<GLfloat>(NUM_LIGHTING_LEVELS));
921 } else {
922 glmprintf((CON_DEBUG, "g3_draw_tmap: unhandled tmap_drawer"));
923 return;
924 }
925
926 flatten_array<GLfloat, 3, MAX_POINTS_PER_POLY> vertices;
927 flatten_array<GLfloat, 4, MAX_POINTS_PER_POLY> color_array;
928 flatten_array<GLfloat, 2, MAX_POINTS_PER_POLY> texcoord_array;
929
930 const auto nv = pointlist.size();
931 for (auto &&[point, light, uvl, vert, color, texcoord] : zip(
932 pointlist,
933 unchecked_partial_range(light_rgb, nv),
934 unchecked_partial_range(uvl_list, nv),
935 unchecked_partial_range(vertices.nested, nv),
936 unchecked_partial_range(color_array.nested, nv),
937 partial_range(texcoord_array.nested, nv)
938 )
939 )
940 {
941 vert[0] = f2glf(point->p3_vec.x);
942 vert[1] = f2glf(point->p3_vec.y);
943 vert[2] = -f2glf(point->p3_vec.z);
944 color[3] = color_alpha;
945 if (tmap_drawer_ptr == draw_tmap_flat) {
946 color[0] = color[1] = color[2] = 0;
947 }
948 else
949 {
950 if (bm.get_flag_mask(BM_FLAG_NO_LIGHTING))
951 {
952 color[0] = color[1] = color[2] = 1.0;
953 }
954 else
955 {
956 color[0] = f2glf(light.r);
957 color[1] = f2glf(light.g);
958 color[2] = f2glf(light.b);
959 }
960 texcoord[0] = f2glf(uvl.u);
961 texcoord[1] = f2glf(uvl.v);
962 }
963 }
964
965 glVertexPointer(3, GL_FLOAT, 0, vertices.flat.data());
966 glColorPointer(4, GL_FLOAT, 0, color_array.flat.data());
967 if (tmap_drawer_ptr == draw_tmap) {
968 glTexCoordPointer(2, GL_FLOAT, 0, texcoord_array.flat.data());
969 }
970
971 glDrawArrays(GL_TRIANGLE_FAN, 0, nv);
972
973 glDisableClientState(GL_VERTEX_ARRAY);
974 glDisableClientState(GL_COLOR_ARRAY);
975 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
976 }
977
978 /*
979 * Everything texturemapped with secondary texture (walls with secondary texture)
980 */
981 void _g3_draw_tmap_2(grs_canvas &canvas, const std::span<g3_draw_tmap_point *const> pointlist, const std::span<const g3s_uvl, 4> uvl_list, const std::span<const g3s_lrgb, 4> light_rgb, grs_bitmap &bmbot, grs_bitmap &bm, const texture2_rotation_low orient, const tmap_drawer_type tmap_drawer_ptr)
982 {
983 _g3_draw_tmap(canvas, pointlist, uvl_list.data(), light_rgb.data(), bmbot, tmap_drawer_ptr);//draw the bottom texture first.. could be optimized with multitexturing..
984 ogl_client_states<int, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY> cs;
985 (void)cs;
986 r_tpolyc++;
987 OGL_ENABLE(TEXTURE_2D);
988 ogl_bindbmtex(bm, 1);
989 ogl_texwrap(bm.gltexture, GL_REPEAT);
990
991 flatten_array<GLfloat, 4, MAX_POINTS_PER_POLY> color_array;
992 {
993 const GLfloat alpha = (canvas.cv_fade_level >= GR_FADE_OFF)
994 ? 1.0
995 : (1.0 - static_cast<float>(canvas.cv_fade_level) / (static_cast<float>(GR_FADE_LEVELS) - 1.0));
996 auto &&color_range = unchecked_partial_range(color_array.nested, pointlist.size());
997 if (bm.get_flag_mask(BM_FLAG_NO_LIGHTING))
998 {
999 for (auto &e : color_range)
1000 {
1001 e[0] = e[1] = e[2] = 1.0;
1002 e[3] = alpha;
1003 }
1004 }
1005 else
1006 {
1007 for (auto &&[e, l] : zip(
1008 color_range,
1009 unchecked_partial_range(light_rgb, pointlist.size())
1010 )
1011 )
1012 {
1013 e[0] = f2glf(l.r);
1014 e[1] = f2glf(l.g);
1015 e[2] = f2glf(l.b);
1016 e[3] = alpha;
1017 }
1018 }
1019 }
1020
1021 flatten_array<GLfloat, 3, MAX_POINTS_PER_POLY> vertices;
1022 flatten_array<GLfloat, 2, MAX_POINTS_PER_POLY> texcoord_array;
1023
1024 const auto nv = pointlist.size();
1025 for (auto &&[point, uvl, vert, texcoord] : zip(
1026 pointlist,
1027 unchecked_partial_range(uvl_list, nv),
1028 unchecked_partial_range(vertices.nested, nv),
1029 partial_range(texcoord_array.nested, nv)
1030 )
1031 )
1032 {
1033 const GLfloat uf = f2glf(uvl.u), vf = f2glf(uvl.v);
1034 switch(orient){
1035 case texture2_rotation_low::_1:
1036 texcoord[0] = 1.0 - vf;
1037 texcoord[1] = uf;
1038 break;
1039 case texture2_rotation_low::_2:
1040 texcoord[0] = 1.0 - uf;
1041 texcoord[1] = 1.0 - vf;
1042 break;
1043 case texture2_rotation_low::_3:
1044 texcoord[0] = vf;
1045 texcoord[1] = 1.0 - uf;
1046 break;
1047 default:
1048 texcoord[0] = uf;
1049 texcoord[1] = vf;
1050 break;
1051 }
1052 vert[0] = f2glf(point->p3_vec.x);
1053 vert[1] = f2glf(point->p3_vec.y);
1054 vert[2] = -f2glf(point->p3_vec.z);
1055 }
1056 glVertexPointer(3, GL_FLOAT, 0, vertices.flat.data());
1057 glColorPointer(4, GL_FLOAT, 0, color_array.flat.data());
1058 glTexCoordPointer(2, GL_FLOAT, 0, texcoord_array.flat.data());
1059 glDrawArrays(GL_TRIANGLE_FAN, 0, nv);
1060 }
1061
1062 /*
1063 * 2d Sprites (Fireaballs, powerups, explosions). NOT hostages
1064 */
1065 void g3_draw_bitmap(grs_canvas &canvas, const vms_vector &pos, const fix iwidth, const fix iheight, grs_bitmap &bm)
1066 {
1067 r_bitmapc++;
1068
1069 ogl_client_states<int, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY> cs;
1070 auto &i = std::get<0>(cs);
1071
1072 OGL_ENABLE(TEXTURE_2D);
1073 ogl_bindbmtex(bm, 0);
1074 ogl_texwrap(bm.gltexture,GL_CLAMP_TO_EDGE);
1075
1076 const auto width = fixmul(iwidth, Matrix_scale.x);
1077 const auto height = fixmul(iheight, Matrix_scale.y);
1078 constexpr unsigned point_count = 4;
1079 struct fvertex_t
1080 {
1081 GLfloat x, y, z;
1082 };
1083 struct fcolor_t
1084 {
1085 GLfloat r, g, b, a;
1086 };
1087 struct ftexcoord_t
1088 {
1089 GLfloat u, v;
1090 };
1091 std::array<fvertex_t, point_count> vertices;
1092 std::array<fcolor_t, point_count> color_array;
1093 std::array<ftexcoord_t, point_count> texcoord_array;
1094 const auto &&rpv{vm_vec_build_rotated(vm_vec_build_sub(pos, View_position), View_matrix)};
1095 const auto bmglu = bm.gltexture->u;
1096 const auto bmglv = bm.gltexture->v;
1097 const auto alpha = canvas.cv_fade_level >= GR_FADE_OFF ? 1.0 : (1.0 - static_cast<float>(canvas.cv_fade_level) / (static_cast<float>(GR_FADE_LEVELS) - 1.0));
1098 const auto vert_z = -f2glf(rpv.z);
1099 for (i=0;i<4;i++){
1100 auto pv = rpv;
1101 switch (i){
1102 case 0:
1103 texcoord_array[i].u = 0.0;
1104 texcoord_array[i].v = 0.0;
1105 pv.x+=-width;
1106 pv.y+=height;
1107 break;
1108 case 1:
1109 texcoord_array[i].u = bmglu;
1110 texcoord_array[i].v = 0.0;
1111 pv.x+=width;
1112 pv.y+=height;
1113 break;
1114 case 2:
1115 texcoord_array[i].u = bmglu;
1116 texcoord_array[i].v = bmglv;
1117 pv.x+=width;
1118 pv.y+=-height;
1119 break;
1120 case 3:
1121 texcoord_array[i].u = 0.0;
1122 texcoord_array[i].v = bmglv;
1123 pv.x+=-width;
1124 pv.y+=-height;
1125 break;
1126 }
1127
1128 color_array[i].r = 1.0;
1129 color_array[i].g = 1.0;
1130 color_array[i].b = 1.0;
1131 color_array[i].a = alpha;
1132 vertices[i].x = f2glf(pv.x);
1133 vertices[i].y = f2glf(pv.y);
1134 vertices[i].z = vert_z;
1135 }
1136 glVertexPointer(3, GL_FLOAT, 0, vertices.data());
1137 glColorPointer(4, GL_FLOAT, 0, color_array.data());
1138 glTexCoordPointer(2, GL_FLOAT, 0, texcoord_array.data());
1139 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); // Replaced GL_QUADS
1140 }
1141
1142 /*
1143 * Movies
1144 * Since this function will create a new texture each call, mipmapping can be very GPU intensive - so it has an optional setting for texture filtering.
1145 */
1146 bool ogl_ubitblt_i(unsigned dw,unsigned dh,unsigned dx,unsigned dy, unsigned sw, unsigned sh, unsigned sx, unsigned sy, const grs_bitmap &src, grs_bitmap &dest, const opengl_texture_filter texfilt)
1147 {
1148 GLfloat xo,yo,xs,ys,u1,v1;
1149 struct bitblt_free_ogl_texture
1150 {
1151 ogl_texture t;
1152 ~bitblt_free_ogl_texture()
1153 {
1154 ogl_freetexture(t);
1155 }
1156 };
1157 ogl_client_states<bitblt_free_ogl_texture, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY> cs;
1158 ogl_texture &tex = std::get<0>(cs).t;
1159 r_ubitbltc++;
1160
1161 ogl_init_texture(tex, sw, sh, OGL_FLAG_ALPHA);
1162 tex.prio = 0.0;
1163 tex.lw=src.bm_rowsize;
1164
1165 u1=v1=0;
1166
1167 dx+=dest.bm_x;
1168 dy+=dest.bm_y;
1169 xo=dx/static_cast<float>(last_width);
1170 xs=dw/static_cast<float>(last_width);
1171 yo=1.0-dy/static_cast<float>(last_height);
1172 ys=dh/static_cast<float>(last_height);
1173
1174 OGL_ENABLE(TEXTURE_2D);
1175
1176 ogl_loadtexture(gr_current_pal, src.get_bitmap_data(), sx, sy, tex, src.get_flags(), 0, texfilt, 0, 0);
1177 OGL_BINDTEXTURE(tex.handle);
1178
1179 ogl_texwrap(&tex,GL_CLAMP_TO_EDGE);
1180
1181 const GLfloat vertices[] = {
1182 xo,
1183 yo,
1184 xo + xs,
1185 yo,
1186 xo + xs,
1187 yo - ys,
1188 xo,
1189 yo - ys
1190 };
1191
1192 const GLfloat texcoord_array[] = {
1193 u1,
1194 v1,
1195 tex.u,
1196 v1,
1197 tex.u,
1198 tex.v,
1199 u1,
1200 tex.v
1201 };
1202
1203 glVertexPointer(2, GL_FLOAT, 0, vertices);
1204 static constexpr GLfloat color_array[] = {
1205 1.0, 1.0, 1.0, 1.0,
1206 1.0, 1.0, 1.0, 1.0,
1207 1.0, 1.0, 1.0, 1.0,
1208 1.0, 1.0, 1.0, 1.0
1209 };
1210 glColorPointer(4, GL_FLOAT, 0, color_array);
1211 glTexCoordPointer(2, GL_FLOAT, 0, texcoord_array);
1212 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);//replaced GL_QUADS
1213 return 0;
1214 }
1215
1216 bool ogl_ubitblt(unsigned w,unsigned h,unsigned dx,unsigned dy, unsigned sx, unsigned sy, const grs_bitmap &src, grs_bitmap &dest){
1217 return ogl_ubitblt_i(w, h, dx, dy, w, h, sx, sy, src, dest, opengl_texture_filter::classic);
1218 }
1219
1220 /*
1221 * set depth testing on or off
1222 */
1223 void ogl_toggle_depth_test(int enable)
1224 {
1225 if (enable)
1226 glEnable(GL_DEPTH_TEST);
1227 else
1228 glDisable(GL_DEPTH_TEST);
1229 }
1230
1231 /*
1232 * set blending function
1233 */
1234 void ogl_set_blending(const gr_blend cv_blend_func)
1235 {
1236 GLenum s, d;
1237 switch (cv_blend_func)
1238 {
1239 case gr_blend::additive_a:
1240 s = GL_SRC_ALPHA;
1241 d = GL_ONE;
1242 break;
1243 case gr_blend::additive_c:
1244 s = GL_ONE;
1245 d = GL_ONE;
1246 break;
1247 case gr_blend::normal:
1248 default:
1249 s = GL_SRC_ALPHA;
1250 d = GL_ONE_MINUS_SRC_ALPHA;
1251 break;
1252 }
1253 glBlendFunc(s, d);
1254 }
1255
1256 void ogl_start_frame(grs_canvas &canvas)
1257 {
1258 r_polyc=0;r_tpolyc=0;r_bitmapc=0;r_ubitbltc=0;
1259
1260 OGL_VIEWPORT(canvas.cv_bitmap.bm_x, canvas.cv_bitmap.bm_y, canvas.cv_bitmap.bm_w, canvas.cv_bitmap.bm_h);
1261 glClearColor(0.0, 0.0, 0.0, 0.0);
1262
1263 glLineWidth(linedotscale);
1264 glEnable(GL_BLEND);
1265 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1266
1267 glEnable(GL_ALPHA_TEST);
1268 glAlphaFunc(GL_GEQUAL,0.02);
1269 glEnable(GL_DEPTH_TEST);
1270 glDepthFunc(GL_LEQUAL);
1271
1272 glClear(GL_DEPTH_BUFFER_BIT);
1273 glEnable(GL_CULL_FACE);
1274 glCullFace(GL_FRONT);
1275
1276 glShadeModel(GL_SMOOTH);
1277 glMatrixMode(GL_PROJECTION);
1278 glLoadIdentity();//clear matrix
1279 #if DXX_USE_OGLES
1280 perspective(90.0,1.0,0.1,5000.0);
1281 #else
1282 gluPerspective(90.0,1.0,0.1,5000.0);
1283 #endif
1284 glMatrixMode(GL_MODELVIEW);
1285 glLoadIdentity();//clear matrix
1286 }
1287
1288 #if DXX_USE_STEREOSCOPIC_RENDER
1289 void ogl_stereo_frame(const bool left_eye, const int xoff)
1290 {
1291 const float dxoff = xoff * 2.0f / grd_curscreen->sc_canvas.cv_bitmap.bm_w;
1292 float stereo_transform_dxoff;
1293
1294 // query if stereo quad buffering available?
1295 GLboolean ogl_stereo_enabled = false;
1296 glGetBooleanv(GL_STEREO, &ogl_stereo_enabled);
1297
1298 int gl_buffer;
1299 if (left_eye) {
1300 // left eye view
1301 if (!ogl_stereo_enabled)
1302 {
1303 []() {
1304 std::array<GLint, 4> ogl_stereo_viewport;
1305 glGetIntegerv(GL_VIEWPORT, ogl_stereo_viewport.data());
1306 // center unsqueezed side-by-side format
1307 switch (VR_stereo) {
1308 case StereoFormat::None:
1309 /* Not reached */
1310 case StereoFormat::AboveBelow:
1311 case StereoFormat::SideBySideFullHeight:
1312 /* No modification needed */
1313 return;
1314 case StereoFormat::SideBySideHalfHeight:
1315 ogl_stereo_viewport[1] -= ogl_stereo_viewport[3] / 2; // y = h/4
1316 break;
1317 case StereoFormat::AboveBelowSync:
1318 {
1319 const int dy = VR_sync_width / 2;
1320 ogl_stereo_viewport[3] -= dy;
1321 ogl_stereo_viewport[1] += dy;
1322 break;
1323 }
1324 }
1325 glViewport(ogl_stereo_viewport[0], ogl_stereo_viewport[1], ogl_stereo_viewport[2], ogl_stereo_viewport[3]);
1326 }();
1327 }
1328 // rightward image shift adjustment for left eye offset
1329 stereo_transform_dxoff = -dxoff; // xoff < 0
1330 gl_buffer = GL_BACK_LEFT;
1331 }
1332 else
1333 {
1334 // right eye view
1335 if (!ogl_stereo_enabled)
1336 {
1337 std::array<GLint, 4> ogl_stereo_viewport;
1338 glGetIntegerv(GL_VIEWPORT, ogl_stereo_viewport.data());
1339 switch (VR_stereo) {
1340 case StereoFormat::None:
1341 /* Not reached */
1342 break;
1343 // center unsqueezed side-by-side format
1344 case StereoFormat::SideBySideHalfHeight:
1345 ogl_stereo_viewport[1] -= ogl_stereo_viewport[3] / 2; // y = h/4
1346 [[fallthrough]];
1347 // half-width viewports for side-by-side format
1348 case StereoFormat::SideBySideFullHeight:
1349 ogl_stereo_viewport[0] += ogl_stereo_viewport[2]; // x = w/2
1350 break;
1351 // half-height viewports for above/below format
1352 case StereoFormat::AboveBelowSync:
1353 case StereoFormat::AboveBelow:
1354 ogl_stereo_viewport[1] -= ogl_stereo_viewport[3]; // y = h/2
1355 if (VR_stereo == StereoFormat::AboveBelowSync)
1356 ogl_stereo_viewport[3] -= VR_sync_width / 2;
1357 break;
1358 }
1359 glViewport(ogl_stereo_viewport[0], ogl_stereo_viewport[1], ogl_stereo_viewport[2], ogl_stereo_viewport[3]);
1360 }
1361 // leftward image shift adjustment for right eye offset
1362 stereo_transform_dxoff = dxoff; // xoff < 0
1363 gl_buffer = GL_BACK_RIGHT;
1364 }
1365 if (ogl_stereo_enabled)
1366 glDrawBuffer(gl_buffer);
1367 glMatrixMode(GL_PROJECTION);
1368 std::array<GLfloat, 16> ogl_stereo_transform;
1369 glGetFloatv(GL_PROJECTION_MATRIX, ogl_stereo_transform.data());
1370 ogl_stereo_transform[8] += stereo_transform_dxoff;
1371 glLoadMatrixf(ogl_stereo_transform.data());
1372 glMatrixMode(GL_MODELVIEW);
1373 }
1374 #endif
1375
1376 void ogl_end_frame(void){
1377 OGL_VIEWPORT(0, 0, grd_curscreen->get_screen_width(), grd_curscreen->get_screen_height());
1378 glMatrixMode(GL_PROJECTION);
1379 glLoadIdentity();//clear matrix
1380 #if DXX_USE_OGLES
1381 glOrthof(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
1382 #else
1383 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
1384 #endif
1385 glMatrixMode(GL_MODELVIEW);
1386 glLoadIdentity();//clear matrix
1387 glDisable(GL_CULL_FACE);
1388 glDisable(GL_DEPTH_TEST);
1389 }
1390
1391 void gr_flip(void)
1392 {
1393 if (CGameArg.DbgRenderStats)
1394 {
1395 gr_set_default_canvas();
1396 ogl_texture_stats(*grd_curcanv);
1397 }
1398
1399 ogl_do_palfx();
1400 ogl_swap_buffers_internal();
1401 glClear(GL_COLOR_BUFFER_BIT);
1402 }
1403
1404 // Allocate the pixel buffers 'pixels' and 'texbuf' based on current screen resolution
1405 void ogl_init_pixel_buffers(unsigned w, unsigned h)
1406 {
1407 w = std::bit_ceil(w); // convert to OpenGL texture size
1408 h = std::bit_ceil(h);
1409
1410 texbuf = std::make_unique<GLubyte[]>(max(w, 1024u)*max(h, 256u)*4); // must also fit big font texture
1411 }
1412
1413 void ogl_close_pixel_buffers(void)
1414 {
1415 texbuf.reset();
1416 }
1417
1418 static void ogl_filltexbuf(const palette_array_t &pal, const uint8_t *const data, GLubyte *texp, const unsigned truewidth, const unsigned width, const unsigned height, const int dxo, const int dyo, const unsigned twidth, const unsigned theight, const int type, const int bm_flags, const int data_format)
1419 {
1420 if ((width > max(static_cast<unsigned>(grd_curscreen->get_screen_width()), 1024u)) ||
1421 (height > max(static_cast<unsigned>(grd_curscreen->get_screen_height()), 256u)))
1422 Error("Texture is too big: %ix%i", width, height);
1423
1424 for (unsigned y=0;y<theight;y++)
1425 {
1426 int i=dxo+truewidth*(y+dyo);
1427 for (unsigned x=0;x<twidth;x++)
1428 {
1429 int c;
1430 if (x<width && y<height)
1431 {
1432 if (data_format)
1433 {
1434 int j;
1435
1436 for (j = 0; j < data_format; ++j)
1437 (*(texp++)) = data[i * data_format + j];
1438 i++;
1439 continue;
1440 }
1441 else
1442 {
1443 c = data[i++];
1444 }
1445 }
1446 else if (x == width && y < height) // end of bitmap reached - fill this pixel with last color to make a clean border when filtering this texture
1447 {
1448 c = data[(width*(y+1))-1];
1449 }
1450 else if (y == height && x < width) // end of bitmap reached - fill this row with color or last row to make a clean border when filtering this texture
1451 {
1452 c = data[(width*(height-1))+x];
1453 }
1454 else
1455 {
1456 c = 256; // fill the pad space with transparency (or blackness)
1457 }
1458
1459 if (c == 254 && (bm_flags & BM_FLAG_SUPER_TRANSPARENT))
1460 {
1461 switch (type)
1462 {
1463 case GL_RGBA:
1464 (*(texp++)) = 255;
1465 (*(texp++)) = 255;
1466 [[fallthrough]];
1467 case GL_LUMINANCE_ALPHA:
1468 (*(texp++)) = 255;
1469 (*(texp++)) = 0; // transparent pixel
1470 break;
1471 #if !DXX_USE_OGLES
1472 case GL_COLOR_INDEX:
1473 (*(texp++)) = c;
1474 break;
1475 #endif
1476 default:
1477 Error("ogl_filltexbuf unhandled super-transparent texformat\n");
1478 break;
1479 }
1480 }
1481 else if ((c == 255 && (bm_flags & BM_FLAG_TRANSPARENT)) || c == 256)
1482 {
1483 switch (type)
1484 {
1485 case GL_RGBA:
1486 (*(texp++))=0;
1487 [[fallthrough]];
1488 case GL_RGB:
1489 (*(texp++))=0;
1490 [[fallthrough]];
1491 case GL_LUMINANCE_ALPHA:
1492 (*(texp++))=0;
1493 [[fallthrough]];
1494 case GL_LUMINANCE:
1495 (*(texp++))=0;//transparent pixel
1496 break;
1497 #if !DXX_USE_OGLES
1498 case GL_COLOR_INDEX:
1499 (*(texp++)) = c;
1500 break;
1501 #endif
1502 default:
1503 Error("ogl_filltexbuf unknown texformat\n");
1504 break;
1505 }
1506 }
1507 else
1508 {
1509 switch (type)
1510 {
1511 case GL_LUMINANCE_ALPHA:
1512 (*(texp++))=255;
1513 [[fallthrough]];
1514 case GL_LUMINANCE://these could prolly be done to make the intensity based upon the intensity of the resulting color, but its not needed for anything (yet?) so no point. :)
1515 (*(texp++))=255;
1516 break;
1517 case GL_RGB:
1518 (*(texp++)) = pal[c].r * 4;
1519 (*(texp++)) = pal[c].g * 4;
1520 (*(texp++)) = pal[c].b * 4;
1521 break;
1522 case GL_RGBA:
1523 (*(texp++)) = pal[c].r * 4;
1524 (*(texp++)) = pal[c].g * 4;
1525 (*(texp++)) = pal[c].b * 4;
1526 (*(texp++)) = 255;//not transparent
1527 break;
1528 #if !DXX_USE_OGLES
1529 case GL_COLOR_INDEX:
1530 (*(texp++)) = c;
1531 break;
1532 #endif
1533 default:
1534 Error("ogl_filltexbuf unknown texformat\n");
1535 break;
1536 }
1537 }
1538 }
1539 }
1540 }
1541
1542 static void tex_set_size1(ogl_texture &tex, const unsigned dbits, const unsigned bits, const unsigned w, const unsigned h)
1543 {
1544 int u;
1545 if (tex.tw!=w || tex.th!=h){
1546 u=(tex.w/static_cast<float>(tex.tw)*w) * (tex.h/static_cast<float>(tex.th)*h);
1547 glmprintf((CON_DEBUG, "shrunken texture?"));
1548 }else
1549 u=tex.w*tex.h;
1550 if (bits<=0){//the beta nvidia GLX server. doesn't ever return any bit sizes, so just use some assumptions.
1551 tex.bytes=(static_cast<float>(w)*h*dbits)/8.0;
1552 tex.bytesu=(static_cast<float>(u)*dbits)/8.0;
1553 }else{
1554 tex.bytes=(static_cast<float>(w)*h*bits)/8.0;
1555 tex.bytesu=(static_cast<float>(u)*bits)/8.0;
1556 }
1557 glmprintf((CON_DEBUG, "tex_set_size1: %ix%i, %ib(%i) %iB", w, h, bits, dbits, tex.bytes));
1558 }
1559
1560 static void tex_set_size(ogl_texture &tex)
1561 {
1562 GLint w,h;
1563 int bi{16},a=0;
1564 #if !DXX_USE_OGLES
1565 if (CGameArg.DbgGlGetTexLevelParamOk)
1566 {
1567 GLint t;
1568 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&w);
1569 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&h);
1570 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_LUMINANCE_SIZE,&t);a+=t;
1571 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_INTENSITY_SIZE,&t);a+=t;
1572 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_RED_SIZE,&t);a+=t;
1573 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_GREEN_SIZE,&t);a+=t;
1574 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_BLUE_SIZE,&t);a+=t;
1575 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_ALPHA_SIZE,&t);a+=t;
1576 }
1577 else
1578 #endif
1579 {
1580 w=tex.tw;
1581 h=tex.th;
1582 }
1583 switch (tex.format){
1584 case GL_LUMINANCE:
1585 bi=8;
1586 break;
1587 case GL_LUMINANCE_ALPHA:
1588 bi=8;
1589 break;
1590 case GL_RGB:
1591 case GL_RGBA:
1592 bi=16;
1593 break;
1594 #if !DXX_USE_OGLES
1595 case GL_COLOR_INDEX:
1596 bi = 8;
1597 break;
1598 #endif
1599 default:
1600 throw std::runtime_error("unknown texture format");
1601 }
1602 tex_set_size1(tex,bi,a,w,h);
1603 }
1604
1605 //loads a palettized bitmap into a ogl RGBA texture.
1606 //Sizes and pads dimensions to multiples of 2 if necessary.
1607 //In theory this could be a problem for repeating textures, but all real
1608 //textures (not sprites, etc) in descent are 64x64, so we are ok.
1609 //stores OpenGL textured id in *texid and u/v values required to get only the real data in *u/*v
1610 static int ogl_loadtexture(const palette_array_t &pal, const uint8_t *data, const int dxo, int dyo, ogl_texture &tex, const int bm_flags, const int data_format, opengl_texture_filter texfilt, const bool texanis, const bool edgepad)
1611 {
1612 tex.tw = {std::bit_ceil(tex.w)};
1613 tex.th = {std::bit_ceil(tex.h)}; //calculate smallest texture size that can accommodate us (must be power of 2)
1614
1615 //calculate u/v values that would make the resulting texture correctly sized
1616 tex.u = static_cast<float>(static_cast<double>(tex.w) / static_cast<double>(tex.tw));
1617 tex.v = static_cast<float>(static_cast<double>(tex.h) / static_cast<double>(tex.th));
1618
1619 auto *bufP = texbuf.get();
1620 const uint8_t *outP = texbuf.get();
1621 {
1622 if (bm_flags >= 0)
1623 ogl_filltexbuf (pal, data, texbuf.get(), tex.lw, tex.w, tex.h, dxo, dyo, tex.tw, tex.th,
1624 tex.format, bm_flags, data_format);
1625 else {
1626 if (!dxo && !dyo && (tex.w == tex.tw) && (tex.h == tex.th))
1627 outP = data;
1628 else {
1629 int h, w, tw;
1630
1631 h = tex.lw / tex.w;
1632 w = (tex.w - dxo) * h;
1633 data += tex.lw * dyo + h * dxo;
1634
1635 tw = tex.tw * h;
1636 h = tw - w;
1637 for (; dyo < tex.h; dyo++, data += tex.lw) {
1638 memcpy (bufP, data, w);
1639 bufP += w;
1640 memset (bufP, 0, h);
1641 bufP += h;
1642 }
1643 memset (bufP, 0, tex.th * tw - (bufP - texbuf.get()));
1644 }
1645 }
1646 }
1647
1648 //bleed color (rgb) into the alpha area, to deal with "dark edges problem"
1649 if (tex.format == GL_RGBA && texfilt != opengl_texture_filter::classic && edgepad && !CGameArg.OglDarkEdges)
1650 {
1651 GLubyte *p = bufP;
1652 GLubyte *pdone = p + (4 * tex.tw * tex.th);
1653 int line = 4 * tex.tw;
1654 p += 4;
1655 pdone -= 4;
1656 GLubyte *ptop = p + line;
1657 GLubyte *pbottom = pdone - line;
1658
1659 for (; p < pdone; p += 4)
1660 {
1661 //offsets 0 to 2 are r, g, b. offset 3 is alpha. 0x00 is transparent, 0xff is opaque.
1662 if (! *(p + 3))
1663 {
1664 if (*(p - 1))
1665 {
1666 *p = *(p - 4);
1667 *(p + 1) = *(p - 3);
1668 *(p + 2) = *(p - 2);
1669 continue;
1670 } //from left
1671 if (*(p + 7))
1672 {
1673 *p = *(p + 4);
1674 *(p + 1) = *(p + 5);
1675 *(p + 2) = *(p + 6);
1676 continue;
1677 } //from right
1678 if (p >= ptop)
1679 {
1680 if (*(p - line + 3))
1681 {
1682 *p = *(p - line);
1683 *(p + 1) = *(p - line + 1);
1684 *(p + 2) = *(p - line + 2);
1685 continue;
1686 } //from above
1687 }
1688 if (p < pbottom)
1689 {
1690 if (*(p + line + 3))
1691 {
1692 *p = *(p + line);
1693 *(p + 1) = *(p + line + 1);
1694 *(p + 2) = *(p + line + 2);
1695 continue;
1696 } //from below
1697 if (*(p + line - 1))
1698 {
1699 *p = *(p + line - 4);
1700 *(p + 1) = *(p + line - 3);
1701 *(p + 2) = *(p + line - 2);
1702 continue;
1703 } //bottom left
1704 if (*(p + line + 7))
1705 {
1706 *p = *(p + line + 4);
1707 *(p + 1) = *(p + line + 5);
1708 *(p + 2) = *(p + line + 6);
1709 continue;
1710 } //bottom right
1711 }
1712 if (p >= ptop)
1713 {
1714 if (*(p - line - 1))
1715 {
1716 *p = *(p - line - 4);
1717 *(p + 1) = *(p - line - 3);
1718 *(p + 2) = *(p - line - 2);
1719 continue;
1720 } //top left
1721 if (*(p - line + 7))
1722 {
1723 *p = *(p - line + 4);
1724 *(p + 1) = *(p - line + 5);
1725 *(p + 2) = *(p - line + 6);
1726 continue;
1727 } //top right
1728 }
1729 }
1730 }
1731 }
1732
1733 int rescale{1};
1734 if (texfilt == opengl_texture_filter::upscale)
1735 {
1736 rescale = 4;
1737 if ((tex.tw > 256) || (tex.th > 256))
1738 {
1739 //don't scale big textures, instead "cheat" and render them as normal (nearest)
1740 //these are normally 320x200 and 640x480 images.
1741 //this deals with texture size limits, as well as large textures causing noticeable performance issues/hiccups.
1742 texfilt = opengl_texture_filter::classic;
1743 rescale = 1;
1744 }
1745 }
1746 std::unique_ptr<GLubyte[]> buftemp;
1747 if (rescale > 1)
1748 {
1749 int rebpp{3};
1750 if (tex.format == GL_RGBA) rebpp = 4;
1751 if (tex.format == GL_LUMINANCE) rebpp = 1;
1752 buftemp = std::make_unique<GLubyte[]>(rescale * tex.tw * rescale * tex.th * rebpp);
1753 int x,y;
1754 GLubyte *p = buftemp.get();
1755 int len = tex.tw*rebpp*rescale;
1756 int bppscale = (rebpp*rescale);
1757
1758 for (y = 0; y < tex.th; y++)
1759 {
1760 GLubyte *p1 = bufP;
1761
1762 for (x = 0; x < len; x++)
1763 {
1764 *(p+x) = *(outP + ((x / bppscale)*rebpp + (x % rebpp)));
1765 }
1766 p1 = p;
1767 p += len;
1768 int y2;
1769 for (y2 = 1; y2 < rescale; y2++)
1770 {
1771 memcpy (p, p1, len);
1772 p += len;
1773 }
1774 outP += tex.tw*rebpp;
1775 }
1776 outP = buftemp.get();
1777 }
1778
1779 // Generate OpenGL texture IDs.
1780 glGenTextures (1, &tex.handle);
1781 #if !DXX_USE_OGLES
1782 //set priority
1783 glPrioritizeTextures (1, &tex.handle, &tex.prio);
1784 #endif
1785 // Give our data to OpenGL.
1786 OGL_BINDTEXTURE(tex.handle);
1787 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1788
1789 // should match structue in menu.cpp
1790 // organized in switches for better readability
1791 bool buildmipmap = false;
1792 GLint gl_mag_filter_int, gl_min_filter_int;
1793 switch (texfilt)
1794 {
1795 default:
1796 case opengl_texture_filter::classic: // Classic - Nearest
1797 gl_mag_filter_int = GL_NEAREST;
1798 if (texanis && ogl_maxanisotropy > 1.0f)
1799 {
1800 // looks nicer if anisotropy is applied.
1801 gl_min_filter_int = GL_NEAREST_MIPMAP_LINEAR;
1802 buildmipmap = true;
1803 }
1804 else
1805 {
1806 gl_min_filter_int = GL_NEAREST;
1807 buildmipmap = false;
1808 }
1809 break;
1810 case opengl_texture_filter::upscale: // Upscaled - i.e. Blocky Filtered (Bilinear)
1811 case opengl_texture_filter::trilinear: // Smooth - Trilinear
1812 gl_mag_filter_int = GL_LINEAR;
1813 gl_min_filter_int = GL_LINEAR_MIPMAP_LINEAR;
1814 buildmipmap = true;
1815 break;
1816 }
1817 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_mag_filter_int);
1818 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_min_filter_int);
1819 if (texanis && ogl_maxanisotropy > 1.0f)
1820 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, ogl_maxanisotropy);
1821
1822 #if DXX_USE_OGLES // in OpenGL ES 1.1 the mipmaps are automatically generated by a parameter
1823 glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, buildmipmap ? GL_TRUE : GL_FALSE);
1824 #else
1825 if (buildmipmap)
1826 {
1827 gluBuild2DMipmaps (
1828 GL_TEXTURE_2D, tex.internalformat,
1829 tex.tw * rescale, tex.th * rescale, tex.format,
1830 GL_UNSIGNED_BYTE,
1831 outP);
1832 }
1833 else
1834 #endif
1835 {
1836 glTexImage2D (
1837 GL_TEXTURE_2D, 0, tex.internalformat,
1838 tex.tw * rescale, tex.th * rescale, 0, tex.format, // RGBA textures.
1839 GL_UNSIGNED_BYTE, // imageData is a GLubyte pointer.
1840 outP);
1841 }
1842
1843 tex_set_size(tex);
1844 r_texcount++;
1845 return 0;
1846 }
1847
1848 void ogl_loadbmtexture_f(grs_bitmap &rbm, const opengl_texture_filter texfilt, bool texanis, bool edgepad)
1849 {
1850 assert(!rbm.get_flag_mask(BM_FLAG_PAGED_OUT));
1851 assert(rbm.bm_data);
1852 grs_bitmap *bm = &rbm;
1853 while (const auto bm_parent = bm->bm_parent)
1854 bm = bm_parent;
1855 if (bm->gltexture && bm->gltexture->handle > 0)
1856 return;
1857 auto buf=bm->get_bitmap_data();
1858 const unsigned bm_w = bm->bm_w;
1859 if (bm->gltexture == NULL){
1860 ogl_init_texture(*(bm->gltexture = ogl_get_free_texture()), bm_w, bm->bm_h, ((bm->get_flag_mask(BM_FLAG_TRANSPARENT | BM_FLAG_SUPER_TRANSPARENT)) ? OGL_FLAG_ALPHA : 0));
1861 }
1862 else {
1863 if (bm->gltexture->handle>0)
1864 return;
1865 if (bm->gltexture->w==0){
1866 bm->gltexture->lw = bm_w;
1867 bm->gltexture->w = bm_w;
1868 bm->gltexture->h=bm->bm_h;
1869 }
1870 }
1871
1872 std::array<uint8_t, 300*1024> decodebuf;
1873 if (bm->get_flag_mask(BM_FLAG_RLE))
1874 {
1875 class bm_rle_expand_state
1876 {
1877 uint8_t *dbits;
1878 uint8_t *const ebits;
1879 public:
1880 bm_rle_expand_state(uint8_t *const b, uint8_t *const e) :
1881 dbits(b), ebits(e)
1882 {
1883 }
1884 uint8_t *get_begin_dbits() const
1885 {
1886 return dbits;
1887 }
1888 uint8_t *get_end_dbits() const
1889 {
1890 return ebits;
1891 }
1892 void consume_dbits(const unsigned w)
1893 {
1894 dbits += w;
1895 }
1896 };
1897 decodebuf = {};
1898 buf = decodebuf.data();
1899 if (!bm_rle_expand(*bm).loop(bm_w, bm_rle_expand_state(begin(decodebuf), end(decodebuf))))
1900 {
1901 con_printf(CON_URGENT, "error: insufficient space to decode %ux%hu bitmap. Please report this as a bug.", bm_w, bm->bm_h);
1902 }
1903 }
1904 ogl_loadtexture(gr_palette, buf, 0, 0, *bm->gltexture, bm->get_flags(), 0, texfilt, texanis, edgepad);
1905 }
1906
1907 static void ogl_freetexture(ogl_texture &gltexture)
1908 {
1909 if (gltexture.handle>0) {
1910 r_texcount--;
1911 glmprintf((CON_DEBUG, "ogl_freetexture(%p):%i (%i left)", &gltexture, gltexture.handle, r_texcount));
1912 glDeleteTextures( 1, &gltexture.handle );
1913 // gltexture->handle=0;
1914 ogl_reset_texture(gltexture);
1915 }
1916 }
1917
1918 void ogl_freebmtexture(grs_bitmap &bm)
1919 {
1920 if (auto &gltexture = bm.gltexture)
1921 ogl_freetexture(*std::exchange(gltexture, nullptr));
1922 }
1923
1924 const ogl_colors::array_type ogl_colors::white = {{
1925 1.0, 1.0, 1.0, 1.0,
1926 1.0, 1.0, 1.0, 1.0,
1927 1.0, 1.0, 1.0, 1.0,
1928 1.0, 1.0, 1.0, 1.0,
1929 }};
1930
1931 const ogl_colors::array_type &ogl_colors::init_maybe_white(const int c)
1932 {
1933 return c == -1 ? white : init_palette(c);
1934 }
1935
1936 const ogl_colors::array_type &ogl_colors::init_palette(const unsigned c)
1937 {
1938 const auto &rgb = gr_current_pal[c];
1939 const GLfloat r = rgb.r / 63.0, g = rgb.g / 63.0, b = rgb.b / 63.0;
1940 a = {{
1941 r, g, b, 1.0,
1942 r, g, b, 1.0,
1943 r, g, b, 1.0,
1944 r, g, b, 1.0,
1945 }};
1946 return a;
1947 }
1948
1949 bool ogl_ubitmapm_cs(grs_canvas &canvas, int x, int y,int dw, int dh, grs_bitmap &bm, int c)
1950 {
1951 ogl_colors color;
1952 return ogl_ubitmapm_cs(canvas, x, y, dw, dh, bm, color.init(c));
1953 }
1954
1955 /*
1956 * Menu / gauges
1957 */
1958 bool ogl_ubitmapm_cs(grs_canvas &canvas, const int entry_x, const int entry_y, const int entry_dw, const int entry_dh, grs_bitmap &bm, const ogl_colors::array_type &color_array)
1959 {
1960 GLfloat u1,u2,v1,v2;
1961 ogl_client_states<GLfloat, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY> cs;
1962 auto &xo = std::get<0>(cs);
1963 const int adjusted_canvas_x = entry_x + canvas.cv_bitmap.bm_x;
1964 const int adjusted_canvas_y = entry_y + canvas.cv_bitmap.bm_y;
1965
1966 const int effective_dw = (entry_dw == opengl_bitmap_use_dst_canvas)
1967 ? canvas.cv_bitmap.bm_w
1968 : ((entry_dw == opengl_bitmap_use_src_bitmap)
1969 ? bm.bm_w
1970 : entry_dw);
1971 const int effective_dh = (entry_dh == opengl_bitmap_use_dst_canvas)
1972 ? canvas.cv_bitmap.bm_h
1973 : ((entry_dh == opengl_bitmap_use_src_bitmap)
1974 ? bm.bm_h
1975 : entry_dh);
1976
1977 xo = adjusted_canvas_x / (static_cast<double>(last_width));
1978 const GLfloat xf = (effective_dw + adjusted_canvas_x) / (static_cast<double>(last_width));
1979 const GLfloat yo = 1.0 - adjusted_canvas_y / (static_cast<double>(last_height));
1980 const GLfloat yf = 1.0 - (effective_dh + adjusted_canvas_y) / (static_cast<double>(last_height));
1981
1982 OGL_ENABLE(TEXTURE_2D);
1983 ogl_bindbmtex(bm, 0);
1984 ogl_texwrap(bm.gltexture,GL_CLAMP_TO_EDGE);
1985
1986 if (bm.bm_x==0){
1987 u1=0;
1988 if (bm.bm_w==bm.gltexture->w)
1989 u2=bm.gltexture->u;
1990 else
1991 u2=(bm.bm_w+bm.bm_x)/static_cast<float>(bm.gltexture->tw);
1992 }else {
1993 u1=bm.bm_x/static_cast<float>(bm.gltexture->tw);
1994 u2=(bm.bm_w+bm.bm_x)/static_cast<float>(bm.gltexture->tw);
1995 }
1996 if (bm.bm_y==0){
1997 v1=0;
1998 if (bm.bm_h==bm.gltexture->h)
1999 v2=bm.gltexture->v;
2000 else
2001 v2=(bm.bm_h+bm.bm_y)/static_cast<float>(bm.gltexture->th);
2002 }else{
2003 v1=bm.bm_y/static_cast<float>(bm.gltexture->th);
2004 v2=(bm.bm_h+bm.bm_y)/static_cast<float>(bm.gltexture->th);
2005 }
2006
2007 const std::array<GLfloat, 8> vertices{{
2008 xo, yo,
2009 xf, yo,
2010 xf, yf,
2011 xo, yf,
2012 }};
2013 const std::array<GLfloat, 8> texcoord_array{{
2014 u1, v1,
2015 u2, v1,
2016 u2, v2,
2017 u1, v2,
2018 }};
2019 glVertexPointer(2, GL_FLOAT, 0, vertices.data());
2020 glColorPointer(4, GL_FLOAT, 0, color_array.data());
2021 glTexCoordPointer(2, GL_FLOAT, 0, texcoord_array.data());
2022 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);//replaced GL_QUADS
2023 return 0;
2024 }
2025
2026 }