1 /* $Id: gr.c,v 1.11 2003-03-15 12:07:43 btb Exp $ */
4 * OGL video functions. - Added 9/15/99 Matthew Mueller
21 //#include <OpenGL/gl.h>
26 #include <sys/types.h>
52 #include <OpenGL/glu.h>
53 #undef GL_ARB_multitexture // hack!
63 void gr_palette_clear(); // Function prototype for gr_init;
69 int gr_check_fullscreen(void){
70 return ogl_fullscreen;
73 void gr_do_fullscreen(int f){
75 ogl_fullscreen=1;//force fullscreen mode on voodoos.
79 ogl_do_fullscreen_internal();
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;
89 int arch_toggle_fullscreen_menu(void){
90 unsigned char *buf=NULL;
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);
98 gr_do_fullscreen(!ogl_fullscreen);
100 if (ogl_readpixels_ok){
101 // glWritePixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
103 glDrawPixels(grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
106 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
108 return ogl_fullscreen;
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);
116 /* initialize viewing values */
117 glMatrixMode(GL_PROJECTION);
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
125 int last_screen_mode=-1;
127 void ogl_set_screen_mode(void){
128 if (last_screen_mode==Screen_mode)
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);
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
144 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
146 last_screen_mode=Screen_mode;
153 if(Screen_mode != SCREEN_GAME){
159 const char *gl_vendor,*gl_renderer,*gl_version,*gl_extensions;
161 void ogl_get_verinfo(void){
163 gl_vendor=glGetString(GL_VENDOR);
164 gl_renderer=glGetString(GL_RENDERER);
165 gl_version=glGetString(GL_VERSION);
166 gl_extensions=glGetString(GL_EXTENSIONS);
168 con_printf(CON_VERBOSE, "gl vendor:%s renderer:%s version:%s extensions:%s\n",gl_vendor,gl_renderer,gl_version,gl_extensions);
170 ogl_intensity4_ok=1;ogl_luminance4_alpha4_ok=1;ogl_rgba2_ok=1;ogl_gettexlevelparam_ok=1;
173 dglMultiTexCoord2fARB = (glMultiTexCoord2fARB_fp)wglGetProcAddress("glMultiTexCoord2fARB");
174 dglActiveTextureARB = (glActiveTextureARB_fp)wglGetProcAddress("glActiveTextureARB");
175 dglMultiTexCoord2fSGIS = (glMultiTexCoord2fSGIS_fp)wglGetProcAddress("glMultiTexCoord2fSGIS");
176 dglSelectTextureSGIS = (glSelectTextureSGIS_fp)wglGetProcAddress("glSelectTextureSGIS");
179 //multitexturing doesn't work yet.
180 #ifdef GL_ARB_multitexture
181 ogl_arb_multitexture_ok=0;//(strstr(gl_extensions,"GL_ARB_multitexture")!=0 && glActiveTextureARB!=0 && 0);
182 mprintf((0,"c:%p d:%p e:%p\n",strstr(gl_extensions,"GL_ARB_multitexture"),glActiveTextureARB,glBegin));
184 #ifdef GL_SGIS_multitexture
185 ogl_sgis_multitexture_ok=0;//(strstr(gl_extensions,"GL_SGIS_multitexture")!=0 && glSelectTextureSGIS!=0 && 0);
186 mprintf((0,"a:%p b:%p\n",strstr(gl_extensions,"GL_SGIS_multitexture"),glSelectTextureSGIS));
189 //add driver specific hacks here. whee.
190 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){
191 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
192 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
193 ogl_gettexlevelparam_ok=0;//returns random data..
195 if (stricmp(gl_vendor,"Matrox Graphics Inc.")==0){
196 //displays garbage. reported by
197 // redomen@crcwnet.com (render="Matrox G400" version="1.1.3 5.52.015")
198 // orulz (Matrox G200)
202 //allow overriding of stuff.
203 #ifdef GL_ARB_multitexture
204 if ((t=FindArg("-gl_arb_multitexture_ok"))){
205 ogl_arb_multitexture_ok=atoi(Args[t+1]);
208 #ifdef GL_SGIS_multitexture
209 if ((t=FindArg("-gl_sgis_multitexture_ok"))){
210 ogl_sgis_multitexture_ok=atoi(Args[t+1]);
213 if ((t=FindArg("-gl_intensity4_ok"))){
214 ogl_intensity4_ok=atoi(Args[t+1]);
216 if ((t=FindArg("-gl_luminance4_alpha4_ok"))){
217 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
219 if ((t=FindArg("-gl_rgba2_ok"))){
220 ogl_rgba2_ok=atoi(Args[t+1]);
222 if ((t=FindArg("-gl_readpixels_ok"))){
223 ogl_readpixels_ok=atoi(Args[t+1]);
225 if ((t=FindArg("-gl_gettexlevelparam_ok"))){
226 ogl_gettexlevelparam_ok=atoi(Args[t+1]);
229 con_printf(CON_VERBOSE, "gl_arb_multitexture:%i gl_sgis_multitexture:%i\n",ogl_arb_multitexture_ok,ogl_sgis_multitexture_ok);
230 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);
233 int gr_set_mode(u_int32_t mode)
248 //if (screen != NULL) gr_palette_clear();
252 gr_bm_data=grd_curscreen->sc_canvas.cv_bitmap.bm_data;//since we use realloc, we want to keep this pointer around.
253 memset( grd_curscreen, 0, sizeof(grs_screen));
254 grd_curscreen->sc_mode = mode;
255 grd_curscreen->sc_w = w;
256 grd_curscreen->sc_h = h;
257 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
258 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
259 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
260 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
261 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
262 //grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
263 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = w;
264 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_OGL;
265 //grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
266 // mprintf((0,"ogl/gr.c: reallocing %p to %i\n",grd_curscreen->sc_canvas.cv_bitmap.bm_data,w*h));
267 grd_curscreen->sc_canvas.cv_bitmap.bm_data = d_realloc(gr_bm_data,w*h);
268 gr_set_current_canvas(NULL);
269 //gr_enable_default_palette_loading();
271 ogl_init_window(w,h);//platform specific code
275 OGL_VIEWPORT(0,0,w,h);
277 ogl_set_screen_mode();
279 // gamefont_choose_game_font(w,h);
284 #define GLstrcmptestr(a,b) if (stricmp(a,#b)==0 || stricmp(a,"GL_" #b)==0)return GL_ ## b;
285 int ogl_atotexfilti(char *a,int min){
286 GLstrcmptestr(a,NEAREST);
287 GLstrcmptestr(a,LINEAR);
288 if (min){//mipmaps are valid only for the min filter
289 GLstrcmptestr(a,NEAREST_MIPMAP_NEAREST);
290 GLstrcmptestr(a,NEAREST_MIPMAP_LINEAR);
291 GLstrcmptestr(a,LINEAR_MIPMAP_NEAREST);
292 GLstrcmptestr(a,LINEAR_MIPMAP_LINEAR);
294 Error("unknown/invalid texture filter %s\n",a);
295 // return GL_NEAREST;
297 int ogl_testneedmipmaps(int i){
302 case GL_NEAREST_MIPMAP_NEAREST:
303 case GL_NEAREST_MIPMAP_LINEAR:
304 case GL_LINEAR_MIPMAP_NEAREST:
305 case GL_LINEAR_MIPMAP_LINEAR:
308 Error("unknown texture filter %x\n",i);
311 #ifdef OGL_RUNTIME_LOAD
312 #if defined(__WINDOWS__) || defined(__MINGW32__)
313 char *OglLibPath="opengl32.dll";
316 char *OglLibPath="libGL.so";
320 int ogl_init_load_library(void)
325 if ((t=FindArg("-gl_library")))
326 OglLibPath=Args[t+1];
328 retcode = OpenGL_LoadLibrary(true);
331 mprintf((0,"Opengl loaded ok\n"));
335 Error("Opengl: Functions not imported\n");
338 Error("Opengl: error loading %s\n",OglLibPath);
348 int mode = SM(640,480);
349 int retcode, t, glt = 0;
351 // Only do this function once!
356 #ifdef OGL_RUNTIME_LOAD
357 ogl_init_load_library();
360 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
361 if (FindArg("-gl_voodoo")){
363 gr_toggle_fullscreen();
365 if (FindArg("-fullscreen"))
366 gr_toggle_fullscreen();
368 if ((glt=FindArg("-gl_alttexmerge")))
370 if ((t=FindArg("-gl_stdtexmerge")))
371 if (t>=glt)//allow overriding of earlier args
374 if ((glt=FindArg("-gl_16bittextures")))
375 ogl_rgba_format=GL_RGB5_A1;
377 if ((glt=FindArg("-gl_mipmap"))){
378 GL_texmagfilt=GL_LINEAR;
379 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
381 if ((t=FindArg("-gl_simple"))){
382 if (t>=glt){//allow overriding of earlier args
384 GL_texmagfilt=GL_NEAREST;
385 GL_texminfilt=GL_NEAREST;
388 if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
389 if (t>=glt)//allow overriding of earlier args
390 GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
392 if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
393 if (t>=glt)//allow overriding of earlier args
394 GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
396 GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
397 mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps));
399 if ((t=FindArg("-gl_vidmem"))){
400 ogl_mem_target=atoi(Args[t+1])*1024*1024;
402 if ((t=FindArg("-gl_reticle"))){
403 gl_reticle=atoi(Args[t+1]);
405 //printf("ogl_mem_target=%i\n",ogl_mem_target);
407 ogl_init();//platform specific initialization
409 ogl_init_texture_list_internal();
411 MALLOC( grd_curscreen,grs_screen,1 );
412 memset( grd_curscreen, 0, sizeof(grs_screen));
413 grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
416 if ((retcode=gr_set_mode(mode)))
421 grd_curscreen->sc_canvas.cv_color = 0;
422 grd_curscreen->sc_canvas.cv_drawmode = 0;
423 grd_curscreen->sc_canvas.cv_font = NULL;
424 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
425 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
426 gr_set_current_canvas( &grd_curscreen->sc_canvas );
437 // mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
439 ogl_close();//platform specific code
441 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
442 d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
443 d_free(grd_curscreen);
445 #ifdef OGL_RUNTIME_LOAD
447 OpenGL_LoadLibrary(false);
450 extern int r_upixelc;
451 void ogl_upixelc(int x, int y, int c){
453 // printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
454 // if(Function_mode != FMODE_GAME){
455 // grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
457 OGL_DISABLE(TEXTURE_2D);
460 // glBegin(GL_LINES);
461 // ogl_pal=gr_current_pal;
462 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
463 // ogl_pal=gr_palette;
464 glVertex2f((x+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,1.0-(y+grd_curcanv->cv_bitmap.bm_y)/(float)last_height);
465 // glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
469 void ogl_urect(int left,int top,int right,int bot){
473 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
474 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
475 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
476 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
478 OGL_DISABLE(TEXTURE_2D);
479 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
487 void ogl_ulinec(int left,int top,int right,int bot,int c){
490 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
491 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
492 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
493 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
495 OGL_DISABLE(TEXTURE_2D);
496 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
504 GLfloat last_r=0, last_g=0, last_b=0;
506 void ogl_do_palfx(void){
508 OGL_DISABLE(TEXTURE_2D);
509 if (gr_palette_faded_out){
510 /* glEnable(GL_BLEND);
511 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
516 //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
518 glBlendFunc(GL_ONE,GL_ONE);
519 glColor3f(last_r,last_g,last_b);
520 // r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
534 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
537 void gr_palette_clear()
539 gr_palette_faded_out=1;
543 void gr_palette_step_up( int r, int g, int b )
545 if (gr_palette_faded_out) return;
547 // if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
552 do_pal_step=(r || g || b);*/
554 last_r = (r+gr_palette_gamma)/63.0;
555 last_g = (g+gr_palette_gamma)/63.0;
556 last_b = (b+gr_palette_gamma)/63.0;
558 do_pal_step=(r || g || b || gr_palette_gamma);
562 //added on 980913 by adb to fix palette problems
563 // need a min without side effects...
565 static inline int min(int x, int y) { return x < y ? x : y; }
568 void gr_palette_load( ubyte *pal )
572 for (i=0; i<768; i++ ) {
573 gr_current_pal[i] = pal[i];
574 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
576 //palette = screen->format->palette;
578 gr_palette_faded_out=0;
580 init_computed_colors();
585 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
587 gr_palette_faded_out=1;
593 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
595 gr_palette_faded_out=0;
601 void gr_palette_read(ubyte * pal)
604 for (i=0; i<768; i++ ) {
605 pal[i]=gr_current_pal[i];
606 if (pal[i] > 63) pal[i] = 63;
610 //writes out an uncompressed RGB .tga file
611 //if we got really spiffy, we could optionally link in libpng or something, and use that.
612 void write_bmp(char *savename,int w,int h,unsigned char *buf){
614 #if defined(__WINDOWS__) || defined(__MINGW32__)
615 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR);
617 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
620 GLubyte targaMagic[12] = { 0, //no identification field
622 2,//RGB image (well, BGR, actually)
623 0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
630 write (f,targaMagic,sizeof(targaMagic));
631 blah=w%256;write (f,&blah,1);//w, low
632 blah=w/256;write (f,&blah,1);//w, high
633 blah=h%256;write (f,&blah,1);//h, low
634 blah=h/256;write (f,&blah,1);//h, high
635 blah=24;write (f,&blah,1);//24 bpp
636 blah=0;write (f,&blah,1);//no attribute bits, origin is lowerleft, no interleave
639 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
651 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
658 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
661 void save_screen_shot(int automap_flag)
665 static int savenum=0;
669 if (!ogl_readpixels_ok){
671 hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
677 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
678 if ( savenum == 9999 ) savenum = 0;
679 sprintf(savename,"scrn%04d.tga",savenum++);
681 while(!access(savename,0))
683 if ( savenum == 9999 ) savenum = 0;
684 sprintf(savename,"scrn%04d.tga",savenum++);
686 sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
687 //end this section addition/change - Victor Rachels
690 // save_font = grd_curcanv->cv_font;
691 // gr_set_curfont(GAME_FONT);
692 // gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
693 // gr_get_string_size(message,&w,&h,&aw);
694 // modex_print_message(32, 2, message);
696 hud_message(MSGC_GAME_FEEDBACK,message);
699 buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
700 glReadBuffer(GL_FRONT);
701 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
702 write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);