2 * $Source: /cvs/cvsroot/d2x/arch/ogl/gr.c,v $
5 * $Date: 2001-11-04 09:00:25 $
7 * // OGL video functions. - Added 9/15/99 Matthew Mueller
9 * $Log: not supported by cvs2svn $
10 * Revision 1.2 2001/10/31 07:35:47 bradleyb
13 * Revision 1.1 2001/10/25 08:25:34 bradleyb
14 * Finished moving stuff to arch/blah. I know, it's ugly, but It'll be easier to sync with d1x.
16 * Revision 1.7 2001/10/25 02:23:48 bradleyb
19 * Revision 1.6 2001/10/18 23:59:23 bradleyb
20 * Changed __ENV_LINUX__ to __linux__
22 * Revision 1.5 2001/10/12 00:18:40 bradleyb
23 * Switched from Cygwin to mingw32 on MS boxes. Vastly improved compilability.
25 * Revision 1.4 2001/01/29 13:47:52 bradleyb
26 * Fixed build, some minor cleanups.
43 #include <sys/types.h>
75 void gr_palette_clear(); // Function prototype for gr_init;
81 int gr_check_fullscreen(void){
82 return ogl_fullscreen;
85 void gr_do_fullscreen(int f){
87 ogl_fullscreen=1;//force fullscreen mode on voodoos.
91 ogl_do_fullscreen_internal();
95 int gr_toggle_fullscreen(void){
96 gr_do_fullscreen(!ogl_fullscreen);
97 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
98 return ogl_fullscreen;
101 int arch_toggle_fullscreen_menu(void){
102 unsigned char *buf=NULL;
104 if (ogl_readpixels_ok){
105 MALLOC(buf,unsigned char,grd_curscreen->sc_w*grd_curscreen->sc_h*3);
106 glReadBuffer(GL_FRONT);
107 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
110 gr_do_fullscreen(!ogl_fullscreen);
112 if (ogl_readpixels_ok){
113 // glWritePixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
115 glDrawPixels(grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
118 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
120 return ogl_fullscreen;
123 void ogl_init_state(void){
124 /* select clearing (background) color */
125 glClearColor(0.0, 0.0, 0.0, 0.0);
126 glShadeModel(GL_SMOOTH);
128 /* initialize viewing values */
129 glMatrixMode(GL_PROJECTION);
131 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
132 glScalef(1.0, -1.0, 1.0);
133 glTranslatef(0.0, -1.0, 0.0);
134 gr_palette_step_up(0,0,0);//in case its left over from in game
137 int last_screen_mode=-1;
139 void ogl_set_screen_mode(void){
140 if (last_screen_mode==Screen_mode)
142 OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
143 // 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);
144 if (Screen_mode==SCREEN_GAME){
145 glDrawBuffer(GL_BACK);
147 glClearColor(0.0, 0.0, 0.0, 0.0);
148 glDrawBuffer(GL_FRONT);
149 glClear(GL_COLOR_BUFFER_BIT);
150 glMatrixMode(GL_PROJECTION);
151 glLoadIdentity();//clear matrix
152 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
153 glMatrixMode(GL_MODELVIEW);
154 glLoadIdentity();//clear matrix
156 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
158 last_screen_mode=Screen_mode;
165 if(Screen_mode != SCREEN_GAME){
171 const char *gl_vendor,*gl_renderer,*gl_version,*gl_extensions;
173 void ogl_get_verinfo(void){
175 gl_vendor=glGetString(GL_VENDOR);
176 gl_renderer=glGetString(GL_RENDERER);
177 gl_version=glGetString(GL_VERSION);
178 gl_extensions=glGetString(GL_EXTENSIONS);
180 printf("gl vendor:%s renderer:%s version:%s extensions:%s\n",gl_vendor,gl_renderer,gl_version,gl_extensions);
182 ogl_intensity4_ok=1;ogl_luminance4_alpha4_ok=1;ogl_rgba2_ok=1;ogl_gettexlevelparam_ok=1;
185 dglMultiTexCoord2fARB = (glMultiTexCoord2fARB_fp)wglGetProcAddress("glMultiTexCoord2fARB");
186 dglActiveTextureARB = (glActiveTextureARB_fp)wglGetProcAddress("glActiveTextureARB");
187 dglMultiTexCoord2fSGIS = (glMultiTexCoord2fSGIS_fp)wglGetProcAddress("glMultiTexCoord2fSGIS");
188 dglSelectTextureSGIS = (glSelectTextureSGIS_fp)wglGetProcAddress("glSelectTextureSGIS");
191 //multitexturing doesn't work yet.
192 #ifdef GL_ARB_multitexture
193 ogl_arb_multitexture_ok=0;//(strstr(gl_extensions,"GL_ARB_multitexture")!=0 && glActiveTextureARB!=0 && 0);
194 mprintf((0,"c:%p d:%p e:%p\n",strstr(gl_extensions,"GL_ARB_multitexture"),glActiveTextureARB,glBegin));
196 #ifdef GL_SGIS_multitexture
197 ogl_sgis_multitexture_ok=0;//(strstr(gl_extensions,"GL_SGIS_multitexture")!=0 && glSelectTextureSGIS!=0 && 0);
198 mprintf((0,"a:%p b:%p\n",strstr(gl_extensions,"GL_SGIS_multitexture"),glSelectTextureSGIS));
201 //add driver specific hacks here. whee.
202 if ((stricmp(gl_renderer,"Mesa NVIDIA RIVA 1.0\n")==0 || stricmp(gl_renderer,"Mesa NVIDIA RIVA 1.2\n")==0) && stricmp(gl_version,"1.2 Mesa 3.0")==0){
203 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
204 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
205 ogl_gettexlevelparam_ok=0;//returns random data..
207 if (stricmp(gl_vendor,"Matrox Graphics Inc.")==0){
208 //displays garbage. reported by
209 // redomen@crcwnet.com (render="Matrox G400" version="1.1.3 5.52.015")
210 // orulz (Matrox G200)
214 //allow overriding of stuff.
215 #ifdef GL_ARB_multitexture
216 if ((t=FindArg("-gl_arb_multitexture_ok"))){
217 ogl_arb_multitexture_ok=atoi(Args[t+1]);
220 #ifdef GL_SGIS_multitexture
221 if ((t=FindArg("-gl_sgis_multitexture_ok"))){
222 ogl_sgis_multitexture_ok=atoi(Args[t+1]);
225 if ((t=FindArg("-gl_intensity4_ok"))){
226 ogl_intensity4_ok=atoi(Args[t+1]);
228 if ((t=FindArg("-gl_luminance4_alpha4_ok"))){
229 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
231 if ((t=FindArg("-gl_rgba2_ok"))){
232 ogl_rgba2_ok=atoi(Args[t+1]);
234 if ((t=FindArg("-gl_readpixels_ok"))){
235 ogl_readpixels_ok=atoi(Args[t+1]);
237 if ((t=FindArg("-gl_gettexlevelparam_ok"))){
238 ogl_gettexlevelparam_ok=atoi(Args[t+1]);
241 printf("gl_arb_multitexture:%i gl_sgis_multitexture:%i\n",ogl_arb_multitexture_ok,ogl_sgis_multitexture_ok);
242 printf("gl_intensity4:%i gl_luminance4_alpha4:%i gl_rgba2:%i gl_readpixels:%i gl_gettexlevelparam:%i\n",ogl_intensity4_ok,ogl_luminance4_alpha4_ok,ogl_rgba2_ok,ogl_readpixels_ok,ogl_gettexlevelparam_ok);
245 int gr_set_mode(u_int32_t mode)
260 //if (screen != NULL) gr_palette_clear();
264 gr_bm_data=grd_curscreen->sc_canvas.cv_bitmap.bm_data;//since we use realloc, we want to keep this pointer around.
265 memset( grd_curscreen, 0, sizeof(grs_screen));
266 grd_curscreen->sc_mode = mode;
267 grd_curscreen->sc_w = w;
268 grd_curscreen->sc_h = h;
269 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
270 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
271 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
272 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
273 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
274 //grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
275 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = w;
276 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_OGL;
277 //grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
278 // mprintf((0,"ogl/gr.c: reallocing %p to %i\n",grd_curscreen->sc_canvas.cv_bitmap.bm_data,w*h));
279 grd_curscreen->sc_canvas.cv_bitmap.bm_data = realloc(gr_bm_data,w*h);
280 gr_set_current_canvas(NULL);
281 //gr_enable_default_palette_loading();
283 ogl_init_window(w,h);//platform specific code
287 OGL_VIEWPORT(0,0,w,h);
289 ogl_set_screen_mode();
291 // gamefont_choose_game_font(w,h);
296 #define GLstrcmptestr(a,b) if (stricmp(a,#b)==0 || stricmp(a,"GL_" #b)==0)return GL_ ## b;
297 int ogl_atotexfilti(char *a,int min){
298 GLstrcmptestr(a,NEAREST);
299 GLstrcmptestr(a,LINEAR);
300 if (min){//mipmaps are valid only for the min filter
301 GLstrcmptestr(a,NEAREST_MIPMAP_NEAREST);
302 GLstrcmptestr(a,NEAREST_MIPMAP_LINEAR);
303 GLstrcmptestr(a,LINEAR_MIPMAP_NEAREST);
304 GLstrcmptestr(a,LINEAR_MIPMAP_LINEAR);
306 Error("unknown/invalid texture filter %s\n",a);
307 // return GL_NEAREST;
309 int ogl_testneedmipmaps(int i){
314 case GL_NEAREST_MIPMAP_NEAREST:
315 case GL_NEAREST_MIPMAP_LINEAR:
316 case GL_LINEAR_MIPMAP_NEAREST:
317 case GL_LINEAR_MIPMAP_LINEAR:
320 Error("unknown texture filter %x\n",i);
323 #ifdef OGL_RUNTIME_LOAD
324 #if defined(__WINDOWS__) || defined(__MINGW32__)
325 char *OglLibPath="opengl32.dll";
328 char *OglLibPath="libGL.so";
332 int ogl_init_load_library(void)
337 if ((t=FindArg("-gl_library")))
338 OglLibPath=Args[t+1];
340 retcode = OpenGL_LoadLibrary(true);
343 mprintf((0,"Opengl loaded ok\n"));
347 Error("Opengl: Functions not imported\n");
350 Error("Opengl: error loading %s\n",OglLibPath);
360 int mode = SM(640,480);
362 // Only do this function once!
367 #ifdef OGL_RUNTIME_LOAD
368 ogl_init_load_library();
371 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
372 if (FindArg("-gl_voodoo")){
374 gr_toggle_fullscreen();
376 if (FindArg("-fullscreen"))
377 gr_toggle_fullscreen();
379 if ((glt=FindArg("-gl_alttexmerge")))
381 if ((t=FindArg("-gl_stdtexmerge")))
382 if (t>=glt)//allow overriding of earlier args
385 if ((glt=FindArg("-gl_16bittextures")))
386 ogl_rgba_format=GL_RGB5_A1;
388 if ((glt=FindArg("-gl_mipmap"))){
389 GL_texmagfilt=GL_LINEAR;
390 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
392 if ((t=FindArg("-gl_simple"))){
393 if (t>=glt){//allow overriding of earlier args
395 GL_texmagfilt=GL_NEAREST;
396 GL_texminfilt=GL_NEAREST;
399 if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
400 if (t>=glt)//allow overriding of earlier args
401 GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
403 if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
404 if (t>=glt)//allow overriding of earlier args
405 GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
407 GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
408 mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps));
410 if ((t=FindArg("-gl_vidmem"))){
411 ogl_mem_target=atoi(Args[t+1])*1024*1024;
413 if ((t=FindArg("-gl_reticle"))){
414 gl_reticle=atoi(Args[t+1]);
416 //printf("ogl_mem_target=%i\n",ogl_mem_target);
418 ogl_init();//platform specific initialization
420 ogl_init_texture_list_internal();
422 MALLOC( grd_curscreen,grs_screen,1 );
423 memset( grd_curscreen, 0, sizeof(grs_screen));
424 grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
427 if ((retcode=gr_set_mode(mode)))
431 grd_curscreen->sc_canvas.cv_color = 0;
432 grd_curscreen->sc_canvas.cv_drawmode = 0;
433 grd_curscreen->sc_canvas.cv_font = NULL;
434 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
435 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
436 gr_set_current_canvas( &grd_curscreen->sc_canvas );
447 // mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
449 ogl_close();//platform specific code
451 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
452 d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
453 d_free(grd_curscreen);
455 #ifdef OGL_RUNTIME_LOAD
457 OpenGL_LoadLibrary(false);
460 extern int r_upixelc;
461 void ogl_upixelc(int x, int y, int c){
463 // printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
464 // if(Function_mode != FMODE_GAME){
465 // grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
467 OGL_DISABLE(TEXTURE_2D);
470 // glBegin(GL_LINES);
471 // ogl_pal=gr_current_pal;
472 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
473 // ogl_pal=gr_palette;
474 glVertex2f((x+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,1.0-(y+grd_curcanv->cv_bitmap.bm_y)/(float)last_height);
475 // glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
479 void ogl_urect(int left,int top,int right,int bot){
483 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
484 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
485 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
486 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
488 OGL_DISABLE(TEXTURE_2D);
489 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
497 void ogl_ulinec(int left,int top,int right,int bot,int c){
500 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
501 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
502 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
503 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
505 OGL_DISABLE(TEXTURE_2D);
506 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
514 GLfloat last_r=0, last_g=0, last_b=0;
516 void ogl_do_palfx(void){
518 OGL_DISABLE(TEXTURE_2D);
519 if (gr_palette_faded_out){
520 /* glEnable(GL_BLEND);
521 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
526 //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
528 glBlendFunc(GL_ONE,GL_ONE);
529 glColor3f(last_r,last_g,last_b);
530 // r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
544 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
547 void gr_palette_clear()
549 gr_palette_faded_out=1;
553 void gr_palette_step_up( int r, int g, int b )
555 if (gr_palette_faded_out) return;
557 // if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
562 do_pal_step=(r || g || b);*/
564 last_r = (r+gr_palette_gamma)/63.0;
565 last_g = (g+gr_palette_gamma)/63.0;
566 last_b = (b+gr_palette_gamma)/63.0;
568 do_pal_step=(r || g || b || gr_palette_gamma);
572 //added on 980913 by adb to fix palette problems
573 // need a min without side effects...
575 static inline int min(int x, int y) { return x < y ? x : y; }
578 void gr_palette_load( ubyte *pal )
582 for (i=0; i<768; i++ ) {
583 gr_current_pal[i] = pal[i];
584 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
586 //palette = screen->format->palette;
588 gr_palette_faded_out=0;
590 init_computed_colors();
595 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
597 gr_palette_faded_out=1;
603 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
605 gr_palette_faded_out=0;
611 void gr_palette_read(ubyte * pal)
614 for (i=0; i<768; i++ ) {
615 pal[i]=gr_current_pal[i];
616 if (pal[i] > 63) pal[i] = 63;
620 //writes out an uncompressed RGB .tga file
621 //if we got really spiffy, we could optionally link in libpng or something, and use that.
622 void write_bmp(char *savename,int w,int h,unsigned char *buf){
624 #if defined(__WINDOWS__) || defined(__MINGW32__)
625 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR);
627 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
630 GLubyte targaMagic[12] = { 0, //no identification field
632 2,//RGB image (well, BGR, actually)
633 0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
640 write (f,targaMagic,sizeof(targaMagic));
641 blah=w%256;write (f,&blah,1);//w, low
642 blah=w/256;write (f,&blah,1);//w, high
643 blah=h%256;write (f,&blah,1);//h, low
644 blah=h/256;write (f,&blah,1);//h, high
645 blah=24;write (f,&blah,1);//24 bpp
646 blah=0;write (f,&blah,1);//no attribute bits, origin is lowerleft, no interleave
649 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
661 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
668 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
671 void save_screen_shot(int automap_flag)
675 static int savenum=0;
679 if (!ogl_readpixels_ok){
681 hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
687 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
688 if ( savenum == 9999 ) savenum = 0;
689 sprintf(savename,"scrn%04d.tga",savenum++);
691 while(!access(savename,0))
693 if ( savenum == 9999 ) savenum = 0;
694 sprintf(savename,"scrn%04d.tga",savenum++);
696 sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
697 //end this section addition/change - Victor Rachels
700 // save_font = grd_curcanv->cv_font;
701 // gr_set_curfont(GAME_FONT);
702 // gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
703 // gr_get_string_size(message,&w,&h,&aw);
704 // modex_print_message(32, 2, message);
706 hud_message(MSGC_GAME_FEEDBACK,message);
709 buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
710 glReadBuffer(GL_FRONT);
711 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
712 write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);