]> icculus.org git repositories - btb/d2x.git/blob - arch/ogl/gr.c
add gamma cvar
[btb/d2x.git] / arch / ogl / gr.c
1 /*
2  *
3  * OGL video functions. - Added 9/15/99 Matthew Mueller
4  *
5  *
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <conf.h>
10 #endif
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #ifdef _MSC_VER
16 #include <windows.h>
17 #endif
18
19 #if !defined(_MSC_VER) && !defined(macintosh)
20 #include <unistd.h>
21 #endif
22 #if !defined(macintosh)
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #endif
27
28 #include <errno.h>
29
30 #include "hudmsg.h"
31 #include "inferno.h"
32 #include "text.h"
33 #include "gr.h"
34 #include "gamefont.h"
35 #include "grdef.h"
36 #include "palette.h"
37 #include "u_mem.h"
38 #include "error.h"
39
40 #include "inferno.h"
41 #include "screens.h"
42 #include "timer.h"
43 #include "strutil.h"
44 #include "mono.h"
45 #include "args.h"
46 #include "key.h"
47 #include "physfsx.h"
48
49 #define DECLARE_VARS
50 #include "internal.h"
51 #if defined(__APPLE__) && defined(__MACH__)
52 #include <OpenGL/glu.h>
53 #undef GL_ARB_multitexture // hack!
54 #else
55 #include <GL/glu.h>
56 #endif
57
58 int ogl_voodoohack=0;
59
60 int gr_installed = 0;
61
62
63 void gr_palette_clear(); // Function prototype for gr_init;
64 int gl_initialized=0;
65 int gl_reticle = 0;
66
67 int ogl_fullscreen=0;
68
69 int gr_check_fullscreen(void){
70         return ogl_fullscreen;
71 }
72
73 void gr_do_fullscreen(int f){
74         if (ogl_voodoohack)
75                 ogl_fullscreen=1;//force fullscreen mode on voodoos.
76         else
77                 ogl_fullscreen=f;
78         if (gl_initialized){
79                 ogl_do_fullscreen_internal();
80         }
81 }
82
83 int gr_toggle_fullscreen(void){
84         gr_do_fullscreen(!ogl_fullscreen);
85         //      grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
86         return ogl_fullscreen;
87 }
88
89 int arch_toggle_fullscreen_menu(void){
90         unsigned char *buf=NULL;
91
92         if (ogl_readpixels_ok){
93                 MALLOC(buf,unsigned char,grd_curscreen->sc_w*grd_curscreen->sc_h*3);
94                 glReadBuffer(GL_FRONT);
95                 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
96         }
97
98         gr_do_fullscreen(!ogl_fullscreen);
99
100         if (ogl_readpixels_ok){
101 //              glWritePixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
102                 glRasterPos2f(0,0);
103                 glDrawPixels(grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
104                 free(buf);
105         }
106         //      grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
107
108         return ogl_fullscreen;
109 }
110
111 void ogl_init_state(void){
112         /* select clearing (background) color   */
113         glClearColor(0.0, 0.0, 0.0, 0.0);
114         glShadeModel(GL_SMOOTH);
115
116         /* initialize viewing values */
117         glMatrixMode(GL_PROJECTION);
118         glLoadIdentity();
119         glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
120         glScalef(1.0, -1.0, 1.0);
121         glTranslatef(0.0, -1.0, 0.0);
122         gr_palette_step_up(0,0,0);//in case its left over from in game
123 }
124
125 int last_screen_mode=-1;
126
127 void ogl_set_screen_mode(void){
128         if (last_screen_mode==Screen_mode)
129                 return;
130         OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
131 //      OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,grd_curcanv->cv_bitmap.bm_w,grd_curcanv->cv_bitmap.bm_h);
132         if (Screen_mode==SCREEN_GAME){
133                 glDrawBuffer(GL_BACK);
134         }else{
135                 glClearColor(0.0, 0.0, 0.0, 0.0);
136                 glDrawBuffer(GL_FRONT);
137                 glClear(GL_COLOR_BUFFER_BIT);
138                 glMatrixMode(GL_PROJECTION);
139                 glLoadIdentity();//clear matrix
140                 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
141                 glMatrixMode(GL_MODELVIEW);
142                 glLoadIdentity();//clear matrix
143                 glEnable(GL_BLEND);
144                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
145         }
146         last_screen_mode=Screen_mode;
147 }
148
149 void gr_update()
150 {
151         if (gl_initialized){
152
153                 if(Screen_mode != SCREEN_GAME){
154                         glFlush();
155                 }
156         }
157 }
158
159 const char *gl_vendor, *gl_renderer, *gl_version, *gl_extensions;
160
161 void ogl_get_verinfo(void)
162 {
163         long t, sgi_max_textures = -1;
164         GLint arb_max_textures = -1;
165 #ifdef GL_NV_register_combiners
166         long nv_register_combiners = -1;
167 #endif
168         float anisotropic_max = 0;
169
170         gl_vendor = (const char *) glGetString (GL_VENDOR);
171         gl_renderer = (const char *) glGetString (GL_RENDERER);
172         gl_version = (const char *) glGetString (GL_VERSION);
173         gl_extensions = (const char *) glGetString (GL_EXTENSIONS);
174
175         con_printf(CON_VERBOSE, "OpenGL: vendor: %s\nOpenGL: renderer: %s\nOpenGL: version: %s\n",gl_vendor,gl_renderer,gl_version);
176
177         ogl_intensity4_ok = 1;
178         ogl_luminance4_alpha4_ok = 1;
179         ogl_rgba2_ok = 1;
180         ogl_gettexlevelparam_ok = 1;
181         ogl_setgammaramp_ok = 1;
182
183 #ifdef WGL_VIDEO
184         dglMultiTexCoord2fARB = (glMultiTexCoord2fARB_fp)wglGetProcAddress("glMultiTexCoord2fARB");
185         dglActiveTextureARB = (glActiveTextureARB_fp)wglGetProcAddress("glActiveTextureARB");
186         dglMultiTexCoord2fSGIS = (glMultiTexCoord2fSGIS_fp)wglGetProcAddress("glMultiTexCoord2fSGIS");
187         dglSelectTextureSGIS = (glSelectTextureSGIS_fp)wglGetProcAddress("glSelectTextureSGIS");
188         dglColorTableEXT = (glColorTableEXT_fp)wglGetProcAddress("glColorTableEXT");
189         dglCombinerParameteriNV = (glCombinerParameteriNV_fp)wglGetProcAddress("glCombinerParameteriNV");
190         dglCombinerInputNV = (glCombinerInputNV_fp)wglGetProcAddress("glCombinerInputNV");
191         dglCombinerOutputNV = (glCombinerOutputNV_fp)wglGetProcAddress("glCombinerOutputNV");
192         dglFinalCombinerInputNV = (glFinalCombinerInputNV_fp)wglGetProcAddress("glFinalCombinerInputNV");
193 #endif
194
195 #ifdef GL_ARB_multitexture
196         ogl_arb_multitexture_ok = (strstr(gl_extensions, "GL_ARB_multitexture") != 0 && glActiveTextureARB != 0);
197         mprintf((0,"c:%p d:%p e:%p\n",strstr(gl_extensions,"GL_ARB_multitexture"),glActiveTextureARB,glBegin));
198 #endif
199 #ifdef GL_SGIS_multitexture
200         ogl_sgis_multitexture_ok = (strstr(gl_extensions, "GL_SGIS_multitexture") != 0 && glSelectTextureSGIS != 0);
201         mprintf((0,"a:%p b:%p\n",strstr(gl_extensions,"GL_SGIS_multitexture"),glSelectTextureSGIS));
202 #endif
203         ogl_nv_texture_env_combine4_ok = (strstr(gl_extensions, "GL_NV_texture_env_combine4") != 0);
204 #ifdef GL_NV_register_combiners
205         ogl_nv_register_combiners_ok=(strstr(gl_extensions,"GL_NV_register_combiners")!=0 && glCombinerOutputNV!=0);
206 #endif
207
208         ogl_ext_texture_filter_anisotropic_ok = (strstr(gl_extensions, "GL_EXT_texture_filter_anisotropic") != 0);
209         if (ogl_ext_texture_filter_anisotropic_ok)
210                 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &anisotropic_max);
211
212 #ifdef GL_EXT_paletted_texture
213         ogl_paletted_texture_ok = (strstr(gl_extensions, "GL_EXT_paletted_texture") != 0 && glColorTableEXT != 0);
214         ogl_shared_palette_ok = (strstr(gl_extensions, "GL_EXT_shared_texture_palette") != 0 && ogl_paletted_texture_ok);
215 #endif
216         //add driver specific hacks here.  whee.
217         if ((stricmp(gl_renderer,"Mesa NVIDIA RIVA 1.0\n")==0 || stricmp(gl_renderer,"Mesa NVIDIA RIVA 1.2\n")==0) && stricmp(gl_version,"1.2 Mesa 3.0")==0){
218                 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
219                 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
220                 ogl_gettexlevelparam_ok=0;//returns random data..
221         }
222         if (stricmp(gl_vendor,"Matrox Graphics Inc.")==0){
223                 //displays garbage. reported by
224                 //  redomen@crcwnet.com (render="Matrox G400" version="1.1.3 5.52.015")
225                 //  orulz (Matrox G200)
226                 ogl_intensity4_ok=0;
227         }
228 #ifdef macintosh
229         if (stricmp(gl_renderer,"3dfx Voodoo 3")==0){ // strangely, includes Voodoo 2
230                 ogl_gettexlevelparam_ok=0; // Always returns 0
231         }
232 #endif
233
234         //allow overriding of stuff.
235 #ifdef GL_ARB_multitexture
236         if ((t=FindArg("-gl_arb_multitexture_ok"))){
237                 ogl_arb_multitexture_ok=atoi(Args[t+1]);
238         }
239         if (ogl_arb_multitexture_ok)
240                 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &arb_max_textures);
241 #endif
242 #ifdef GL_SGIS_multitexture
243         if ((t=FindArg("-gl_sgis_multitexture_ok"))){
244                 ogl_sgis_multitexture_ok=atoi(Args[t+1]);
245         }
246         if (ogl_sgis_multitexture_ok)
247                 glGetIntegerv(GL_MAX_TEXTURES_SGIS, &sgi_max_textures);
248 #endif
249 #ifdef GL_NV_register_combiners
250         if ((t = FindArg("-gl_nv_register_combiners_ok")))
251         {
252                 ogl_nv_register_combiners_ok=atoi(Args[t + 1]);
253         }
254         if (ogl_nv_register_combiners_ok)
255                 glGetIntegerv(GL_MAX_GENERAL_COMBINERS_NV, &nv_register_combiners);
256 #endif
257 #ifdef GL_EXT_paletted_texture
258         if ((t = FindArg("-gl_paletted_texture_ok")))
259         {
260                 ogl_paletted_texture_ok = atoi(Args[t + 1]);
261         }
262         if ((t = FindArg("-gl_shared_palette_ok")))
263         {
264                 ogl_shared_palette_ok = atoi(Args[t + 1]);
265         }
266         ogl_shared_palette_ok = ogl_shared_palette_ok && ogl_paletted_texture_ok; // shared palettes require palette support in the first place, obviously ;)
267         printf("gl_paletted_texture: %i  gl_shared_palette: %i  (using paletted textures: %i)\n", ogl_paletted_texture_ok, ogl_shared_palette_ok, ogl_shared_palette_ok);
268 #endif
269         if ((t=FindArg("-gl_intensity4_ok"))){
270                 ogl_intensity4_ok=atoi(Args[t+1]);
271         }
272         if ((t=FindArg("-gl_luminance4_alpha4_ok"))){
273                 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
274         }
275         if ((t=FindArg("-gl_rgba2_ok"))){
276                 ogl_rgba2_ok=atoi(Args[t+1]);
277         }
278         if ((t=FindArg("-gl_readpixels_ok"))){
279                 ogl_readpixels_ok=atoi(Args[t+1]);
280         }
281         if ((t=FindArg("-gl_gettexlevelparam_ok"))){
282                 ogl_gettexlevelparam_ok=atoi(Args[t+1]);
283         }
284         if ((t=FindArg("-gl_setgammaramp_ok")))
285         {
286                 ogl_setgammaramp_ok = atoi(Args[t + 1]);
287         }
288
289         con_printf(CON_DEBUG, "gl_arb_multitexture:%i(%i units) gl_sgis_multitexture:%i(%i units) gl_nv_texture_env_combine4:%i\n", ogl_arb_multitexture_ok, arb_max_textures, ogl_sgis_multitexture_ok, sgi_max_textures, ogl_nv_texture_env_combine4_ok);
290 #ifdef GL_NV_register_combiners
291         con_printf(CON_DEBUG, "gl_nv_register_combiners:%i(%i stages)\n", ogl_nv_register_combiners_ok, nv_register_combiners);
292 #endif
293         con_printf(CON_DEBUG, "gl_intensity4:%i gl_luminance4_alpha4:%i gl_rgba2:%i gl_readpixels:%i gl_gettexlevelparam:%i gl_setgammaramp_ok:%i gl_ext_texture_filter_anisotropic:%i(%f max)\n", ogl_intensity4_ok, ogl_luminance4_alpha4_ok, ogl_rgba2_ok, ogl_readpixels_ok, ogl_gettexlevelparam_ok, ogl_setgammaramp_ok, ogl_ext_texture_filter_anisotropic_ok, anisotropic_max);
294 }
295
296
297 int gr_check_mode(uint32_t mode)
298 {
299         int w, h;
300
301         w = SM_W(mode);
302         h = SM_H(mode);
303         return ogl_check_mode(w, h); // platform specific code
304 }
305
306
307 extern uint32_t VGA_current_mode; // DPH: kludge - remove at all costs
308
309 int gr_set_mode(uint32_t mode)
310 {
311         short w, h;
312         char *gr_bm_data;
313
314 #ifdef NOGRAPH
315 return 0;
316 #endif
317 //      mode=0;
318         if (mode<=0)
319                 return 0;
320
321         if (mode == VGA_current_mode)
322                 return 0;
323
324         w=SM_W(mode);
325         h=SM_H(mode);
326         VGA_current_mode = mode;
327
328         //if (screen != NULL) gr_palette_clear();
329
330 //      ogl_init_state();
331         
332         gr_bm_data=(char *)grd_curscreen->sc_canvas.cv_bitmap.bm_data;//since we use realloc, we want to keep this pointer around.
333         memset( grd_curscreen, 0, sizeof(grs_screen));
334         grd_curscreen->sc_mode = mode;
335         grd_curscreen->sc_w = w;
336         grd_curscreen->sc_h = h;
337         grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
338         grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
339         grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
340         grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
341         grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
342         //grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
343         grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = w;
344         grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_OGL;
345         //grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
346 //      mprintf((0,"ogl/gr.c: reallocing %p to %i\n",grd_curscreen->sc_canvas.cv_bitmap.bm_data,w*h));
347         grd_curscreen->sc_canvas.cv_bitmap.bm_data = d_realloc(gr_bm_data,w*h);
348         gr_set_current_canvas(NULL);
349         //gr_enable_default_palette_loading();
350         
351         ogl_init_window(w,h);//platform specific code
352
353         ogl_get_verinfo();
354
355         OGL_VIEWPORT(0,0,w,h);
356
357         ogl_set_screen_mode();
358
359 //      gamefont_choose_game_font(w,h);
360         
361         return 0;
362 }
363
364 #define GLstrcmptestr(a,b) if (stricmp(a,#b)==0 || stricmp(a,"GL_" #b)==0)return GL_ ## b;
365 int ogl_atotexfilti(char *a,int min){
366         GLstrcmptestr(a,NEAREST);
367         GLstrcmptestr(a,LINEAR);
368         if (min){//mipmaps are valid only for the min filter
369                 GLstrcmptestr(a,NEAREST_MIPMAP_NEAREST);
370                 GLstrcmptestr(a,NEAREST_MIPMAP_LINEAR);
371                 GLstrcmptestr(a,LINEAR_MIPMAP_NEAREST);
372                 GLstrcmptestr(a,LINEAR_MIPMAP_LINEAR);
373         }
374         Error("unknown/invalid texture filter %s\n",a);
375 //      return GL_NEAREST;
376 }
377 int ogl_testneedmipmaps(int i){
378         switch (i){
379                 case GL_NEAREST:
380                 case GL_LINEAR:
381                         return 0;
382                 case GL_NEAREST_MIPMAP_NEAREST:
383                 case GL_NEAREST_MIPMAP_LINEAR:
384                 case GL_LINEAR_MIPMAP_NEAREST:
385                 case GL_LINEAR_MIPMAP_LINEAR:
386                         return 1;
387         }
388         Error("unknown texture filter %x\n",i);
389 //      return -1;
390 }
391
392
393 void gr_cmd_texturemode(int argc, char **argv)
394 {
395         if (argc < 2)
396                 return;
397
398         if (!stricmp(argv[1], "GL_NEAREST")) {
399                 GL_texminfilt = GL_NEAREST;
400                 GL_texmagfilt = GL_NEAREST;
401         } else if (!stricmp(argv[1], "GL_LINEAR")) {
402                 GL_texminfilt = GL_LINEAR;
403                 GL_texmagfilt = GL_LINEAR;
404         } else if (!stricmp(argv[1], "GL_NEAREST_MIPMAP_NEAREST")) {
405                 GL_texminfilt = GL_NEAREST_MIPMAP_NEAREST;
406                 GL_texmagfilt = GL_NEAREST;
407         } else if (!stricmp(argv[1], "GL_LINEAR_MIPMAP_NEAREST")) {
408                 GL_texminfilt = GL_LINEAR_MIPMAP_NEAREST;
409                 GL_texmagfilt = GL_LINEAR;
410         } else if (!stricmp(argv[1], "GL_NEAREST_MIPMAP_LINEAR")) {
411                 GL_texminfilt = GL_NEAREST_MIPMAP_LINEAR;
412                 GL_texmagfilt = GL_NEAREST;
413         } else if (!stricmp(argv[1], "GL_LINEAR_MIPMAP_LINEAR")) {
414                 GL_texminfilt = GL_LINEAR_MIPMAP_LINEAR;
415                 GL_texmagfilt = GL_LINEAR;
416         } else
417                 return;
418
419         GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
420
421         ogl_smash_texture_list_internal();
422 }
423
424
425 #ifdef OGL_RUNTIME_LOAD
426 #ifdef _WIN32
427 char *OglLibPath="opengl32.dll";
428 #endif
429 #ifdef __unix__
430 char *OglLibPath="libGL.so";
431 #endif
432 #ifdef macintosh
433 char *OglLibPath = NULL;
434 #endif
435
436 int ogl_rt_loaded=0;
437 int ogl_init_load_library(void)
438 {
439         int retcode=0;
440         if (!ogl_rt_loaded){
441                 int t;
442                 if ((t=FindArg("-gl_library")))
443                         OglLibPath=Args[t+1];
444
445                 retcode = OpenGL_LoadLibrary(true);
446                 if(retcode)
447                 {
448                         mprintf((0,"Opengl loaded ok\n"));
449         
450                         if(!glEnd)
451                         {
452                                 Error("Opengl: Functions not imported\n");
453                         }
454                 }else{
455                         Error("Opengl: error loading %s\n", OglLibPath? OglLibPath : SDL_GetError());
456                 }
457                 ogl_rt_loaded=1;
458         }
459         return retcode;
460 }
461 #endif
462
463 int gr_init()
464 {
465         int t, glt = 0;
466
467         // Only do this function once!
468         if (gr_installed==1)
469                 return -1;
470
471
472 #ifdef OGL_RUNTIME_LOAD
473         ogl_init_load_library();
474 #endif
475
476 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
477         if (FindArg("-gl_voodoo")){
478                 ogl_voodoohack=1;
479                 gr_toggle_fullscreen();
480         }
481         if (FindArg("-fullscreen"))
482                 gr_toggle_fullscreen();
483 #endif
484         if ((glt=FindArg("-gl_alttexmerge")))
485                 ogl_alttexmerge=1;
486         if ((t=FindArg("-gl_stdtexmerge")))
487                 if (t>=glt)//allow overriding of earlier args
488                         ogl_alttexmerge=0;
489
490         if ((glt = FindArg("-gl_16bittextures")))
491         {
492                 ogl_rgba_internalformat = GL_RGB5_A1;
493                 ogl_rgb_internalformat = GL_RGB5;
494         }
495
496         if ((glt=FindArg("-gl_mipmap"))){
497                 GL_texmagfilt=GL_LINEAR;
498                 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
499         }
500         if ((glt=FindArg("-gl_trilinear")))
501         {
502                 GL_texmagfilt = GL_LINEAR;
503                 GL_texminfilt = GL_LINEAR_MIPMAP_LINEAR;
504         }
505         if ((t=FindArg("-gl_simple"))){
506                 if (t>=glt){//allow overriding of earlier args
507                         glt=t;
508                         GL_texmagfilt=GL_NEAREST;
509                         GL_texminfilt=GL_NEAREST;
510                 }
511         }
512         if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
513                 if (t>=glt)//allow overriding of earlier args
514                         GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
515         }
516         if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
517                 if (t>=glt)//allow overriding of earlier args
518                         GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
519         }
520         GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
521
522         if ((t = FindArg("-gl_anisotropy")) || (t = FindArg("-gl_anisotropic")))
523         {
524                 GL_texanisofilt=atof(Args[t + 1]);
525         }
526
527         mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i anisotropic:%f\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps,GL_texanisofilt));
528
529         
530         if ((t=FindArg("-gl_vidmem"))){
531                 ogl_mem_target=atoi(Args[t+1])*1024*1024;
532         }
533         if ((t=FindArg("-gl_reticle"))){
534                 gl_reticle=atoi(Args[t+1]);
535         }
536         //printf("ogl_mem_target=%i\n",ogl_mem_target);
537         
538         ogl_init();//platform specific initialization
539
540         ogl_init_texture_list_internal();
541                 
542         MALLOC( grd_curscreen,grs_screen,1 );
543         memset( grd_curscreen, 0, sizeof(grs_screen));
544         grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
545         grd_curscreen->sc_canvas.cv_color = 0;
546         grd_curscreen->sc_canvas.cv_drawmode = 0;
547         grd_curscreen->sc_canvas.cv_font = NULL;
548         grd_curscreen->sc_canvas.cv_font_fg_color = 0;
549         grd_curscreen->sc_canvas.cv_font_bg_color = 0;
550         gr_set_current_canvas( &grd_curscreen->sc_canvas );
551
552         cmd_addcommand("gl_texturemode", gr_cmd_texturemode);
553
554         cvar_registervariable(&gr_palette_gamma);
555
556         gr_installed = 1;
557         
558         atexit(gr_close);
559
560         return 0;
561 }
562
563 void gr_close()
564 {
565 //      mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
566         ogl_brightness_r = ogl_brightness_g = ogl_brightness_b = 0;
567         ogl_setbrightness_internal();
568
569         ogl_close();//platform specific code
570         if (grd_curscreen){
571                 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
572                         d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
573                 d_free(grd_curscreen);
574         }
575 #ifdef OGL_RUNTIME_LOAD
576         if (ogl_rt_loaded)
577                 OpenGL_LoadLibrary(false);
578 #endif
579 }
580 extern int r_upixelc;
581 void ogl_upixelc(int x, int y, int c){
582         r_upixelc++;
583 //      printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
584 //      if(Function_mode != FMODE_GAME){
585 //              grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
586 //      }else{
587                 OGL_DISABLE(TEXTURE_2D);
588                 glPointSize(1.0);
589                 glBegin(GL_POINTS);
590 //              glBegin(GL_LINES);
591 //      ogl_pal=gr_current_pal;
592                 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
593 //      ogl_pal=gr_palette;
594                 glVertex2f((x + grd_curcanv->cv_bitmap.bm_x + 0.5) / (float)last_width, 1.0 - (y + grd_curcanv->cv_bitmap.bm_y + 0.5) / (float)last_height);
595 //              glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
596                 glEnd();
597 //      }
598 }
599 void ogl_urect(int left,int top,int right,int bot){
600         GLfloat xo,yo,xf,yf;
601         int c=COLOR;
602         
603         xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
604         xf = (right + 1 + grd_curcanv->cv_bitmap.bm_x) / (float)last_width;
605         yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
606         yf = 1.0 - (bot + 1 + grd_curcanv->cv_bitmap.bm_y) / (float)last_height;
607
608         OGL_DISABLE(TEXTURE_2D);
609         if (Gr_scanline_darkening_level >= GR_FADE_LEVELS)
610                 glColor3f(CPAL2Tr(c), CPAL2Tg(c), CPAL2Tb(c));
611         else
612                 glColor4f(CPAL2Tr(c), CPAL2Tg(c), CPAL2Tb(c), 1.0 - (float)Gr_scanline_darkening_level / ((float)GR_FADE_LEVELS - 1.0));
613         glBegin(GL_QUADS);
614         glVertex2f(xo,yo);
615         glVertex2f(xo,yf);
616         glVertex2f(xf,yf);
617         glVertex2f(xf,yo);
618         glEnd();
619 }
620 void ogl_ulinec(int left,int top,int right,int bot,int c){
621         GLfloat xo,yo,xf,yf;
622
623         xo = (left + grd_curcanv->cv_bitmap.bm_x + 0.5) / (float)last_width;
624         xf = (right + grd_curcanv->cv_bitmap.bm_x + 0.5) / (float)last_width;
625         yo = 1.0 - (top + grd_curcanv->cv_bitmap.bm_y + 0.5) / (float)last_height;
626         yf = 1.0 - (bot + grd_curcanv->cv_bitmap.bm_y + 0.5) / (float)last_height;
627
628         OGL_DISABLE(TEXTURE_2D);
629         glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
630         glBegin(GL_LINES);
631         glVertex2f(xo,yo);
632         glVertex2f(xf,yf);
633         glEnd();
634 }
635         
636
637 GLfloat last_r=0, last_g=0, last_b=0;
638 int do_pal_step=0;
639 void ogl_do_palfx(void){
640 //      GLfloat r,g,b,a;
641         OGL_DISABLE(TEXTURE_2D);
642         if (gr_palette_faded_out){
643 /*              glEnable(GL_BLEND);
644                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
645                 glColor3f(0,0,0);
646 //              r=g=b=0.0;a=1.0;
647         }else{
648                 if (do_pal_step){
649                         //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
650                         glEnable(GL_BLEND);
651                         glBlendFunc(GL_ONE,GL_ONE);
652                         glColor3f(last_r,last_g,last_b);
653 //                      r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
654                 }else
655                         return;
656         }
657         
658         
659         glBegin(GL_QUADS);
660         glVertex2f(0,0);
661         glVertex2f(0,1);
662         glVertex2f(1,1);
663         glVertex2f(1,0);
664         glEnd();
665         
666         glEnable(GL_BLEND);     
667         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
668 }
669
670 void gr_palette_clear()
671 {
672         gr_palette_faded_out=1;
673 }
674
675
676 int ogl_brightness_ok = 0;
677 int ogl_setgammaramp_ok = 1;
678 int ogl_brightness_r = 0, ogl_brightness_g = 0, ogl_brightness_b = 0;
679 static int old_b_r = 0, old_b_g = 0, old_b_b = 0;
680
681 void gr_palette_step_up(int r, int g, int b)
682 {
683         if (gr_palette_faded_out)
684                 return;
685
686         old_b_r = ogl_brightness_r;
687         old_b_g = ogl_brightness_g;
688         old_b_b = ogl_brightness_b;
689
690         ogl_brightness_r = max(r + gr_palette_gamma.intval, 0);
691         ogl_brightness_g = max(g + gr_palette_gamma.intval, 0);
692         ogl_brightness_b = max(b + gr_palette_gamma.intval, 0);
693
694         if (ogl_setgammaramp_ok &&
695             (old_b_r != ogl_brightness_r ||
696              old_b_g != ogl_brightness_g ||
697              old_b_b != ogl_brightness_b))
698                 ogl_brightness_ok = !ogl_setbrightness_internal();
699
700         if (!ogl_setgammaramp_ok || !ogl_brightness_ok)
701         {
702                 last_r = ogl_brightness_r / 63.0;
703                 last_g = ogl_brightness_g / 63.0;
704                 last_b = ogl_brightness_b / 63.0;
705
706                 do_pal_step = (r || g || b || gr_palette_gamma.intval);
707         }
708         else
709         {
710                 do_pal_step = 0;
711         }
712 }
713
714
715 void gr_palette_load( ubyte *pal )      
716 {
717  int i;//, j;
718
719  for (i=0; i<768; i++ ) {
720      gr_current_pal[i] = pal[i];
721      if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
722  }
723  //palette = screen->format->palette;
724
725  gr_palette_faded_out=0;
726
727         gr_palette_step_up(0, 0, 0); // make ogl_setbrightness_internal get run so that menus get brightened too.
728
729  init_computed_colors();
730
731         ogl_init_shared_palette();
732 }
733
734
735
736 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
737 {
738         int j;
739         unsigned char *buf;
740
741         if (gr_palette_faded_out) return 0;
742
743         if (grd_fades_disabled || !ogl_readpixels_ok) {
744                 gr_palette_faded_out = 1;
745                 return 0;
746         }
747
748         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
749         glEnable(GL_BLEND);
750         glDisable(GL_DEPTH_TEST);
751
752         MALLOC(buf, unsigned char, grd_curscreen->sc_w * grd_curscreen->sc_h * 3);
753         glReadBuffer(GL_FRONT);
754         glReadPixels(0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
755
756         for (j = 0; j < nsteps; j++) {
757                 glRasterPos2f(0,0);
758                 glDrawPixels(grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
759
760                 glColor4f(0.0, 0.0, 0.0, (float)(j) / (nsteps - 1)); // go from 0 to 1
761                 glBegin(GL_QUADS);
762                 glVertex2i(0, 0);
763                 glVertex2i(0, 1);
764                 glVertex2i(1, 1);
765                 glVertex2i(1, 0);
766                 glEnd();
767
768                 glFlush();
769
770                 timer_delay(f0_1 / 10);
771         }
772         d_free(buf);
773
774         gr_palette_faded_out=1;
775         return 0;
776 }
777
778
779
780 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
781 {
782         int j;
783         unsigned char *buf;
784
785         if (!gr_palette_faded_out) return 0;
786
787         if (grd_fades_disabled || !ogl_readpixels_ok) {
788                 gr_palette_faded_out = 0;
789                 return 0;
790         }
791
792         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
793         glEnable(GL_BLEND);
794         glDisable(GL_DEPTH_TEST);
795
796         MALLOC(buf, unsigned char, grd_curscreen->sc_w * grd_curscreen->sc_h * 3);
797         glReadBuffer(GL_FRONT);
798         glReadPixels(0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
799
800         for (j = 0; j < nsteps; j++) {
801                 glRasterPos2f(0,0);
802                 glDrawPixels(grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
803
804                 glColor4f(0.0, 0.0, 0.0, (float)(nsteps - 1 - j) / (nsteps - 1)); // go from 1 to 0
805                 glBegin(GL_QUADS);
806                 glVertex2i(0, 0);
807                 glVertex2i(0, 1);
808                 glVertex2i(1, 1);
809                 glVertex2i(1, 0);
810                 glEnd();
811
812                 glFlush();
813
814                 timer_delay(f0_1 / 10);
815         }
816         d_free(buf);
817
818         gr_palette_faded_out=0;
819         return 0;
820 }
821
822
823
824 void gr_palette_read(ubyte * pal)
825 {
826         int i;
827         for (i=0; i<768; i++ ) {
828                 pal[i]=gr_current_pal[i];
829                 if (pal[i] > 63) pal[i] = 63;
830         }
831 }
832
833 //writes out an uncompressed RGB .tga file
834 //if we got really spiffy, we could optionally link in libpng or something, and use that.
835 void write_bmp(char *savename,int w,int h,unsigned char *buf){
836         PHYSFS_file *f;
837
838         f = PHYSFSX_openWriteBuffered(savename);
839                 
840         if (f) {
841                 GLubyte    targaMagic[12] = { 0, //no identification field
842                          0,//no colormap
843                          2,//RGB image (well, BGR, actually)
844                          0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
845                 GLubyte blah;
846                 int r;
847                 GLubyte *s;
848                 int x,y;
849                 
850                 //write .TGA header.
851                 PHYSFS_write(f, targaMagic, sizeof(targaMagic), 1);
852                 PHYSFS_writeSLE16(f, w);
853                 PHYSFS_writeSLE16(f, h);
854                 PHYSFSX_writeU8(f, 24); // 24 bpp
855                 PHYSFSX_writeU8(f, 0);  // no attribute bits, origin is lowerleft, no interleave
856                 
857                 s=buf;
858                 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
859                         for (x=0;x<w;x++){
860                                 blah=s[0];
861                                 s[0]=s[2];
862                                 s[2]=blah;
863                                 s+=3;                           
864                         }
865                 }
866                 x=0;y=w*h*3;
867                 while (y > 0)
868                 {
869                         r = (int)PHYSFS_write(f, buf + x, 1, y);
870                         if (r<=0){
871                                 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
872                                 break;
873                         }
874                         x+=r;y-=r;
875                 }
876                 PHYSFS_close(f);
877         }else{
878                 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
879         }
880 }
881 void save_screen_shot(int automap_flag)
882 {
883 //      fix t1;
884         char message[100];
885         static int savenum=0;
886         char savename[13];
887         unsigned char *buf;
888         
889         if (!ogl_readpixels_ok){
890                 if (!automap_flag)
891                         hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
892                 return;
893         }
894
895         stop_time();
896
897 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
898         do
899         {
900                 if (savenum == 9999)
901                         savenum = 0;
902                 sprintf(savename, "scrn%04d.tga", savenum++);
903         } while (PHYSFS_exists(savename));
904
905         sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
906 //end this section addition/change - Victor Rachels
907
908         if (automap_flag) {
909 //      save_font = grd_curcanv->cv_font;
910 //      gr_set_curfont(GAME_FONT);
911 //      gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
912 //      gr_get_string_size(message,&w,&h,&aw);
913 //              modex_print_message(32, 2, message);
914         } else {
915                 hud_message(MSGC_GAME_FEEDBACK, "%s", message);
916         }
917         
918         buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
919         glReadBuffer(GL_FRONT);
920         glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
921         write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);
922         d_free(buf);
923
924         key_flush();
925         start_time();
926 }