2 * $Source: /cvs/cvsroot/d2x/arch/ogl/gr.c,v $
5 * $Date: 2001-11-09 06:53:37 $
7 * // OGL video functions. - Added 9/15/99 Matthew Mueller
9 * $Log: not supported by cvs2svn $
10 * Revision 1.4 2001/11/08 10:19:52 bradleyb
11 * use new d_realloc function, so mem manager doesn't die
13 * Revision 1.3 2001/11/04 09:00:25 bradleyb
14 * Enable d1x-style hud_message
16 * Revision 1.2 2001/10/31 07:35:47 bradleyb
19 * Revision 1.1 2001/10/25 08:25:34 bradleyb
20 * Finished moving stuff to arch/blah. I know, it's ugly, but It'll be easier to sync with d1x.
22 * Revision 1.7 2001/10/25 02:23:48 bradleyb
25 * Revision 1.6 2001/10/18 23:59:23 bradleyb
26 * Changed __ENV_LINUX__ to __linux__
28 * Revision 1.5 2001/10/12 00:18:40 bradleyb
29 * Switched from Cygwin to mingw32 on MS boxes. Vastly improved compilability.
31 * Revision 1.4 2001/01/29 13:47:52 bradleyb
32 * Fixed build, some minor cleanups.
49 #include <sys/types.h>
81 void gr_palette_clear(); // Function prototype for gr_init;
87 int gr_check_fullscreen(void){
88 return ogl_fullscreen;
91 void gr_do_fullscreen(int f){
93 ogl_fullscreen=1;//force fullscreen mode on voodoos.
97 ogl_do_fullscreen_internal();
101 int gr_toggle_fullscreen(void){
102 gr_do_fullscreen(!ogl_fullscreen);
103 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
104 return ogl_fullscreen;
107 int arch_toggle_fullscreen_menu(void){
108 unsigned char *buf=NULL;
110 if (ogl_readpixels_ok){
111 MALLOC(buf,unsigned char,grd_curscreen->sc_w*grd_curscreen->sc_h*3);
112 glReadBuffer(GL_FRONT);
113 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
116 gr_do_fullscreen(!ogl_fullscreen);
118 if (ogl_readpixels_ok){
119 // glWritePixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
121 glDrawPixels(grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
124 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
126 return ogl_fullscreen;
129 void ogl_init_state(void){
130 /* select clearing (background) color */
131 glClearColor(0.0, 0.0, 0.0, 0.0);
132 glShadeModel(GL_SMOOTH);
134 /* initialize viewing values */
135 glMatrixMode(GL_PROJECTION);
137 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
138 glScalef(1.0, -1.0, 1.0);
139 glTranslatef(0.0, -1.0, 0.0);
140 gr_palette_step_up(0,0,0);//in case its left over from in game
143 int last_screen_mode=-1;
145 void ogl_set_screen_mode(void){
146 if (last_screen_mode==Screen_mode)
148 OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
149 // 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);
150 if (Screen_mode==SCREEN_GAME){
151 glDrawBuffer(GL_BACK);
153 glClearColor(0.0, 0.0, 0.0, 0.0);
154 glDrawBuffer(GL_FRONT);
155 glClear(GL_COLOR_BUFFER_BIT);
156 glMatrixMode(GL_PROJECTION);
157 glLoadIdentity();//clear matrix
158 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
159 glMatrixMode(GL_MODELVIEW);
160 glLoadIdentity();//clear matrix
162 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
164 last_screen_mode=Screen_mode;
171 if(Screen_mode != SCREEN_GAME){
177 const char *gl_vendor,*gl_renderer,*gl_version,*gl_extensions;
179 void ogl_get_verinfo(void){
181 gl_vendor=glGetString(GL_VENDOR);
182 gl_renderer=glGetString(GL_RENDERER);
183 gl_version=glGetString(GL_VERSION);
184 gl_extensions=glGetString(GL_EXTENSIONS);
186 con_printf(CON_VERBOSE, "gl vendor:%s renderer:%s version:%s extensions:%s\n",gl_vendor,gl_renderer,gl_version,gl_extensions);
188 ogl_intensity4_ok=1;ogl_luminance4_alpha4_ok=1;ogl_rgba2_ok=1;ogl_gettexlevelparam_ok=1;
191 dglMultiTexCoord2fARB = (glMultiTexCoord2fARB_fp)wglGetProcAddress("glMultiTexCoord2fARB");
192 dglActiveTextureARB = (glActiveTextureARB_fp)wglGetProcAddress("glActiveTextureARB");
193 dglMultiTexCoord2fSGIS = (glMultiTexCoord2fSGIS_fp)wglGetProcAddress("glMultiTexCoord2fSGIS");
194 dglSelectTextureSGIS = (glSelectTextureSGIS_fp)wglGetProcAddress("glSelectTextureSGIS");
197 //multitexturing doesn't work yet.
198 #ifdef GL_ARB_multitexture
199 ogl_arb_multitexture_ok=0;//(strstr(gl_extensions,"GL_ARB_multitexture")!=0 && glActiveTextureARB!=0 && 0);
200 mprintf((0,"c:%p d:%p e:%p\n",strstr(gl_extensions,"GL_ARB_multitexture"),glActiveTextureARB,glBegin));
202 #ifdef GL_SGIS_multitexture
203 ogl_sgis_multitexture_ok=0;//(strstr(gl_extensions,"GL_SGIS_multitexture")!=0 && glSelectTextureSGIS!=0 && 0);
204 mprintf((0,"a:%p b:%p\n",strstr(gl_extensions,"GL_SGIS_multitexture"),glSelectTextureSGIS));
207 //add driver specific hacks here. whee.
208 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){
209 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
210 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
211 ogl_gettexlevelparam_ok=0;//returns random data..
213 if (stricmp(gl_vendor,"Matrox Graphics Inc.")==0){
214 //displays garbage. reported by
215 // redomen@crcwnet.com (render="Matrox G400" version="1.1.3 5.52.015")
216 // orulz (Matrox G200)
220 //allow overriding of stuff.
221 #ifdef GL_ARB_multitexture
222 if ((t=FindArg("-gl_arb_multitexture_ok"))){
223 ogl_arb_multitexture_ok=atoi(Args[t+1]);
226 #ifdef GL_SGIS_multitexture
227 if ((t=FindArg("-gl_sgis_multitexture_ok"))){
228 ogl_sgis_multitexture_ok=atoi(Args[t+1]);
231 if ((t=FindArg("-gl_intensity4_ok"))){
232 ogl_intensity4_ok=atoi(Args[t+1]);
234 if ((t=FindArg("-gl_luminance4_alpha4_ok"))){
235 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
237 if ((t=FindArg("-gl_rgba2_ok"))){
238 ogl_rgba2_ok=atoi(Args[t+1]);
240 if ((t=FindArg("-gl_readpixels_ok"))){
241 ogl_readpixels_ok=atoi(Args[t+1]);
243 if ((t=FindArg("-gl_gettexlevelparam_ok"))){
244 ogl_gettexlevelparam_ok=atoi(Args[t+1]);
247 con_printf(CON_VERBOSE, "gl_arb_multitexture:%i gl_sgis_multitexture:%i\n",ogl_arb_multitexture_ok,ogl_sgis_multitexture_ok);
248 con_printf(CON_VERBOSE, "gl_intensity4:%i gl_luminance4_alpha4:%i gl_rgba2:%i gl_readpixels:%i gl_gettexlevelparam:%i\n",ogl_intensity4_ok,ogl_luminance4_alpha4_ok,ogl_rgba2_ok,ogl_readpixels_ok,ogl_gettexlevelparam_ok);
251 int gr_set_mode(u_int32_t mode)
266 //if (screen != NULL) gr_palette_clear();
270 gr_bm_data=grd_curscreen->sc_canvas.cv_bitmap.bm_data;//since we use realloc, we want to keep this pointer around.
271 memset( grd_curscreen, 0, sizeof(grs_screen));
272 grd_curscreen->sc_mode = mode;
273 grd_curscreen->sc_w = w;
274 grd_curscreen->sc_h = h;
275 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
276 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
277 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
278 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
279 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
280 //grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
281 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = w;
282 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_OGL;
283 //grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
284 // mprintf((0,"ogl/gr.c: reallocing %p to %i\n",grd_curscreen->sc_canvas.cv_bitmap.bm_data,w*h));
285 grd_curscreen->sc_canvas.cv_bitmap.bm_data = d_realloc(gr_bm_data,w*h);
286 gr_set_current_canvas(NULL);
287 //gr_enable_default_palette_loading();
289 ogl_init_window(w,h);//platform specific code
293 OGL_VIEWPORT(0,0,w,h);
295 ogl_set_screen_mode();
297 // gamefont_choose_game_font(w,h);
302 #define GLstrcmptestr(a,b) if (stricmp(a,#b)==0 || stricmp(a,"GL_" #b)==0)return GL_ ## b;
303 int ogl_atotexfilti(char *a,int min){
304 GLstrcmptestr(a,NEAREST);
305 GLstrcmptestr(a,LINEAR);
306 if (min){//mipmaps are valid only for the min filter
307 GLstrcmptestr(a,NEAREST_MIPMAP_NEAREST);
308 GLstrcmptestr(a,NEAREST_MIPMAP_LINEAR);
309 GLstrcmptestr(a,LINEAR_MIPMAP_NEAREST);
310 GLstrcmptestr(a,LINEAR_MIPMAP_LINEAR);
312 Error("unknown/invalid texture filter %s\n",a);
313 // return GL_NEAREST;
315 int ogl_testneedmipmaps(int i){
320 case GL_NEAREST_MIPMAP_NEAREST:
321 case GL_NEAREST_MIPMAP_LINEAR:
322 case GL_LINEAR_MIPMAP_NEAREST:
323 case GL_LINEAR_MIPMAP_LINEAR:
326 Error("unknown texture filter %x\n",i);
329 #ifdef OGL_RUNTIME_LOAD
330 #if defined(__WINDOWS__) || defined(__MINGW32__)
331 char *OglLibPath="opengl32.dll";
334 char *OglLibPath="libGL.so";
338 int ogl_init_load_library(void)
343 if ((t=FindArg("-gl_library")))
344 OglLibPath=Args[t+1];
346 retcode = OpenGL_LoadLibrary(true);
349 mprintf((0,"Opengl loaded ok\n"));
353 Error("Opengl: Functions not imported\n");
356 Error("Opengl: error loading %s\n",OglLibPath);
366 int mode = SM(640,480);
368 // Only do this function once!
373 #ifdef OGL_RUNTIME_LOAD
374 ogl_init_load_library();
377 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
378 if (FindArg("-gl_voodoo")){
380 gr_toggle_fullscreen();
382 if (FindArg("-fullscreen"))
383 gr_toggle_fullscreen();
385 if ((glt=FindArg("-gl_alttexmerge")))
387 if ((t=FindArg("-gl_stdtexmerge")))
388 if (t>=glt)//allow overriding of earlier args
391 if ((glt=FindArg("-gl_16bittextures")))
392 ogl_rgba_format=GL_RGB5_A1;
394 if ((glt=FindArg("-gl_mipmap"))){
395 GL_texmagfilt=GL_LINEAR;
396 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
398 if ((t=FindArg("-gl_simple"))){
399 if (t>=glt){//allow overriding of earlier args
401 GL_texmagfilt=GL_NEAREST;
402 GL_texminfilt=GL_NEAREST;
405 if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
406 if (t>=glt)//allow overriding of earlier args
407 GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
409 if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
410 if (t>=glt)//allow overriding of earlier args
411 GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
413 GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
414 mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps));
416 if ((t=FindArg("-gl_vidmem"))){
417 ogl_mem_target=atoi(Args[t+1])*1024*1024;
419 if ((t=FindArg("-gl_reticle"))){
420 gl_reticle=atoi(Args[t+1]);
422 //printf("ogl_mem_target=%i\n",ogl_mem_target);
424 ogl_init();//platform specific initialization
426 ogl_init_texture_list_internal();
428 MALLOC( grd_curscreen,grs_screen,1 );
429 memset( grd_curscreen, 0, sizeof(grs_screen));
430 grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
433 if ((retcode=gr_set_mode(mode)))
437 grd_curscreen->sc_canvas.cv_color = 0;
438 grd_curscreen->sc_canvas.cv_drawmode = 0;
439 grd_curscreen->sc_canvas.cv_font = NULL;
440 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
441 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
442 gr_set_current_canvas( &grd_curscreen->sc_canvas );
453 // mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
455 ogl_close();//platform specific code
457 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
458 d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
459 d_free(grd_curscreen);
461 #ifdef OGL_RUNTIME_LOAD
463 OpenGL_LoadLibrary(false);
466 extern int r_upixelc;
467 void ogl_upixelc(int x, int y, int c){
469 // printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
470 // if(Function_mode != FMODE_GAME){
471 // grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
473 OGL_DISABLE(TEXTURE_2D);
476 // glBegin(GL_LINES);
477 // ogl_pal=gr_current_pal;
478 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
479 // ogl_pal=gr_palette;
480 glVertex2f((x+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,1.0-(y+grd_curcanv->cv_bitmap.bm_y)/(float)last_height);
481 // glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
485 void ogl_urect(int left,int top,int right,int bot){
489 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
490 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
491 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
492 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
494 OGL_DISABLE(TEXTURE_2D);
495 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
503 void ogl_ulinec(int left,int top,int right,int bot,int c){
506 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
507 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
508 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
509 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
511 OGL_DISABLE(TEXTURE_2D);
512 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
520 GLfloat last_r=0, last_g=0, last_b=0;
522 void ogl_do_palfx(void){
524 OGL_DISABLE(TEXTURE_2D);
525 if (gr_palette_faded_out){
526 /* glEnable(GL_BLEND);
527 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
532 //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
534 glBlendFunc(GL_ONE,GL_ONE);
535 glColor3f(last_r,last_g,last_b);
536 // r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
550 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
553 void gr_palette_clear()
555 gr_palette_faded_out=1;
559 void gr_palette_step_up( int r, int g, int b )
561 if (gr_palette_faded_out) return;
563 // if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
568 do_pal_step=(r || g || b);*/
570 last_r = (r+gr_palette_gamma)/63.0;
571 last_g = (g+gr_palette_gamma)/63.0;
572 last_b = (b+gr_palette_gamma)/63.0;
574 do_pal_step=(r || g || b || gr_palette_gamma);
578 //added on 980913 by adb to fix palette problems
579 // need a min without side effects...
581 static inline int min(int x, int y) { return x < y ? x : y; }
584 void gr_palette_load( ubyte *pal )
588 for (i=0; i<768; i++ ) {
589 gr_current_pal[i] = pal[i];
590 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
592 //palette = screen->format->palette;
594 gr_palette_faded_out=0;
596 init_computed_colors();
601 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
603 gr_palette_faded_out=1;
609 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
611 gr_palette_faded_out=0;
617 void gr_palette_read(ubyte * pal)
620 for (i=0; i<768; i++ ) {
621 pal[i]=gr_current_pal[i];
622 if (pal[i] > 63) pal[i] = 63;
626 //writes out an uncompressed RGB .tga file
627 //if we got really spiffy, we could optionally link in libpng or something, and use that.
628 void write_bmp(char *savename,int w,int h,unsigned char *buf){
630 #if defined(__WINDOWS__) || defined(__MINGW32__)
631 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR);
633 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
636 GLubyte targaMagic[12] = { 0, //no identification field
638 2,//RGB image (well, BGR, actually)
639 0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
646 write (f,targaMagic,sizeof(targaMagic));
647 blah=w%256;write (f,&blah,1);//w, low
648 blah=w/256;write (f,&blah,1);//w, high
649 blah=h%256;write (f,&blah,1);//h, low
650 blah=h/256;write (f,&blah,1);//h, high
651 blah=24;write (f,&blah,1);//24 bpp
652 blah=0;write (f,&blah,1);//no attribute bits, origin is lowerleft, no interleave
655 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
667 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
674 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
677 void save_screen_shot(int automap_flag)
681 static int savenum=0;
685 if (!ogl_readpixels_ok){
687 hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
693 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
694 if ( savenum == 9999 ) savenum = 0;
695 sprintf(savename,"scrn%04d.tga",savenum++);
697 while(!access(savename,0))
699 if ( savenum == 9999 ) savenum = 0;
700 sprintf(savename,"scrn%04d.tga",savenum++);
702 sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
703 //end this section addition/change - Victor Rachels
706 // save_font = grd_curcanv->cv_font;
707 // gr_set_curfont(GAME_FONT);
708 // gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
709 // gr_get_string_size(message,&w,&h,&aw);
710 // modex_print_message(32, 2, message);
712 hud_message(MSGC_GAME_FEEDBACK,message);
715 buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
716 glReadBuffer(GL_FRONT);
717 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
718 write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);