2 * $Source: /cvs/cvsroot/d2x/video/ogl_gr.c,v $
5 * $Date: 2001-01-29 13:47:52 $
7 * // OGL video functions. - Added 9/15/99 Matthew Mueller
9 * $Log: not supported by cvs2svn $
25 #include <sys/types.h>
58 void gr_palette_clear(); // Function prototype for gr_init;
63 int gr_check_fullscreen(void){
64 return ogl_fullscreen;
66 void gr_do_fullscreen(int f){
68 ogl_fullscreen=1;//force fullscreen mode on voodoos.
72 ogl_do_fullscreen_internal();
75 int gr_toggle_fullscreen(void){
76 gr_do_fullscreen(!ogl_fullscreen);
77 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
78 return ogl_fullscreen;
82 void ogl_init_state(void){
83 /* select clearing (background) color */
84 glClearColor(0.0, 0.0, 0.0, 0.0);
85 glShadeModel(GL_SMOOTH);
87 /* initialize viewing values */
88 glMatrixMode(GL_PROJECTION);
90 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
91 glScalef(1.0, -1.0, 1.0);
92 glTranslatef(0.0, -1.0, 0.0);
93 gr_palette_step_up(0,0,0);//in case its left over from in game
96 int last_screen_mode=-1;
97 void ogl_set_screen_mode(void){
98 if (last_screen_mode==Screen_mode)
100 OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
101 // 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);
102 if (Screen_mode==SCREEN_GAME){
103 glDrawBuffer(GL_BACK);
105 glClearColor(0.0, 0.0, 0.0, 0.0);
106 glDrawBuffer(GL_FRONT);
107 glClear(GL_COLOR_BUFFER_BIT);
108 glMatrixMode(GL_PROJECTION);
109 glLoadIdentity();//clear matrix
110 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
111 glMatrixMode(GL_MODELVIEW);
112 glLoadIdentity();//clear matrix
114 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
116 last_screen_mode=Screen_mode;
122 if(Screen_mode != SCREEN_GAME){
128 const char *gl_vendor,*gl_renderer,*gl_version,*gl_extensions;
129 void ogl_get_verinfo(void){
131 gl_vendor=glGetString(GL_VENDOR);
132 gl_renderer=glGetString(GL_RENDERER);
133 gl_version=glGetString(GL_VERSION);
134 gl_extensions=glGetString(GL_EXTENSIONS);
136 printf("gl vendor:%s renderer:%s version:%s extensions:%s\n",gl_vendor,gl_renderer,gl_version,gl_extensions);
138 ogl_intensity4_ok=1;ogl_luminance4_alpha4_ok=1;ogl_rgba2_ok=1;
140 //add driver specific hacks here. whee.
141 if (stricmp(gl_renderer,"Mesa NVIDIA RIVA 1.0\n")==0 && stricmp(gl_version,"1.2 Mesa 3.0")==0){
142 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
143 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
146 //allow overriding of stuff.
147 if ((t=FindArg("-gl_intensity4_ok"))){
148 ogl_intensity4_ok=atoi(Args[t+1]);
150 if ((t=FindArg("-gl_luminance4_alpha4_ok"))){
151 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
153 if ((t=FindArg("-gl_rgba2_ok"))){
154 ogl_rgba2_ok=atoi(Args[t+1]);
156 if ((t=FindArg("-gl_readpixels_ok"))){
157 ogl_readpixels_ok=atoi(Args[t+1]);
160 printf("gl_intensity4:%i gl_luminance4_alpha4:%i gl_rgba2:%i gl_readpixels:%i\n",ogl_intensity4_ok,ogl_luminance4_alpha4_ok,ogl_rgba2_ok,ogl_readpixels_ok);
163 int gr_set_mode(u_int32_t mode)
177 //if (screen != NULL) gr_palette_clear();
181 memset( grd_curscreen, 0, sizeof(grs_screen));
182 grd_curscreen->sc_mode = mode;
183 grd_curscreen->sc_w = w;
184 grd_curscreen->sc_h = h;
185 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
186 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
187 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
188 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
189 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
190 //grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
191 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = w;
192 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_OGL;
193 //grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
194 grd_curscreen->sc_canvas.cv_bitmap.bm_data = realloc(grd_curscreen->sc_canvas.cv_bitmap.bm_data,w*h);
195 gr_set_current_canvas(NULL);
196 //gr_enable_default_palette_loading();
198 ogl_init_window(w,h);//platform specific code
202 OGL_VIEWPORT(0,0,w,h);
204 ogl_set_screen_mode();
206 //gamefont_choose_game_font(w,h);
211 #define GLstrcmptestr(a,b) if (stricmp(a,#b)==0 || stricmp(a,"GL_" #b)==0)return GL_ ## b;
212 int ogl_atotexfilti(char *a,int min){
213 GLstrcmptestr(a,NEAREST);
214 GLstrcmptestr(a,LINEAR);
215 if (min){//mipmaps are valid only for the min filter
216 GLstrcmptestr(a,NEAREST_MIPMAP_NEAREST);
217 GLstrcmptestr(a,NEAREST_MIPMAP_LINEAR);
218 GLstrcmptestr(a,LINEAR_MIPMAP_NEAREST);
219 GLstrcmptestr(a,LINEAR_MIPMAP_LINEAR);
221 Error("unknown/invalid texture filter %s\n",a);
222 // return GL_NEAREST;
224 int ogl_testneedmipmaps(int i){
229 case GL_NEAREST_MIPMAP_NEAREST:
230 case GL_NEAREST_MIPMAP_LINEAR:
231 case GL_LINEAR_MIPMAP_NEAREST:
232 case GL_LINEAR_MIPMAP_LINEAR:
235 Error("unknown texture filter %x\n",i);
238 #ifdef OGL_RUNTIME_LOAD
240 char *OglLibPath="opengl32.dll";
243 char *OglLibPath="libGL.so";
247 int ogl_init_load_library(void)
252 if ((t=FindArg("-gl_library")))
253 OglLibPath=Args[t+1];
255 retcode = OpenGL_LoadLibrary(true);
258 mprintf((0,"Opengl loaded ok\n"));
262 Error("Opengl: Functions not imported\n");
265 Error("Opengl: error loading %s\n",OglLibPath);
275 int mode = SM(640,480);
277 // Only do this function once!
282 #ifdef OGL_RUNTIME_LOAD
283 ogl_init_load_library();
286 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
287 if (FindArg("-gl_voodoo")){
289 gr_toggle_fullscreen();
291 if (FindArg("-fullscreen"))
292 gr_toggle_fullscreen();
294 if ((glt=FindArg("-gl_alttexmerge")))
296 if ((t=FindArg("-gl_stdtexmerge")))
297 if (t>=glt)//allow overriding of earlier args
300 if ((glt=FindArg("-gl_16bittextures")))
301 ogl_rgba_format=GL_RGB5_A1;
303 if ((glt=FindArg("-gl_mipmap"))){
304 GL_texmagfilt=GL_LINEAR;
305 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
307 if ((t=FindArg("-gl_simple"))){
308 if (t>=glt){//allow overriding of earlier args
310 GL_texmagfilt=GL_NEAREST;
311 GL_texminfilt=GL_NEAREST;
314 if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
315 if (t>=glt)//allow overriding of earlier args
316 GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
318 if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
319 if (t>=glt)//allow overriding of earlier args
320 GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
322 GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
323 mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps));
325 if ((t=FindArg("-gl_vidmem"))){
326 ogl_mem_target=atoi(Args[t+1])*1024*1024;
328 if ((t=FindArg("-gl_reticle"))){
329 gl_reticle=atoi(Args[t+1]);
331 //printf("ogl_mem_target=%i\n",ogl_mem_target);
333 ogl_init();//platform specific initialization
335 ogl_init_texture_list_internal();
337 MALLOC( grd_curscreen,grs_screen,1 );
338 memset( grd_curscreen, 0, sizeof(grs_screen));
339 grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
342 if ((retcode=gr_set_mode(mode)))
346 grd_curscreen->sc_canvas.cv_color = 0;
347 grd_curscreen->sc_canvas.cv_drawmode = 0;
348 grd_curscreen->sc_canvas.cv_font = NULL;
349 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
350 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
351 gr_set_current_canvas( &grd_curscreen->sc_canvas );
362 // mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
364 ogl_close();//platform specific code
366 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
367 d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
368 d_free(grd_curscreen);
370 #ifdef OGL_RUNTIME_LOAD
372 OpenGL_LoadLibrary(false);
375 extern int r_upixelc;
376 void ogl_upixelc(int x, int y, int c){
378 // printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
379 // if(Function_mode != FMODE_GAME){
380 // grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
382 OGL_DISABLE(TEXTURE_2D);
385 // glBegin(GL_LINES);
386 // ogl_pal=gr_current_pal;
387 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
388 // ogl_pal=gr_palette;
389 glVertex2f((x+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,1.0-(y+grd_curcanv->cv_bitmap.bm_y)/(float)last_height);
390 // glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
394 void ogl_urect(int left,int top,int right,int bot){
398 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
399 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
400 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
401 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
403 OGL_DISABLE(TEXTURE_2D);
404 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
412 void ogl_ulinec(int left,int top,int right,int bot,int c){
415 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
416 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
417 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
418 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
420 OGL_DISABLE(TEXTURE_2D);
421 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
429 GLfloat last_r=0, last_g=0, last_b=0;
431 void ogl_do_palfx(void){
433 OGL_DISABLE(TEXTURE_2D);
434 if (gr_palette_faded_out){
435 /* glEnable(GL_BLEND);
436 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
441 //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
443 glBlendFunc(GL_ONE,GL_ONE);
444 glColor3f(last_r,last_g,last_b);
445 // r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
459 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
462 void gr_palette_clear()
464 gr_palette_faded_out=1;
468 void gr_palette_step_up( int r, int g, int b )
470 if (gr_palette_faded_out) return;
472 // if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
477 do_pal_step=(r || g || b);*/
479 last_r = (r+gr_palette_gamma)/63.0;
480 last_g = (g+gr_palette_gamma)/63.0;
481 last_b = (b+gr_palette_gamma)/63.0;
483 do_pal_step=(r || g || b || gr_palette_gamma);
487 //added on 980913 by adb to fix palette problems
488 // need a min without side effects...
490 static inline int min(int x, int y) { return x < y ? x : y; }
493 void gr_palette_load( ubyte *pal )
497 for (i=0; i<768; i++ ) {
498 gr_current_pal[i] = pal[i];
499 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
501 //palette = screen->format->palette;
503 gr_palette_faded_out=0;
505 init_computed_colors();
510 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
512 gr_palette_faded_out=1;
518 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
520 gr_palette_faded_out=0;
526 void gr_palette_read(ubyte * pal)
529 for (i=0; i<768; i++ ) {
530 pal[i]=gr_current_pal[i];
531 if (pal[i] > 63) pal[i] = 63;
535 //writes out an uncompressed RGB .tga file
536 //if we got really spiffy, we could optionally link in libpng or something, and use that.
537 void write_bmp(char *savename,int w,int h,unsigned char *buf){
540 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR);
542 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
545 GLubyte targaMagic[12] = { 0, //no identification field
547 2,//RGB image (well, BGR, actually)
548 0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
555 write (f,targaMagic,sizeof(targaMagic));
556 blah=w%256;write (f,&blah,1);//w, low
557 blah=w/256;write (f,&blah,1);//w, high
558 blah=h%256;write (f,&blah,1);//h, low
559 blah=h/256;write (f,&blah,1);//h, high
560 blah=24;write (f,&blah,1);//24 bpp
561 blah=0;write (f,&blah,1);//no attribute bits, origin is lowerleft, no interleave
564 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
576 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
583 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
586 void save_screen_shot(int automap_flag)
590 static int savenum=0;
594 if (!ogl_readpixels_ok){
596 // hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
602 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
603 if ( savenum == 9999 ) savenum = 0;
604 sprintf(savename,"scrn%04d.tga",savenum++);
606 while(!access(savename,0))
608 if ( savenum == 9999 ) savenum = 0;
609 sprintf(savename,"scrn%04d.tga",savenum++);
611 sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
612 //end this section addition/change - Victor Rachels
615 // save_font = grd_curcanv->cv_font;
616 // gr_set_curfont(GAME_FONT);
617 // gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
618 // gr_get_string_size(message,&w,&h,&aw);
619 // modex_print_message(32, 2, message);
621 // hud_message(MSGC_GAME_FEEDBACK,message);
624 buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
625 glReadBuffer(GL_FRONT);
626 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
627 write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);