1 // OGL video functions. - Added 9/15/99 Matthew Mueller
16 #include <sys/types.h>
49 void gr_palette_clear(); // Function prototype for gr_init;
54 int gr_check_fullscreen(void){
55 return ogl_fullscreen;
57 void gr_do_fullscreen(int f){
59 ogl_fullscreen=1;//force fullscreen mode on voodoos.
63 ogl_do_fullscreen_internal();
66 int gr_toggle_fullscreen(void){
67 gr_do_fullscreen(!ogl_fullscreen);
68 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
69 return ogl_fullscreen;
73 void ogl_init_state(void){
74 /* select clearing (background) color */
75 glClearColor(0.0, 0.0, 0.0, 0.0);
76 glShadeModel(GL_SMOOTH);
78 /* initialize viewing values */
79 glMatrixMode(GL_PROJECTION);
81 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
82 glScalef(1.0, -1.0, 1.0);
83 glTranslatef(0.0, -1.0, 0.0);
84 gr_palette_step_up(0,0,0);//in case its left over from in game
87 int last_screen_mode=-1;
88 void ogl_set_screen_mode(void){
89 if (last_screen_mode==Screen_mode)
91 OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
92 // 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);
93 if (Screen_mode==SCREEN_GAME){
94 glDrawBuffer(GL_BACK);
96 glClearColor(0.0, 0.0, 0.0, 0.0);
97 glDrawBuffer(GL_FRONT);
98 glClear(GL_COLOR_BUFFER_BIT);
99 glMatrixMode(GL_PROJECTION);
100 glLoadIdentity();//clear matrix
101 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
102 glMatrixMode(GL_MODELVIEW);
103 glLoadIdentity();//clear matrix
105 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
107 last_screen_mode=Screen_mode;
113 if(Screen_mode != SCREEN_GAME){
119 const char *gl_vendor,*gl_renderer,*gl_version,*gl_extensions;
120 void ogl_get_verinfo(void){
122 gl_vendor=glGetString(GL_VENDOR);
123 gl_renderer=glGetString(GL_RENDERER);
124 gl_version=glGetString(GL_VERSION);
125 gl_extensions=glGetString(GL_EXTENSIONS);
127 printf("gl vendor:%s renderer:%s version:%s extensions:%s\n",gl_vendor,gl_renderer,gl_version,gl_extensions);
129 ogl_intensity4_ok=1;ogl_luminance4_alpha4_ok=1;ogl_rgba2_ok=1;
131 //add driver specific hacks here. whee.
132 if (stricmp(gl_renderer,"Mesa NVIDIA RIVA 1.0\n")==0 && stricmp(gl_version,"1.2 Mesa 3.0")==0){
133 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
134 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
137 //allow overriding of stuff.
138 if ((t=args_find("-gl_intensity4_ok"))){
139 ogl_intensity4_ok=atoi(Args[t+1]);
141 if ((t=args_find("-gl_luminance4_alpha4_ok"))){
142 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
144 if ((t=args_find("-gl_rgba2_ok"))){
145 ogl_rgba2_ok=atoi(Args[t+1]);
147 if ((t=args_find("-gl_readpixels_ok"))){
148 ogl_readpixels_ok=atoi(Args[t+1]);
151 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);
154 int gr_set_mode(u_int32_t mode)
168 //if (screen != NULL) gr_palette_clear();
172 memset( grd_curscreen, 0, sizeof(grs_screen));
173 grd_curscreen->sc_mode = mode;
174 grd_curscreen->sc_w = w;
175 grd_curscreen->sc_h = h;
176 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
177 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
178 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
179 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
180 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
181 //grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
182 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = w;
183 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_OGL;
184 //grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
185 grd_curscreen->sc_canvas.cv_bitmap.bm_data = realloc(grd_curscreen->sc_canvas.cv_bitmap.bm_data,w*h);
186 gr_set_current_canvas(NULL);
187 //gr_enable_default_palette_loading();
189 ogl_init_window(w,h);//platform specific code
193 OGL_VIEWPORT(0,0,w,h);
195 ogl_set_screen_mode();
197 //gamefont_choose_game_font(w,h);
202 #define GLstrcmptestr(a,b) if (stricmp(a,#b)==0 || stricmp(a,"GL_" #b)==0)return GL_ ## b;
203 int ogl_atotexfilti(char *a,int min){
204 GLstrcmptestr(a,NEAREST);
205 GLstrcmptestr(a,LINEAR);
206 if (min){//mipmaps are valid only for the min filter
207 GLstrcmptestr(a,NEAREST_MIPMAP_NEAREST);
208 GLstrcmptestr(a,NEAREST_MIPMAP_LINEAR);
209 GLstrcmptestr(a,LINEAR_MIPMAP_NEAREST);
210 GLstrcmptestr(a,LINEAR_MIPMAP_LINEAR);
212 Error("unknown/invalid texture filter %s\n",a);
213 // return GL_NEAREST;
215 int ogl_testneedmipmaps(int i){
220 case GL_NEAREST_MIPMAP_NEAREST:
221 case GL_NEAREST_MIPMAP_LINEAR:
222 case GL_LINEAR_MIPMAP_NEAREST:
223 case GL_LINEAR_MIPMAP_LINEAR:
226 Error("unknown texture filter %x\n",i);
229 #ifdef OGL_RUNTIME_LOAD
231 char *OglLibPath="opengl32.dll";
234 char *OglLibPath="libGL.so";
238 int ogl_init_load_library(void)
243 if ((t=args_find("-gl_library")))
244 OglLibPath=Args[t+1];
246 retcode = OpenGL_LoadLibrary(true);
249 mprintf((0,"Opengl loaded ok\n"));
253 Error("Opengl: Functions not imported\n");
256 Error("Opengl: error loading %s\n",OglLibPath);
266 int mode = SM(640,480);
268 // Only do this function once!
273 #ifdef OGL_RUNTIME_LOAD
274 ogl_init_load_library();
277 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
278 if (args_find("-gl_voodoo")){
280 gr_toggle_fullscreen();
282 if (args_find("-fullscreen"))
283 gr_toggle_fullscreen();
285 if ((glt=args_find("-gl_alttexmerge")))
287 if ((t=args_find("-gl_stdtexmerge")))
288 if (t>=glt)//allow overriding of earlier args
291 if ((glt=args_find("-gl_16bittextures")))
292 ogl_rgba_format=GL_RGB5_A1;
294 if ((glt=args_find("-gl_mipmap"))){
295 GL_texmagfilt=GL_LINEAR;
296 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
298 if ((t=args_find("-gl_simple"))){
299 if (t>=glt){//allow overriding of earlier args
301 GL_texmagfilt=GL_NEAREST;
302 GL_texminfilt=GL_NEAREST;
305 if ((t=args_find("-gl_texmagfilt")) || (t=args_find("-gl_texmagfilter"))){
306 if (t>=glt)//allow overriding of earlier args
307 GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
309 if ((t=args_find("-gl_texminfilt")) || (t=args_find("-gl_texminfilter"))){
310 if (t>=glt)//allow overriding of earlier args
311 GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
313 GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
314 mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps));
316 if ((t=args_find("-gl_vidmem"))){
317 ogl_mem_target=atoi(Args[t+1])*1024*1024;
319 if ((t=args_find("-gl_reticle"))){
320 gl_reticle=atoi(Args[t+1]);
322 //printf("ogl_mem_target=%i\n",ogl_mem_target);
324 ogl_init();//platform specific initialization
326 ogl_init_texture_list_internal();
328 MALLOC( grd_curscreen,grs_screen,1 );
329 memset( grd_curscreen, 0, sizeof(grs_screen));
330 grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
333 if ((retcode=gr_set_mode(mode)))
337 grd_curscreen->sc_canvas.cv_color = 0;
338 grd_curscreen->sc_canvas.cv_drawmode = 0;
339 grd_curscreen->sc_canvas.cv_font = NULL;
340 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
341 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
342 gr_set_current_canvas( &grd_curscreen->sc_canvas );
353 // mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
355 ogl_close();//platform specific code
357 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
358 d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
359 d_free(grd_curscreen);
361 #ifdef OGL_RUNTIME_LOAD
363 OpenGL_LoadLibrary(false);
366 extern int r_upixelc;
367 void ogl_upixelc(int x, int y, int c){
369 // printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
370 // if(Function_mode != FMODE_GAME){
371 // grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
373 OGL_DISABLE(TEXTURE_2D);
376 // glBegin(GL_LINES);
377 // ogl_pal=gr_current_pal;
378 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
379 // ogl_pal=gr_palette;
380 glVertex2f((x+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,1.0-(y+grd_curcanv->cv_bitmap.bm_y)/(float)last_height);
381 // glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
385 void ogl_urect(int left,int top,int right,int bot){
389 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
390 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
391 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
392 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
394 OGL_DISABLE(TEXTURE_2D);
395 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
403 void ogl_ulinec(int left,int top,int right,int bot,int c){
406 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
407 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
408 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
409 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
411 OGL_DISABLE(TEXTURE_2D);
412 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
420 GLfloat last_r=0, last_g=0, last_b=0;
422 void ogl_do_palfx(void){
424 OGL_DISABLE(TEXTURE_2D);
425 if (gr_palette_faded_out){
426 /* glEnable(GL_BLEND);
427 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
432 //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
434 glBlendFunc(GL_ONE,GL_ONE);
435 glColor3f(last_r,last_g,last_b);
436 // r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
450 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
453 void gr_palette_clear()
455 gr_palette_faded_out=1;
459 void gr_palette_step_up( int r, int g, int b )
461 if (gr_palette_faded_out) return;
463 // if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
468 do_pal_step=(r || g || b);*/
470 last_r = (r+gr_palette_gamma)/63.0;
471 last_g = (g+gr_palette_gamma)/63.0;
472 last_b = (b+gr_palette_gamma)/63.0;
474 do_pal_step=(r || g || b || gr_palette_gamma);
478 //added on 980913 by adb to fix palette problems
479 // need a min without side effects...
481 static inline int min(int x, int y) { return x < y ? x : y; }
484 void gr_palette_load( ubyte *pal )
488 for (i=0; i<768; i++ ) {
489 gr_current_pal[i] = pal[i];
490 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
492 //palette = screen->format->palette;
494 gr_palette_faded_out=0;
496 init_computed_colors();
501 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
503 gr_palette_faded_out=1;
509 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
511 gr_palette_faded_out=0;
517 void gr_palette_read(ubyte * pal)
520 for (i=0; i<768; i++ ) {
521 pal[i]=gr_current_pal[i];
522 if (pal[i] > 63) pal[i] = 63;
526 //writes out an uncompressed RGB .tga file
527 //if we got really spiffy, we could optionally link in libpng or something, and use that.
528 void write_bmp(char *savename,int w,int h,unsigned char *buf){
531 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR);
533 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
536 GLubyte targaMagic[12] = { 0, //no identification field
538 2,//RGB image (well, BGR, actually)
539 0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
546 write (f,targaMagic,sizeof(targaMagic));
547 blah=w%256;write (f,&blah,1);//w, low
548 blah=w/256;write (f,&blah,1);//w, high
549 blah=h%256;write (f,&blah,1);//h, low
550 blah=h/256;write (f,&blah,1);//h, high
551 blah=24;write (f,&blah,1);//24 bpp
552 blah=0;write (f,&blah,1);//no attribute bits, origin is lowerleft, no interleave
555 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
567 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
574 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
577 void save_screen_shot(int automap_flag)
581 static int savenum=0;
585 if (!ogl_readpixels_ok){
587 // hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
593 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
594 if ( savenum == 9999 ) savenum = 0;
595 sprintf(savename,"scrn%04d.tga",savenum++);
597 while(!access(savename,0))
599 if ( savenum == 9999 ) savenum = 0;
600 sprintf(savename,"scrn%04d.tga",savenum++);
602 sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
603 //end this section addition/change - Victor Rachels
606 // save_font = grd_curcanv->cv_font;
607 // gr_set_curfont(GAME_FONT);
608 // gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
609 // gr_get_string_size(message,&w,&h,&aw);
610 // modex_print_message(32, 2, message);
612 // hud_message(MSGC_GAME_FEEDBACK,message);
615 buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
616 glReadBuffer(GL_FRONT);
617 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
618 write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);