2 * $Source: /cvs/cvsroot/d2x/video/ogl_gr.c,v $
5 * $Date: 2001-10-18 23:59:23 $
7 * // OGL video functions. - Added 9/15/99 Matthew Mueller
9 * $Log: not supported by cvs2svn $
10 * Revision 1.5 2001/10/12 00:18:40 bradleyb
11 * Switched from Cygwin to mingw32 on MS boxes. Vastly improved compilability.
13 * Revision 1.4 2001/01/29 13:47:52 bradleyb
14 * Fixed build, some minor cleanups.
31 #include <sys/types.h>
64 void gr_palette_clear(); // Function prototype for gr_init;
69 int gr_check_fullscreen(void){
70 return ogl_fullscreen;
72 void gr_do_fullscreen(int f){
74 ogl_fullscreen=1;//force fullscreen mode on voodoos.
78 ogl_do_fullscreen_internal();
81 int gr_toggle_fullscreen(void){
82 gr_do_fullscreen(!ogl_fullscreen);
83 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
84 return ogl_fullscreen;
88 void ogl_init_state(void){
89 /* select clearing (background) color */
90 glClearColor(0.0, 0.0, 0.0, 0.0);
91 glShadeModel(GL_SMOOTH);
93 /* initialize viewing values */
94 glMatrixMode(GL_PROJECTION);
96 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
97 glScalef(1.0, -1.0, 1.0);
98 glTranslatef(0.0, -1.0, 0.0);
99 gr_palette_step_up(0,0,0);//in case its left over from in game
102 int last_screen_mode=-1;
103 void ogl_set_screen_mode(void){
104 if (last_screen_mode==Screen_mode)
106 OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
107 // 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);
108 if (Screen_mode==SCREEN_GAME){
109 glDrawBuffer(GL_BACK);
111 glClearColor(0.0, 0.0, 0.0, 0.0);
112 glDrawBuffer(GL_FRONT);
113 glClear(GL_COLOR_BUFFER_BIT);
114 glMatrixMode(GL_PROJECTION);
115 glLoadIdentity();//clear matrix
116 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
117 glMatrixMode(GL_MODELVIEW);
118 glLoadIdentity();//clear matrix
120 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
122 last_screen_mode=Screen_mode;
128 if(Screen_mode != SCREEN_GAME){
134 const char *gl_vendor,*gl_renderer,*gl_version,*gl_extensions;
135 void ogl_get_verinfo(void){
137 gl_vendor=glGetString(GL_VENDOR);
138 gl_renderer=glGetString(GL_RENDERER);
139 gl_version=glGetString(GL_VERSION);
140 gl_extensions=glGetString(GL_EXTENSIONS);
142 printf("gl vendor:%s renderer:%s version:%s extensions:%s\n",gl_vendor,gl_renderer,gl_version,gl_extensions);
144 ogl_intensity4_ok=1;ogl_luminance4_alpha4_ok=1;ogl_rgba2_ok=1;
146 //add driver specific hacks here. whee.
147 if (stricmp(gl_renderer,"Mesa NVIDIA RIVA 1.0\n")==0 && stricmp(gl_version,"1.2 Mesa 3.0")==0){
148 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
149 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
152 //allow overriding of stuff.
153 if ((t=FindArg("-gl_intensity4_ok"))){
154 ogl_intensity4_ok=atoi(Args[t+1]);
156 if ((t=FindArg("-gl_luminance4_alpha4_ok"))){
157 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
159 if ((t=FindArg("-gl_rgba2_ok"))){
160 ogl_rgba2_ok=atoi(Args[t+1]);
162 if ((t=FindArg("-gl_readpixels_ok"))){
163 ogl_readpixels_ok=atoi(Args[t+1]);
166 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);
169 int gr_set_mode(u_int32_t mode)
183 //if (screen != NULL) gr_palette_clear();
187 memset( grd_curscreen, 0, sizeof(grs_screen));
188 grd_curscreen->sc_mode = mode;
189 grd_curscreen->sc_w = w;
190 grd_curscreen->sc_h = h;
191 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
192 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
193 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
194 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
195 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
196 //grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
197 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = w;
198 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_OGL;
199 //grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
200 grd_curscreen->sc_canvas.cv_bitmap.bm_data = realloc(grd_curscreen->sc_canvas.cv_bitmap.bm_data,w*h);
201 gr_set_current_canvas(NULL);
202 //gr_enable_default_palette_loading();
204 ogl_init_window(w,h);//platform specific code
208 OGL_VIEWPORT(0,0,w,h);
210 ogl_set_screen_mode();
212 //gamefont_choose_game_font(w,h);
217 #define GLstrcmptestr(a,b) if (stricmp(a,#b)==0 || stricmp(a,"GL_" #b)==0)return GL_ ## b;
218 int ogl_atotexfilti(char *a,int min){
219 GLstrcmptestr(a,NEAREST);
220 GLstrcmptestr(a,LINEAR);
221 if (min){//mipmaps are valid only for the min filter
222 GLstrcmptestr(a,NEAREST_MIPMAP_NEAREST);
223 GLstrcmptestr(a,NEAREST_MIPMAP_LINEAR);
224 GLstrcmptestr(a,LINEAR_MIPMAP_NEAREST);
225 GLstrcmptestr(a,LINEAR_MIPMAP_LINEAR);
227 Error("unknown/invalid texture filter %s\n",a);
228 // return GL_NEAREST;
230 int ogl_testneedmipmaps(int i){
235 case GL_NEAREST_MIPMAP_NEAREST:
236 case GL_NEAREST_MIPMAP_LINEAR:
237 case GL_LINEAR_MIPMAP_NEAREST:
238 case GL_LINEAR_MIPMAP_LINEAR:
241 Error("unknown texture filter %x\n",i);
244 #ifdef OGL_RUNTIME_LOAD
245 #if defined(__WINDOWS__) || defined(__MINGW32__)
246 char *OglLibPath="opengl32.dll";
248 char *OglLibPath="libGL.so";
252 int ogl_init_load_library(void)
257 if ((t=FindArg("-gl_library")))
258 OglLibPath=Args[t+1];
260 retcode = OpenGL_LoadLibrary(true);
263 mprintf((0,"Opengl loaded ok\n"));
267 Error("Opengl: Functions not imported\n");
270 Error("Opengl: error loading %s\n",OglLibPath);
280 int mode = SM(640,480);
282 // Only do this function once!
287 #ifdef OGL_RUNTIME_LOAD
288 ogl_init_load_library();
291 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
292 if (FindArg("-gl_voodoo")){
294 gr_toggle_fullscreen();
296 if (FindArg("-fullscreen"))
297 gr_toggle_fullscreen();
299 if ((glt=FindArg("-gl_alttexmerge")))
301 if ((t=FindArg("-gl_stdtexmerge")))
302 if (t>=glt)//allow overriding of earlier args
305 if ((glt=FindArg("-gl_16bittextures")))
306 ogl_rgba_format=GL_RGB5_A1;
308 if ((glt=FindArg("-gl_mipmap"))){
309 GL_texmagfilt=GL_LINEAR;
310 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
312 if ((t=FindArg("-gl_simple"))){
313 if (t>=glt){//allow overriding of earlier args
315 GL_texmagfilt=GL_NEAREST;
316 GL_texminfilt=GL_NEAREST;
319 if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
320 if (t>=glt)//allow overriding of earlier args
321 GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
323 if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
324 if (t>=glt)//allow overriding of earlier args
325 GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
327 GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
328 mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps));
330 if ((t=FindArg("-gl_vidmem"))){
331 ogl_mem_target=atoi(Args[t+1])*1024*1024;
333 if ((t=FindArg("-gl_reticle"))){
334 gl_reticle=atoi(Args[t+1]);
336 //printf("ogl_mem_target=%i\n",ogl_mem_target);
338 ogl_init();//platform specific initialization
340 ogl_init_texture_list_internal();
342 MALLOC( grd_curscreen,grs_screen,1 );
343 memset( grd_curscreen, 0, sizeof(grs_screen));
344 grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
347 if ((retcode=gr_set_mode(mode)))
351 grd_curscreen->sc_canvas.cv_color = 0;
352 grd_curscreen->sc_canvas.cv_drawmode = 0;
353 grd_curscreen->sc_canvas.cv_font = NULL;
354 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
355 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
356 gr_set_current_canvas( &grd_curscreen->sc_canvas );
367 // mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
369 ogl_close();//platform specific code
371 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
372 d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
373 d_free(grd_curscreen);
375 #ifdef OGL_RUNTIME_LOAD
377 OpenGL_LoadLibrary(false);
380 extern int r_upixelc;
381 void ogl_upixelc(int x, int y, int c){
383 // printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
384 // if(Function_mode != FMODE_GAME){
385 // grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
387 OGL_DISABLE(TEXTURE_2D);
390 // glBegin(GL_LINES);
391 // ogl_pal=gr_current_pal;
392 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
393 // ogl_pal=gr_palette;
394 glVertex2f((x+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,1.0-(y+grd_curcanv->cv_bitmap.bm_y)/(float)last_height);
395 // glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
399 void ogl_urect(int left,int top,int right,int bot){
403 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
404 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
405 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
406 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
408 OGL_DISABLE(TEXTURE_2D);
409 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
417 void ogl_ulinec(int left,int top,int right,int bot,int c){
420 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
421 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
422 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
423 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
425 OGL_DISABLE(TEXTURE_2D);
426 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
434 GLfloat last_r=0, last_g=0, last_b=0;
436 void ogl_do_palfx(void){
438 OGL_DISABLE(TEXTURE_2D);
439 if (gr_palette_faded_out){
440 /* glEnable(GL_BLEND);
441 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
446 //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
448 glBlendFunc(GL_ONE,GL_ONE);
449 glColor3f(last_r,last_g,last_b);
450 // r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
464 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
467 void gr_palette_clear()
469 gr_palette_faded_out=1;
473 void gr_palette_step_up( int r, int g, int b )
475 if (gr_palette_faded_out) return;
477 // if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
482 do_pal_step=(r || g || b);*/
484 last_r = (r+gr_palette_gamma)/63.0;
485 last_g = (g+gr_palette_gamma)/63.0;
486 last_b = (b+gr_palette_gamma)/63.0;
488 do_pal_step=(r || g || b || gr_palette_gamma);
492 //added on 980913 by adb to fix palette problems
493 // need a min without side effects...
495 static inline int min(int x, int y) { return x < y ? x : y; }
498 void gr_palette_load( ubyte *pal )
502 for (i=0; i<768; i++ ) {
503 gr_current_pal[i] = pal[i];
504 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
506 //palette = screen->format->palette;
508 gr_palette_faded_out=0;
510 init_computed_colors();
515 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
517 gr_palette_faded_out=1;
523 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
525 gr_palette_faded_out=0;
531 void gr_palette_read(ubyte * pal)
534 for (i=0; i<768; i++ ) {
535 pal[i]=gr_current_pal[i];
536 if (pal[i] > 63) pal[i] = 63;
540 //writes out an uncompressed RGB .tga file
541 //if we got really spiffy, we could optionally link in libpng or something, and use that.
542 void write_bmp(char *savename,int w,int h,unsigned char *buf){
544 #if defined(__WINDOWS__) || defined (__MINGW32__)
545 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR);
547 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
550 GLubyte targaMagic[12] = { 0, //no identification field
552 2,//RGB image (well, BGR, actually)
553 0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
560 write (f,targaMagic,sizeof(targaMagic));
561 blah=w%256;write (f,&blah,1);//w, low
562 blah=w/256;write (f,&blah,1);//w, high
563 blah=h%256;write (f,&blah,1);//h, low
564 blah=h/256;write (f,&blah,1);//h, high
565 blah=24;write (f,&blah,1);//24 bpp
566 blah=0;write (f,&blah,1);//no attribute bits, origin is lowerleft, no interleave
569 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
581 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
588 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
591 void save_screen_shot(int automap_flag)
595 static int savenum=0;
599 if (!ogl_readpixels_ok){
601 // hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
607 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
608 if ( savenum == 9999 ) savenum = 0;
609 sprintf(savename,"scrn%04d.tga",savenum++);
611 while(!access(savename,0))
613 if ( savenum == 9999 ) savenum = 0;
614 sprintf(savename,"scrn%04d.tga",savenum++);
616 sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
617 //end this section addition/change - Victor Rachels
620 // save_font = grd_curcanv->cv_font;
621 // gr_set_curfont(GAME_FONT);
622 // gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
623 // gr_get_string_size(message,&w,&h,&aw);
624 // modex_print_message(32, 2, message);
626 // hud_message(MSGC_GAME_FEEDBACK,message);
629 buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
630 glReadBuffer(GL_FRONT);
631 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
632 write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);