2 * $Source: /cvs/cvsroot/d2x/video/ogl_gr.c,v $
5 * $Date: 2001-10-12 00:18:40 $
7 * // OGL video functions. - Added 9/15/99 Matthew Mueller
9 * $Log: not supported by cvs2svn $
10 * Revision 1.4 2001/01/29 13:47:52 bradleyb
11 * Fixed build, some minor cleanups.
28 #include <sys/types.h>
61 void gr_palette_clear(); // Function prototype for gr_init;
66 int gr_check_fullscreen(void){
67 return ogl_fullscreen;
69 void gr_do_fullscreen(int f){
71 ogl_fullscreen=1;//force fullscreen mode on voodoos.
75 ogl_do_fullscreen_internal();
78 int gr_toggle_fullscreen(void){
79 gr_do_fullscreen(!ogl_fullscreen);
80 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
81 return ogl_fullscreen;
85 void ogl_init_state(void){
86 /* select clearing (background) color */
87 glClearColor(0.0, 0.0, 0.0, 0.0);
88 glShadeModel(GL_SMOOTH);
90 /* initialize viewing values */
91 glMatrixMode(GL_PROJECTION);
93 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
94 glScalef(1.0, -1.0, 1.0);
95 glTranslatef(0.0, -1.0, 0.0);
96 gr_palette_step_up(0,0,0);//in case its left over from in game
99 int last_screen_mode=-1;
100 void ogl_set_screen_mode(void){
101 if (last_screen_mode==Screen_mode)
103 OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
104 // 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);
105 if (Screen_mode==SCREEN_GAME){
106 glDrawBuffer(GL_BACK);
108 glClearColor(0.0, 0.0, 0.0, 0.0);
109 glDrawBuffer(GL_FRONT);
110 glClear(GL_COLOR_BUFFER_BIT);
111 glMatrixMode(GL_PROJECTION);
112 glLoadIdentity();//clear matrix
113 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
114 glMatrixMode(GL_MODELVIEW);
115 glLoadIdentity();//clear matrix
117 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
119 last_screen_mode=Screen_mode;
125 if(Screen_mode != SCREEN_GAME){
131 const char *gl_vendor,*gl_renderer,*gl_version,*gl_extensions;
132 void ogl_get_verinfo(void){
134 gl_vendor=glGetString(GL_VENDOR);
135 gl_renderer=glGetString(GL_RENDERER);
136 gl_version=glGetString(GL_VERSION);
137 gl_extensions=glGetString(GL_EXTENSIONS);
139 printf("gl vendor:%s renderer:%s version:%s extensions:%s\n",gl_vendor,gl_renderer,gl_version,gl_extensions);
141 ogl_intensity4_ok=1;ogl_luminance4_alpha4_ok=1;ogl_rgba2_ok=1;
143 //add driver specific hacks here. whee.
144 if (stricmp(gl_renderer,"Mesa NVIDIA RIVA 1.0\n")==0 && stricmp(gl_version,"1.2 Mesa 3.0")==0){
145 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
146 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
149 //allow overriding of stuff.
150 if ((t=FindArg("-gl_intensity4_ok"))){
151 ogl_intensity4_ok=atoi(Args[t+1]);
153 if ((t=FindArg("-gl_luminance4_alpha4_ok"))){
154 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
156 if ((t=FindArg("-gl_rgba2_ok"))){
157 ogl_rgba2_ok=atoi(Args[t+1]);
159 if ((t=FindArg("-gl_readpixels_ok"))){
160 ogl_readpixels_ok=atoi(Args[t+1]);
163 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);
166 int gr_set_mode(u_int32_t mode)
180 //if (screen != NULL) gr_palette_clear();
184 memset( grd_curscreen, 0, sizeof(grs_screen));
185 grd_curscreen->sc_mode = mode;
186 grd_curscreen->sc_w = w;
187 grd_curscreen->sc_h = h;
188 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
189 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
190 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
191 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
192 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
193 //grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
194 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = w;
195 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_OGL;
196 //grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
197 grd_curscreen->sc_canvas.cv_bitmap.bm_data = realloc(grd_curscreen->sc_canvas.cv_bitmap.bm_data,w*h);
198 gr_set_current_canvas(NULL);
199 //gr_enable_default_palette_loading();
201 ogl_init_window(w,h);//platform specific code
205 OGL_VIEWPORT(0,0,w,h);
207 ogl_set_screen_mode();
209 //gamefont_choose_game_font(w,h);
214 #define GLstrcmptestr(a,b) if (stricmp(a,#b)==0 || stricmp(a,"GL_" #b)==0)return GL_ ## b;
215 int ogl_atotexfilti(char *a,int min){
216 GLstrcmptestr(a,NEAREST);
217 GLstrcmptestr(a,LINEAR);
218 if (min){//mipmaps are valid only for the min filter
219 GLstrcmptestr(a,NEAREST_MIPMAP_NEAREST);
220 GLstrcmptestr(a,NEAREST_MIPMAP_LINEAR);
221 GLstrcmptestr(a,LINEAR_MIPMAP_NEAREST);
222 GLstrcmptestr(a,LINEAR_MIPMAP_LINEAR);
224 Error("unknown/invalid texture filter %s\n",a);
225 // return GL_NEAREST;
227 int ogl_testneedmipmaps(int i){
232 case GL_NEAREST_MIPMAP_NEAREST:
233 case GL_NEAREST_MIPMAP_LINEAR:
234 case GL_LINEAR_MIPMAP_NEAREST:
235 case GL_LINEAR_MIPMAP_LINEAR:
238 Error("unknown texture filter %x\n",i);
241 #ifdef OGL_RUNTIME_LOAD
242 #if defined(__WINDOWS__) || defined(__MINGW32__)
243 char *OglLibPath="opengl32.dll";
244 #elif defined(__ENV_LINUX__)
245 char *OglLibPath="libGL.so";
249 int ogl_init_load_library(void)
254 if ((t=FindArg("-gl_library")))
255 OglLibPath=Args[t+1];
257 retcode = OpenGL_LoadLibrary(true);
260 mprintf((0,"Opengl loaded ok\n"));
264 Error("Opengl: Functions not imported\n");
267 Error("Opengl: error loading %s\n",OglLibPath);
277 int mode = SM(640,480);
279 // Only do this function once!
284 #ifdef OGL_RUNTIME_LOAD
285 ogl_init_load_library();
288 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
289 if (FindArg("-gl_voodoo")){
291 gr_toggle_fullscreen();
293 if (FindArg("-fullscreen"))
294 gr_toggle_fullscreen();
296 if ((glt=FindArg("-gl_alttexmerge")))
298 if ((t=FindArg("-gl_stdtexmerge")))
299 if (t>=glt)//allow overriding of earlier args
302 if ((glt=FindArg("-gl_16bittextures")))
303 ogl_rgba_format=GL_RGB5_A1;
305 if ((glt=FindArg("-gl_mipmap"))){
306 GL_texmagfilt=GL_LINEAR;
307 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
309 if ((t=FindArg("-gl_simple"))){
310 if (t>=glt){//allow overriding of earlier args
312 GL_texmagfilt=GL_NEAREST;
313 GL_texminfilt=GL_NEAREST;
316 if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
317 if (t>=glt)//allow overriding of earlier args
318 GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
320 if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
321 if (t>=glt)//allow overriding of earlier args
322 GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
324 GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
325 mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps));
327 if ((t=FindArg("-gl_vidmem"))){
328 ogl_mem_target=atoi(Args[t+1])*1024*1024;
330 if ((t=FindArg("-gl_reticle"))){
331 gl_reticle=atoi(Args[t+1]);
333 //printf("ogl_mem_target=%i\n",ogl_mem_target);
335 ogl_init();//platform specific initialization
337 ogl_init_texture_list_internal();
339 MALLOC( grd_curscreen,grs_screen,1 );
340 memset( grd_curscreen, 0, sizeof(grs_screen));
341 grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
344 if ((retcode=gr_set_mode(mode)))
348 grd_curscreen->sc_canvas.cv_color = 0;
349 grd_curscreen->sc_canvas.cv_drawmode = 0;
350 grd_curscreen->sc_canvas.cv_font = NULL;
351 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
352 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
353 gr_set_current_canvas( &grd_curscreen->sc_canvas );
364 // mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
366 ogl_close();//platform specific code
368 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
369 d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
370 d_free(grd_curscreen);
372 #ifdef OGL_RUNTIME_LOAD
374 OpenGL_LoadLibrary(false);
377 extern int r_upixelc;
378 void ogl_upixelc(int x, int y, int c){
380 // printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
381 // if(Function_mode != FMODE_GAME){
382 // grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
384 OGL_DISABLE(TEXTURE_2D);
387 // glBegin(GL_LINES);
388 // ogl_pal=gr_current_pal;
389 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
390 // ogl_pal=gr_palette;
391 glVertex2f((x+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,1.0-(y+grd_curcanv->cv_bitmap.bm_y)/(float)last_height);
392 // glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
396 void ogl_urect(int left,int top,int right,int bot){
400 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
401 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
402 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
403 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
405 OGL_DISABLE(TEXTURE_2D);
406 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
414 void ogl_ulinec(int left,int top,int right,int bot,int c){
417 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
418 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
419 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
420 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
422 OGL_DISABLE(TEXTURE_2D);
423 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
431 GLfloat last_r=0, last_g=0, last_b=0;
433 void ogl_do_palfx(void){
435 OGL_DISABLE(TEXTURE_2D);
436 if (gr_palette_faded_out){
437 /* glEnable(GL_BLEND);
438 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
443 //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
445 glBlendFunc(GL_ONE,GL_ONE);
446 glColor3f(last_r,last_g,last_b);
447 // r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
461 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
464 void gr_palette_clear()
466 gr_palette_faded_out=1;
470 void gr_palette_step_up( int r, int g, int b )
472 if (gr_palette_faded_out) return;
474 // if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
479 do_pal_step=(r || g || b);*/
481 last_r = (r+gr_palette_gamma)/63.0;
482 last_g = (g+gr_palette_gamma)/63.0;
483 last_b = (b+gr_palette_gamma)/63.0;
485 do_pal_step=(r || g || b || gr_palette_gamma);
489 //added on 980913 by adb to fix palette problems
490 // need a min without side effects...
492 static inline int min(int x, int y) { return x < y ? x : y; }
495 void gr_palette_load( ubyte *pal )
499 for (i=0; i<768; i++ ) {
500 gr_current_pal[i] = pal[i];
501 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
503 //palette = screen->format->palette;
505 gr_palette_faded_out=0;
507 init_computed_colors();
512 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
514 gr_palette_faded_out=1;
520 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
522 gr_palette_faded_out=0;
528 void gr_palette_read(ubyte * pal)
531 for (i=0; i<768; i++ ) {
532 pal[i]=gr_current_pal[i];
533 if (pal[i] > 63) pal[i] = 63;
537 //writes out an uncompressed RGB .tga file
538 //if we got really spiffy, we could optionally link in libpng or something, and use that.
539 void write_bmp(char *savename,int w,int h,unsigned char *buf){
541 #if defined(__WINDOWS__) || defined (__MINGW32__)
542 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR);
544 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
547 GLubyte targaMagic[12] = { 0, //no identification field
549 2,//RGB image (well, BGR, actually)
550 0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
557 write (f,targaMagic,sizeof(targaMagic));
558 blah=w%256;write (f,&blah,1);//w, low
559 blah=w/256;write (f,&blah,1);//w, high
560 blah=h%256;write (f,&blah,1);//h, low
561 blah=h/256;write (f,&blah,1);//h, high
562 blah=24;write (f,&blah,1);//24 bpp
563 blah=0;write (f,&blah,1);//no attribute bits, origin is lowerleft, no interleave
566 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
578 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
585 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
588 void save_screen_shot(int automap_flag)
592 static int savenum=0;
596 if (!ogl_readpixels_ok){
598 // hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
604 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
605 if ( savenum == 9999 ) savenum = 0;
606 sprintf(savename,"scrn%04d.tga",savenum++);
608 while(!access(savename,0))
610 if ( savenum == 9999 ) savenum = 0;
611 sprintf(savename,"scrn%04d.tga",savenum++);
613 sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
614 //end this section addition/change - Victor Rachels
617 // save_font = grd_curcanv->cv_font;
618 // gr_set_curfont(GAME_FONT);
619 // gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
620 // gr_get_string_size(message,&w,&h,&aw);
621 // modex_print_message(32, 2, message);
623 // hud_message(MSGC_GAME_FEEDBACK,message);
626 buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
627 glReadBuffer(GL_FRONT);
628 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
629 write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);