]> icculus.org git repositories - btb/d2x.git/blob - arch/ogl/gr.c
implement gl_texturemode command
[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 int VGA_current_mode; // DPH: kludge - remove at all costs
308
309 int gr_set_mode(uint32_t mode)
310 {
311         unsigned int 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         gr_installed = 1;
555         
556         atexit(gr_close);
557
558         return 0;
559 }
560
561 void gr_close()
562 {
563 //      mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
564         ogl_brightness_r = ogl_brightness_g = ogl_brightness_b = 0;
565         ogl_setbrightness_internal();
566
567         ogl_close();//platform specific code
568         if (grd_curscreen){
569                 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
570                         d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
571                 d_free(grd_curscreen);
572         }
573 #ifdef OGL_RUNTIME_LOAD
574         if (ogl_rt_loaded)
575                 OpenGL_LoadLibrary(false);
576 #endif
577 }
578 extern int r_upixelc;
579 void ogl_upixelc(int x, int y, int c){
580         r_upixelc++;
581 //      printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
582 //      if(Function_mode != FMODE_GAME){
583 //              grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
584 //      }else{
585                 OGL_DISABLE(TEXTURE_2D);
586                 glPointSize(1.0);
587                 glBegin(GL_POINTS);
588 //              glBegin(GL_LINES);
589 //      ogl_pal=gr_current_pal;
590                 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
591 //      ogl_pal=gr_palette;
592                 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);
593 //              glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
594                 glEnd();
595 //      }
596 }
597 void ogl_urect(int left,int top,int right,int bot){
598         GLfloat xo,yo,xf,yf;
599         int c=COLOR;
600         
601         xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
602         xf = (right + 1 + grd_curcanv->cv_bitmap.bm_x) / (float)last_width;
603         yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
604         yf = 1.0 - (bot + 1 + grd_curcanv->cv_bitmap.bm_y) / (float)last_height;
605
606         OGL_DISABLE(TEXTURE_2D);
607         if (Gr_scanline_darkening_level >= GR_FADE_LEVELS)
608                 glColor3f(CPAL2Tr(c), CPAL2Tg(c), CPAL2Tb(c));
609         else
610                 glColor4f(CPAL2Tr(c), CPAL2Tg(c), CPAL2Tb(c), 1.0 - (float)Gr_scanline_darkening_level / ((float)GR_FADE_LEVELS - 1.0));
611         glBegin(GL_QUADS);
612         glVertex2f(xo,yo);
613         glVertex2f(xo,yf);
614         glVertex2f(xf,yf);
615         glVertex2f(xf,yo);
616         glEnd();
617 }
618 void ogl_ulinec(int left,int top,int right,int bot,int c){
619         GLfloat xo,yo,xf,yf;
620
621         xo = (left + grd_curcanv->cv_bitmap.bm_x + 0.5) / (float)last_width;
622         xf = (right + grd_curcanv->cv_bitmap.bm_x + 0.5) / (float)last_width;
623         yo = 1.0 - (top + grd_curcanv->cv_bitmap.bm_y + 0.5) / (float)last_height;
624         yf = 1.0 - (bot + grd_curcanv->cv_bitmap.bm_y + 0.5) / (float)last_height;
625
626         OGL_DISABLE(TEXTURE_2D);
627         glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
628         glBegin(GL_LINES);
629         glVertex2f(xo,yo);
630         glVertex2f(xf,yf);
631         glEnd();
632 }
633         
634
635 GLfloat last_r=0, last_g=0, last_b=0;
636 int do_pal_step=0;
637 void ogl_do_palfx(void){
638 //      GLfloat r,g,b,a;
639         OGL_DISABLE(TEXTURE_2D);
640         if (gr_palette_faded_out){
641 /*              glEnable(GL_BLEND);
642                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
643                 glColor3f(0,0,0);
644 //              r=g=b=0.0;a=1.0;
645         }else{
646                 if (do_pal_step){
647                         //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
648                         glEnable(GL_BLEND);
649                         glBlendFunc(GL_ONE,GL_ONE);
650                         glColor3f(last_r,last_g,last_b);
651 //                      r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
652                 }else
653                         return;
654         }
655         
656         
657         glBegin(GL_QUADS);
658         glVertex2f(0,0);
659         glVertex2f(0,1);
660         glVertex2f(1,1);
661         glVertex2f(1,0);
662         glEnd();
663         
664         glEnable(GL_BLEND);     
665         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
666 }
667
668 void gr_palette_clear()
669 {
670         gr_palette_faded_out=1;
671 }
672
673
674 int ogl_brightness_ok = 0;
675 int ogl_setgammaramp_ok = 1;
676 int ogl_brightness_r = 0, ogl_brightness_g = 0, ogl_brightness_b = 0;
677 static int old_b_r = 0, old_b_g = 0, old_b_b = 0;
678
679 void gr_palette_step_up(int r, int g, int b)
680 {
681         if (gr_palette_faded_out)
682                 return;
683
684         old_b_r = ogl_brightness_r;
685         old_b_g = ogl_brightness_g;
686         old_b_b = ogl_brightness_b;
687
688         ogl_brightness_r = max(r + gr_palette_gamma, 0);
689         ogl_brightness_g = max(g + gr_palette_gamma, 0);
690         ogl_brightness_b = max(b + gr_palette_gamma, 0);
691
692         if (ogl_setgammaramp_ok &&
693             (old_b_r != ogl_brightness_r ||
694              old_b_g != ogl_brightness_g ||
695              old_b_b != ogl_brightness_b))
696                 ogl_brightness_ok = !ogl_setbrightness_internal();
697
698         if (!ogl_setgammaramp_ok || !ogl_brightness_ok)
699         {
700                 last_r = ogl_brightness_r / 63.0;
701                 last_g = ogl_brightness_g / 63.0;
702                 last_b = ogl_brightness_b / 63.0;
703
704                 do_pal_step = (r || g || b || gr_palette_gamma);
705         }
706         else
707         {
708                 do_pal_step = 0;
709         }
710 }
711
712
713 void gr_palette_load( ubyte *pal )      
714 {
715  int i;//, j;
716
717  for (i=0; i<768; i++ ) {
718      gr_current_pal[i] = pal[i];
719      if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
720  }
721  //palette = screen->format->palette;
722
723  gr_palette_faded_out=0;
724
725         gr_palette_step_up(0, 0, 0); // make ogl_setbrightness_internal get run so that menus get brightened too.
726
727  init_computed_colors();
728
729         ogl_init_shared_palette();
730 }
731
732
733
734 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
735 {
736         int j;
737         unsigned char *buf;
738
739         if (gr_palette_faded_out) return 0;
740
741         if (grd_fades_disabled || !ogl_readpixels_ok) {
742                 gr_palette_faded_out = 1;
743                 return 0;
744         }
745
746         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
747         glEnable(GL_BLEND);
748         glDisable(GL_DEPTH_TEST);
749
750         MALLOC(buf, unsigned char, grd_curscreen->sc_w * grd_curscreen->sc_h * 3);
751         glReadBuffer(GL_FRONT);
752         glReadPixels(0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
753
754         for (j = 0; j < nsteps; j++) {
755                 glRasterPos2f(0,0);
756                 glDrawPixels(grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
757
758                 glColor4f(0.0, 0.0, 0.0, (float)(j) / (nsteps - 1)); // go from 0 to 1
759                 glBegin(GL_QUADS);
760                 glVertex2i(0, 0);
761                 glVertex2i(0, 1);
762                 glVertex2i(1, 1);
763                 glVertex2i(1, 0);
764                 glEnd();
765
766                 glFlush();
767
768                 timer_delay(f0_1 / 10);
769         }
770         d_free(buf);
771
772         gr_palette_faded_out=1;
773         return 0;
774 }
775
776
777
778 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
779 {
780         int j;
781         unsigned char *buf;
782
783         if (!gr_palette_faded_out) return 0;
784
785         if (grd_fades_disabled || !ogl_readpixels_ok) {
786                 gr_palette_faded_out = 0;
787                 return 0;
788         }
789
790         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
791         glEnable(GL_BLEND);
792         glDisable(GL_DEPTH_TEST);
793
794         MALLOC(buf, unsigned char, grd_curscreen->sc_w * grd_curscreen->sc_h * 3);
795         glReadBuffer(GL_FRONT);
796         glReadPixels(0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
797
798         for (j = 0; j < nsteps; j++) {
799                 glRasterPos2f(0,0);
800                 glDrawPixels(grd_curscreen->sc_w, grd_curscreen->sc_h, GL_RGB, GL_UNSIGNED_BYTE, buf);
801
802                 glColor4f(0.0, 0.0, 0.0, (float)(nsteps - 1 - j) / (nsteps - 1)); // go from 1 to 0
803                 glBegin(GL_QUADS);
804                 glVertex2i(0, 0);
805                 glVertex2i(0, 1);
806                 glVertex2i(1, 1);
807                 glVertex2i(1, 0);
808                 glEnd();
809
810                 glFlush();
811
812                 timer_delay(f0_1 / 10);
813         }
814         d_free(buf);
815
816         gr_palette_faded_out=0;
817         return 0;
818 }
819
820
821
822 void gr_palette_read(ubyte * pal)
823 {
824         int i;
825         for (i=0; i<768; i++ ) {
826                 pal[i]=gr_current_pal[i];
827                 if (pal[i] > 63) pal[i] = 63;
828         }
829 }
830
831 //writes out an uncompressed RGB .tga file
832 //if we got really spiffy, we could optionally link in libpng or something, and use that.
833 void write_bmp(char *savename,int w,int h,unsigned char *buf){
834         PHYSFS_file *f;
835
836         f = PHYSFSX_openWriteBuffered(savename);
837                 
838         if (f>=0){
839                 GLubyte    targaMagic[12] = { 0, //no identification field
840                          0,//no colormap
841                          2,//RGB image (well, BGR, actually)
842                          0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
843                 GLubyte blah;
844                 int r;
845                 GLubyte *s;
846                 int x,y;
847                 
848                 //write .TGA header.
849                 PHYSFS_write(f, targaMagic, sizeof(targaMagic), 1);
850                 PHYSFS_writeSLE16(f, w);
851                 PHYSFS_writeSLE16(f, h);
852                 PHYSFSX_writeU8(f, 24); // 24 bpp
853                 PHYSFSX_writeU8(f, 0);  // no attribute bits, origin is lowerleft, no interleave
854                 
855                 s=buf;
856                 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
857                         for (x=0;x<w;x++){
858                                 blah=s[0];
859                                 s[0]=s[2];
860                                 s[2]=blah;
861                                 s+=3;                           
862                         }
863                 }
864                 x=0;y=w*h*3;
865                 while (y > 0)
866                 {
867                         r = PHYSFS_write(f, buf + x, 1, y);
868                         if (r<=0){
869                                 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
870                                 break;
871                         }
872                         x+=r;y-=r;
873                 }
874                 PHYSFS_close(f);
875         }else{
876                 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
877         }
878 }
879 void save_screen_shot(int automap_flag)
880 {
881 //      fix t1;
882         char message[100];
883         static int savenum=0;
884         char savename[13];
885         unsigned char *buf;
886         
887         if (!ogl_readpixels_ok){
888                 if (!automap_flag)
889                         hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
890                 return;
891         }
892
893         stop_time();
894
895 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
896         do
897         {
898                 if (savenum == 9999)
899                         savenum = 0;
900                 sprintf(savename, "scrn%04d.tga", savenum++);
901         } while (PHYSFS_exists(savename));
902
903         sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
904 //end this section addition/change - Victor Rachels
905
906         if (automap_flag) {
907 //      save_font = grd_curcanv->cv_font;
908 //      gr_set_curfont(GAME_FONT);
909 //      gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
910 //      gr_get_string_size(message,&w,&h,&aw);
911 //              modex_print_message(32, 2, message);
912         } else {
913                 hud_message(MSGC_GAME_FEEDBACK, "%s", message);
914         }
915         
916         buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
917         glReadBuffer(GL_FRONT);
918         glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
919         write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);
920         d_free(buf);
921
922         key_flush();
923         start_time();
924 }