2 * $Source: /cvs/cvsroot/d2x/video/ogl_gr.c,v $
5 * $Date: 2001-10-25 02:23:48 $
7 * // OGL video functions. - Added 9/15/99 Matthew Mueller
9 * $Log: not supported by cvs2svn $
10 * Revision 1.6 2001/10/18 23:59:23 bradleyb
11 * Changed __ENV_LINUX__ to __linux__
13 * Revision 1.5 2001/10/12 00:18:40 bradleyb
14 * Switched from Cygwin to mingw32 on MS boxes. Vastly improved compilability.
16 * Revision 1.4 2001/01/29 13:47:52 bradleyb
17 * Fixed build, some minor cleanups.
34 #include <sys/types.h>
67 void gr_palette_clear(); // Function prototype for gr_init;
72 int gr_check_fullscreen(void){
73 return ogl_fullscreen;
75 void gr_do_fullscreen(int f){
77 ogl_fullscreen=1;//force fullscreen mode on voodoos.
81 ogl_do_fullscreen_internal();
84 int gr_toggle_fullscreen(void){
85 gr_do_fullscreen(!ogl_fullscreen);
86 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
87 return ogl_fullscreen;
91 void ogl_init_state(void){
92 /* select clearing (background) color */
93 glClearColor(0.0, 0.0, 0.0, 0.0);
94 glShadeModel(GL_SMOOTH);
96 /* initialize viewing values */
97 glMatrixMode(GL_PROJECTION);
99 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
100 glScalef(1.0, -1.0, 1.0);
101 glTranslatef(0.0, -1.0, 0.0);
102 gr_palette_step_up(0,0,0);//in case its left over from in game
105 int last_screen_mode=-1;
106 void ogl_set_screen_mode(void){
107 if (last_screen_mode==Screen_mode)
109 OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
110 // 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);
111 if (Screen_mode==SCREEN_GAME){
112 glDrawBuffer(GL_BACK);
114 glClearColor(0.0, 0.0, 0.0, 0.0);
115 glDrawBuffer(GL_FRONT);
116 glClear(GL_COLOR_BUFFER_BIT);
117 glMatrixMode(GL_PROJECTION);
118 glLoadIdentity();//clear matrix
119 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
120 glMatrixMode(GL_MODELVIEW);
121 glLoadIdentity();//clear matrix
123 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
125 last_screen_mode=Screen_mode;
131 if(Screen_mode != SCREEN_GAME){
137 const char *gl_vendor,*gl_renderer,*gl_version,*gl_extensions;
138 void ogl_get_verinfo(void){
140 gl_vendor=glGetString(GL_VENDOR);
141 gl_renderer=glGetString(GL_RENDERER);
142 gl_version=glGetString(GL_VERSION);
143 gl_extensions=glGetString(GL_EXTENSIONS);
145 printf("gl vendor:%s renderer:%s version:%s extensions:%s\n",gl_vendor,gl_renderer,gl_version,gl_extensions);
147 ogl_intensity4_ok=1;ogl_luminance4_alpha4_ok=1;ogl_rgba2_ok=1;
149 //add driver specific hacks here. whee.
150 if (stricmp(gl_renderer,"Mesa NVIDIA RIVA 1.0\n")==0 && stricmp(gl_version,"1.2 Mesa 3.0")==0){
151 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
152 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
155 //allow overriding of stuff.
156 if ((t=FindArg("-gl_intensity4_ok"))){
157 ogl_intensity4_ok=atoi(Args[t+1]);
159 if ((t=FindArg("-gl_luminance4_alpha4_ok"))){
160 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
162 if ((t=FindArg("-gl_rgba2_ok"))){
163 ogl_rgba2_ok=atoi(Args[t+1]);
165 if ((t=FindArg("-gl_readpixels_ok"))){
166 ogl_readpixels_ok=atoi(Args[t+1]);
169 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);
172 int gr_set_mode(u_int32_t mode)
186 //if (screen != NULL) gr_palette_clear();
190 memset( grd_curscreen, 0, sizeof(grs_screen));
191 grd_curscreen->sc_mode = mode;
192 grd_curscreen->sc_w = w;
193 grd_curscreen->sc_h = h;
194 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
195 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
196 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
197 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
198 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
199 //grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
200 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = w;
201 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_OGL;
202 //grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
203 grd_curscreen->sc_canvas.cv_bitmap.bm_data = realloc(grd_curscreen->sc_canvas.cv_bitmap.bm_data,w*h);
204 gr_set_current_canvas(NULL);
205 //gr_enable_default_palette_loading();
207 ogl_init_window(w,h);//platform specific code
211 OGL_VIEWPORT(0,0,w,h);
213 ogl_set_screen_mode();
215 //gamefont_choose_game_font(w,h);
220 #define GLstrcmptestr(a,b) if (stricmp(a,#b)==0 || stricmp(a,"GL_" #b)==0)return GL_ ## b;
221 int ogl_atotexfilti(char *a,int min){
222 GLstrcmptestr(a,NEAREST);
223 GLstrcmptestr(a,LINEAR);
224 if (min){//mipmaps are valid only for the min filter
225 GLstrcmptestr(a,NEAREST_MIPMAP_NEAREST);
226 GLstrcmptestr(a,NEAREST_MIPMAP_LINEAR);
227 GLstrcmptestr(a,LINEAR_MIPMAP_NEAREST);
228 GLstrcmptestr(a,LINEAR_MIPMAP_LINEAR);
230 Error("unknown/invalid texture filter %s\n",a);
231 // return GL_NEAREST;
233 int ogl_testneedmipmaps(int i){
238 case GL_NEAREST_MIPMAP_NEAREST:
239 case GL_NEAREST_MIPMAP_LINEAR:
240 case GL_LINEAR_MIPMAP_NEAREST:
241 case GL_LINEAR_MIPMAP_LINEAR:
244 Error("unknown texture filter %x\n",i);
247 #ifdef OGL_RUNTIME_LOAD
248 #if defined(__WINDOWS__) || defined(__MINGW32__)
249 char *OglLibPath="opengl32.dll";
251 char *OglLibPath="libGL.so";
255 int ogl_init_load_library(void)
260 if ((t=FindArg("-gl_library")))
261 OglLibPath=Args[t+1];
263 retcode = OpenGL_LoadLibrary(true);
266 mprintf((0,"Opengl loaded ok\n"));
270 Error("Opengl: Functions not imported\n");
273 Error("Opengl: error loading %s\n",OglLibPath);
283 int mode = SM(640,480);
285 // Only do this function once!
290 #ifdef OGL_RUNTIME_LOAD
291 ogl_init_load_library();
294 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
295 if (FindArg("-gl_voodoo")){
297 gr_toggle_fullscreen();
299 if (FindArg("-fullscreen"))
300 gr_toggle_fullscreen();
302 if ((glt=FindArg("-gl_alttexmerge")))
304 if ((t=FindArg("-gl_stdtexmerge")))
305 if (t>=glt)//allow overriding of earlier args
308 if ((glt=FindArg("-gl_16bittextures")))
309 ogl_rgba_format=GL_RGB5_A1;
311 if ((glt=FindArg("-gl_mipmap"))){
312 GL_texmagfilt=GL_LINEAR;
313 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
315 if ((t=FindArg("-gl_simple"))){
316 if (t>=glt){//allow overriding of earlier args
318 GL_texmagfilt=GL_NEAREST;
319 GL_texminfilt=GL_NEAREST;
322 if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
323 if (t>=glt)//allow overriding of earlier args
324 GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
326 if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
327 if (t>=glt)//allow overriding of earlier args
328 GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
330 GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
331 mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps));
333 if ((t=FindArg("-gl_vidmem"))){
334 ogl_mem_target=atoi(Args[t+1])*1024*1024;
336 if ((t=FindArg("-gl_reticle"))){
337 gl_reticle=atoi(Args[t+1]);
339 //printf("ogl_mem_target=%i\n",ogl_mem_target);
341 ogl_init();//platform specific initialization
343 ogl_init_texture_list_internal();
345 MALLOC( grd_curscreen,grs_screen,1 );
346 memset( grd_curscreen, 0, sizeof(grs_screen));
347 grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
350 if ((retcode=gr_set_mode(mode)))
354 grd_curscreen->sc_canvas.cv_color = 0;
355 grd_curscreen->sc_canvas.cv_drawmode = 0;
356 grd_curscreen->sc_canvas.cv_font = NULL;
357 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
358 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
359 gr_set_current_canvas( &grd_curscreen->sc_canvas );
370 // mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
372 ogl_close();//platform specific code
374 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
375 d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
376 d_free(grd_curscreen);
378 #ifdef OGL_RUNTIME_LOAD
380 OpenGL_LoadLibrary(false);
383 extern int r_upixelc;
384 void ogl_upixelc(int x, int y, int c){
386 // printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
387 // if(Function_mode != FMODE_GAME){
388 // grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
390 OGL_DISABLE(TEXTURE_2D);
393 // glBegin(GL_LINES);
394 // ogl_pal=gr_current_pal;
395 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
396 // ogl_pal=gr_palette;
397 glVertex2f((x+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,1.0-(y+grd_curcanv->cv_bitmap.bm_y)/(float)last_height);
398 // glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
402 void ogl_urect(int left,int top,int right,int bot){
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 void ogl_ulinec(int left,int top,int right,int bot,int c){
423 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
424 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
425 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
426 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
428 OGL_DISABLE(TEXTURE_2D);
429 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
437 GLfloat last_r=0, last_g=0, last_b=0;
439 void ogl_do_palfx(void){
441 OGL_DISABLE(TEXTURE_2D);
442 if (gr_palette_faded_out){
443 /* glEnable(GL_BLEND);
444 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
449 //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
451 glBlendFunc(GL_ONE,GL_ONE);
452 glColor3f(last_r,last_g,last_b);
453 // r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
467 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
470 void gr_palette_clear()
472 gr_palette_faded_out=1;
476 void gr_palette_step_up( int r, int g, int b )
478 if (gr_palette_faded_out) return;
480 // if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
485 do_pal_step=(r || g || b);*/
487 last_r = (r+gr_palette_gamma)/63.0;
488 last_g = (g+gr_palette_gamma)/63.0;
489 last_b = (b+gr_palette_gamma)/63.0;
491 do_pal_step=(r || g || b || gr_palette_gamma);
495 //added on 980913 by adb to fix palette problems
496 // need a min without side effects...
498 static inline int min(int x, int y) { return x < y ? x : y; }
501 void gr_palette_load( ubyte *pal )
505 for (i=0; i<768; i++ ) {
506 gr_current_pal[i] = pal[i];
507 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
509 //palette = screen->format->palette;
511 gr_palette_faded_out=0;
513 init_computed_colors();
518 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
520 gr_palette_faded_out=1;
526 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
528 gr_palette_faded_out=0;
534 void gr_palette_read(ubyte * pal)
537 for (i=0; i<768; i++ ) {
538 pal[i]=gr_current_pal[i];
539 if (pal[i] > 63) pal[i] = 63;
543 //writes out an uncompressed RGB .tga file
544 //if we got really spiffy, we could optionally link in libpng or something, and use that.
545 void write_bmp(char *savename,int w,int h,unsigned char *buf){
547 #if defined(__WINDOWS__) || defined(__MINGW32__)
548 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR);
550 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
553 GLubyte targaMagic[12] = { 0, //no identification field
555 2,//RGB image (well, BGR, actually)
556 0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
563 write (f,targaMagic,sizeof(targaMagic));
564 blah=w%256;write (f,&blah,1);//w, low
565 blah=w/256;write (f,&blah,1);//w, high
566 blah=h%256;write (f,&blah,1);//h, low
567 blah=h/256;write (f,&blah,1);//h, high
568 blah=24;write (f,&blah,1);//24 bpp
569 blah=0;write (f,&blah,1);//no attribute bits, origin is lowerleft, no interleave
572 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
584 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
591 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
594 void save_screen_shot(int automap_flag)
598 static int savenum=0;
602 if (!ogl_readpixels_ok){
604 // hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
610 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
611 if ( savenum == 9999 ) savenum = 0;
612 sprintf(savename,"scrn%04d.tga",savenum++);
614 while(!access(savename,0))
616 if ( savenum == 9999 ) savenum = 0;
617 sprintf(savename,"scrn%04d.tga",savenum++);
619 sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
620 //end this section addition/change - Victor Rachels
623 // save_font = grd_curcanv->cv_font;
624 // gr_set_curfont(GAME_FONT);
625 // gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
626 // gr_get_string_size(message,&w,&h,&aw);
627 // modex_print_message(32, 2, message);
629 // hud_message(MSGC_GAME_FEEDBACK,message);
632 buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
633 glReadBuffer(GL_FRONT);
634 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
635 write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);