4 * OGL video functions. - Added 9/15/99 Matthew Mueller
21 //#include <OpenGL/gl.h>
26 #include <sys/types.h>
52 #include <OpenGL/glu.h>
62 void gr_palette_clear(); // Function prototype for gr_init;
68 int gr_check_fullscreen(void){
69 return ogl_fullscreen;
72 void gr_do_fullscreen(int f){
74 ogl_fullscreen=1;//force fullscreen mode on voodoos.
78 ogl_do_fullscreen_internal();
82 int gr_toggle_fullscreen(void){
83 gr_do_fullscreen(!ogl_fullscreen);
84 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
85 return ogl_fullscreen;
88 int arch_toggle_fullscreen_menu(void){
89 unsigned char *buf=NULL;
91 if (ogl_readpixels_ok){
92 MALLOC(buf,unsigned char,grd_curscreen->sc_w*grd_curscreen->sc_h*3);
93 glReadBuffer(GL_FRONT);
94 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
97 gr_do_fullscreen(!ogl_fullscreen);
99 if (ogl_readpixels_ok){
100 // glWritePixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
102 glDrawPixels(grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
105 // grd_curscreen->sc_mode=0;//hack to get it to reset screen mode
107 return ogl_fullscreen;
110 void ogl_init_state(void){
111 /* select clearing (background) color */
112 glClearColor(0.0, 0.0, 0.0, 0.0);
113 glShadeModel(GL_SMOOTH);
115 /* initialize viewing values */
116 glMatrixMode(GL_PROJECTION);
118 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
119 glScalef(1.0, -1.0, 1.0);
120 glTranslatef(0.0, -1.0, 0.0);
121 gr_palette_step_up(0,0,0);//in case its left over from in game
124 int last_screen_mode=-1;
126 void ogl_set_screen_mode(void){
127 if (last_screen_mode==Screen_mode)
129 OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
130 // 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);
131 if (Screen_mode==SCREEN_GAME){
132 glDrawBuffer(GL_BACK);
134 glClearColor(0.0, 0.0, 0.0, 0.0);
135 glDrawBuffer(GL_FRONT);
136 glClear(GL_COLOR_BUFFER_BIT);
137 glMatrixMode(GL_PROJECTION);
138 glLoadIdentity();//clear matrix
139 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
140 glMatrixMode(GL_MODELVIEW);
141 glLoadIdentity();//clear matrix
143 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
145 last_screen_mode=Screen_mode;
152 if(Screen_mode != SCREEN_GAME){
158 const char *gl_vendor,*gl_renderer,*gl_version,*gl_extensions;
160 void ogl_get_verinfo(void){
162 gl_vendor=glGetString(GL_VENDOR);
163 gl_renderer=glGetString(GL_RENDERER);
164 gl_version=glGetString(GL_VERSION);
165 gl_extensions=glGetString(GL_EXTENSIONS);
167 con_printf(CON_VERBOSE, "gl vendor:%s renderer:%s version:%s extensions:%s\n",gl_vendor,gl_renderer,gl_version,gl_extensions);
169 ogl_intensity4_ok=1;ogl_luminance4_alpha4_ok=1;ogl_rgba2_ok=1;ogl_gettexlevelparam_ok=1;
172 dglMultiTexCoord2fARB = (glMultiTexCoord2fARB_fp)wglGetProcAddress("glMultiTexCoord2fARB");
173 dglActiveTextureARB = (glActiveTextureARB_fp)wglGetProcAddress("glActiveTextureARB");
174 dglMultiTexCoord2fSGIS = (glMultiTexCoord2fSGIS_fp)wglGetProcAddress("glMultiTexCoord2fSGIS");
175 dglSelectTextureSGIS = (glSelectTextureSGIS_fp)wglGetProcAddress("glSelectTextureSGIS");
178 //multitexturing doesn't work yet.
179 #ifdef GL_ARB_multitexture
180 ogl_arb_multitexture_ok=0;//(strstr(gl_extensions,"GL_ARB_multitexture")!=0 && glActiveTextureARB!=0 && 0);
181 mprintf((0,"c:%p d:%p e:%p\n",strstr(gl_extensions,"GL_ARB_multitexture"),glActiveTextureARB,glBegin));
183 #ifdef GL_SGIS_multitexture
184 ogl_sgis_multitexture_ok=0;//(strstr(gl_extensions,"GL_SGIS_multitexture")!=0 && glSelectTextureSGIS!=0 && 0);
185 mprintf((0,"a:%p b:%p\n",strstr(gl_extensions,"GL_SGIS_multitexture"),glSelectTextureSGIS));
188 //add driver specific hacks here. whee.
189 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){
190 ogl_intensity4_ok=0;//ignores alpha, always black background instead of transparent.
191 ogl_readpixels_ok=0;//either just returns all black, or kills the X server entirely
192 ogl_gettexlevelparam_ok=0;//returns random data..
194 if (stricmp(gl_vendor,"Matrox Graphics Inc.")==0){
195 //displays garbage. reported by
196 // redomen@crcwnet.com (render="Matrox G400" version="1.1.3 5.52.015")
197 // orulz (Matrox G200)
201 //allow overriding of stuff.
202 #ifdef GL_ARB_multitexture
203 if ((t=FindArg("-gl_arb_multitexture_ok"))){
204 ogl_arb_multitexture_ok=atoi(Args[t+1]);
207 #ifdef GL_SGIS_multitexture
208 if ((t=FindArg("-gl_sgis_multitexture_ok"))){
209 ogl_sgis_multitexture_ok=atoi(Args[t+1]);
212 if ((t=FindArg("-gl_intensity4_ok"))){
213 ogl_intensity4_ok=atoi(Args[t+1]);
215 if ((t=FindArg("-gl_luminance4_alpha4_ok"))){
216 ogl_luminance4_alpha4_ok=atoi(Args[t+1]);
218 if ((t=FindArg("-gl_rgba2_ok"))){
219 ogl_rgba2_ok=atoi(Args[t+1]);
221 if ((t=FindArg("-gl_readpixels_ok"))){
222 ogl_readpixels_ok=atoi(Args[t+1]);
224 if ((t=FindArg("-gl_gettexlevelparam_ok"))){
225 ogl_gettexlevelparam_ok=atoi(Args[t+1]);
228 con_printf(CON_VERBOSE, "gl_arb_multitexture:%i gl_sgis_multitexture:%i\n",ogl_arb_multitexture_ok,ogl_sgis_multitexture_ok);
229 con_printf(CON_VERBOSE, "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);
232 int gr_set_mode(u_int32_t mode)
247 //if (screen != NULL) gr_palette_clear();
251 gr_bm_data=grd_curscreen->sc_canvas.cv_bitmap.bm_data;//since we use realloc, we want to keep this pointer around.
252 memset( grd_curscreen, 0, sizeof(grs_screen));
253 grd_curscreen->sc_mode = mode;
254 grd_curscreen->sc_w = w;
255 grd_curscreen->sc_h = h;
256 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
257 grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
258 grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
259 grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
260 grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
261 //grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = screen->pitch;
262 grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = w;
263 grd_curscreen->sc_canvas.cv_bitmap.bm_type = BM_OGL;
264 //grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)screen->pixels;
265 // mprintf((0,"ogl/gr.c: reallocing %p to %i\n",grd_curscreen->sc_canvas.cv_bitmap.bm_data,w*h));
266 grd_curscreen->sc_canvas.cv_bitmap.bm_data = d_realloc(gr_bm_data,w*h);
267 gr_set_current_canvas(NULL);
268 //gr_enable_default_palette_loading();
270 ogl_init_window(w,h);//platform specific code
274 OGL_VIEWPORT(0,0,w,h);
276 ogl_set_screen_mode();
278 // gamefont_choose_game_font(w,h);
283 #define GLstrcmptestr(a,b) if (stricmp(a,#b)==0 || stricmp(a,"GL_" #b)==0)return GL_ ## b;
284 int ogl_atotexfilti(char *a,int min){
285 GLstrcmptestr(a,NEAREST);
286 GLstrcmptestr(a,LINEAR);
287 if (min){//mipmaps are valid only for the min filter
288 GLstrcmptestr(a,NEAREST_MIPMAP_NEAREST);
289 GLstrcmptestr(a,NEAREST_MIPMAP_LINEAR);
290 GLstrcmptestr(a,LINEAR_MIPMAP_NEAREST);
291 GLstrcmptestr(a,LINEAR_MIPMAP_LINEAR);
293 Error("unknown/invalid texture filter %s\n",a);
294 // return GL_NEAREST;
296 int ogl_testneedmipmaps(int i){
301 case GL_NEAREST_MIPMAP_NEAREST:
302 case GL_NEAREST_MIPMAP_LINEAR:
303 case GL_LINEAR_MIPMAP_NEAREST:
304 case GL_LINEAR_MIPMAP_LINEAR:
307 Error("unknown texture filter %x\n",i);
310 #ifdef OGL_RUNTIME_LOAD
311 #if defined(__WINDOWS__) || defined(__MINGW32__)
312 char *OglLibPath="opengl32.dll";
315 char *OglLibPath="libGL.so";
319 int ogl_init_load_library(void)
324 if ((t=FindArg("-gl_library")))
325 OglLibPath=Args[t+1];
327 retcode = OpenGL_LoadLibrary(true);
330 mprintf((0,"Opengl loaded ok\n"));
334 Error("Opengl: Functions not imported\n");
337 Error("Opengl: error loading %s\n",OglLibPath);
347 int mode = SM(640,480);
349 // Only do this function once!
354 #ifdef OGL_RUNTIME_LOAD
355 ogl_init_load_library();
358 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
359 if (FindArg("-gl_voodoo")){
361 gr_toggle_fullscreen();
363 if (FindArg("-fullscreen"))
364 gr_toggle_fullscreen();
366 if ((glt=FindArg("-gl_alttexmerge")))
368 if ((t=FindArg("-gl_stdtexmerge")))
369 if (t>=glt)//allow overriding of earlier args
372 if ((glt=FindArg("-gl_16bittextures")))
373 ogl_rgba_format=GL_RGB5_A1;
375 if ((glt=FindArg("-gl_mipmap"))){
376 GL_texmagfilt=GL_LINEAR;
377 GL_texminfilt=GL_LINEAR_MIPMAP_NEAREST;
379 if ((t=FindArg("-gl_simple"))){
380 if (t>=glt){//allow overriding of earlier args
382 GL_texmagfilt=GL_NEAREST;
383 GL_texminfilt=GL_NEAREST;
386 if ((t=FindArg("-gl_texmagfilt")) || (t=FindArg("-gl_texmagfilter"))){
387 if (t>=glt)//allow overriding of earlier args
388 GL_texmagfilt=ogl_atotexfilti(Args[t+1],0);
390 if ((t=FindArg("-gl_texminfilt")) || (t=FindArg("-gl_texminfilter"))){
391 if (t>=glt)//allow overriding of earlier args
392 GL_texminfilt=ogl_atotexfilti(Args[t+1],1);
394 GL_needmipmaps=ogl_testneedmipmaps(GL_texminfilt);
395 mprintf((0,"gr_init: texmagfilt:%x texminfilt:%x needmipmaps=%i\n",GL_texmagfilt,GL_texminfilt,GL_needmipmaps));
397 if ((t=FindArg("-gl_vidmem"))){
398 ogl_mem_target=atoi(Args[t+1])*1024*1024;
400 if ((t=FindArg("-gl_reticle"))){
401 gl_reticle=atoi(Args[t+1]);
403 //printf("ogl_mem_target=%i\n",ogl_mem_target);
405 ogl_init();//platform specific initialization
407 ogl_init_texture_list_internal();
409 MALLOC( grd_curscreen,grs_screen,1 );
410 memset( grd_curscreen, 0, sizeof(grs_screen));
411 grd_curscreen->sc_canvas.cv_bitmap.bm_data = NULL;
414 if ((retcode=gr_set_mode(mode)))
418 grd_curscreen->sc_canvas.cv_color = 0;
419 grd_curscreen->sc_canvas.cv_drawmode = 0;
420 grd_curscreen->sc_canvas.cv_font = NULL;
421 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
422 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
423 gr_set_current_canvas( &grd_curscreen->sc_canvas );
434 // mprintf((0,"ogl init: %s %s %s - %s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER),glGetString(GL_VERSION),glGetString,(GL_EXTENSIONS)));
436 ogl_close();//platform specific code
438 if (grd_curscreen->sc_canvas.cv_bitmap.bm_data)
439 d_free(grd_curscreen->sc_canvas.cv_bitmap.bm_data);
440 d_free(grd_curscreen);
442 #ifdef OGL_RUNTIME_LOAD
444 OpenGL_LoadLibrary(false);
447 extern int r_upixelc;
448 void ogl_upixelc(int x, int y, int c){
450 // printf("gr_upixelc(%i,%i,%i)%i\n",x,y,c,Function_mode==FMODE_GAME);
451 // if(Function_mode != FMODE_GAME){
452 // grd_curcanv->cv_bitmap.bm_data[y*grd_curscreen->sc_canvas.cv_bitmap.bm_w+x]=c;
454 OGL_DISABLE(TEXTURE_2D);
457 // glBegin(GL_LINES);
458 // ogl_pal=gr_current_pal;
459 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
460 // ogl_pal=gr_palette;
461 glVertex2f((x+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,1.0-(y+grd_curcanv->cv_bitmap.bm_y)/(float)last_height);
462 // glVertex2f(x/((float)last_width+1),1.0-y/((float)last_height+1));
466 void ogl_urect(int left,int top,int right,int bot){
470 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
471 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
472 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
473 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
475 OGL_DISABLE(TEXTURE_2D);
476 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
484 void ogl_ulinec(int left,int top,int right,int bot,int c){
487 xo=(left+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
488 xf=(right+grd_curcanv->cv_bitmap.bm_x)/(float)last_width;
489 yo=1.0-(top+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
490 yf=1.0-(bot+grd_curcanv->cv_bitmap.bm_y)/(float)last_height;
492 OGL_DISABLE(TEXTURE_2D);
493 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
501 GLfloat last_r=0, last_g=0, last_b=0;
503 void ogl_do_palfx(void){
505 OGL_DISABLE(TEXTURE_2D);
506 if (gr_palette_faded_out){
507 /* glEnable(GL_BLEND);
508 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
513 //glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
515 glBlendFunc(GL_ONE,GL_ONE);
516 glColor3f(last_r,last_g,last_b);
517 // r=f2fl(last_r);g=f2fl(last_g);b=f2fl(last_b);a=0.5;
531 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
534 void gr_palette_clear()
536 gr_palette_faded_out=1;
540 void gr_palette_step_up( int r, int g, int b )
542 if (gr_palette_faded_out) return;
544 // if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
549 do_pal_step=(r || g || b);*/
551 last_r = (r+gr_palette_gamma)/63.0;
552 last_g = (g+gr_palette_gamma)/63.0;
553 last_b = (b+gr_palette_gamma)/63.0;
555 do_pal_step=(r || g || b || gr_palette_gamma);
559 //added on 980913 by adb to fix palette problems
560 // need a min without side effects...
562 static inline int min(int x, int y) { return x < y ? x : y; }
565 void gr_palette_load( ubyte *pal )
569 for (i=0; i<768; i++ ) {
570 gr_current_pal[i] = pal[i];
571 if (gr_current_pal[i] > 63) gr_current_pal[i] = 63;
573 //palette = screen->format->palette;
575 gr_palette_faded_out=0;
577 init_computed_colors();
582 int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys)
584 gr_palette_faded_out=1;
590 int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)
592 gr_palette_faded_out=0;
598 void gr_palette_read(ubyte * pal)
601 for (i=0; i<768; i++ ) {
602 pal[i]=gr_current_pal[i];
603 if (pal[i] > 63) pal[i] = 63;
607 //writes out an uncompressed RGB .tga file
608 //if we got really spiffy, we could optionally link in libpng or something, and use that.
609 void write_bmp(char *savename,int w,int h,unsigned char *buf){
611 #if defined(__WINDOWS__) || defined(__MINGW32__)
612 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR);
614 f=open(savename,O_CREAT|O_EXCL|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
617 GLubyte targaMagic[12] = { 0, //no identification field
619 2,//RGB image (well, BGR, actually)
620 0, 0, 0, 0, 0, 0, 0, 0, 0 };//no colormap or image origin stuff.
627 write (f,targaMagic,sizeof(targaMagic));
628 blah=w%256;write (f,&blah,1);//w, low
629 blah=w/256;write (f,&blah,1);//w, high
630 blah=h%256;write (f,&blah,1);//h, low
631 blah=h/256;write (f,&blah,1);//h, high
632 blah=24;write (f,&blah,1);//24 bpp
633 blah=0;write (f,&blah,1);//no attribute bits, origin is lowerleft, no interleave
636 for (y=0;y<h;y++){//TGAs use BGR ordering of data.
648 mprintf((0,"screenshot error, couldn't write to %s (err %i)\n",savename,errno));
655 mprintf((0,"screenshot error, couldn't open %s (err %i)\n",savename,errno));
658 void save_screen_shot(int automap_flag)
662 static int savenum=0;
666 if (!ogl_readpixels_ok){
668 hud_message(MSGC_GAME_FEEDBACK,"glReadPixels not supported on your configuration");
674 //added/changed on 10/31/98 by Victor Rachels to fix overwrite each new game
675 if ( savenum == 9999 ) savenum = 0;
676 sprintf(savename,"scrn%04d.tga",savenum++);
678 while(!access(savename,0))
680 if ( savenum == 9999 ) savenum = 0;
681 sprintf(savename,"scrn%04d.tga",savenum++);
683 sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
684 //end this section addition/change - Victor Rachels
687 // save_font = grd_curcanv->cv_font;
688 // gr_set_curfont(GAME_FONT);
689 // gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
690 // gr_get_string_size(message,&w,&h,&aw);
691 // modex_print_message(32, 2, message);
693 hud_message(MSGC_GAME_FEEDBACK,message);
696 buf = d_malloc(grd_curscreen->sc_w*grd_curscreen->sc_h*3);
697 glReadBuffer(GL_FRONT);
698 glReadPixels(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h,GL_RGB,GL_UNSIGNED_BYTE,buf);
699 write_bmp(savename,grd_curscreen->sc_w,grd_curscreen->sc_h,buf);