Debian Codegraph @

renderer/HardwareOpenGL.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <algorithm>
20 #include <cstdlib>
21 #include <cstdio>
22 #include <cstring>
23 #include <SDL.h>
24
25 #if defined(WIN32)
26 #include <windows.h>
27 #endif
28
29 #include "byteswap.h"
30 #include "pserror.h"
31 #include "mono.h"
32 #include "3d.h"
33 #include "renderer.h"
34 #include "application.h"
35 #include "bitmap.h"
36 #include "lightmap.h"
37 #include "rend_opengl.h"
38 #include "grdefs.h"
39 #include "mem.h"
40 #include "config.h"
41 #include "rtperformance.h"
42 #include "HardwareInternal.h"
43 #include "../Descent3/args.h"
44 #include "NewBitmap.h"
45
46 #define DECLARE_OPENGL
47 #include "dyna_gl.h"
48
49 #if defined(WIN32)
50 #include "win/arb_extensions.h"
51 #endif
52
53 int FindArg(const char *);
54 void rend_SetLightingState(light_state state);
55
56 // General renderer states
57 extern int gpu_Overlay_map;
58 int Bump_map = 0;
59 int Bumpmap_ready = 0;
60 extern uint8_t gpu_Overlay_type;
61 float Z_bias = 0.0f;
62 uint8_t Renderer_close_flag = 0;
63 extern uint8_t Renderer_initted;
64 renderer_type Renderer_type = RENDERER_OPENGL;
65 int WindowGL = 0;
66
67 #ifndef GL_UNSIGNED_SHORT_5_5_5_1
68 #define GL_UNSIGNED_SHORT_5_5_5_1 0x8034
69 #endif
70
71 #ifndef GL_UNSIGNED_SHORT_4_4_4_4
72 #define GL_UNSIGNED_SHORT_4_4_4_4 0x8033
73 #endif
74
75 #define CHECK_ERROR(x)
76
77 SDL_Window *GSDLWindow = NULL;
78 SDL_GLContext GSDLGLContext = NULL;
79 char loadedLibrary[_MAX_PATH];
80
81 #define GET_WRAP_STATE(x) (x >> 4)
82 #define GET_FILTER_STATE(x) (x & 0x0f)
83
84 #define SET_WRAP_STATE(x, s) \
85 { \
86 x &= 0x0F; \
87 x |= (s << 4); \
88 }
89 #define SET_FILTER_STATE(x, s) \
90 { \
91 x &= 0xF0; \
92 x |= (s); \
93 }
94
95 // OpenGL Stuff
96 static int OpenGL_window_initted = 0;
97 static int OpenGL_polys_drawn = 0;
98 static int OpenGL_verts_processed = 0;
99 static int OpenGL_uploads = 0;
100 static int OpenGL_sets_this_frame[10];
101 static int OpenGL_packed_pixels = 0;
102 static int Cur_texture_object_num = 1;
103 static int OpenGL_cache_initted = 0;
104 static int OpenGL_last_bound[2];
105 static int Last_texel_unit_set = -1;
106
107 extern int gpu_last_frame_polys_drawn;
108 extern int gpu_last_frame_verts_processed;
109 extern int gpu_last_uploaded;
110
111 extern float gpu_Alpha_factor;
112 extern float gpu_Alpha_multiplier;
113
114 #if defined(_USE_OGL_ACTIVE_TEXTURES)
115 PFNGLACTIVETEXTUREARBPROC oglActiveTextureARB = NULL;
116 PFNGLCLIENTACTIVETEXTUREARBPROC oglClientActiveTextureARB = NULL;
117 PFNGLMULTITEXCOORD4FARBPROC oglMultiTexCoord4f = NULL;
118 #endif
119
120 uint16_t *OpenGL_bitmap_remap = NULL;
121 uint16_t *OpenGL_lightmap_remap = NULL;
122 uint8_t *OpenGL_bitmap_states = NULL;
123 uint8_t *OpenGL_lightmap_states = NULL;
124
125 uint32_t *opengl_Upload_data = NULL;
126 uint32_t *opengl_Translate_table = NULL;
127 uint32_t *opengl_4444_translate_table = NULL;
128
129 uint16_t *opengl_packed_Upload_data = NULL;
130 uint16_t *opengl_packed_Translate_table = NULL;
131 uint16_t *opengl_packed_4444_translate_table = NULL;
132
133 extern rendering_state gpu_state;
134 extern renderer_preferred_state gpu_preferred_state;
135
136 bool OpenGL_multitexture_state = false;
137 module *OpenGLDLLHandle = NULL;
138 int Already_loaded = 0;
139 bool opengl_Blending_on = 0;
140
141 static oeApplication *ParentApplication = NULL;
142
143 /* framebuffer object for backbuffer, scale to window size without changing resolution. --ryan, 2019. */
144 #define GL_DEPTH_COMPONENT16_EXT 0x81A5
145 #define GL_READ_FRAMEBUFFER_EXT 0x8CA8
146 #define GL_DRAW_FRAMEBUFFER_EXT 0x8CA9
147 #define GL_FRAMEBUFFER_COMPLETE_EXT 0x8CD5
148 #define GL_COLOR_ATTACHMENT0_EXT 0x8CE0
149 #define GL_DEPTH_ATTACHMENT_EXT 0x8D00
150 #define GL_STENCIL_ATTACHMENT_EXT 0x8D20
151 #define GL_FRAMEBUFFER_EXT 0x8D40
152 #define GL_RENDERBUFFER_EXT 0x8D41
153 static GLuint GOpenGLFBO = 0;
154 static GLuint GOpenGLRBOColor = 0;
155 static GLuint GOpenGLRBODepth = 0;
156 static GLuint GOpenGLFBOWidth = 0;
157 static GLuint GOpenGLFBOHeight = 0;
158
159
160 #if 0
161 int checkForGLErrors( const char *file, int line )
162 {
163 /*
164 int errors = 0 ;
165 int counter = 0 ;
166 static int errcnt = 0;
167 if(!dglGetError)
168 return 0;
169 while ( counter < 1000 )
170 {
171 GLenum x = dglGetError() ;
172
173 if ( x == GL_NO_ERROR )
174 return errors ;
175
176 printf( "%s:%d OpenGL error: %s [%08x]\n", file,line, gluErrorString ( x ), errcnt++ ) ;
177 errors++ ;
178 counter++ ;
179 }
180 */
181 const char *sdlp = SDL_GetError();
182 if(sdlp && *sdlp)
183 mprintf(0,"SDL: %s",sdlp);
184 return 1;
185 }
186 #endif
187
188 // Sets up multi-texturing using ARB extensions
189 void opengl_GetDLLFunctions(void) {
190 #if defined(WIN32)
191 oglActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)dwglGetProcAddress("glActiveTextureARB");
192 if (!oglActiveTextureARB)
193 goto dll_error;
194
195 oglClientActiveTextureARB = (PFNGLCLIENTACTIVETEXTUREARBPROC)dwglGetProcAddress("glClientActiveTextureARB");
196 if (!oglClientActiveTextureARB)
197 goto dll_error;
198
199 oglMultiTexCoord4f = (PFNGLMULTITEXCOORD4FARBPROC)dwglGetProcAddress("glMultiTexCoord4f");
200 if (!oglMultiTexCoord4f)
201 goto dll_error;
202 #else
203 #define mod_GetSymbol(x, funcStr, y) __SDL_mod_GetSymbol(funcStr)
204
205 oglActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)mod_GetSymbol(OpenGLDLLHandle, "glActiveTextureARB", 255);
206 oglClientActiveTextureARB =
207 (PFNGLCLIENTACTIVETEXTUREARBPROC)mod_GetSymbol(OpenGLDLLHandle, "glClientActiveTextureARB", 255);
208 oglMultiTexCoord4f = (PFNGLMULTITEXCOORD4FARBPROC)mod_GetSymbol(OpenGLDLLHandle, "glMultiTexCoord4f", 255);
209 if (!oglMultiTexCoord4f) {
210 oglMultiTexCoord4f = (PFNGLMULTITEXCOORD4FARBPROC)mod_GetSymbol(OpenGLDLLHandle, "glMultiTexCoord4fARB", 255);
211 }
212 if (oglActiveTextureARB == NULL || oglClientActiveTextureARB == NULL || oglMultiTexCoord4f == NULL) {
213 goto dll_error;
214 }
215
216 #undef mod_GetSymbol
217 #endif
218
219 UseMultitexture = true;
220 return;
221
222 dll_error:
223 oglActiveTextureARB = NULL;
224 oglClientActiveTextureARB = NULL;
225 oglMultiTexCoord4f = NULL;
226 UseMultitexture = false;
227 }
228
229 // returns true if the passed in extension name is supported
230 bool opengl_CheckExtension(const char *extName) {
231 const char *p = (const char *)dglGetString(GL_EXTENSIONS);
232 int extNameLen = strlen(extName);
233 const char *end = p + strlen(p);
234
235 while (p < end) {
236 int n = strcspn(p, " ");
237 if ((extNameLen == n) && (strncmp(extName, p, n) == 0))
238 return true;
239
240 p += (n + 1);
241 }
242
243 return false;
244 }
245
246 // Gets some specific information about this particular flavor of opengl
247 void opengl_GetInformation() {
248 mprintf(0, "OpenGL Vendor: %s\n", dglGetString(GL_VENDOR));
249 mprintf(0, "OpenGL Renderer: %s\n", dglGetString(GL_RENDERER));
250 mprintf(0, "OpenGL Version: %s\n", dglGetString(GL_VERSION));
251 mprintf(0, "OpenGL Extensions: %s\n", dglGetString(GL_EXTENSIONS));
252 }
253
254 int opengl_MakeTextureObject(int tn) {
255 int num = Cur_texture_object_num;
256
257 Cur_texture_object_num++;
258
259 if (UseMultitexture && Last_texel_unit_set != tn) {
260 #if (defined(_USE_OGL_ACTIVE_TEXTURES))
261 oglActiveTextureARB(GL_TEXTURE0_ARB + tn);
262 Last_texel_unit_set = tn;
263 #endif
264 }
265
266 dglBindTexture(GL_TEXTURE_2D, num);
267 dglPixelStorei(GL_UNPACK_ALIGNMENT, 2);
268
269 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
270 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
271
272 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
273 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
274
275 // glTexEnvf (GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
276
277 CHECK_ERROR(2)
278
279 return num;
280 }
281
282 int opengl_InitCache(void) {
283
284 OpenGL_bitmap_remap = (uint16_t *)mem_malloc(MAX_BITMAPS * 2);
285 ASSERT(OpenGL_bitmap_remap);
286 OpenGL_lightmap_remap = (uint16_t *)mem_malloc(MAX_LIGHTMAPS * 2);
287 ASSERT(OpenGL_lightmap_remap);
288
289 OpenGL_bitmap_states = (uint8_t *)mem_malloc(MAX_BITMAPS);
290 ASSERT(OpenGL_bitmap_states);
291 OpenGL_lightmap_states = (uint8_t *)mem_malloc(MAX_LIGHTMAPS);
292 ASSERT(OpenGL_lightmap_states);
293
294 Cur_texture_object_num = 1;
295
296 // Setup textures and cacheing
297 int i;
298 for (i = 0; i < MAX_BITMAPS; i++) {
299 OpenGL_bitmap_remap[i] = 65535;
300 OpenGL_bitmap_states[i] = 255;
301 GameBitmaps[i].flags |= BF_CHANGED | BF_BRAND_NEW;
302 }
303
304 for (i = 0; i < MAX_LIGHTMAPS; i++) {
305 OpenGL_lightmap_remap[i] = 65535;
306 OpenGL_lightmap_states[i] = 255;
307 GameLightmaps[i].flags |= LF_CHANGED | LF_BRAND_NEW;
308 }
309
310 dglTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
311
312 if (UseMultitexture) {
313 #if (defined(_USE_OGL_ACTIVE_TEXTURES))
314 oglActiveTextureARB(GL_TEXTURE0_ARB + 1);
315 dglTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
316 oglActiveTextureARB(GL_TEXTURE0_ARB + 0);
317 #endif
318 }
319
320 CHECK_ERROR(3)
321
322 OpenGL_cache_initted = 1;
323 return 1;
324 }
325
326 // Sets default states for our renderer
327 void opengl_SetDefaults() {
328 mprintf(0, "Setting states\n");
329
330 gpu_state.cur_color = 0x00FFFFFF;
331 gpu_state.cur_bilinear_state = -1;
332 gpu_state.cur_zbuffer_state = -1;
333 gpu_state.cur_texture_quality = -1;
334 gpu_state.cur_light_state = LS_GOURAUD;
335 gpu_state.cur_color_model = CM_MONO;
336 gpu_state.cur_bilinear_state = -1;
337 gpu_state.cur_alpha_type = AT_TEXTURE;
338
339 // Enable some states
340 dglAlphaFunc(GL_GREATER, 0);
341 dglEnable(GL_ALPHA_TEST);
342 dglEnable(GL_BLEND);
343 dglEnable(GL_DITHER);
344 opengl_Blending_on = true;
345
346 rend_SetAlphaType(AT_ALWAYS);
347 rend_SetAlphaValue(255);
348 rend_SetFiltering(1);
349 rend_SetLightingState(LS_NONE);
350 rend_SetTextureType(TT_FLAT);
351 rend_SetColorModel(CM_RGB);
352 rend_SetZBufferState(1);
353 rend_SetZValues(0, 3000);
354 rend_SetGammaValue(gpu_preferred_state.gamma);
355 OpenGL_last_bound[0] = 9999999;
356 OpenGL_last_bound[1] = 9999999;
357 Last_texel_unit_set = -1;
358 OpenGL_multitexture_state = false;
359
360 dglEnableClientState(GL_VERTEX_ARRAY);
361 dglEnableClientState(GL_COLOR_ARRAY);
362 dglEnableClientState(GL_TEXTURE_COORD_ARRAY);
363
364 dglHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
365 dglHint(GL_FOG_HINT, GL_NICEST);
366 dglEnable(GL_SCISSOR_TEST);
367 dglScissor(0, 0, gpu_state.screen_width, gpu_state.screen_height);
368 dglDisable(GL_SCISSOR_TEST);
369 dglDepthRange(0.0f, 1.0f);
370
371 if (UseMultitexture) {
372 #if (defined(_USE_OGL_ACTIVE_TEXTURES))
373 oglActiveTextureARB(GL_TEXTURE0_ARB + 1);
374 oglClientActiveTextureARB(GL_TEXTURE0_ARB + 1);
375 dglEnableClientState(GL_TEXTURE_COORD_ARRAY);
376 dglHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
377 dglHint(GL_FOG_HINT, GL_NICEST);
378
379 oglClientActiveTextureARB(GL_TEXTURE0_ARB + 0);
380
381 dglDisable(GL_TEXTURE_2D);
382 dglAlphaFunc(GL_GREATER, 0);
383 dglEnable(GL_ALPHA_TEST);
384 dglEnable(GL_BLEND);
385 dglEnable(GL_DITHER);
386 dglBlendFunc(GL_DST_COLOR, GL_ZERO);
387 oglActiveTextureARB(GL_TEXTURE0_ARB + 0);
388 #endif
389 }
390 }
391
392 extern bool linux_permit_gamma;
393 extern renderer_preferred_state Render_preferred_state;
394 extern bool ddio_mouseGrabbed;
395 int SDLCALL d3SDLEventFilter(void *userdata, SDL_Event *event);
396
397 int opengl_Setup(oeApplication *app, int *width, int *height) {
398 int winw = Video_res_list[Game_video_resolution].width;
399 int winh = Video_res_list[Game_video_resolution].height;
400
401 // rcg09182000 don't need to quitsubsystem anymore...
402 // SDL_QuitSubSystem(SDL_INIT_VIDEO); // here goes nothing...
403 // Already_loaded = false;
404 SDL_ClearError();
405 if (!SDL_WasInit(SDL_INIT_VIDEO)) {
406 const int rc = SDL_Init(SDL_INIT_VIDEO);
407 if (rc != 0) {
408 char buffer[512];
409 snprintf(buffer, sizeof(buffer), "SDL_GetError() reports \"%s\".\n", SDL_GetError());
410 fprintf(stderr, "SDL: SDL_Init() failed! rc == (%d).\n", rc);
411 fprintf(stderr, "%s", buffer);
412 rend_SetErrorMessage(buffer);
413 return (0);
414 }
415 }
416
417 SDL_SetEventFilter(d3SDLEventFilter, NULL);
418
419 bool fullscreen = true;
420
421 if (FindArgChar("-fullscreen", 'f')) {
422 fullscreen = true;
423 } else if (FindArgChar("-windowed", 'w')) {
424 fullscreen = false;
425 }
426
427 if (!Already_loaded) {
428 #define MAX_ARGS 30
429 #define MAX_CHARS_PER_ARG 100
430 extern char GameArgs[MAX_ARGS][MAX_CHARS_PER_ARG];
431
432 char gl_library[256];
433 int arg;
434 arg = FindArgChar("-gllibrary", 'g');
435 if (arg != 0) {
436 strcpy(gl_library, GameArgs[arg + 1]);
437 } else {
438 gl_library[0] = 0;
439 }
440
441 mprintf(0, "OpenGL: Attempting to use \"%s\" for OpenGL\n", gl_library[0] ? gl_library : "[system default library]");
442
443 // ryan's adds. 04/18/2000...SDL stuff on 04/25/2000
444 bool success = true;
445
446 OpenGLDLLHandle = LoadOpenGLDLL(gl_library);
447 if (!(OpenGLDLLHandle)) {
448 // rcg07072000 last ditch effort...
449 #if defined(POSIX)
450 strcpy(gl_library, "libGL.so.1");
451 #else
452 strcpy(gl_library, "opengl32.dll");
453 #endif
454 OpenGLDLLHandle = LoadOpenGLDLL(gl_library);
455 if (!(OpenGLDLLHandle)) {
456 success = false;
457 }
458 } // if
459
460 if (!success) {
461 char buffer[512];
462 snprintf(buffer, sizeof(buffer), "Failed to load library [%s].\n", gl_library);
463 fprintf(stderr, "%s", buffer);
464 rend_SetErrorMessage(buffer);
465 return 0;
466 } // if
467 }
468
469 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8 );
470 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
471 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
472 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
473 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
474 Uint32 flags = SDL_WINDOW_OPENGL;
475
476 if (fullscreen) {
477 flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
478 }
479
480 if (!GSDLWindow) {
481 GSDLWindow = SDL_CreateWindow("Descent 3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, winw, winh, flags);
482 if (!GSDLWindow) {
483 mprintf(0, "OpenGL: SDL window creation failed: %s", SDL_GetError());
484 return 0;
485 }
486 } else {
487 SDL_SetWindowSize(GSDLWindow, winw, winh);
488 SDL_SetWindowFullscreen(GSDLWindow, flags);
489 }
490
491 if (!GSDLGLContext) {
492 GSDLGLContext = SDL_GL_CreateContext(GSDLWindow);
493 if (!GSDLGLContext) {
494 mprintf(0, "OpenGL: OpenGL context creation failed: %s", SDL_GetError());
495 SDL_DestroyWindow(GSDLWindow);
496 GSDLWindow = NULL;
497 return 0;
498 }
499 }
500
501 // clear the window framebuffer to start.
502 dglClearColor(0.0f, 0.0f, 0.0f, 1.0f);
503 dglClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
504 SDL_GL_SwapWindow(GSDLWindow);
505
506 bool fbo_available = true;
507 if (!SDL_GL_ExtensionSupported("GL_EXT_framebuffer_object")) {
508 mprintf(0, "OpenGL: GL_EXT_framebuffer_object extension is not available");
509 fbo_available = false;
510 }
511
512 if (!SDL_GL_ExtensionSupported("GL_EXT_framebuffer_blit")) {
513 mprintf(0, "OpenGL: GL_EXT_framebuffer_blit extension is not available");
514 fbo_available = false;
515 }
516
517 if (fbo_available) {
518 #define LOOKUP_GL_SYM(x) \
519 dgl##x = (gl##x##_fp) SDL_GL_GetProcAddress("gl" #x); \
520 if (dgl##x == NULL) { \
521 mprintf(0, "OpenGL: gl%s function not found!", #x); \
522 fbo_available = false; \
523 }
524 LOOKUP_GL_SYM(GenFramebuffersEXT);
525 LOOKUP_GL_SYM(GenRenderbuffersEXT);
526 LOOKUP_GL_SYM(BindFramebufferEXT);
527 LOOKUP_GL_SYM(BindRenderbufferEXT);
528 LOOKUP_GL_SYM(RenderbufferStorageEXT);
529 LOOKUP_GL_SYM(FramebufferRenderbufferEXT);
530 LOOKUP_GL_SYM(CheckFramebufferStatusEXT);
531 LOOKUP_GL_SYM(DeleteFramebuffersEXT);
532 LOOKUP_GL_SYM(DeleteRenderbuffersEXT);
533 LOOKUP_GL_SYM(BlitFramebufferEXT);
534 }
535
536 if (!fbo_available) {
537 mprintf(0, "OpenGL: We need missing Framebuffer Object support, giving up");
538 SDL_GL_DeleteContext(GSDLGLContext);
539 SDL_DestroyWindow(GSDLWindow);
540 GSDLGLContext = NULL;
541 GSDLWindow = NULL;
542 return 0;
543 }
544
545 /* Tear down the backbuffer and rebuild at new dimensions... */
546 if (GOpenGLFBO) {
547 dglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, GOpenGLFBO);
548 dglFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, 0);
549 dglFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
550 dglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
551 dglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
552 dglDeleteFramebuffersEXT(1, &GOpenGLFBO);
553 dglDeleteRenderbuffersEXT(1, &GOpenGLRBOColor);
554 dglDeleteRenderbuffersEXT(1, &GOpenGLRBODepth);
555 GOpenGLFBOWidth = GOpenGLFBOHeight = GOpenGLFBO = GOpenGLRBOColor = GOpenGLRBODepth = 0;
556 }
557
558 const GLsizei w = (GLsizei) *width;
559 const GLsizei h = (GLsizei) *height;
560
561 GOpenGLFBOWidth = w;
562 GOpenGLFBOHeight = h;
563
564 dglGenFramebuffersEXT(1, &GOpenGLFBO);
565 dglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, GOpenGLFBO);
566
567 dglGenRenderbuffersEXT(1, &GOpenGLRBOColor);
568 dglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, GOpenGLRBOColor);
569 dglRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, w, h);
570 dglFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, GOpenGLRBOColor);
571
572 dglGenRenderbuffersEXT(1, &GOpenGLRBODepth);
573 dglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, GOpenGLRBODepth);
574 dglRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT16_EXT, w, h);
575 dglFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, GOpenGLRBODepth);
576
577 if (dglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT) {
578 mprintf(0, "OpenGL: our framebuffer object is incomplete, giving up");
579 dglFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, 0);
580 dglFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
581 dglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
582 dglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
583 dglDeleteFramebuffersEXT(1, &GOpenGLFBO);
584 dglDeleteRenderbuffersEXT(1, &GOpenGLRBOColor);
585 dglDeleteRenderbuffersEXT(1, &GOpenGLRBODepth);
586 GOpenGLFBO = GOpenGLRBOColor = GOpenGLRBODepth = 0;
587 SDL_GL_DeleteContext(GSDLGLContext);
588 SDL_DestroyWindow(GSDLWindow);
589 GSDLGLContext = NULL;
590 GSDLWindow = NULL;
591 return 0;
592 }
593
594 if (!FindArg("-nomousegrab")) {
595 ddio_mouseGrabbed = true;
596 }
597
598 SDL_SetRelativeMouseMode(ddio_mouseGrabbed ? SDL_TRUE : SDL_FALSE);
599
600 // rcg09182000 gamma fun.
601 // rcg01112000 --nogamma fun.
602 if (FindArgChar("-nogamma", 'M')) {
603 linux_permit_gamma = false;
604 } else {
605 Uint16 ramp[256];
606 SDL_CalculateGammaRamp(Render_preferred_state.gamma, ramp);
607 linux_permit_gamma = (SDL_SetWindowGammaRamp(GSDLWindow, ramp, ramp, ramp) == 0);
608 } // else
609
610 if (ParentApplication) {
611 reinterpret_cast<oeLnxApplication *>(ParentApplication)->set_sizepos(0, 0, *width, *height);
612 }
613
614 Already_loaded = 1;
615 return 1;
616 }
617
618 // Sets up our OpenGL rendering context
619 // Returns 1 if ok, 0 if something bad
620 int opengl_Init(oeApplication *app, renderer_preferred_state *pref_state) {
621 int width, height;
622 int retval = 1;
623 int i;
624
625 mprintf(0, "Setting up opengl mode!\n");
626
627 if (pref_state) {
628 gpu_preferred_state = *pref_state;
629 }
630
631 if (app != NULL) {
632 ParentApplication = app;
633 }
634
635 int windowX = 0, windowY = 0;
636
637 /***********************************************************
638 * LINUX OPENGL
639 ***********************************************************
640 */
641 // Setup gpu_state.screen_width & gpu_state.screen_height & width & height
642 width = gpu_preferred_state.width;
643 height = gpu_preferred_state.height;
644
645 if (!opengl_Setup(app, &width, &height)) {
646 opengl_Close();
647 return 0;
648 }
649
650 memset(&gpu_state, 0, sizeof(rendering_state));
651 gpu_state.screen_width = width;
652 gpu_state.screen_height = height;
653
654 // Get some info
655 opengl_GetInformation();
656
657 mprintf(0, "Setting up multitexture...\n");
658
659 // Determine if Multitexture is supported
660 bool supportsMultiTexture = opengl_CheckExtension("GL_ARB_multitexture");
661 if (!supportsMultiTexture) {
662 supportsMultiTexture = opengl_CheckExtension("GL_SGIS_multitexture");
663 }
664
665 if (FindArg("-NoMultitexture")) {
666 supportsMultiTexture = false;
667 }
668
669 if (supportsMultiTexture) {
670 // attempt to grab Multitexture functions
671 opengl_GetDLLFunctions();
672 } else {
673 // No multitexture at all
674 UseMultitexture = false;
675 }
676
677 // Do we have packed pixel formats?
678 OpenGL_packed_pixels = opengl_CheckExtension("GL_EXT_packed_pixels");
679
680 opengl_InitCache();
681
682 if (UseMultitexture) {
683 mprintf(0, "Using multitexture.");
684 } else {
685 mprintf(0, "Not using multitexture.");
686 }
687
688 if (OpenGL_packed_pixels) {
689 opengl_packed_Upload_data = (uint16_t *)mem_malloc(2048 * 2048 * 2);
690 opengl_packed_Translate_table = (uint16_t *)mem_malloc(65536 * 2);
691 opengl_packed_4444_translate_table = (uint16_t *)mem_malloc(65536 * 2);
692
693 ASSERT(opengl_packed_Upload_data);
694 ASSERT(opengl_packed_Translate_table);
695 ASSERT(opengl_packed_4444_translate_table);
696
697 mprintf(0, "Building packed OpenGL translate table...\n");
698
699 for (i = 0; i < 65536; i++) {
700 int r = (i >> 10) & 0x1f;
701 int g = (i >> 5) & 0x1f;
702 int b = i & 0x1f;
703
704 #ifdef BRIGHTNESS_HACK
705 r *= BRIGHTNESS_HACK;
706 g *= BRIGHTNESS_HACK;
707 b *= BRIGHTNESS_HACK;
708 if (r > 0x1F)
709 r = 0x1F;
710 if (g > 0x1F)
711 g = 0x1F;
712 if (b > 0x1F)
713 b = 0x1F;
714 #endif
715
716 uint16_t pix;
717
718 if (!(i & OPAQUE_FLAG)) {
719 pix = 0;
720 } else {
721 pix = (r << 11) | (g << 6) | (b << 1) | 1;
722 }
723
724 opengl_packed_Translate_table[i] = INTEL_INT(pix);
725
726 // 4444 table
727 int a = (i >> 12) & 0xf;
728 r = (i >> 8) & 0xf;
729 g = (i >> 4) & 0xf;
730 b = i & 0xf;
731
732 pix = (r << 12) | (g << 8) | (b << 4) | a;
733 opengl_packed_4444_translate_table[i] = INTEL_INT(pix);
734 }
735 } else {
736 opengl_Upload_data = (uint32_t *)mem_malloc(2048 * 2048 * 4);
737 opengl_Translate_table = (uint32_t *)mem_malloc(65536 * 4);
738 opengl_4444_translate_table = (uint32_t *)mem_malloc(65536 * 4);
739
740 ASSERT(opengl_Upload_data);
741 ASSERT(opengl_Translate_table);
742 ASSERT(opengl_4444_translate_table);
743
744 mprintf(0, "Building OpenGL translate table...\n");
745
746 for (i = 0; i < 65536; i++) {
747 uint32_t pix;
748 int r = (i >> 10) & 0x1f;
749 int g = (i >> 5) & 0x1f;
750 int b = i & 0x1f;
751
752 #ifdef BRIGHTNESS_HACK
753 r *= BRIGHTNESS_HACK;
754 g *= BRIGHTNESS_HACK;
755 b *= BRIGHTNESS_HACK;
756 if (r > 0x1F)
757 r = 0x1F;
758 if (g > 0x1F)
759 g = 0x1F;
760 if (b > 0x1F)
761 b = 0x1F;
762 #endif
763
764 float fr = (float)r / 31.0f;
765 float fg = (float)g / 31.0f;
766 float fb = (float)b / 31.0f;
767
768 r = 255 * fr;
769 g = 255 * fg;
770 b = 255 * fb;
771
772 if (!(i & OPAQUE_FLAG)) {
773 pix = 0;
774 } else {
775 pix = (255 << 24) | (b << 16) | (g << 8) | (r);
776 }
777
778 opengl_Translate_table[i] = INTEL_INT(pix);
779
780 // Do 4444
781 int a = (i >> 12) & 0xf;
782 r = (i >> 8) & 0xf;
783 g = (i >> 4) & 0xf;
784 b = i & 0xf;
785
786 float fa = (float)a / 15.0f;
787 fr = (float)r / 15.0f;
788 fg = (float)g / 15.0f;
789 fb = (float)b / 15.0f;
790
791 a = 255 * fa;
792 r = 255 * fr;
793 g = 255 * fg;
794 b = 255 * fb;
795
796 pix = (a << 24) | (b << 16) | (g << 8) | (r);
797
798 opengl_4444_translate_table[i] = INTEL_INT(pix);
799 }
800 }
801
802 opengl_SetDefaults();
803
804 g3_ForceTransformRefresh();
805
806 CHECK_ERROR(4)
807
808 gpu_state.initted = 1;
809
810 mprintf(0, "OpenGL initialization at %d x %d was successful.\n", width, height);
811
812 return retval;
813 }
814
815 // Releases the rendering context
816 void opengl_Close(const bool just_resizing) {
817 CHECK_ERROR(5)
818
819 uint32_t *delete_list = (uint32_t *)mem_malloc(Cur_texture_object_num * sizeof(int));
820 ASSERT(delete_list);
821 for (int i = 1; i < Cur_texture_object_num; i++)
822 delete_list[i] = i;
823
824 if (Cur_texture_object_num > 1)
825 dglDeleteTextures(Cur_texture_object_num, (const uint32_t *)delete_list);
826
827 mem_free(delete_list);
828
829 if (GSDLGLContext) {
830 SDL_GL_MakeCurrent(NULL, NULL);
831 SDL_GL_DeleteContext(GSDLGLContext);
832 GSDLGLContext = NULL;
833 GOpenGLFBOWidth = GOpenGLFBOHeight = GOpenGLFBO = GOpenGLRBOColor = GOpenGLRBODepth = 0;
834 }
835
836 if (!just_resizing && GSDLWindow) {
837 SDL_DestroyWindow(GSDLWindow);
838 GSDLWindow = NULL;
839 }
840
841
842 if (OpenGL_packed_pixels) {
843 if (opengl_packed_Upload_data) {
844 mem_free(opengl_packed_Upload_data);
845 }
846 if (opengl_packed_Translate_table) {
847 mem_free(opengl_packed_Translate_table);
848 }
849 if (opengl_packed_4444_translate_table) {
850 mem_free(opengl_packed_4444_translate_table);
851 }
852 opengl_packed_Upload_data = NULL;
853 opengl_packed_Translate_table = NULL;
854 opengl_packed_4444_translate_table = NULL;
855 } else {
856 if (opengl_Upload_data)
857 mem_free(opengl_Upload_data);
858 if (opengl_Translate_table)
859 mem_free(opengl_Translate_table);
860 if (opengl_4444_translate_table)
861 mem_free(opengl_4444_translate_table);
862 opengl_Upload_data = NULL;
863 opengl_Translate_table = NULL;
864 opengl_4444_translate_table = NULL;
865 }
866
867 if (OpenGL_cache_initted) {
868 mem_free(OpenGL_lightmap_remap);
869 mem_free(OpenGL_bitmap_remap);
870 mem_free(OpenGL_lightmap_states);
871 mem_free(OpenGL_bitmap_states);
872 OpenGL_cache_initted = 0;
873 }
874
875 // mod_FreeModule (OpenGLDLLHandle);
876 gpu_state.initted = 0;
877 }
878
879 // Takes our 16bit format and converts it into the memory scheme that OpenGL wants
880 void opengl_TranslateBitmapToOpenGL(int texnum, int bm_handle, int map_type, int replace, int tn) {
881 uint16_t *bm_ptr;
882
883 int w, h;
884 int size;
885
886 if (UseMultitexture && Last_texel_unit_set != tn) {
887 #if (defined(_USE_OGL_ACTIVE_TEXTURES))
888 oglActiveTextureARB(GL_TEXTURE0_ARB + tn);
889 Last_texel_unit_set = tn;
890 #endif
891 }
892
893 if (map_type == MAP_TYPE_LIGHTMAP) {
894 if (GameLightmaps[bm_handle].flags & LF_BRAND_NEW)
895 replace = 0;
896
897 bm_ptr = lm_data(bm_handle);
898 GameLightmaps[bm_handle].flags &= ~(LF_CHANGED | LF_BRAND_NEW);
899
900 w = lm_w(bm_handle);
901 h = lm_h(bm_handle);
902 size = GameLightmaps[bm_handle].square_res;
903 } else {
904 if (GameBitmaps[bm_handle].flags & BF_BRAND_NEW)
905 replace = 0;
906
907 bm_ptr = bm_data(bm_handle, 0);
908 GameBitmaps[bm_handle].flags &= ~(BF_CHANGED | BF_BRAND_NEW);
909 w = bm_w(bm_handle, 0);
910 h = bm_h(bm_handle, 0);
911 size = w;
912 }
913
914 if (OpenGL_last_bound[tn] != texnum) {
915 dglBindTexture(GL_TEXTURE_2D, texnum);
916 OpenGL_sets_this_frame[0]++;
917 OpenGL_last_bound[tn] = texnum;
918 }
919
920 int i;
921
922 if (OpenGL_packed_pixels) {
923 if (map_type == MAP_TYPE_LIGHTMAP) {
924 uint16_t *left_data = (uint16_t *)opengl_packed_Upload_data;
925 int bm_left = 0;
926
927 for (int i = 0; i < h; i++, left_data += size, bm_left += w) {
928 uint16_t *dest_data = left_data;
929 for (int t = 0; t < w; t++) {
930 *dest_data++ = opengl_packed_Translate_table[bm_ptr[bm_left + t]];
931 }
932 }
933
934 if (replace) {
935 dglTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size, size, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1,
936 opengl_packed_Upload_data);
937 } else {
938 dglTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, size, size, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1,
939 opengl_packed_Upload_data);
940 }
941 } else {
942 int limit = 0;
943
944 if (bm_mipped(bm_handle)) {
945 limit = NUM_MIP_LEVELS + 3;
946 } else {
947 limit = 1;
948 }
949
950 for (int m = 0; m < limit; m++) {
951 if (m < NUM_MIP_LEVELS) {
952 bm_ptr = bm_data(bm_handle, m);
953 w = bm_w(bm_handle, m);
954 h = bm_h(bm_handle, m);
955 } else {
956 bm_ptr = bm_data(bm_handle, NUM_MIP_LEVELS - 1);
957 w = bm_w(bm_handle, NUM_MIP_LEVELS - 1);
958 h = bm_h(bm_handle, NUM_MIP_LEVELS - 1);
959
960 w >>= m - (NUM_MIP_LEVELS - 1);
961 h >>= m - (NUM_MIP_LEVELS - 1);
962
963 if ((w < 1) || (h < 1))
964 continue;
965 }
966
967 if (bm_format(bm_handle) == BITMAP_FORMAT_4444) {
968 // Do 4444
969 if (bm_mipped(bm_handle)) {
970 for (i = 0; i < w * h; i++)
971 opengl_packed_Upload_data[i] = 0xf | (opengl_packed_4444_translate_table[bm_ptr[i]]);
972 } else {
973 for (i = 0; i < w * h; i++)
974 opengl_packed_Upload_data[i] = opengl_packed_4444_translate_table[bm_ptr[i]];
975 }
976
977 if (replace) {
978 dglTexSubImage2D(GL_TEXTURE_2D, m, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
979 opengl_packed_Upload_data);
980 } else {
981 dglTexImage2D(GL_TEXTURE_2D, m, GL_RGBA4, w, h, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
982 opengl_packed_Upload_data);
983 }
984 } else {
985 // Do 1555
986 for (i = 0; i < w * h; i++) {
987 opengl_packed_Upload_data[i] = opengl_packed_Translate_table[bm_ptr[i]];
988 }
989
990 if (replace) {
991 dglTexSubImage2D(GL_TEXTURE_2D, m, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1,
992 opengl_packed_Upload_data);
993 } else {
994 dglTexImage2D(GL_TEXTURE_2D, m, GL_RGB5_A1, w, h, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1,
995 opengl_packed_Upload_data);
996 }
997 }
998 }
999 }
1000 } else {
1001 if (map_type == MAP_TYPE_LIGHTMAP) {
1002 uint32_t *left_data = (uint32_t *)opengl_Upload_data;
1003 int bm_left = 0;
1004
1005 for (int i = 0; i < h; i++, left_data += size, bm_left += w) {
1006 uint32_t *dest_data = left_data;
1007 for (int t = 0; t < w; t++) {
1008 *dest_data++ = opengl_Translate_table[bm_ptr[bm_left + t]];
1009 }
1010 }
1011 if (size > 0) {
1012 if (replace) {
1013 dglTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size, size, GL_RGBA, GL_UNSIGNED_BYTE, opengl_Upload_data);
1014 } else {
1015 dglTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, opengl_Upload_data);
1016 }
1017 }
1018 } else {
1019 int limit = 0;
1020
1021 if (bm_mipped(bm_handle)) {
1022 limit = NUM_MIP_LEVELS + 3; // ryan added +3.
1023 } else {
1024 limit = 1;
1025 }
1026
1027 for (int m = 0; m < limit; m++) {
1028 bm_ptr = bm_data(bm_handle, m);
1029 w = bm_w(bm_handle, m);
1030 h = bm_h(bm_handle, m);
1031
1032 if (bm_format(bm_handle) == BITMAP_FORMAT_4444) {
1033 // Do 4444
1034
1035 if (bm_mipped(bm_handle)) {
1036 for (i = 0; i < w * h; i++)
1037 opengl_Upload_data[i] = INTEL_INT((255 << 24)) | opengl_4444_translate_table[bm_ptr[i]];
1038 } else {
1039 for (i = 0; i < w * h; i++)
1040 opengl_Upload_data[i] = opengl_4444_translate_table[bm_ptr[i]];
1041 }
1042 } else {
1043 // Do 1555
1044
1045 for (i = 0; i < w * h; i++)
1046 opengl_Upload_data[i] = opengl_Translate_table[bm_ptr[i]];
1047 }
1048
1049 // rcg06262000 my if wrapper.
1050 if ((w > 0) && (h > 0)) {
1051 if (replace) {
1052 dglTexSubImage2D(GL_TEXTURE_2D, m, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, opengl_Upload_data);
1053 } else {
1054 dglTexImage2D(GL_TEXTURE_2D, m, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, opengl_Upload_data);
1055 }
1056 }
1057 }
1058 }
1059 }
1060
1061 // mprintf(1,"Doing slow upload to opengl!\n");
1062
1063 if (map_type == MAP_TYPE_LIGHTMAP) {
1064 GameLightmaps[bm_handle].flags &= ~LF_LIMITS;
1065 }
1066
1067 CHECK_ERROR(6)
1068 OpenGL_uploads++;
1069 }
1070
1071 extern bool Force_one_texture;
1072
1073 // Utilizes a LRU cacheing scheme to select/upload textures the opengl driver
1074 int opengl_MakeBitmapCurrent(int handle, int map_type, int tn) {
1075 int w, h;
1076 int texnum;
1077
1078 if (map_type == MAP_TYPE_LIGHTMAP) {
1079 w = GameLightmaps[handle].square_res;
1080 h = GameLightmaps[handle].square_res;
1081 } else {
1082 if (Force_one_texture) {
1083 handle = 0;
1084 }
1085
1086 w = bm_w(handle, 0);
1087 h = bm_h(handle, 0);
1088 }
1089
1090 if (w != h) {
1091 mprintf(0, "Can't use non-square textures with OpenGL!\n");
1092 return 0;
1093 }
1094
1095 // See if the bitmaps is already in the cache
1096 if (map_type == MAP_TYPE_LIGHTMAP) {
1097 if (OpenGL_lightmap_remap[handle] == 65535) {
1098 texnum = opengl_MakeTextureObject(tn);
1099 SET_WRAP_STATE(OpenGL_lightmap_states[handle], 1);
1100 SET_FILTER_STATE(OpenGL_lightmap_states[handle], 0);
1101 OpenGL_lightmap_remap[handle] = texnum;
1102 opengl_TranslateBitmapToOpenGL(texnum, handle, map_type, 0, tn);
1103 } else {
1104 texnum = OpenGL_lightmap_remap[handle];
1105 if (GameLightmaps[handle].flags & LF_CHANGED)
1106 opengl_TranslateBitmapToOpenGL(texnum, handle, map_type, 1, tn);
1107 }
1108 } else {
1109 if (OpenGL_bitmap_remap[handle] == 65535) {
1110 texnum = opengl_MakeTextureObject(tn);
1111 SET_WRAP_STATE(OpenGL_bitmap_states[handle], 1);
1112 SET_FILTER_STATE(OpenGL_bitmap_states[handle], 0);
1113 OpenGL_bitmap_remap[handle] = texnum;
1114 opengl_TranslateBitmapToOpenGL(texnum, handle, map_type, 0, tn);
1115 } else {
1116 texnum = OpenGL_bitmap_remap[handle];
1117 if (GameBitmaps[handle].flags & BF_CHANGED) {
1118 opengl_TranslateBitmapToOpenGL(texnum, handle, map_type, 1, tn);
1119 }
1120 }
1121 }
1122
1123 if (OpenGL_last_bound[tn] != texnum) {
1124 if (UseMultitexture && Last_texel_unit_set != tn) {
1125 #if (defined(_USE_OGL_ACTIVE_TEXTURES))
1126 oglActiveTextureARB(GL_TEXTURE0_ARB + tn);
1127 Last_texel_unit_set = tn;
1128 #endif
1129 }
1130
1131 dglBindTexture(GL_TEXTURE_2D, texnum);
1132 OpenGL_last_bound[tn] = texnum;
1133 OpenGL_sets_this_frame[0]++;
1134 }
1135
1136 CHECK_ERROR(7)
1137 return 1;
1138 }
1139
1140 // Sets up an appropriate wrap type for the current bound texture
1141 void opengl_MakeWrapTypeCurrent(int handle, int map_type, int tn) {
1142 int uwrap;
1143 wrap_type dest_wrap;
1144
1145 if (tn == 1)
1146 dest_wrap = WT_CLAMP;
1147 else
1148 dest_wrap = gpu_state.cur_wrap_type;
1149
1150 if (map_type == MAP_TYPE_LIGHTMAP)
1151 uwrap = GET_WRAP_STATE(OpenGL_lightmap_states[handle]);
1152 else
1153 uwrap = GET_WRAP_STATE(OpenGL_bitmap_states[handle]);
1154
1155 if (uwrap == dest_wrap)
1156 return;
1157
1158 if (UseMultitexture && Last_texel_unit_set != tn) {
1159 #if (defined(_USE_OGL_ACTIVE_TEXTURES))
1160 oglActiveTextureARB(GL_TEXTURE0_ARB + tn);
1161 Last_texel_unit_set = tn;
1162 #endif
1163 }
1164
1165 OpenGL_sets_this_frame[1]++;
1166
1167 if (gpu_state.cur_wrap_type == WT_CLAMP) {
1168 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1169 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1170
1171 } else if (gpu_state.cur_wrap_type == WT_WRAP_V) {
1172 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1173 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1174 } else {
1175 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1176 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1177 }
1178
1179 if (map_type == MAP_TYPE_LIGHTMAP) {
1180 SET_WRAP_STATE(OpenGL_lightmap_states[handle], dest_wrap);
1181 } else {
1182 SET_WRAP_STATE(OpenGL_bitmap_states[handle], dest_wrap);
1183 }
1184
1185 CHECK_ERROR(8)
1186 }
1187
1188 // Chooses the correct filter type for the currently bound texture
1189 void opengl_MakeFilterTypeCurrent(int handle, int map_type, int tn) {
1190 int magf;
1191 int8_t dest_state;
1192
1193 if (map_type == MAP_TYPE_LIGHTMAP) {
1194 magf = GET_FILTER_STATE(OpenGL_lightmap_states[handle]);
1195 dest_state = 1;
1196 } else {
1197 magf = GET_FILTER_STATE(OpenGL_bitmap_states[handle]);
1198 dest_state = gpu_preferred_state.filtering;
1199 if (!gpu_state.cur_bilinear_state)
1200 dest_state = 0;
1201 }
1202
1203 if (magf == dest_state)
1204 return;
1205 #if (defined(_USE_OGL_ACTIVE_TEXTURES))
1206 if (UseMultitexture && Last_texel_unit_set != tn) {
1207 oglActiveTextureARB(GL_TEXTURE0_ARB + tn);
1208 Last_texel_unit_set = tn;
1209 }
1210 #endif
1211
1212 OpenGL_sets_this_frame[2]++;
1213
1214 if (dest_state) {
1215 if (map_type == MAP_TYPE_BITMAP && bm_mipped(handle)) {
1216 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1217 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
1218 } else {
1219 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1220 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1221 }
1222 } else {
1223 if (map_type == MAP_TYPE_BITMAP && bm_mipped(handle)) {
1224 // dglTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST_MIPMAP_NEAREST);
1225 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1226 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
1227 } else {
1228 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1229 dglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1230 }
1231 }
1232
1233 if (map_type == MAP_TYPE_LIGHTMAP) {
1234 SET_FILTER_STATE(OpenGL_lightmap_states[handle], dest_state);
1235 } else {
1236 SET_FILTER_STATE(OpenGL_bitmap_states[handle], dest_state);
1237 }
1238
1239 CHECK_ERROR(9)
1240 }
1241
1242 // Turns on/off multitexture blending
1243 void gpu_SetMultitextureBlendMode(bool state) {
1244 if (OpenGL_multitexture_state == state)
1245 return;
1246 OpenGL_multitexture_state = state;
1247
1248 #if (defined(_USE_OGL_ACTIVE_TEXTURES))
1249 oglActiveTextureARB(GL_TEXTURE1_ARB);
1250 oglClientActiveTextureARB(GL_TEXTURE1_ARB);
1251 if (state) {
1252 dglEnableClientState(GL_TEXTURE_COORD_ARRAY);
1253 dglEnable(GL_TEXTURE_2D);
1254 } else {
1255 dglDisableClientState(GL_TEXTURE_COORD_ARRAY);
1256 dglDisable(GL_TEXTURE_2D);
1257 }
1258 oglActiveTextureARB(GL_TEXTURE0_ARB);
1259 oglClientActiveTextureARB(GL_TEXTURE0_ARB);
1260 Last_texel_unit_set = 0;
1261 #endif
1262 }
1263
1264 void gpu_DrawFlatPolygon3D(g3Point **p, int nv) {
1265 float fr, fg, fb;
1266 int i;
1267
1268 if (UseMultitexture) {
1269 gpu_SetMultitextureBlendMode(false);
1270 }
1271
1272 float alpha = gpu_Alpha_multiplier * gpu_Alpha_factor;
1273
1274 fr = GR_COLOR_RED(gpu_state.cur_color);
1275 fg = GR_COLOR_GREEN(gpu_state.cur_color);
1276 fb = GR_COLOR_BLUE(gpu_state.cur_color);
1277 fr /= 255.0;
1278 fg /= 255.0;
1279 fb /= 255.0;
1280
1281 // And draw!
1282 dglBegin(GL_POLYGON);
1283 for (i = 0; i < nv; i++) {
1284 g3Point *pnt = p[i];
1285 ASSERT(pnt->p3_flags & PF_ORIGPOINT);
1286
1287 if (gpu_state.cur_alpha_type & ATF_VERTEX)
1288 alpha = pnt->p3_a * gpu_Alpha_multiplier * gpu_Alpha_factor;
1289
1290 // If we have a lighting model, apply the correct lighting!
1291 if (gpu_state.cur_light_state != LS_NONE) {
1292 // Do lighting based on intesity (MONO) or colored (RGB)
1293 if (gpu_state.cur_color_model == CM_MONO)
1294 dglColor4f(pnt->p3_l, pnt->p3_l, pnt->p3_l, alpha);
1295 else {
1296 dglColor4f(pnt->p3_r, pnt->p3_g, pnt->p3_b, alpha);
1297 }
1298
1299 } else {
1300 dglColor4f(fr, fg, fb, alpha);
1301 }
1302
1303 /*
1304 // Finally, specify a vertex
1305 float z = std::max(0,std::min(1.0,1.0-(1.0/(pnt->p3_z+Z_bias))));
1306 dglVertex3f (pnt->p3_sx+x_add,pnt->p3_sy+y_add,-z);
1307 */
1308 dglVertex3f(pnt->p3_vecPreRot.x, pnt->p3_vecPreRot.y, pnt->p3_vecPreRot.z);
1309 }
1310
1311 dglEnd();
1312 CHECK_ERROR(11)
1313 OpenGL_polys_drawn++;
1314 OpenGL_verts_processed += nv;
1315 }
1316
1317 // Sets the gamma correction value
1318 void rend_SetGammaValue(float val) {
1319 // if( WindowGL )
1320 // return;
1321
1322 gpu_preferred_state.gamma = val;
1323 mprintf(0, "Setting gamma to %f\n", val);
1324 }
1325
1326 // Resets the texture cache
1327 void opengl_ResetCache(void) {
1328 if (OpenGL_cache_initted) {
1329 mem_free(OpenGL_lightmap_remap);
1330 mem_free(OpenGL_bitmap_remap);
1331 mem_free(OpenGL_lightmap_states);
1332 mem_free(OpenGL_bitmap_states);
1333 OpenGL_cache_initted = 0;
1334 }
1335
1336 opengl_InitCache();
1337 }
1338
1339 uint8_t opengl_Framebuffer_ready = 0;
1340 chunked_bitmap opengl_Chunked_bitmap;
1341
1342 void opengl_ChangeChunkedBitmap(int bm_handle, chunked_bitmap *chunk) {
1343 int bw = bm_w(bm_handle, 0);
1344 int bh = bm_h(bm_handle, 0);
1345
1346 // determine optimal size of the square bitmaps
1347 float fopt = 128.0f;
1348 int iopt;
1349
1350 // find the smallest dimension and base off that
1351 int smallest = std::min(bw, bh);
1352
1353 if (smallest <= 32)
1354 fopt = 32;
1355 else if (smallest <= 64)
1356 fopt = 64;
1357 else
1358 fopt = 128;
1359
1360 iopt = (int)fopt;
1361
1362 // Get how many pieces we need across and down
1363 float temp = bw / fopt;
1364 int how_many_across = temp;
1365 if ((temp - how_many_across) > 0)
1366 how_many_across++;
1367
1368 temp = bh / fopt;
1369 int how_many_down = temp;
1370 if ((temp - how_many_down) > 0)
1371 how_many_down++;
1372
1373 ASSERT(how_many_across > 0);
1374 ASSERT(how_many_down > 0);
1375
1376 // Now go through our big bitmap and partition it into pieces
1377 uint16_t *src_data = bm_data(bm_handle, 0);
1378 uint16_t *sdata;
1379 uint16_t *ddata;
1380
1381 int shift;
1382 switch (iopt) {
1383 case 32:
1384 shift = 5;
1385 break;
1386 case 64:
1387 shift = 6;
1388 break;
1389 case 128:
1390 shift = 7;
1391 break;
1392 default:
1393 Int3(); // Get Jeff
1394 break;
1395 }
1396 int maxx, maxy;
1397 int windex, hindex;
1398 int s_y, s_x, d_y, d_x;
1399
1400 for (hindex = 0; hindex < how_many_down; hindex++) {
1401 for (windex = 0; windex < how_many_across; windex++) {
1402 // loop through the chunks
1403 // find end x and y
1404 if (windex < how_many_across - 1)
1405 maxx = iopt;
1406 else
1407 maxx = bw - (windex << shift);
1408 if (hindex < how_many_down - 1)
1409 maxy = iopt;
1410 else
1411 maxy = bh - (hindex << shift);
1412
1413 // find the starting source x and y
1414 s_x = (windex << shift);
1415 s_y = (hindex << shift);
1416
1417 // get the pointers pointing to the right spot
1418 ddata = bm_data(chunk->bm_array[hindex * how_many_across + windex], 0);
1419 GameBitmaps[chunk->bm_array[hindex * how_many_across + windex]].flags |= BF_CHANGED;
1420 sdata = &src_data[s_y * bw + s_x];
1421
1422 // copy the data
1423 for (d_y = 0; d_y < maxy; d_y++) {
1424 for (d_x = 0; d_x < maxx; d_x++) {
1425 ddata[d_x] = sdata[d_x];
1426 } // end for d_x
1427 sdata += bw;
1428 ddata += iopt;
1429 } // end for d_y
1430
1431 } // end for windex
1432 } // end for hindex
1433 }
1434
1435 // Tells the software renderer whether or not to use mipping
1436 void rend_SetMipState(int8_t mipstate) {}
1437
1438 // Init our renderer
1439 int rend_Init(renderer_type state, oeApplication *app, renderer_preferred_state *pref_state) {
1440 #ifndef DEDICATED_ONLY
1441 int retval = 0;
1442 rend_SetRendererType(state);
1443 if (!Renderer_initted) {
1444 if (!Renderer_close_flag) {
1445 atexit(rend_Close);
1446 Renderer_close_flag = 1;
1447 }
1448
1449 Renderer_initted = 1;
1450 }
1451
1452 if (OpenGL_window_initted) {
1453 rend_CloseOpenGLWindow();
1454 OpenGL_window_initted = 0;
1455 }
1456
1457 mprintf(0, "Renderer init is set to %d\n", Renderer_initted);
1458
1459 #ifndef OEM_V3
1460 int flags = app->flags();
1461 if (flags & OEAPP_WINDOWED) {
1462 // initialize for windowed
1463 retval = rend_InitOpenGLWindow(app, pref_state);
1464 } else {
1465 // initialize for full screen
1466 retval = opengl_Init(app, pref_state);
1467 }
1468 #endif
1469
1470 return retval;
1471 #else
1472 return 0;
1473 #endif // #ifdef DEDICATED_ONLY
1474 }
1475
1476 void rend_Close(void) {
1477 mprintf(0, "CLOSE:Renderer init is set to %d\n", Renderer_initted);
1478 if (!Renderer_initted)
1479 return;
1480
1481 if (OpenGL_window_initted) {
1482 if (Renderer_type == RENDERER_OPENGL) {
1483 rend_CloseOpenGLWindow();
1484 }
1485 OpenGL_window_initted = 0;
1486 }
1487
1488 opengl_Close();
1489
1490 Renderer_initted = 0;
1491 }
1492
1493 void gpu_BindTexture(int handle, int map_type, int slot) {
1494 opengl_MakeBitmapCurrent(handle, map_type, slot);
1495 opengl_MakeWrapTypeCurrent(handle, map_type, slot);
1496 opengl_MakeFilterTypeCurrent(handle, map_type, slot);
1497 }
1498
1499 void gpu_RenderPolygon(PosColorUVVertex *vData, uint32_t nv) {
1500 dglVertexPointer(3, GL_FLOAT, sizeof(*vData), &vData->pos);
1501 dglColorPointer(4, GL_FLOAT, sizeof(*vData), &vData->color);
1502 oglClientActiveTextureARB(GL_TEXTURE0_ARB + 0);
1503 dglTexCoordPointer(4, GL_FLOAT, sizeof(*vData), &vData->uv);
1504
1505 if (gpu_state.cur_texture_quality == 0) {
1506 // force disable textures
1507 dglDisableClientState(GL_TEXTURE_COORD_ARRAY);
1508 }
1509
1510 oglClientActiveTextureARB(GL_TEXTURE0_ARB + 1);
1511 dglDisableClientState(GL_TEXTURE_COORD_ARRAY);
1512
1513 // draw the data in the arrays
1514 dglDrawArrays(GL_POLYGON, 0, nv);
1515
1516 if (gpu_state.cur_texture_quality == 0) {
1517 // re-enable textures
1518 oglClientActiveTextureARB(GL_TEXTURE0_ARB + 0);
1519 dglEnableClientState(GL_TEXTURE_COORD_ARRAY);
1520 }
1521
1522 OpenGL_polys_drawn++;
1523 OpenGL_verts_processed += nv;
1524 }
1525
1526 void gpu_RenderPolygonUV2(PosColorUV2Vertex *vData, uint32_t nv) {
1527 dglVertexPointer(3, GL_FLOAT, sizeof(*vData), &vData->pos);
1528 dglColorPointer(4, GL_FLOAT, sizeof(*vData), &vData->color);
1529 oglClientActiveTextureARB(GL_TEXTURE0_ARB + 0);
1530 dglTexCoordPointer(4, GL_FLOAT, sizeof(*vData), &vData->uv0);
1531 oglClientActiveTextureARB(GL_TEXTURE0_ARB + 1);
1532 dglEnableClientState(GL_TEXTURE_COORD_ARRAY);
1533 dglTexCoordPointer(4, GL_FLOAT, sizeof(*vData), &vData->uv1);
1534
1535 dglDrawArrays(GL_POLYGON, 0, nv);
1536 OpenGL_polys_drawn++;
1537 OpenGL_verts_processed += nv;
1538
1539 CHECK_ERROR(10)
1540 }
1541
1542 void rend_SetFlatColor(ddgr_color color) { gpu_state.cur_color = color; }
1543
1544 // Sets the fog state to TRUE or FALSE
1545 void rend_SetFogState(int8_t state) {
1546 if (state == gpu_state.cur_fog_state)
1547 return;
1548
1549 gpu_state.cur_fog_state = state;
1550 if (state == 1) {
1551 dglEnable(GL_FOG);
1552 } else {
1553 dglDisable(GL_FOG);
1554 }
1555 }
1556
1557 // Sets the near and far plane of fog
1558 void rend_SetFogBorders(float nearz, float farz) {
1559 // Sets the near and far plane of fog
1560 float fogStart = nearz;
1561 float fogEnd = farz;
1562
1563 gpu_state.cur_fog_start = fogStart;
1564 gpu_state.cur_fog_end = fogEnd;
1565
1566 dglFogi(GL_FOG_MODE, GL_LINEAR);
1567 dglFogf(GL_FOG_START, fogStart);
1568 dglFogf(GL_FOG_END, fogEnd);
1569 }
1570
1571 void rend_SetRendererType(renderer_type state) {
1572 Renderer_type = state;
1573 mprintf(0, "RendererType is set to %d.\n", state);
1574 }
1575
1576 void rend_SetLighting(light_state state) {
1577 if (state == gpu_state.cur_light_state)
1578 return; // No redundant state setting
1579 #if (defined(_USE_OGL_ACTIVE_TEXTURES))
1580 if (UseMultitexture && Last_texel_unit_set != 0) {
1581 oglActiveTextureARB(GL_TEXTURE0_ARB + 0);
1582 Last_texel_unit_set = 0;
1583 }
1584 #endif
1585
1586 OpenGL_sets_this_frame[4]++;
1587
1588 switch (state) {
1589 case LS_NONE:
1590 dglShadeModel(GL_SMOOTH);
1591 gpu_state.cur_light_state = LS_NONE;
1592 break;
1593 case LS_FLAT_GOURAUD:
1594 dglShadeModel(GL_SMOOTH);
1595 gpu_state.cur_light_state = LS_FLAT_GOURAUD;
1596 break;
1597 case LS_GOURAUD:
1598 case LS_PHONG:
1599 dglShadeModel(GL_SMOOTH);
1600 gpu_state.cur_light_state = LS_GOURAUD;
1601 break;
1602 default:
1603 Int3();
1604 break;
1605 }
1606
1607 CHECK_ERROR(13)
1608 }
1609
1610 void rend_SetColorModel(color_model state) {
1611 switch (state) {
1612 case CM_MONO:
1613 gpu_state.cur_color_model = CM_MONO;
1614 break;
1615 case CM_RGB:
1616 gpu_state.cur_color_model = CM_RGB;
1617 break;
1618 default:
1619 Int3();
1620 break;
1621 }
1622 }
1623
1624 void rend_SetTextureType(texture_type state) {
1625 if (state == gpu_state.cur_texture_type)
1626 return; // No redundant state setting
1627 #if (defined(_USE_OGL_ACTIVE_TEXTURES))
1628 if (UseMultitexture && Last_texel_unit_set != 0) {
1629 oglActiveTextureARB(GL_TEXTURE0_ARB + 0);
1630 Last_texel_unit_set = 0;
1631 }
1632 #endif
1633 OpenGL_sets_this_frame[3]++;
1634
1635 switch (state) {
1636 case TT_FLAT:
1637 dglDisable(GL_TEXTURE_2D);
1638 gpu_state.cur_texture_quality = 0;
1639 break;
1640 case TT_LINEAR:
1641 case TT_LINEAR_SPECIAL:
1642 case TT_PERSPECTIVE:
1643 case TT_PERSPECTIVE_SPECIAL:
1644 dglEnable(GL_TEXTURE_2D);
1645 gpu_state.cur_texture_quality = 2;
1646 break;
1647 default:
1648 Int3(); // huh? Get Jason
1649 break;
1650 }
1651
1652 CHECK_ERROR(12)
1653 gpu_state.cur_texture_type = state;
1654 }
1655
1656 void rend_StartFrame(int x1, int y1, int x2, int y2, int clear_flags) {
1657 if (clear_flags & RF_CLEAR_ZBUFFER) {
1658 dglClear(GL_DEPTH_BUFFER_BIT);
1659 }
1660 gpu_state.clip_x1 = x1;
1661 gpu_state.clip_y1 = y1;
1662 gpu_state.clip_x2 = x2;
1663 gpu_state.clip_y2 = y2;
1664 }
1665
1666
1667 // Flips the screen
1668 void rend_Flip(void) {
1669 #ifndef RELEASE
1670 int i;
1671
1672 RTP_INCRVALUE(texture_uploads, OpenGL_uploads);
1673 RTP_INCRVALUE(polys_drawn, OpenGL_polys_drawn);
1674
1675 mprintf_at(1, 1, 0, "Uploads=%d Polys=%d Verts=%d ", OpenGL_uploads, OpenGL_polys_drawn, OpenGL_verts_processed);
1676 mprintf_at(1, 2, 0, "Sets= 0:%d 1:%d 2:%d 3:%d ", OpenGL_sets_this_frame[0], OpenGL_sets_this_frame[1],
1677 OpenGL_sets_this_frame[2], OpenGL_sets_this_frame[3]);
1678 mprintf_at(1, 3, 0, "Sets= 4:%d 5:%d ", OpenGL_sets_this_frame[4], OpenGL_sets_this_frame[5]);
1679 for (i = 0; i < 10; i++) {
1680 OpenGL_sets_this_frame[i] = 0;
1681 }
1682 #endif
1683
1684 gpu_last_frame_polys_drawn = OpenGL_polys_drawn;
1685 gpu_last_frame_verts_processed = OpenGL_verts_processed;
1686 gpu_last_uploaded = OpenGL_uploads;
1687
1688 OpenGL_uploads = 0;
1689 OpenGL_polys_drawn = 0;
1690 OpenGL_verts_processed = 0;
1691
1692 // if we're rendering to an FBO, scale to the window framebuffer!
1693 if (GOpenGLFBO != 0) {
1694 int w, h;
1695 SDL_GL_GetDrawableSize(GSDLWindow, &w, &h);
1696
1697 int scaledHeight, scaledWidth;
1698 if (w < h) {
1699 scaledWidth = w;
1700 scaledHeight = (int) (((((double)GOpenGLFBOHeight) / ((double)GOpenGLFBOWidth))) * ((double)w));
1701 } else {
1702 scaledHeight = h;
1703 scaledWidth = (int) (((((double)GOpenGLFBOWidth) / ((double)GOpenGLFBOHeight))) * ((double)h));
1704 }
1705
1706 const int centeredX = (w - scaledWidth) / 2;
1707 const int centeredY = (h - scaledHeight) / 2;
1708
1709 dglBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1710 dglClearColor (0.0f, 0.0f, 0.0f, 1.0f);
1711 dglClear(GL_COLOR_BUFFER_BIT); // in case the Steam Overlay wrote to places we don't blit over.
1712 dglBlitFramebufferEXT(0, 0, GOpenGLFBOWidth, GOpenGLFBOHeight,
1713 centeredX, centeredY, centeredX + scaledWidth, centeredY + scaledHeight,
1714 GL_COLOR_BUFFER_BIT, GL_LINEAR);
1715 dglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1716 }
1717
1718 SDL_GL_SwapWindow(GSDLWindow);
1719
1720 // go back to drawing on the FBO until we want to blit to the window framebuffer again.
1721 if (GOpenGLFBO != 0) {
1722 dglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, GOpenGLFBO);
1723 dglViewport(0, 0, GOpenGLFBOWidth, GOpenGLFBOHeight);
1724 dglScissor(0, 0, GOpenGLFBOWidth, GOpenGLFBOHeight);
1725 }
1726 }
1727
1728 void rend_EndFrame(void) {}
1729
1730 // Sets the state of z-buffering to on or off
1731 void rend_SetZBufferState(int8_t state) {
1732 if (state == gpu_state.cur_zbuffer_state)
1733 return; // No redundant state setting
1734
1735 OpenGL_sets_this_frame[5]++;
1736 gpu_state.cur_zbuffer_state = state;
1737
1738 // mprintf(0,"OPENGL: Setting zbuffer state to %d.\n",state);
1739
1740 if (state) {
1741 dglEnable(GL_DEPTH_TEST);
1742 dglDepthFunc(GL_LEQUAL);
1743 } else {
1744 dglDisable(GL_DEPTH_TEST);
1745 }
1746
1747 CHECK_ERROR(14)
1748 }
1749
1750 // Clears the display to a specified color
1751 void rend_ClearScreen(ddgr_color color) {
1752 int r = (color >> 16 & 0xFF);
1753 int g = (color >> 8 & 0xFF);
1754 int b = (color & 0xFF);
1755
1756 dglClearColor((float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, 0);
1757
1758 dglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1759 }
1760
1761 // Clears the zbuffer for the screen
1762 void rend_ClearZBuffer(void) { dglClear(GL_DEPTH_BUFFER_BIT); }
1763
1764 // Clears the zbuffer for the screen
1765 void rend_ResetCache(void) {
1766 mprintf(0, "Resetting texture cache!\n");
1767 opengl_ResetCache();
1768 }
1769
1770 // Fills a rectangle on the display
1771 void rend_FillRect(ddgr_color color, int x1, int y1, int x2, int y2) {
1772 int r = GR_COLOR_RED(color);
1773 int g = GR_COLOR_GREEN(color);
1774 int b = GR_COLOR_BLUE(color);
1775
1776 int width = x2 - x1;
1777 int height = y2 - y1;
1778
1779 x1 += gpu_state.clip_x1;
1780 y1 += gpu_state.clip_y1;
1781
1782 dglEnable(GL_SCISSOR_TEST);
1783 dglScissor(x1, gpu_state.screen_height - (height + y1), width, height);
1784 dglClearColor((float)r / 255.0, (float)g / 255.0, (float)b / 255.0, 0);
1785 dglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1786
1787 width = gpu_state.clip_x2 - gpu_state.clip_x1;
1788 height = gpu_state.clip_y2 - gpu_state.clip_y1;
1789
1790 dglScissor(gpu_state.clip_x1, gpu_state.screen_height - (gpu_state.clip_y1 + height), width, height);
1791 dglDisable(GL_SCISSOR_TEST);
1792 }
1793
1794 // Sets a pixel on the display
1795 void rend_SetPixel(ddgr_color color, int x, int y) {
1796 int r = (color >> 16 & 0xFF);
1797 int g = (color >> 8 & 0xFF);
1798 int b = (color & 0xFF);
1799
1800 g3_RefreshTransforms(true);
1801
1802 dglColor3ub(r, g, b);
1803
1804 dglBegin(GL_POINTS);
1805 dglVertex2i(x, y);
1806 dglEnd();
1807 }
1808
1809 // Sets a pixel on the display
1810 ddgr_color rend_GetPixel(int x, int y) {
1811 ddgr_color color[4];
1812 dglReadPixels(x, (gpu_state.screen_height - 1) - y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)color);
1813 return color[0];
1814 }
1815
1816 // Draws a line
1817 void rend_DrawLine(int x1, int y1, int x2, int y2) {
1818 int8_t atype;
1819 light_state ltype;
1820 texture_type ttype;
1821 int color = gpu_state.cur_color;
1822
1823 g3_RefreshTransforms(true);
1824
1825 int r = GR_COLOR_RED(color);
1826 int g = GR_COLOR_GREEN(color);
1827 int b = GR_COLOR_BLUE(color);
1828
1829 atype = gpu_state.cur_alpha_type;
1830 ltype = gpu_state.cur_light_state;
1831 ttype = gpu_state.cur_texture_type;
1832
1833 rend_SetAlphaType(AT_ALWAYS);
1834 rend_SetLighting(LS_NONE);
1835 rend_SetTextureType(TT_FLAT);
1836
1837 // TODO: Generalize
1838 dglBegin(GL_LINES);
1839 dglColor4ub(r, g, b, 255);
1840 dglVertex2i(x1 + gpu_state.clip_x1, y1 + gpu_state.clip_y1);
1841 dglColor4ub(r, g, b, 255);
1842 dglVertex2i(x2 + gpu_state.clip_x1, y2 + gpu_state.clip_y1);
1843 dglEnd();
1844
1845 rend_SetAlphaType(atype);
1846 rend_SetLighting(ltype);
1847 rend_SetTextureType(ttype);
1848 }
1849
1850 // Sets the color of fog
1851 void rend_SetFogColor(ddgr_color color) {
1852 if (color == gpu_state.cur_fog_color)
1853 return;
1854
1855 float fc[4];
1856 fc[0] = GR_COLOR_RED(color);
1857 fc[1] = GR_COLOR_GREEN(color);
1858 fc[2] = GR_COLOR_BLUE(color);
1859 fc[3] = 1;
1860
1861 fc[0] /= 255.0f;
1862 fc[1] /= 255.0f;
1863 fc[2] /= 255.0f;
1864
1865 dglFogfv(GL_FOG_COLOR, fc);
1866 }
1867
1868 // Sets the lighting state of opengl
1869 void rend_SetLightingState(light_state state) {
1870 if (state == gpu_state.cur_light_state)
1871 return; // No redundant state setting
1872
1873 if (UseMultitexture && Last_texel_unit_set != 0) {
1874 #if (defined(_USE_OGL_ACTIVE_TEXTURES))
1875 oglActiveTextureARB(GL_TEXTURE0_ARB + 0);
1876 Last_texel_unit_set = 0;
1877 #endif
1878 }
1879
1880 OpenGL_sets_this_frame[4]++;
1881
1882 switch (state) {
1883 case LS_NONE:
1884 dglShadeModel(GL_SMOOTH);
1885 gpu_state.cur_light_state = LS_NONE;
1886 break;
1887 case LS_FLAT_GOURAUD:
1888 dglShadeModel(GL_SMOOTH);
1889 gpu_state.cur_light_state = LS_FLAT_GOURAUD;
1890 break;
1891 case LS_GOURAUD:
1892 case LS_PHONG:
1893 dglShadeModel(GL_SMOOTH);
1894 gpu_state.cur_light_state = LS_GOURAUD;
1895 break;
1896 default:
1897 Int3();
1898 break;
1899 }
1900
1901 CHECK_ERROR(13)
1902 }
1903
1904 void rend_SetAlphaType(int8_t atype) {
1905 if (atype == gpu_state.cur_alpha_type)
1906 return; // don't set it redundantly
1907 #if (defined(_USE_OGL_ACTIVE_TEXTURES))
1908 if (UseMultitexture && Last_texel_unit_set != 0) {
1909 oglActiveTextureARB(GL_TEXTURE0_ARB + 0);
1910 Last_texel_unit_set = 0;
1911 }
1912 #endif
1913 OpenGL_sets_this_frame[6]++;
1914
1915 if (atype == AT_ALWAYS) {
1916 if (opengl_Blending_on) {
1917 dglDisable(GL_BLEND);
1918 opengl_Blending_on = false;
1919 }
1920 } else {
1921 if (!opengl_Blending_on) {
1922 dglEnable(GL_BLEND);
1923 opengl_Blending_on = true;
1924 }
1925 }
1926
1927 switch (atype) {
1928 case AT_ALWAYS:
1929 case AT_TEXTURE:
1930 rend_SetAlphaValue(255);
1931 dglBlendFunc(GL_ONE, GL_ZERO);
1932 break;
1933 case AT_CONSTANT:
1934 case AT_CONSTANT_TEXTURE:
1935 case AT_VERTEX:
1936 case AT_CONSTANT_TEXTURE_VERTEX:
1937 case AT_CONSTANT_VERTEX:
1938 case AT_TEXTURE_VERTEX:
1939 dglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1940 break;
1941 case AT_LIGHTMAP_BLEND:
1942 dglBlendFunc(GL_DST_COLOR, GL_ZERO);
1943 break;
1944 case AT_SATURATE_TEXTURE:
1945 case AT_LIGHTMAP_BLEND_SATURATE:
1946 case AT_SATURATE_VERTEX:
1947 case AT_SATURATE_CONSTANT_VERTEX:
1948 case AT_SATURATE_TEXTURE_VERTEX:
1949 dglBlendFunc(GL_SRC_ALPHA, GL_ONE);
1950 break;
1951 case AT_SPECULAR:
1952 break;
1953 default:
1954 Int3(); // no type defined,get jason
1955 break;
1956 }
1957 gpu_state.cur_alpha_type = atype;
1958 gpu_Alpha_multiplier = rend_GetAlphaMultiplier();
1959 CHECK_ERROR(15)
1960 }
1961
1962 // Draws a line using the states of the renderer
1963 void rend_DrawSpecialLine(g3Point *p0, g3Point *p1) {
1964 g3_RefreshTransforms(true);
1965
1966 int x_add = gpu_state.clip_x1;
1967 int y_add = gpu_state.clip_y1;
1968 float fr, fg, fb, alpha;
1969 int i;
1970
1971 fr = GR_COLOR_RED(gpu_state.cur_color);
1972 fg = GR_COLOR_GREEN(gpu_state.cur_color);
1973 fb = GR_COLOR_BLUE(gpu_state.cur_color);
1974
1975 fr /= 255.0f;
1976 fg /= 255.0f;
1977 fb /= 255.0f;
1978
1979 alpha = gpu_Alpha_multiplier * gpu_Alpha_factor;
1980
1981 // And draw!
1982 dglBegin(GL_LINES);
1983 for (i = 0; i < 2; i++) {
1984 g3Point *pnt = p0;
1985
1986 if (i == 1)
1987 pnt = p1;
1988
1989 if (gpu_state.cur_alpha_type & ATF_VERTEX)
1990 alpha = pnt->p3_a * gpu_Alpha_multiplier * gpu_Alpha_factor;
1991
1992 // If we have a lighting model, apply the correct lighting!
1993 if (gpu_state.cur_light_state != LS_NONE) {
1994 if (gpu_state.cur_light_state == LS_FLAT_GOURAUD) {
1995 dglColor4f(fr, fg, fb, alpha);
1996 } else {
1997 // Do lighting based on intesity (MONO) or colored (RGB)
1998 if (gpu_state.cur_color_model == CM_MONO)
1999 dglColor4f(pnt->p3_l, pnt->p3_l, pnt->p3_l, alpha);
2000 else {
2001 dglColor4f(pnt->p3_r, pnt->p3_g, pnt->p3_b, alpha);
2002 }
2003 }
2004 } else {
2005 dglColor4f(fr, fg, fb, alpha);
2006 }
2007
2008 // Finally, specify a vertex
2009 float z = std::clamp(1.0 - (1.0 / (pnt->p3_z + Z_bias)), 0.0, 1.0);
2010 dglVertex3f(pnt->p3_sx + x_add, pnt->p3_sy + y_add, -z);
2011 }
2012
2013 dglEnd();
2014 }
2015
2016 // Takes a screenshot of the current frame and puts it into the handle passed
2017 std::unique_ptr<NewBitmap> rend_Screenshot() {
2018 uint16_t *dest_data;
2019 uint32_t *temp_data;
2020
2021 int total = gpu_state.screen_width * gpu_state.screen_height;
2022 auto result = std::make_unique<NewBitmap>(gpu_state.screen_width, gpu_state.screen_height, PixelDataFormat::RGBA32, true);
2023
2024 if (!result || result->getData() == nullptr) {
2025 return nullptr;
2026 }
2027
2028 dglReadPixels(0, 0, gpu_state.screen_width, gpu_state.screen_height, GL_RGBA, GL_UNSIGNED_BYTE,
2029 (GLvoid *)result->getData());
2030
2031 return result;
2032 }
2033
2034 // Takes a screenshot of the current frame and puts it into the handle passed
2035 void rend_Screenshot(int bm_handle) {
2036 auto screenshot = rend_Screenshot();
2037 auto *temp_data = reinterpret_cast<uint32_t*>(screenshot->getData());
2038
2039 uint32_t w, h;
2040 screenshot->getSize(w, h);
2041
2042 ASSERT((bm_w(bm_handle, 0)) == gpu_state.screen_width);
2043 ASSERT((bm_h(bm_handle, 0)) == gpu_state.screen_height);
2044
2045 uint16_t* dest_data = bm_data(bm_handle, 0);
2046
2047 for (int i = 0; i < h; i++) {
2048 for (int t = 0; t < w; t++) {
2049 uint32_t spix = temp_data[i * w + t];
2050
2051 int r = spix & 0xff;
2052 int g = (spix >> 8) & 0xff;
2053 int b = (spix >> 16) & 0xff;
2054
2055 dest_data[(((h - 1) - i) * w) + t] = GR_RGB16(r, g, b);
2056 }
2057 }
2058 }
2059
2060 // Enables/disables writes the depth buffer
2061 void rend_SetZBufferWriteMask(int state) {
2062 OpenGL_sets_this_frame[5]++;
2063 if (state) {
2064 dglDepthMask(GL_TRUE);
2065 } else {
2066 dglDepthMask(GL_FALSE);
2067 }
2068 }
2069
2070 int rend_ReInit() {
2071 opengl_Close(true);
2072 return opengl_Init(NULL, &gpu_preferred_state);
2073 }
2074
2075 // Takes a bitmap and blits it to the screen using linear frame buffer stuff
2076 // X and Y are the destination X,Y
2077 void rend_CopyBitmapToFramebuffer(int bm_handle, int x, int y) {
2078 ASSERT(opengl_Framebuffer_ready);
2079
2080 if (opengl_Framebuffer_ready == 1) {
2081 bm_CreateChunkedBitmap(bm_handle, &opengl_Chunked_bitmap);
2082 opengl_Framebuffer_ready = 2;
2083 } else {
2084 opengl_ChangeChunkedBitmap(bm_handle, &opengl_Chunked_bitmap);
2085 }
2086
2087 rend_DrawChunkedBitmap(&opengl_Chunked_bitmap, 0, 0, 255);
2088 }
2089
2090 // Gets a renderer ready for a framebuffer copy, or stops a framebuffer copy
2091 void rend_SetFrameBufferCopyState(bool state) {
2092 if (state) {
2093 ASSERT(opengl_Framebuffer_ready == 0);
2094 opengl_Framebuffer_ready = 1;
2095 } else {
2096 ASSERT(opengl_Framebuffer_ready != 0);
2097 opengl_Framebuffer_ready = 0;
2098
2099 if (opengl_Framebuffer_ready == 2) {
2100 bm_DestroyChunkedBitmap(&opengl_Chunked_bitmap);
2101 opengl_ResetCache();
2102 }
2103 }
2104 }
2105
2106 // Gets OpenGL ready to work in a window
2107 int rend_InitOpenGLWindow(oeApplication *app, renderer_preferred_state *pref_state) {
2108 WindowGL = 1;
2109 return opengl_Init(app, pref_state);
2110 }
2111
2112 // Shuts down OpenGL in a window
2113 void rend_CloseOpenGLWindow(void) {
2114 opengl_Close();
2115 WindowGL = 0;
2116 OpenGL_window_initted = 0;
2117 mprintf(1, "SHUTTING DOWN WINDOWED OPENGL!");
2118 }
2119
2120 // Sets the hardware bias level for coplanar polygons
2121 // This helps reduce z buffer artifacts
2122 void rend_SetCoplanarPolygonOffset(float factor) {
2123 if (factor == 0.0f) {
2124 dglDisable(GL_POLYGON_OFFSET_FILL);
2125 } else {
2126 dglEnable(GL_POLYGON_OFFSET_FILL);
2127 dglPolygonOffset(-1.0f, -1.0f);
2128 }
2129 }
2130
2131 // returns the direct draw object
2132 void *rend_RetrieveDirectDrawObj(void **frontsurf, void **backsurf) {
2133 *frontsurf = NULL;
2134 *backsurf = NULL;
2135 return NULL;
2136 }
2137
2138 void rend_TransformSetToPassthru(void) {
2139 int width = gpu_state.screen_width;
2140 int height = gpu_state.screen_height;
2141
2142 // TODO: Generalize
2143 // Projection
2144 dglMatrixMode(GL_PROJECTION);
2145 dglLoadIdentity();
2146 dglOrtho((GLfloat)0.0f, (GLfloat)(width), (GLfloat)(height), (GLfloat)0.0f, 0.0f, 1.0f);
2147
2148 // Viewport
2149 dglViewport(0, 0, width, height);
2150 dglScissor(0, 0, width, height);
2151
2152 // ModelView
2153 dglMatrixMode(GL_MODELVIEW);
2154 dglLoadIdentity();
2155 }
2156
2157 void rend_TransformSetViewport(int lx, int ty, int width, int height) {
2158 dglViewport(lx, gpu_state.screen_height - (ty + height - 1), width, height);
2159 }
2160
2161 void rend_TransformSetProjection(float trans[4][4]) {
2162 dglMatrixMode(GL_PROJECTION);
2163 dglLoadMatrixf(&trans[0][0]);
2164 }
2165
2166 void rend_TransformSetModelView(float trans[4][4]) {
2167 dglMatrixMode(GL_MODELVIEW);
2168 dglLoadMatrixf(&trans[0][0]);
2169 }