1 /* $Id: ogl.c,v 1.24 2004-05-20 07:25:11 btb Exp $ */
4 * Graphics support functions for OpenGL.
19 #if defined(__APPLE__) && defined(__MACH__)
20 #include <OpenGL/gl.h>
21 #include <OpenGL/glu.h>
31 #include "../../3d/globvars.h"
48 //change to 1 for lots of spew.
50 #define glmprintf(a) mprintf(a)
59 #if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__)) || defined(__sun__)
60 #define cosf(a) cos(a)
61 #define sinf(a) sin(a)
64 unsigned char *ogl_pal=gr_palette;
66 int GL_texmagfilt=GL_NEAREST;
67 int GL_texminfilt=GL_NEAREST;
68 float GL_texanisofilt = 0;
71 int last_width=-1,last_height=-1;
72 int GL_TEXTURE_2D_enabled=-1;
73 int GL_texclamp_enabled=-1;
75 extern int gr_badtexture;
76 int r_texcount = 0, r_cachedtexcount = 0;
77 int ogl_alttexmerge=1;//merge textures by just printing the seperate textures?
78 int ogl_rgba_format=4;
79 int ogl_intensity4_ok=1;
80 int ogl_luminance4_alpha4_ok=1;
82 int ogl_readpixels_ok=1;
83 int ogl_gettexlevelparam_ok=1;
84 #ifdef GL_ARB_multitexture
85 int ogl_arb_multitexture_ok=0;
87 #ifdef GL_SGIS_multitexture
88 int ogl_sgis_multitexture_ok=0;
90 int ogl_nv_texture_env_combine4_ok = 0;
91 int ogl_ext_texture_filter_anisotropic_ok = 0;
94 int cross_lh[2]={0,0};
95 int primary_lh[3]={0,0,0};
96 int secondary_lh[5]={0,0,0,0,0};
99 #define OGL_BINDTEXTURE(a) if(gr_badtexture>0) glBindTexture(GL_TEXTURE_2D, 0);\
100 else if(a!=lastbound) {glBindTexture(GL_TEXTURE_2D, a);lastbound=a;}*/
101 #define OGL_BINDTEXTURE(a) if(gr_badtexture>0) glBindTexture(GL_TEXTURE_2D, 0);\
102 else glBindTexture(GL_TEXTURE_2D, a);
105 ogl_texture ogl_texture_list[OGL_TEXTURE_LIST_SIZE];
106 int ogl_texture_list_cur;
108 /* some function prototypes */
110 //#define OGLTEXBUFSIZE (1024*1024*4)
111 #define OGLTEXBUFSIZE (2048*2048*4)
112 extern GLubyte texbuf[OGLTEXBUFSIZE];
113 //void ogl_filltexbuf(unsigned char *data,GLubyte *texp,int width,int height,int twidth,int theight);
114 void ogl_filltexbuf(unsigned char *data,GLubyte *texp,int truewidth,int width,int height,int dxo,int dyo,int twidth,int theight,int type);
115 void ogl_loadbmtexture(grs_bitmap *bm);
116 //void ogl_loadtexture(unsigned char * data, int width, int height,int dxo,intdyo , int *texid,float *u,float *v,char domipmap,float prio);
117 void ogl_loadtexture(unsigned char * data, int dxo,int dyo, ogl_texture *tex);
118 void ogl_freetexture(ogl_texture *gltexture);
119 void ogl_do_palfx(void);
121 void ogl_init_texture_stats(ogl_texture* t){
122 t->prio=0.3;//default prio
126 void ogl_init_texture(ogl_texture* t){
128 t->internalformat=ogl_rgba_format;
130 t->wrapstate[0] = -1;
131 t->wrapstate[1] = -1;
133 ogl_init_texture_stats(t);
135 void ogl_reset_texture_stats_internal(void){
137 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++)
138 if (ogl_texture_list[i].handle>0){
139 ogl_init_texture_stats(&ogl_texture_list[i]);
142 void ogl_init_texture_list_internal(void){
144 ogl_texture_list_cur=0;
145 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++)
146 ogl_init_texture(&ogl_texture_list[i]);
148 void ogl_smash_texture_list_internal(void){
151 memset(cross_lh,0,sizeof(cross_lh));
152 memset(primary_lh,0,sizeof(primary_lh));
153 memset(secondary_lh,0,sizeof(secondary_lh));
154 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
155 if (ogl_texture_list[i].handle>0){
156 glDeleteTextures( 1, &ogl_texture_list[i].handle );
157 ogl_texture_list[i].handle=0;
159 ogl_texture_list[i].wrapstate[0] = -1;
160 ogl_texture_list[i].wrapstate[1] = -1;
163 void ogl_vivify_texture_list_internal(void){
167 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
168 t=&ogl_texture_list[i];
169 if (t->w>0){//erk, realised this can't be done since we'd need the texture bm_data too. hmmm.
170 ogl_loadbmtexture(t);
175 ogl_texture* ogl_get_free_texture(void){
177 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
178 if (ogl_texture_list[ogl_texture_list_cur].handle<=0 && ogl_texture_list[ogl_texture_list_cur].w==0)
179 return &ogl_texture_list[ogl_texture_list_cur];
180 if (++ogl_texture_list_cur>=OGL_TEXTURE_LIST_SIZE)
181 ogl_texture_list_cur=0;
183 Error("OGL: texture list full!\n");
186 int ogl_texture_stats(void){
187 int used=0,usedl4a4=0,usedrgba=0,databytes=0,truebytes=0,datatexel=0,truetexel=0,i;
188 int prio0=0,prio1=0,prio2=0,prio3=0,prioh=0;
191 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
192 t=&ogl_texture_list[i];
195 datatexel+=t->w*t->h;
196 truetexel+=t->tw*t->th;
197 databytes+=t->bytesu;
199 if (t->prio<0.299)prio0++;
200 else if (t->prio<0.399)prio1++;
201 else if (t->prio<0.499)prio2++;
202 else if (t->prio<0.599)prio3++;
210 int idx, r, g, b, a, dbl, depth, res, colorsize, depthsize;
212 res = SWIDTH * SHEIGHT;
213 glGetIntegerv(GL_INDEX_BITS, &idx);
214 glGetIntegerv(GL_RED_BITS, &r);
215 glGetIntegerv(GL_GREEN_BITS, &g);
216 glGetIntegerv(GL_BLUE_BITS, &b);
217 glGetIntegerv(GL_ALPHA_BITS, &a);
218 glGetIntegerv(GL_DOUBLEBUFFER, &dbl);
220 glGetIntegerv(GL_DEPTH_BITS, &depth);
221 colorsize = (idx * res * dbl) / 8;
222 depthsize = res * depth / 8;
223 gr_printf(5, GAME_FONT->ft_h * 14 + 3 * 14, "%i(%i,%i) %iK(%iK wasted) (%i postcachedtex)", used, usedrgba, usedl4a4, truebytes / 1024, (truebytes - databytes) / 1024, r_texcount - r_cachedtexcount);
224 gr_printf(5, GAME_FONT->ft_h * 15 + 3 * 15, "%ibpp(r%i,g%i,b%i,a%i)x%i=%iK depth%i=%iK", idx, r, g, b, a, dbl, colorsize / 1024, depth, depthsize / 1024);
225 gr_printf(5, GAME_FONT->ft_h * 16 + 3 * 16, "total=%iK", (colorsize + depthsize + truebytes) / 1024);
227 // glmprintf((0,"ogl tex stats: %i(%i,%i|%i,%i,%i,%i,%i) %i(%i)b (%i(%i)wasted)\n",used,usedrgba,usedl4a4,prio0,prio1,prio2,prio3,prioh,truebytes,truetexel,truebytes-databytes,truetexel-datatexel));
230 int ogl_mem_target=-1;
231 void ogl_clean_texture_cache(void){
236 if (ogl_mem_target<0){
242 bytes=ogl_texture_stats();
243 while (bytes>ogl_mem_target){
244 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
245 t=&ogl_texture_list[i];
247 if (t->lastrend+f1_0*time<GameTime){
250 if (bytes<ogl_mem_target)
256 Error("not enough mem?");
261 void ogl_bindbmtex(grs_bitmap *bm){
262 if (bm->gltexture==NULL || bm->gltexture->handle<=0)
263 ogl_loadbmtexture(bm);
264 OGL_BINDTEXTURE(bm->gltexture->handle);
265 bm->gltexture->lastrend=GameTime;
266 bm->gltexture->numrend++;
267 //// if (bm->gltexture->numrend==80 || bm->gltexture->numrend==4000 || bm->gltexture->numrend==80000){
268 // if (bm->gltexture->numrend==100){
269 // bm->gltexture->prio+=0.1;
270 //// glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_PRIORITY,bm->gltexture->prio);
271 // glPrioritizeTextures(1,&bm->gltexture->handle,&bm->gltexture->prio);
274 //gltexture MUST be bound first
275 void ogl_texwrap(ogl_texture *gltexture,int state)
277 if (gltexture->wrapstate[active_texture_unit] != state || gltexture->numrend < 1)
279 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, state);
280 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, state);
281 gltexture->wrapstate[active_texture_unit] = state;
285 //crude texture precaching
286 //handles: powerups, walls, weapons, polymodels, etc.
287 //it is done with the horrid do_special_effects kludge so that sides that have to be texmerged and have animated textures will be correctly cached.
288 //similarly, with the objects(esp weapons), we could just go through and cache em all instead, but that would get ones that might not even be on the level
291 void ogl_cache_polymodel_textures(int model_num){
292 polymodel *po=&Polygon_models[model_num];
294 for (i=0;i<po->n_textures;i++) {
295 // texture_list_index[i] = ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]];
296 ogl_loadbmtexture(&GameBitmaps[ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]].index]);
299 void ogl_cache_vclip_textures(vclip *vc){
301 for (i=0;i<vc->num_frames;i++){
302 PIGGY_PAGE_IN(vc->frames[i]);
303 ogl_loadbmtexture(&GameBitmaps[vc->frames[i].index]);
306 #define ogl_cache_vclipn_textures(i) ogl_cache_vclip_textures(&Vclip[i])
307 void ogl_cache_weapon_textures(weapon_info *w){
308 ogl_cache_vclipn_textures(w->flash_vclip);
309 ogl_cache_vclipn_textures(w->robot_hit_vclip);
310 ogl_cache_vclipn_textures(w->wall_hit_vclip);
311 if (w->render_type==WEAPON_RENDER_VCLIP)
312 ogl_cache_vclipn_textures(w->weapon_vclip);
313 else if (w->render_type==WEAPON_RENDER_POLYMODEL)
314 ogl_cache_polymodel_textures(w->model_num);
317 void ogl_cache_level_textures(void)
326 ogl_reset_texture_stats_internal();//loading a new lev should reset textures
328 for (i=0,ec=Effects;i<Num_effects;i++,ec++) {
329 if ((Effects[i].changing_wall_texture == -1) && (Effects[i].changing_object_texture==-1) )
331 if (ec->vc.num_frames>max_efx)
332 max_efx=ec->vc.num_frames;
334 glmprintf((0,"max_efx:%i\n",max_efx));
335 for (ef=0;ef<max_efx;ef++){
336 for (i=0,ec=Effects;i<Num_effects;i++,ec++) {
337 if ((Effects[i].changing_wall_texture == -1) && (Effects[i].changing_object_texture==-1) )
339 // if (ec->vc.num_frames>max_efx)
340 // max_efx=ec->vc.num_frames;
343 do_special_effects();
345 for (seg=0;seg<Num_segments;seg++){
346 for (side=0;side<MAX_SIDES_PER_SEGMENT;side++){
347 sidep=&Segments[seg].sides[side];
348 tmap1=sidep->tmap_num;
349 tmap2=sidep->tmap_num2;
350 if (tmap1<0 || tmap1>=NumTextures){
351 glmprintf((0,"ogl_cache_level_textures %i %i %i %i\n",seg,side,tmap1,NumTextures));
355 PIGGY_PAGE_IN(Textures[tmap1]);
356 bm = &GameBitmaps[Textures[tmap1].index];
358 PIGGY_PAGE_IN(Textures[tmap2&0x3FFF]);
359 bm2 = &GameBitmaps[Textures[tmap2&0x3FFF].index];
360 if (ogl_alttexmerge==0 || (bm2->bm_flags & BM_FLAG_SUPER_TRANSPARENT))
361 bm = texmerge_get_cached_bitmap( tmap1, tmap2 );
363 ogl_loadbmtexture(bm2);
365 // glmprintf((0,"ogl_cache_level_textures seg %i side %i t1 %i t2 %x bm %p NT %i\n",seg,side,tmap1,tmap2,bm,NumTextures));
367 ogl_loadbmtexture(bm);
370 glmprintf((0,"finished ef:%i\n",ef));
372 reset_special_effects();
373 init_special_effects();
376 //always have lasers and concs
377 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[LASER_INDEX]]);
378 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[CONCUSSION_INDEX]]);
379 for (i=0;i<Highest_object_index;i++){
380 if(Objects[i].render_type==RT_POWERUP){
381 ogl_cache_vclipn_textures(Objects[i].rtype.vclip_info.vclip_num);
382 switch (Objects[i].id){
384 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[LASER_INDEX]]);
388 case POW_VULCAN_WEAPON:
389 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[VULCAN_INDEX]]);
391 case POW_SPREADFIRE_WEAPON:
392 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[SPREADFIRE_INDEX]]);
394 case POW_PLASMA_WEAPON:
395 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[PLASMA_INDEX]]);
397 case POW_FUSION_WEAPON:
398 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[FUSION_INDEX]]);
400 /* case POW_MISSILE_1:
402 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[CONCUSSION_INDEX]]);
404 case POW_PROXIMITY_WEAPON:
405 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[PROXIMITY_INDEX]]);
407 case POW_HOMING_AMMO_1:
408 case POW_HOMING_AMMO_4:
409 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[HOMING_INDEX]]);
411 case POW_SMARTBOMB_WEAPON:
412 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[SMART_INDEX]]);
414 case POW_MEGA_WEAPON:
415 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[MEGA_INDEX]]);
419 else if(Objects[i].render_type==RT_POLYOBJ){
420 ogl_cache_polymodel_textures(Objects[i].rtype.pobj_info.model_num);
424 glmprintf((0,"finished caching\n"));
425 r_cachedtexcount = r_texcount;
428 int r_polyc,r_tpolyc,r_bitmapc,r_ubitmapc,r_ubitbltc,r_upixelc;
429 #define f2glf(x) (f2fl(x))
431 bool g3_draw_line(g3s_point *p0,g3s_point *p1)
434 c=grd_curcanv->cv_color;
435 OGL_DISABLE(TEXTURE_2D);
436 glColor3f(PAL2Tr(c),PAL2Tg(c),PAL2Tb(c));
438 glVertex3f(f2glf(p0->p3_vec.x),f2glf(p0->p3_vec.y),-f2glf(p0->p3_vec.z));
439 glVertex3f(f2glf(p1->p3_vec.x),f2glf(p1->p3_vec.y),-f2glf(p1->p3_vec.z));
443 void ogl_drawcircle2(int nsides,int type,float xsc,float xo,float ysc,float yo){
447 for (i=0; i<nsides; i++) {
448 ang = 2.0*M_PI*i/nsides;
449 glVertex2f(cosf(ang)*xsc+xo,sinf(ang)*ysc+yo);
453 void ogl_drawcircle(int nsides,int type){
457 for (i=0; i<nsides; i++) {
458 ang = 2.0*M_PI*i/nsides;
459 glVertex2f(cosf(ang),sinf(ang));
463 int circle_list_init(int nsides,int type,int mode) {
464 int hand=glGenLists(1);
465 glNewList(hand, mode);
466 /* draw a unit radius circle in xy plane centered on origin */
467 ogl_drawcircle(nsides,type);
471 float bright_g[4]={ 32.0/256, 252.0/256, 32.0/256};
472 float dark_g[4]={ 32.0/256, 148.0/256, 32.0/256};
473 float darker_g[4]={ 32.0/256, 128.0/256, 32.0/256};
474 void ogl_draw_reticle(int cross,int primary,int secondary){
475 float scale=(float)Canvas_height/(float)grd_curscreen->sc_h;
477 // glTranslatef(0.5,0.5,0);
478 glTranslatef((grd_curcanv->cv_bitmap.bm_w/2+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,1.0-(grd_curcanv->cv_bitmap.bm_h/2+grd_curcanv->cv_bitmap.bm_y)/(float)last_height,0);
479 glScalef(scale/320.0,scale/200.0,scale);//the positions are based upon the standard reticle at 320x200 res.
481 OGL_DISABLE(TEXTURE_2D);
483 if (!cross_lh[cross]){
484 cross_lh[cross]=glGenLists(1);
485 glNewList(cross_lh[cross], GL_COMPILE_AND_EXECUTE);
488 glColor3fv(darker_g);
489 glVertex2f(-4.0,4.0);
491 glColor3fv(bright_g);
494 glVertex2f(-2.0,2.0);
498 glVertex2f(-3.0,-2.0);
500 glColor3fv(bright_g);
501 glVertex2f(-2.0,-1.0);
504 glColor3fv(darker_g);
507 glColor3fv(bright_g);
514 glVertex2f(3.0,-2.0);
516 glColor3fv(bright_g);
517 glVertex2f(2.0,-1.0);
522 glCallList(cross_lh[cross]);
524 // if (Canvas_height>200)
525 // glLineWidth(Canvas_height/(float)200);
526 if (!primary_lh[primary]){
527 primary_lh[primary]=glGenLists(1);
528 glNewList(primary_lh[primary], GL_COMPILE_AND_EXECUTE);
533 glVertex2f(-14.0,-8.0);
534 glVertex2f(-8.0,-5.0);
536 glVertex2f(14.0,-8.0);
537 glVertex2f(8.0,-5.0);
542 glColor3fv(bright_g);
544 ogl_drawcircle2(6,GL_POLYGON,1.5,-7.0,1.5,-5.0);
546 ogl_drawcircle2(6,GL_POLYGON,1.5,7.0,1.5,-5.0);
550 glColor3fv(bright_g);
552 ogl_drawcircle2(4,GL_POLYGON,1.0,-14.0,1.0,-8.0);
554 ogl_drawcircle2(4,GL_POLYGON,1.0,14.0,1.0,-8.0);
558 glCallList(primary_lh[primary]);
559 // if (Canvas_height>200)
562 if (!secondary_lh[secondary]){
563 secondary_lh[secondary]=glGenLists(1);
564 glNewList(secondary_lh[secondary], GL_COMPILE_AND_EXECUTE);
568 glColor3fv(darker_g);
570 glColor3fv(bright_g);
571 ogl_drawcircle2(8,GL_LINE_LOOP,2.0,-10.0,2.0,-1.0);
574 glColor3fv(darker_g);
576 glColor3fv(bright_g);
577 ogl_drawcircle2(8,GL_LINE_LOOP,2.0,10.0,2.0,-1.0);
579 //bottom/middle secondary
581 glColor3fv(darker_g);
583 glColor3fv(bright_g);
584 ogl_drawcircle2(8,GL_LINE_LOOP,2.0,0.0,2.0,-7.0);
588 glCallList(secondary_lh[secondary]);
592 int g3_draw_sphere(g3s_point *pnt,fix rad){
594 c=grd_curcanv->cv_color;
595 OGL_DISABLE(TEXTURE_2D);
596 // glPointSize(f2glf(rad));
597 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
598 // glBegin(GL_POINTS);
599 // glVertex3f(f2glf(pnt->p3_vec.x),f2glf(pnt->p3_vec.y),-f2glf(pnt->p3_vec.z));
602 glTranslatef(f2glf(pnt->p3_vec.x),f2glf(pnt->p3_vec.y),-f2glf(pnt->p3_vec.z));
603 glScalef(f2glf(rad),f2glf(rad),f2glf(rad));
604 if (!sphereh) sphereh=circle_list_init(20,GL_POLYGON,GL_COMPILE_AND_EXECUTE);
605 else glCallList(sphereh);
610 int gr_ucircle(fix xc1, fix yc1, fix r1)
613 c=grd_curcanv->cv_color;
614 OGL_DISABLE(TEXTURE_2D);
615 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
618 (f2fl(xc1) + grd_curcanv->cv_bitmap.bm_x + 0.5) / (float)last_width,
619 1.0 - (f2fl(yc1) + grd_curcanv->cv_bitmap.bm_y + 0.5) / (float)last_height,0);
620 glScalef(f2fl(r1) / last_width, f2fl(r1) / last_height, 1.0);
621 ogl_drawcircle(10 + 2 * (int)(M_PI * f2fl(r1) / 19), GL_LINE_LOOP);
625 int gr_circle(fix xc1,fix yc1,fix r1){
626 return gr_ucircle(xc1,yc1,r1);
629 bool g3_draw_poly(int nv,g3s_point **pointlist)
633 c=grd_curcanv->cv_color;
634 // glColor3f((gr_palette[c*3]+gr_palette_gamma)/63.0,(gr_palette[c*3+1]+gr_palette_gamma)/63.0,(gr_palette[c*3+2]+gr_palette_gamma)/63.0);
635 OGL_DISABLE(TEXTURE_2D);
636 if (Gr_scanline_darkening_level >= GR_FADE_LEVELS)
637 glColor3f(PAL2Tr(c), PAL2Tg(c), PAL2Tb(c));
639 glColor4f(PAL2Tr(c), PAL2Tg(c), PAL2Tb(c), 1.0 - (float)Gr_scanline_darkening_level / ((float)GR_FADE_LEVELS - 1.0));
640 glBegin(GL_TRIANGLE_FAN);
642 // glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
643 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
649 void gr_upoly_tmap(int nverts, int *vert ){
650 mprintf((0,"gr_upoly_tmap: unhandled\n"));//should never get called
652 void draw_tmap_flat(grs_bitmap *bm,int nv,g3s_point **vertlist){
653 mprintf((0,"draw_tmap_flat: unhandled\n"));//should never get called
655 extern void (*tmap_drawer_ptr)(grs_bitmap *bm,int nv,g3s_point **vertlist);
656 bool g3_draw_tmap(int nv,g3s_point **pointlist,g3s_uvl *uvl_list,grs_bitmap *bm)
660 if (tmap_drawer_ptr==draw_tmap_flat){
661 /* fix average_light=0;
664 average_light += uvl_list[i].l;*/
665 OGL_DISABLE(TEXTURE_2D);
666 // glmprintf((0,"Gr_scanline_darkening_level=%i %f\n",Gr_scanline_darkening_level,Gr_scanline_darkening_level/(float)NUM_LIGHTING_LEVELS));
667 glColor4f(0,0,0,1.0-(Gr_scanline_darkening_level/(float)NUM_LIGHTING_LEVELS));
668 //glColor4f(0,0,0,f2fl(average_light/nv));
669 glBegin(GL_TRIANGLE_FAN);
671 // glColor4f(0,0,0,f2fl(uvl_list[c].l));
672 // glTexCoord2f(f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
673 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
676 }else if (tmap_drawer_ptr==draw_tmap){
678 /* if (bm->bm_w !=64||bm->bm_h!=64)
679 printf("g3_draw_tmap w %i h %i\n",bm->bm_w,bm->bm_h);*/
680 OGL_ENABLE(TEXTURE_2D);
682 ogl_texwrap(bm->gltexture,GL_REPEAT);
683 glBegin(GL_TRIANGLE_FAN);
685 if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
688 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
689 l=f2fl(uvl_list[c].l);
692 glTexCoord2f(f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
693 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
694 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
698 mprintf((0,"g3_draw_tmap: unhandled tmap_drawer %p\n",tmap_drawer_ptr));
703 int active_texture_unit = 0;
705 void ogl_setActiveTexture(int t)
707 if (ogl_arb_multitexture_ok)
709 #ifdef GL_ARB_multitexture
711 glActiveTextureARB(GL_TEXTURE0_ARB);
713 glActiveTextureARB(GL_TEXTURE1_ARB);
716 else if (ogl_sgis_multitexture_ok)
718 #ifdef GL_SGIS_multitexture
720 glSelectTextureSGIS(GL_TEXTURE0_SGIS);
722 glSelectTextureSGIS(GL_TEXTURE1_SGIS);
725 active_texture_unit = t;
728 void ogl_MultiTexCoord2f(int t, float u, float v)
730 if (ogl_arb_multitexture_ok)
732 #ifdef GL_ARB_multitexture
734 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, u, v);
736 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, u, v);
739 else if (ogl_sgis_multitexture_ok)
741 #ifdef GL_SGIS_multitexture
743 glMultiTexCoord2fSGIS(GL_TEXTURE0_SGIS, u, v);
745 glMultiTexCoord2fSGIS(GL_TEXTURE1_SGIS, u, v);
750 bool g3_draw_tmap_2(int nv, g3s_point **pointlist, g3s_uvl *uvl_list, grs_bitmap *bmbot, grs_bitmap *bm, int orient)
752 #if (defined(GL_NV_texture_env_combine4) && (defined(GL_ARB_multitexture) || defined(GL_SGIS_multitexture)))
753 if (ogl_nv_texture_env_combine4_ok && (ogl_arb_multitexture_ok || ogl_sgis_multitexture_ok))
758 if (tmap_drawer_ptr != draw_tmap)
763 //ogl_setActiveTexture(0);
764 OGL_ENABLE(TEXTURE_2D);
765 ogl_bindbmtex(bmbot);
766 ogl_texwrap(bmbot->gltexture, GL_REPEAT);
767 // GL_MODULATE is fine for texture 0
769 ogl_setActiveTexture(1);
770 glEnable(GL_TEXTURE_2D);
772 ogl_texwrap(bm->gltexture,GL_REPEAT);
774 // GL_DECAL works sorta ok but the top texture is fullbright.
775 //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
777 // http://oss.sgi.com/projects/ogl-sample/registry/NV/texture_env_combine4.txt
778 // only GL_NV_texture_env_combine4 lets us do what we need:
779 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV);
781 // multiply top texture by color(vertex lighting) and add bottom texture(where alpha says to)
782 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_ADD);
784 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_EXT, GL_TEXTURE);
785 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_EXT, GL_PRIMARY_COLOR_EXT);
786 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_EXT, GL_TEXTURE);
787 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE3_RGB_NV, GL_PREVIOUS_EXT);
789 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_EXT, GL_SRC_COLOR);
790 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_EXT, GL_SRC_COLOR);
791 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_EXT, GL_ONE_MINUS_SRC_ALPHA);
792 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND3_RGB_NV, GL_SRC_COLOR);
794 // add up alpha channels
795 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_EXT, GL_ADD);
797 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_EXT, GL_TEXTURE);
798 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_EXT, GL_ZERO);
799 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_EXT, GL_PREVIOUS_EXT);
800 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE3_ALPHA_NV, GL_ZERO);
802 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_EXT, GL_SRC_ALPHA);
803 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_EXT, GL_ONE_MINUS_SRC_ALPHA);
804 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_EXT, GL_SRC_ALPHA);
805 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND3_ALPHA_NV, GL_ONE_MINUS_SRC_ALPHA);
807 // GL_ARB_texture_env_combine comes close, but doesn't quite make it.
808 //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
810 //// this gives effect like GL_DECAL:
811 //glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_INTERPOLATE);
812 //glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
813 //glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
814 //glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);
817 // this properly shades the top texture, but the bottom texture doesn't get through.
818 //glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
819 //glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PRIMARY_COLOR_ARB);
820 //glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE);
824 //glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_ADD);
825 //glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);
826 //glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);
829 glBegin(GL_TRIANGLE_FAN);
833 u1=1.0-f2glf(uvl_list[c].v);
834 v1=f2glf(uvl_list[c].u);
837 u1=1.0-f2glf(uvl_list[c].u);
838 v1=1.0-f2glf(uvl_list[c].v);
841 u1=f2glf(uvl_list[c].v);
842 v1=1.0-f2glf(uvl_list[c].u);
845 u1=f2glf(uvl_list[c].u);
846 v1=f2glf(uvl_list[c].v);
849 if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
852 l=f2fl(uvl_list[c].l);
855 ogl_MultiTexCoord2f(0, f2glf(uvl_list[c].u), f2glf(uvl_list[c].v));
856 ogl_MultiTexCoord2f(1, u1, v1);
857 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
858 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
859 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
862 //ogl_setActiveTexture(1); // still the active texture
863 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
864 glDisable(GL_TEXTURE_2D);
865 ogl_setActiveTexture(0);
873 g3_draw_tmap(nv,pointlist,uvl_list,bmbot);//draw the bottom texture first.. could be optimized with multitexturing..
876 /* if (bm->bm_w !=64||bm->bm_h!=64)
877 printf("g3_draw_tmap w %i h %i\n",bm->bm_w,bm->bm_h);*/
878 OGL_ENABLE(TEXTURE_2D);
880 ogl_texwrap(bm->gltexture,GL_REPEAT);
881 glBegin(GL_TRIANGLE_FAN);
885 u1=1.0-f2glf(uvl_list[c].v);
886 v1=f2glf(uvl_list[c].u);
889 u1=1.0-f2glf(uvl_list[c].u);
890 v1=1.0-f2glf(uvl_list[c].v);
893 u1=f2glf(uvl_list[c].v);
894 v1=1.0-f2glf(uvl_list[c].u);
897 u1=f2glf(uvl_list[c].u);
898 v1=f2glf(uvl_list[c].v);
901 if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
904 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
905 l=f2fl(uvl_list[c].l);
909 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
910 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
911 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
918 bool g3_draw_bitmap(vms_vector *pos,fix width,fix height,grs_bitmap *bm, int orientation)
921 vms_vector pv,v1;//,v2;
925 // printf("g3_draw_bitmap: %f,%f,%f - ",f2glf(pos->x),f2glf(pos->y),-f2glf(pos->z));
926 // printf("(%f,%f,%f) ",f2glf(View_position.x),f2glf(View_position.y),-f2glf(View_position.z));
928 OGL_ENABLE(TEXTURE_2D);
930 ogl_texwrap(bm->gltexture,GL_CLAMP);
933 glColor3f(1.0,1.0,1.0);
934 width = fixmul(width,Matrix_scale.x);
935 height = fixmul(height,Matrix_scale.y);
937 // g3_rotate_point(&p[i],pos);
938 vm_vec_sub(&v1,pos,&View_position);
939 vm_vec_rotate(&pv,&v1,&View_matrix);
940 // printf(" %f,%f,%f->",f2glf(pv.x),f2glf(pv.y),-f2glf(pv.z));
943 glTexCoord2f(0.0, 0.0);
948 glTexCoord2f(bm->gltexture->u, 0.0);
953 glTexCoord2f(bm->gltexture->u, bm->gltexture->v);
958 glTexCoord2f(0.0, bm->gltexture->v);
963 // vm_vec_rotate(&v2,&v1,&View_matrix);
964 // vm_vec_sub(&v1,&v2,&pv);
965 //vm_vec_sub(&v1,&pv,&v2);
966 // vm_vec_sub(&v2,&pv,&v1);
967 glVertex3f(f2glf(pv.x),f2glf(pv.y),-f2glf(pv.z));
968 // printf("%f,%f,%f ",f2glf(v1.x),f2glf(v1.y),-f2glf(v1.z));
976 bool ogl_ubitmapm_c(int x, int y,grs_bitmap *bm,int c)
981 x+=grd_curcanv->cv_bitmap.bm_x;
982 y+=grd_curcanv->cv_bitmap.bm_y;
983 xo=x/(float)last_width;
984 xf=(bm->bm_w+x)/(float)last_width;
985 yo=1.0-y/(float)last_height;
986 yf=1.0-(bm->bm_h+y)/(float)last_height;
988 // printf("g3_draw_bitmap: %f,%f,%f - ",f2glf(pos->x),f2glf(pos->y),-f2glf(pos->z));
989 // printf("(%f,%f,%f) ",f2glf(View_position.x),f2glf(View_position.y),-f2glf(View_position.z));
991 /* glEnABLE(ALPHA_TEST);
992 glAlphaFunc(GL_GREATER,0.0);*/
994 OGL_ENABLE(TEXTURE_2D);
996 ogl_texwrap(bm->gltexture,GL_CLAMP);
1000 if (bm->bm_w==bm->gltexture->w)
1001 u2=bm->gltexture->u;
1003 u2=(bm->bm_w+bm->bm_x)/(float)bm->gltexture->tw;
1005 u1=bm->bm_x/(float)bm->gltexture->tw;
1006 u2=(bm->bm_w+bm->bm_x)/(float)bm->gltexture->tw;
1010 if (bm->bm_h==bm->gltexture->h)
1011 v2=bm->gltexture->v;
1013 v2=(bm->bm_h+bm->bm_y)/(float)bm->gltexture->th;
1015 v1=bm->bm_y/(float)bm->gltexture->th;
1016 v2=(bm->bm_h+bm->bm_y)/(float)bm->gltexture->th;
1021 glColor3f(1.0,1.0,1.0);
1023 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
1024 glTexCoord2f(u1, v1); glVertex2f(xo, yo);
1025 glTexCoord2f(u2, v1); glVertex2f(xf, yo);
1026 glTexCoord2f(u2, v2); glVertex2f(xf, yf);
1027 glTexCoord2f(u1, v2); glVertex2f(xo, yf);
1029 // glDisABLE(ALPHA_TEST);
1033 bool ogl_ubitmapm(int x, int y,grs_bitmap *bm){
1034 return ogl_ubitmapm_c(x,y,bm,-1);
1035 // return ogl_ubitblt(bm->bm_w,bm->bm_h,x,y,0,0,bm,NULL);
1038 //also upsidedown, currently.
1039 bool ogl_ubitblt(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest)
1041 GLfloat xo,yo;//,xs,ys;
1042 glmprintf((0,"ogl_ubitblt(w=%i,h=%i,dx=%i,dy=%i,sx=%i,sy=%i,src=%p,dest=%p\n",w,h, dx, dy,sx, sy, src,dest));
1047 xo=dx/(float)last_width;
1048 // xo=dx/(float)grd_curscreen->sc_w;
1049 // xs=w/(float)last_width;
1050 //yo=1.0-dy/(float)last_height;
1051 yo=1.0-(dy+h)/(float)last_height;
1052 // ys=h/(float)last_height;
1054 // OGL_ENABLE(TEXTURE_2D);
1056 OGL_DISABLE(TEXTURE_2D);
1057 glRasterPos2f(xo,yo);
1058 ogl_filltexbuf(src->bm_data,texbuf,src->bm_w,w,h,sx,sy,w,h,GL_RGBA);
1059 glDrawPixels(w,h,GL_RGBA,GL_UNSIGNED_BYTE,texbuf);
1065 bool ogl_ubitblt_i(int dw,int dh,int dx,int dy, int sw, int sh, int sx, int sy, grs_bitmap * src, grs_bitmap * dest)
1067 GLfloat xo,yo,xs,ys;
1068 GLfloat u1,v1;//,u2,v2;
1070 // unsigned char *oldpal;
1073 ogl_init_texture(&tex);
1075 tex.prio=0.0;tex.wantmip=0;
1076 tex.lw=src->bm_rowsize;
1078 /* if (w==src->bm_w && sx==0){
1081 u1=sx/(float)src->bm_w*src->glu;
1082 u2=w/(float)src->bm_w*src->glu+u1;
1084 if (h==src->bm_h && sy==0){
1087 v1=sy/(float)src->bm_h*src->glv;
1088 v2=h/(float)src->bm_h*src->glv+v1;
1094 xo=dx/(float)last_width;
1095 xs=dw/(float)last_width;
1096 yo=1.0-dy/(float)last_height;
1097 ys=dh/(float)last_height;
1099 OGL_ENABLE(TEXTURE_2D);
1102 ogl_pal=gr_current_pal;
1103 ogl_loadtexture(src->bm_data,sx,sy,&tex);
1106 OGL_BINDTEXTURE(tex.handle);
1108 ogl_texwrap(&tex,GL_CLAMP);
1111 glColor3f(1.0,1.0,1.0);
1112 glTexCoord2f(u1, v1); glVertex2f(xo, yo);
1113 glTexCoord2f(tex.u, v1); glVertex2f(xo+xs, yo);
1114 glTexCoord2f(tex.u, tex.v); glVertex2f(xo+xs, yo-ys);
1115 glTexCoord2f(u1, tex.v); glVertex2f(xo, yo-ys);
1117 ogl_freetexture(&tex);
1120 bool ogl_ubitblt(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1121 return ogl_ubitblt_i(w,h,dx,dy,w,h,sx,sy,src,dest);
1124 bool ogl_ubitblt_tolinear(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1126 unsigned char *d,*s;
1130 w1=grd_curscreen->sc_w;h1=grd_curscreen->sc_h;
1131 if (w1*h1*3>OGLTEXBUFSIZE)
1132 Error("ogl_ubitblt_tolinear: screen res larger than OGLTEXBUFSIZE\n");
1134 if (ogl_readpixels_ok>0){
1135 OGL_DISABLE(TEXTURE_2D);
1136 glReadBuffer(GL_FRONT);
1137 glReadPixels(0,0,w1,h1,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1138 // glReadPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1139 // glReadPixels(sx,sy,w+sx,h+sy,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1141 memset(texbuf,0,w1*h1*3);
1145 d=dest->bm_data+dx+(dy+i)*dest->bm_rowsize;
1146 s=texbuf+((h1-(i+sy+1))*w1+sx)*3;
1148 *d=gr_find_closest_color(s[0]/4,s[1]/4,s[2]/4);
1155 unsigned char *d,*s,*e;
1156 if (w*h*3>OGLTEXBUFSIZE)
1157 Error("ogl_ubitblt_tolinear: size larger than OGLTEXBUFSIZE\n");
1160 #if 1//also seems to cause a mess. need to look into it a bit more..
1161 if (ogl_readpixels_ok>0){
1162 OGL_DISABLE(TEXTURE_2D);
1163 glReadBuffer(GL_FRONT);
1164 // glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1165 glReadPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1168 memset(texbuf,0,w*h*3);
1169 // d=dest->bm_data+dx+(dy+i)*dest->bm_rowsize;
1170 d=dest->bm_data+dx+dy*dest->bm_rowsize;
1172 s=texbuf+w*(h-(i+1))*3;
1174 if (s<texbuf){Error("blah1\n");}
1175 if (d<dest->bm_data){Error("blah3\n");}
1176 // d=dest->bm_data+(i*dest->bm_rowsize);
1180 if (s>texbuf+w*h*3-3){Error("blah2\n");}
1181 if (d>dest->bm_data+dest->bm_rowsize*(h+dy)+dx ){Error("blah4\n");}
1182 *d=gr_find_closest_color(s[0]/4,s[1]/4,s[2]/4);
1188 d+=dest->bm_rowsize;
1190 glmprintf((0,"c=%i w*h=%i\n",c,w*h));
1195 bool ogl_ubitblt_copy(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1196 #if 0 //just seems to cause a mess.
1197 GLfloat xo,yo;//,xs,ys;
1202 // xo=dx/(float)last_width;
1203 xo=dx/(float)grd_curscreen->sc_w;
1204 // yo=1.0-(dy+h)/(float)last_height;
1205 yo=1.0-(dy+h)/(float)grd_curscreen->sc_h;
1208 OGL_DISABLE(TEXTURE_2D);
1209 glReadBuffer(GL_FRONT);
1210 glRasterPos2f(xo,yo);
1211 // glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1212 glCopyPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_COLOR);
1218 grs_canvas *offscreen_save_canv = NULL, *offscreen_canv = NULL;
1220 void ogl_start_offscreen_render(int x, int y, int w, int h)
1223 Error("ogl_start_offscreen_render: offscreen_canv!=NULL");
1224 offscreen_save_canv = grd_curcanv;
1225 offscreen_canv = gr_create_sub_canvas(grd_curcanv, x, y, w, h);
1226 gr_set_current_canvas(offscreen_canv);
1227 glDrawBuffer(GL_BACK);
1229 void ogl_end_offscreen_render(void)
1233 if (!offscreen_canv)
1234 Error("ogl_end_offscreen_render: no offscreen_canv");
1236 glDrawBuffer(GL_FRONT);
1237 glReadBuffer(GL_BACK);
1238 OGL_DISABLE(TEXTURE_2D);
1240 y = last_height - offscreen_canv->cv_bitmap.bm_y - offscreen_canv->cv_bitmap.bm_h;
1241 glRasterPos2f(offscreen_canv->cv_bitmap.bm_x/(float)last_width, y/(float)last_height);
1242 glCopyPixels(offscreen_canv->cv_bitmap.bm_x, y,
1243 offscreen_canv->cv_bitmap.bm_w,
1244 offscreen_canv->cv_bitmap.bm_h, GL_COLOR);
1246 gr_free_sub_canvas(offscreen_canv);
1247 gr_set_current_canvas(offscreen_save_canv);
1248 offscreen_canv=NULL;
1251 void ogl_start_frame(void){
1252 r_polyc=0;r_tpolyc=0;r_bitmapc=0;r_ubitmapc=0;r_ubitbltc=0;r_upixelc=0;
1253 // gl_badtexture=500;
1255 OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,Canvas_width,Canvas_height);
1256 glClearColor(0.0, 0.0, 0.0, 0.0);
1257 // glEnable(GL_ALPHA_TEST);
1258 // glAlphaFunc(GL_GREATER,0.01);
1259 glShadeModel(GL_SMOOTH);
1260 glMatrixMode(GL_PROJECTION);
1261 glLoadIdentity();//clear matrix
1262 //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),1.0,1000000.0);
1263 //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),0.01,1000000.0);
1264 gluPerspective(90.0,1.0,0.01,1000000.0);
1265 //gluPerspective(90.0,(GLfloat)(Canvas_width*3)/(GLfloat)(Canvas_height*4),0.01,1000000.0);
1266 // gluPerspective(75.0,(GLfloat)Canvas_width/(GLfloat)Canvas_height,1.0,1000000.0);
1267 glMatrixMode(GL_MODELVIEW);
1268 glLoadIdentity();//clear matrix
1270 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1271 // glDisABLE(DITHER);
1272 // glScalef(1.0,1.0,-1.0);
1273 // glScalef(1.0,1.0,-1.0);
1276 // glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1277 // OGL_TEXENV(GL_TEXTURE_ENV_MODE,GL_MODULATE);
1278 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_texmagfilt);
1279 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_texminfilt);
1280 // OGL_TEXPARAM(GL_TEXTURE_MAG_FILTER,GL_texmagfilt);
1281 // OGL_TEXPARAM(GL_TEXTURE_MIN_FILTER,GL_texminfilt);
1282 // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1283 // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1286 void merge_textures_stats(void);
1288 void ogl_end_frame(void){
1289 // OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,);
1290 OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
1292 // merge_textures_stats();
1293 // ogl_texture_stats();
1295 // glViewport(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
1296 glMatrixMode(GL_PROJECTION);
1297 glLoadIdentity();//clear matrix
1298 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
1299 glMatrixMode(GL_MODELVIEW);
1300 glLoadIdentity();//clear matrix
1301 // glDisABLE(BLEND);
1302 //glDisABLE(ALPHA_TEST);
1303 //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),1.0,1000000.0);
1304 // ogl_swap_buffers();//platform specific code
1305 // glClear(GL_COLOR_BUFFER_BIT);
1307 void ogl_swap_buffers(void){
1308 ogl_clean_texture_cache();
1309 if (gr_renderstats){
1310 gr_printf(5,GAME_FONT->ft_h*13+3*13,"%i flat %i tex %i sprites %i bitmaps",r_polyc,r_tpolyc,r_bitmapc,r_ubitmapc);
1311 // glmprintf((0,"ogl_end_frame: %i polys, %i tmaps, %i sprites, %i bitmaps, %i bitblts, %i pixels\n",r_polyc,r_tpolyc,r_bitmapc,r_ubitmapc,r_ubitbltc,r_upixelc));//we need to do it here because some things get drawn after end_frame
1314 ogl_swap_buffers_internal();
1315 glClear(GL_COLOR_BUFFER_BIT);
1318 int tex_format_supported(int iformat,int format){
1321 if (!ogl_intensity4_ok) return 0; break;
1322 case GL_LUMINANCE4_ALPHA4:
1323 if (!ogl_luminance4_alpha4_ok) return 0; break;
1325 if (!ogl_rgba2_ok) return 0; break;
1327 if (ogl_gettexlevelparam_ok){
1328 GLint internalFormat;
1329 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, iformat, 64, 64, 0,
1330 format, GL_UNSIGNED_BYTE, texbuf);//NULL?
1331 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0,
1332 GL_TEXTURE_INTERNAL_FORMAT,
1334 return (internalFormat==iformat);
1339 //little hack to find the largest or equal multiple of 2 for a given number
1342 for (i=2;i<=4096;i*=2)
1347 //GLubyte texbuf[512*512*4];
1348 GLubyte texbuf[OGLTEXBUFSIZE];
1349 void ogl_filltexbuf(unsigned char *data,GLubyte *texp,int truewidth,int width,int height,int dxo,int dyo,int twidth,int theight,int type)
1351 // GLushort *tex=(GLushort *)texp;
1353 if (twidth*theight*4>sizeof(texbuf))//shouldn't happen, descent never uses textures that big.
1354 Error("texture toobig %i %i",twidth,theight);
1357 for (y=0;y<theight;y++){
1358 i=dxo+truewidth*(y+dyo);
1359 for (x=0;x<twidth;x++){
1360 if (x<width && y<height)
1363 c=255;//fill the pad space with transparancy
1369 case GL_LUMINANCE_ALPHA:
1377 (*(texp++))=0;//transparent pixel
1383 case GL_LUMINANCE://these could prolly be done to make the intensity based upon the intensity of the resulting color, but its not needed for anything (yet?) so no point. :)
1386 case GL_LUMINANCE_ALPHA:
1391 //(*(texp++))=gr_palette[c*3]*4;
1392 //(*(texp++))=gr_palette[c*3+1]*4;
1393 //(*(texp++))=gr_palette[c*3+2]*4;
1394 (*(texp++))=ogl_pal[c*3]*4;
1395 (*(texp++))=ogl_pal[c*3+1]*4;
1396 (*(texp++))=ogl_pal[c*3+2]*4;
1397 (*(texp++))=255;//not transparent
1398 // (*(tex++))=(ogl_pal[c*3]>>1) + ((ogl_pal[c*3+1]>>1)<<5) + ((ogl_pal[c*3+2]>>1)<<10) + (1<<15);
1405 int tex_format_verify(ogl_texture *tex){
1406 while (!tex_format_supported(tex->internalformat,tex->format)){
1407 glmprintf((0,"tex format %x not supported",tex->internalformat));
1408 switch (tex->internalformat){
1410 if (ogl_luminance4_alpha4_ok){
1411 tex->internalformat=GL_LUMINANCE4_ALPHA4;
1412 tex->format=GL_LUMINANCE_ALPHA;
1414 }//note how it will fall through here if the statement is false
1415 case GL_LUMINANCE4_ALPHA4:
1417 tex->internalformat=GL_RGBA2;
1418 tex->format=GL_RGBA;
1420 }//note how it will fall through here if the statement is false
1422 tex->internalformat=ogl_rgba_format;
1423 tex->format=GL_RGBA;
1426 mprintf((0,"...no tex format to fall back on\n"));
1429 glmprintf((0,"...falling back to %x\n",tex->internalformat));
1433 void tex_set_size1(ogl_texture *tex,int dbits,int bits,int w, int h){
1435 if (tex->tw!=w || tex->th!=h){
1436 u=(tex->w/(float)tex->tw*w) * (tex->h/(float)tex->th*h);
1437 glmprintf((0,"shrunken texture?\n"));
1440 if (bits<=0){//the beta nvidia GLX server. doesn't ever return any bit sizes, so just use some assumptions.
1441 tex->bytes=((float)w*h*dbits)/8.0;
1442 tex->bytesu=((float)u*dbits)/8.0;
1444 tex->bytes=((float)w*h*bits)/8.0;
1445 tex->bytesu=((float)u*bits)/8.0;
1447 glmprintf((0,"tex_set_size1: %ix%i, %ib(%i) %iB\n",w,h,bits,dbits,tex->bytes));
1449 void tex_set_size(ogl_texture *tex){
1452 if (ogl_gettexlevelparam_ok){
1454 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&w);
1455 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&h);
1456 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_LUMINANCE_SIZE,&t);a+=t;
1457 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_INTENSITY_SIZE,&t);a+=t;
1458 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_RED_SIZE,&t);a+=t;
1459 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_GREEN_SIZE,&t);a+=t;
1460 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_BLUE_SIZE,&t);a+=t;
1461 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_ALPHA_SIZE,&t);a+=t;
1466 switch (tex->format){
1470 case GL_LUMINANCE_ALPHA:
1477 Error("tex_set_size unknown texformat\n");
1480 tex_set_size1(tex,bi,a,w,h);
1482 //loads a palettized bitmap into a ogl RGBA texture.
1483 //Sizes and pads dimensions to multiples of 2 if necessary.
1484 //In theory this could be a problem for repeating textures, but all real
1485 //textures (not sprites, etc) in descent are 64x64, so we are ok.
1486 //stores OpenGL textured id in *texid and u/v values required to get only the real data in *u/*v
1487 void ogl_loadtexture(unsigned char * data, int dxo,int dyo, ogl_texture *tex)
1489 //void ogl_loadtexture(unsigned char * data, int width, int height,int dxo,int dyo, int *texid,float *u,float *v,char domipmap,float prio){
1490 // int internalformat=GL_RGBA;
1491 // int format=GL_RGBA;
1493 tex->tw=pow2ize(tex->w);tex->th=pow2ize(tex->h);//calculate smallest texture size that can accomodate us (must be multiples of 2)
1494 // tex->tw=tex->w;tex->th=tex->h;//feeling lucky?
1496 if(gr_badtexture>0) return;
1498 #if !(defined(__APPLE__) && defined(__MACH__))
1499 // always fails on OS X, but textures work fine!
1500 if (tex_format_verify(tex))
1504 //calculate u/v values that would make the resulting texture correctly sized
1505 tex->u=(float)tex->w/(float)tex->tw;
1506 tex->v=(float)tex->h/(float)tex->th;
1508 // if (width!=twidth || height!=theight)
1509 // glmprintf((0,"sizing %ix%i texture up to %ix%i\n",width,height,twidth,theight));
1510 ogl_filltexbuf(data,texbuf,tex->lw,tex->w,tex->h,dxo,dyo,tex->tw,tex->th,tex->format);
1512 // Generate OpenGL texture IDs.
1513 glGenTextures(1, &tex->handle);
1516 glPrioritizeTextures(1,&tex->handle,&tex->prio);
1518 // Give our data to OpenGL.
1520 OGL_BINDTEXTURE(tex->handle);
1522 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1524 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_texmagfilt);
1525 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_texminfilt);
1526 if (ogl_ext_texture_filter_anisotropic_ok && GL_texanisofilt)
1527 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, GL_texanisofilt);
1531 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1532 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1534 // domipmap=0;//mipmaps aren't used in GL_NEAREST anyway, and making the mipmaps is pretty slow
1535 //however, if texturing mode becomes an ingame option, they would need to be made regardless, so it could switch to them later. OTOH, texturing mode could just be made a command line arg.
1537 if (tex->wantmip && GL_needmipmaps)
1538 gluBuild2DMipmaps( GL_TEXTURE_2D, tex->internalformat, tex->tw,
1539 tex->th, tex->format, GL_UNSIGNED_BYTE, texbuf);
1541 glTexImage2D(GL_TEXTURE_2D, 0, tex->internalformat,
1542 tex->tw, tex->th, 0, tex->format, // RGBA textures.
1543 GL_UNSIGNED_BYTE, // imageData is a GLubyte pointer.
1549 glmprintf((0,"ogl_loadtexture(%p,%i,%i,%ix%i,%p):%i u=%f v=%f b=%i bu=%i (%i)\n",data,tex->tw,tex->th,dxo,dyo,tex,tex->handle,tex->u,tex->v,tex->bytes,tex->bytesu,r_texcount));
1553 unsigned char decodebuf[512*512];
1555 void ogl_loadbmtexture_m(grs_bitmap *bm,int domipmap)
1558 while (bm->bm_parent)
1561 if (bm->gltexture==NULL){
1562 ogl_init_texture(bm->gltexture=ogl_get_free_texture());
1563 bm->gltexture->lw=bm->bm_w;
1564 bm->gltexture->w=bm->bm_w;
1565 bm->gltexture->h=bm->bm_h;
1566 bm->gltexture->wantmip=domipmap;
1569 if (bm->gltexture->handle>0)
1571 if (bm->gltexture->w==0){
1572 bm->gltexture->lw=bm->bm_w;
1573 bm->gltexture->w=bm->bm_w;
1574 bm->gltexture->h=bm->bm_h;
1577 if (bm->bm_flags & BM_FLAG_RLE){
1578 unsigned char * dbits;
1579 unsigned char * sbits;
1583 if (bm->bm_flags & BM_FLAG_RLE_BIG)
1586 sbits = &bm->bm_data[4 + (bm->bm_h * data_offset)];
1589 for (i=0; i < bm->bm_h; i++ ) {
1590 gr_rle_decode(sbits,dbits);
1591 if ( bm->bm_flags & BM_FLAG_RLE_BIG )
1592 sbits += (int)INTEL_SHORT(*((short *)&(bm->bm_data[4+(i*data_offset)])));
1594 sbits += (int)bm->bm_data[4+i];
1599 ogl_loadtexture(buf,0,0,bm->gltexture);
1602 void ogl_loadbmtexture(grs_bitmap *bm)
1604 ogl_loadbmtexture_m(bm,1);
1607 void ogl_freetexture(ogl_texture *gltexture)
1609 if (gltexture->handle>0) {
1611 glmprintf((0,"ogl_freetexture(%p):%i (last rend %is) (%i left)\n",gltexture,gltexture->handle,(GameTime-gltexture->lastrend)/f1_0,r_texcount));
1612 glDeleteTextures( 1, &gltexture->handle );
1613 // gltexture->handle=0;
1614 ogl_init_texture(gltexture);
1617 void ogl_freebmtexture(grs_bitmap *bm){
1619 ogl_freetexture(bm->gltexture);
1622 // glmprintf((0,"ogl_freebmtexture(%p,%p):%i (%i left)\n",bm->bm_data,&bm->gltexture,bm->gltexture,r_texcount));
1623 // glDeleteTextures( 1, &bm->gltexture );
1624 // bm->gltexture=-1;