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