2 * $Source: /cvs/cvsroot/d2x/arch/ogl/gr.c,v $
5 * $Date: 2001-10-31 07:35:47 $
7 * // OGL video functions. - Added 9/15/99 Matthew Mueller
9 * $Log: not supported by cvs2svn $
10 * Revision 1.1 2001/10/25 08:25:34 bradleyb
11 * Finished moving stuff to arch/blah. I know, it's ugly, but It'll be easier to sync with d1x.
13 * Revision 1.7 2001/10/25 02:23:48 bradleyb
16 * Revision 1.6 2001/10/18 23:59:23 bradleyb
17 * Changed __ENV_LINUX__ to __linux__
19 * Revision 1.5 2001/10/12 00:18:40 bradleyb
20 * Switched from Cygwin to mingw32 on MS boxes. Vastly improved compilability.
22 * Revision 1.4 2001/01/29 13:47:52 bradleyb
23 * Fixed build, some minor cleanups.
40 #include <sys/types.h>
72 void gr_palette_clear(); // Function prototype for gr_init;
78 int gr_check_fullscreen(void){
79 return ogl_fullscreen;
82 void gr_do_fullscreen(int f){
84 ogl_fullscreen=1;//force fullscreen mode on voodoos.
88 ogl_do_fullscreen_internal();
92 int gr_toggle_fullscreen(void){
93 gr_do_fullscreen(!ogl_fullscreen);
94 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
95 return ogl_fullscreen;
98 int arch_toggle_fullscreen_menu(void){
99 unsigned char *buf=NULL;
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);
107 gr_do_fullscreen(!ogl_fullscreen);
109 if (ogl_readpixels_ok){
110 // glWritePixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
112 glDrawPixels(grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
115 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
117 return ogl_fullscreen;
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);
125 /* initialize viewing values */
126 glMatrixMode(GL_PROJECTION);
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
134 int last_screen_mode=-1;
136 void ogl_set_screen_mode(void){
137 if (last_screen_mode==Screen_mode)
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);
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
153 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
155 last_screen_mode=Screen_mode;
162 if(Screen_mode != SCREEN_GAME){
168 const char *gl_vendor,*gl_renderer,*gl_version,*gl_extensions;
170 void ogl_get_verinfo(void){
172 gl_vendor=glGetString(GL_VENDOR);
173 gl_renderer=glGetString(GL_RENDERER);
174 gl_version=glGetString(GL_VERSION);
175 gl_extensions=glGetString(GL_EXTENSIONS);
177 printf("gl vendor:%s renderer:%s version:%s extensions:%s\n",gl_vendor,gl_renderer,gl_version,gl_extensions);
179 ogl_intensity4_ok=1;ogl_luminance4_alpha4_ok=1;ogl_rgba2_ok=1;ogl_gettexlevelparam_ok=1;
182 dglMultiTexCoord2fARB = (glMultiTexCoord2fARB_fp)wglGetProcAddress("glMultiTexCoord2fARB");
183 dglActiveTextureARB = (glActiveTextureARB_fp)wglGetProcAddress("glActiveTextureARB");
184 dglMultiTexCoord2fSGIS = (glMultiTexCoord2fSGIS_fp)wglGetProcAddress("glMultiTexCoord2fSGIS");
185 dglSelectTextureSGIS = (glSelectTextureSGIS_fp)wglGetProcAddress("glSelectTextureSGIS");
188 //multitexturing doesn't work yet.
189 #ifdef GL_ARB_multitexture
190 ogl_arb_multitexture_ok=0;//(strstr(gl_extensions,"GL_ARB_multitexture")!=0 && glActiveTextureARB!=0 && 0);
191 mprintf((0,"c:%p d:%p e:%p\n",strstr(gl_extensions,"GL_ARB_multitexture"),glActiveTextureARB,glBegin));
193 #ifdef GL_SGIS_multitexture
194 ogl_sgis_multitexture_ok=0;//(strstr(gl_extensions,"GL_SGIS_multitexture")!=0 && glSelectTextureSGIS!=0 && 0);
195 mprintf((0,"a:%p b:%p\n",strstr(gl_extensions,"GL_SGIS_multitexture"),glSelectTextureSGIS));
198 //add driver specific hacks here. whee.
199 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){
200 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
201 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
202 ogl_gettexlevelparam_ok=0;//returns random data..
204 if (stricmp(gl_vendor,"Matrox Graphics Inc.")==0){
205 //displays garbage. reported by
206 // redomen@crcwnet.com (render="Matrox G400" version="1.1.3 5.52.015")
207 // orulz (Matrox G200)
211 //allow overriding of stuff.
212 #ifdef GL_ARB_multitexture
213 if ((t=FindArg("-gl_arb_multitexture_ok"))){
214 ogl_arb_multitexture_ok=atoi(Args[t+1]);
217 #ifdef GL_SGIS_multitexture
218 if ((t=FindArg("-gl_sgis_multitexture_ok"))){
219 ogl_sgis_multitexture_ok=atoi(Args[t+1]);
222 if ((t=FindArg("-gl_intensity4_ok"))){
223 ogl_intensity4_ok=atoi(Args[t+1]);
225 if ((t=FindArg("-gl_luminance4_alpha4_ok"))){
226 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
228 if ((t=FindArg("-gl_rgba2_ok"))){
229 ogl_rgba2_ok=atoi(Args[t+1]);
231 if ((t=FindArg("-gl_readpixels_ok"))){
232 ogl_readpixels_ok=atoi(Args[t+1]);
234 if ((t=FindArg("-gl_gettexlevelparam_ok"))){
235 ogl_gettexlevelparam_ok=atoi(Args[t+1]);
238 printf("gl_arb_multitexture:%i gl_sgis_multitexture:%i\n",ogl_arb_multitexture_ok,ogl_sgis_multitexture_ok);
239 printf("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);
242 int gr_set_mode(u_int32_t mode)
257 //if (screen != NULL) gr_palette_clear();
261 gr_bm_data=grd_curscreen->sc_canvas.cv_bitmap.bm_data;//since we use realloc, we want to keep this pointer around.
262 memset( grd_curscreen, 0, sizeof(grs_screen));
263 grd_curscreen->sc_mode = mode;
264 grd_curscreen->sc_w = w;
265 grd_curscreen->sc_h = h;
266 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
267 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
268 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
269 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
270 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
271 //grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
272 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = w;
273 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_OGL;
274 //grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
275 // mprintf((0,"ogl/gr.c: reallocing %p to %i\n",grd_curscreen->sc_canvas.cv_bitmap.bm_data,w*h));
276 grd_curscreen->sc_canvas.cv_bitmap.bm_data = realloc(gr_bm_data,w*h);
277 gr_set_current_canvas(NULL);
278 //gr_enable_default_palette_loading();
280 ogl_init_window(w,h);//platform specific code
284 OGL_VIEWPORT(0,0,w,h);
286 ogl_set_screen_mode();
288 // gamefont_choose_game_font(w,h);
293 #define GLstrcmptestr(a,b) if (stricmp(a,#b)==0 || stricmp(a,"GL_" #b)==0)return GL_ ## b;
294 int ogl_atotexfilti(char *a,int min){
295 GLstrcmptestr(a,NEAREST);
296 GLstrcmptestr(a,LINEAR);
297 if (min){//mipmaps are valid only for the min filter
298 GLstrcmptestr(a,NEAREST_MIPMAP_NEAREST);
299 GLstrcmptestr(a,NEAREST_MIPMAP_LINEAR);
300 GLstrcmptestr(a,LINEAR_MIPMAP_NEAREST);
301 GLstrcmptestr(a,LINEAR_MIPMAP_LINEAR);
303 Error("unknown/invalid texture filter %s\n",a);
304 // return GL_NEAREST;
306 int ogl_testneedmipmaps(int i){
311 case GL_NEAREST_MIPMAP_NEAREST:
312 case GL_NEAREST_MIPMAP_LINEAR:
313 case GL_LINEAR_MIPMAP_NEAREST:
314 case GL_LINEAR_MIPMAP_LINEAR:
317 Error("unknown texture filter %x\n",i);
320 #ifdef OGL_RUNTIME_LOAD
321 #if defined(__WINDOWS__) || defined(__MINGW32__)
322 char *OglLibPath="opengl32.dll";
325 char *OglLibPath="libGL.so";
329 int ogl_init_load_library(void)
334 if ((t=FindArg("-gl_library")))
335 OglLibPath=Args[t+1];
337 retcode = OpenGL_LoadLibrary(true);
340 mprintf((0,"Opengl loaded ok\n"));
344 Error("Opengl: Functions not imported\n");
347 Error("Opengl: error loading %s\n",OglLibPath);
357 int mode = SM(640,480);
359 // Only do this function once!
364 #ifdef OGL_RUNTIME_LOAD
365 ogl_init_load_library();
368 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
369 if (FindArg("-gl_voodoo")){
371 gr_toggle_fullscreen();
373 if (FindArg("-fullscreen"))
374 gr_toggle_fullscreen();
376 if ((glt=FindArg("-gl_alttexmerge")))
378 if ((t=FindArg("-gl_stdtexmerge")))
379 if (t>=glt)//allow overriding of earlier args
382 if ((glt=FindArg("-gl_16bittextures")))
383 ogl_rgba_format=GL_RGB5_A1;
385 if ((glt=FindArg("-gl_mipmap"))){
386 GL_texmagfilt=GL_LINEAR;
387 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
389 if ((t=FindArg("-gl_simple"))){
390 if (t>=glt){//allow overriding of earlier args
392 GL_texmagfilt=GL_NEAREST;
393 GL_texminfilt=GL_NEAREST;
396 if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
397 if (t>=glt)//allow overriding of earlier args
398 GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
400 if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
401 if (t>=glt)//allow overriding of earlier args
402 GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
404 GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
405 mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps));
407 if ((t=FindArg("-gl_vidmem"))){
408 ogl_mem_target=atoi(Args[t+1])*1024*1024;
410 if ((t=FindArg("-gl_reticle"))){
411 gl_reticle=atoi(Args[t+1]);
413 //printf("ogl_mem_target=%i\n",ogl_mem_target);
415 ogl_init();//platform specific initialization
417 ogl_init_texture_list_internal();
419 MALLOC( grd_curscreen,grs_screen,1 );
420 memset( grd_curscreen, 0, sizeof(grs_screen));
421 grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
424 if ((retcode=gr_set_mode(mode)))
428 grd_curscreen->sc_canvas.cv_color = 0;
429 grd_curscreen->sc_canvas.cv_drawmode = 0;
430 grd_curscreen->sc_canvas.cv_font = NULL;
431 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
432 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
433 gr_set_current_canvas( &grd_curscreen->sc_canvas );
444 // mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
446 ogl_close();//platform specific code
448 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
449 d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
450 d_free(grd_curscreen);
452 #ifdef OGL_RUNTIME_LOAD
454 OpenGL_LoadLibrary(false);
457 extern int r_upixelc;
458 void ogl_upixelc(int x, int y, int c){
460 // printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
461 // if(Function_mode != FMODE_GAME){
462 // grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
464 OGL_DISABLE(TEXTURE_2D);
467 // glBegin(GL_LINES);
468 // ogl_pal=gr_current_pal;
469 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
470 // ogl_pal=gr_palette;
471 glVertex2f((x+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,1.0-(y+grd_curcanv->cv_bitmap.bm_y)/(float)last_height);
472 // glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
476 void ogl_urect(int left,int top,int right,int bot){
480 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
481 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
482 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
483 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
485 OGL_DISABLE(TEXTURE_2D);
486 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
494 void ogl_ulinec(int left,int top,int right,int bot,int c){
497 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
498 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
499 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
500 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
502 OGL_DISABLE(TEXTURE_2D);
503 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
511 GLfloat last_r=0, last_g=0, last_b=0;
513 void ogl_do_palfx(void){
515 OGL_DISABLE(TEXTURE_2D);
516 if (gr_palette_faded_out){
517 /* glEnable(GL_BLEND);
518 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
523 //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
525 glBlendFunc(GL_ONE,GL_ONE);
526 glColor3f(last_r,last_g,last_b);
527 // r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
541 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
544 void gr_palette_clear()
546 gr_palette_faded_out=1;
550 void gr_palette_step_up( int r, int g, int b )
552 if (gr_palette_faded_out) return;
554 // if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
559 do_pal_step=(r || g || b);*/
561 last_r = (r+gr_palette_gamma)/63.0;
562 last_g = (g+gr_palette_gamma)/63.0;
563 last_b = (b+gr_palette_gamma)/63.0;
565 do_pal_step=(r || g || b || gr_palette_gamma);
569 //added on 980913 by adb to fix palette problems
570 // need a min without side effects...
572 static inline int min(int x, int y) { return x < y ? x : y; }
575 void gr_palette_load( ubyte *pal )
579 for (i=0; i<768; i++ ) {
580 gr_current_pal[i] = pal[i];
581 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
583 //palette = screen->format->palette;
585 gr_palette_faded_out=0;
587 init_computed_colors();
592 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
594 gr_palette_faded_out=1;
600 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
602 gr_palette_faded_out=0;
608 void gr_palette_read(ubyte * pal)
611 for (i=0; i<768; i++ ) {
612 pal[i]=gr_current_pal[i];
613 if (pal[i] > 63) pal[i] = 63;
617 //writes out an uncompressed RGB .tga file
618 //if we got really spiffy, we could optionally link in libpng or something, and use that.
619 void write_bmp(char *savename,int w,int h,unsigned char *buf){
621 #if defined(__WINDOWS__) || defined(__MINGW32__)
622 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR);
624 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
627 GLubyte targaMagic[12] = { 0, //no identification field
629 2,//RGB image (well, BGR, actually)
630 0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
637 write (f,targaMagic,sizeof(targaMagic));
638 blah=w%256;write (f,&blah,1);//w, low
639 blah=w/256;write (f,&blah,1);//w, high
640 blah=h%256;write (f,&blah,1);//h, low
641 blah=h/256;write (f,&blah,1);//h, high
642 blah=24;write (f,&blah,1);//24 bpp
643 blah=0;write (f,&blah,1);//no attribute bits, origin is lowerleft, no interleave
646 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
658 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
665 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
668 void save_screen_shot(int automap_flag)
672 static int savenum=0;
676 if (!ogl_readpixels_ok){
678 // hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
684 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
685 if ( savenum == 9999 ) savenum = 0;
686 sprintf(savename,"scrn%04d.tga",savenum++);
688 while(!access(savename,0))
690 if ( savenum == 9999 ) savenum = 0;
691 sprintf(savename,"scrn%04d.tga",savenum++);
693 sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
694 //end this section addition/change - Victor Rachels
697 // save_font = grd_curcanv->cv_font;
698 // gr_set_curfont(GAME_FONT);
699 // gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
700 // gr_get_string_size(message,&w,&h,&aw);
701 // modex_print_message(32, 2, message);
703 // hud_message(MSGC_GAME_FEEDBACK,message);
706 buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
707 glReadBuffer(GL_FRONT);
708 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
709 write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);