4 * Graphics support functions for OpenGL.
19 #include <OpenGL/gl.h>
20 #include <OpenGL/glu.h>
31 #include "../../3d/globvars.h"
47 //change to 1 for lots of spew.
49 #define glmprintf(a) mprintf(a)
58 #if defined(__WINDOWS__) || defined(__MINGW32__) || defined(__MACOSX__)
59 #define cosf(a) cos(a)
60 #define sinf(a) sin(a)
63 unsigned char *ogl_pal=gr_palette;
65 int GL_texmagfilt=GL_NEAREST;
66 int GL_texminfilt=GL_NEAREST;
69 int last_width=-1,last_height=-1;
70 int GL_TEXTURE_2D_enabled=-1;
71 int GL_texclamp_enabled=-1;
73 extern int gr_renderstats;
74 extern int gr_badtexture;
75 int ogl_alttexmerge=1;//merge textures by just printing the seperate textures?
76 int ogl_rgba_format=4;
77 int ogl_intensity4_ok=1;
78 int ogl_luminance4_alpha4_ok=1;
80 int ogl_readpixels_ok=1;
81 int ogl_gettexlevelparam_ok=1;
82 #ifdef GL_ARB_multitexture
83 int ogl_arb_multitexture_ok=0;
85 #ifdef GL_SGIS_multitexture
86 int ogl_sgis_multitexture_ok=0;
92 int cross_lh[2]={0,0};
93 int primary_lh[3]={0,0,0};
94 int secondary_lh[5]={0,0,0,0,0};
97 #define OGL_BINDTEXTURE(a) if(gr_badtexture>0) glBindTexture(GL_TEXTURE_2D, 0);\
98 else if(a!=lastbound) {glBindTexture(GL_TEXTURE_2D, a);lastbound=a;}*/
99 #define OGL_BINDTEXTURE(a) if(gr_badtexture>0) glBindTexture(GL_TEXTURE_2D, 0);\
100 else glBindTexture(GL_TEXTURE_2D, a);
103 ogl_texture ogl_texture_list[OGL_TEXTURE_LIST_SIZE];
104 int ogl_texture_list_cur;
106 void ogl_init_texture_stats(ogl_texture* t){
107 t->prio=0.3;//default prio
111 void ogl_init_texture(ogl_texture* t){
113 t->internalformat=ogl_rgba_format;
117 ogl_init_texture_stats(t);
119 void ogl_reset_texture_stats_internal(void){
121 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++)
122 if (ogl_texture_list[i].handle>0){
123 ogl_init_texture_stats(&ogl_texture_list[i]);
126 void ogl_init_texture_list_internal(void){
128 ogl_texture_list_cur=0;
129 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++)
130 ogl_init_texture(&ogl_texture_list[i]);
132 void ogl_smash_texture_list_internal(void){
137 memset(cross_lh,0,sizeof(cross_lh));
138 memset(primary_lh,0,sizeof(primary_lh));
139 memset(secondary_lh,0,sizeof(secondary_lh));
140 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
141 if (ogl_texture_list[i].handle>0){
142 glDeleteTextures( 1, &ogl_texture_list[i].handle );
143 ogl_texture_list[i].handle=0;
145 ogl_texture_list[i].wrapstate=-1;
148 void ogl_vivify_texture_list_internal(void){
152 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
153 t=&ogl_texture_list[i];
154 if (t->w>0){//erk, realised this can't be done since we'd need the texture bm_data too. hmmm.
155 ogl_loadbmtexture(t);
160 ogl_texture* ogl_get_free_texture(void){
162 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
163 if (ogl_texture_list[ogl_texture_list_cur].handle<=0 && ogl_texture_list[ogl_texture_list_cur].w==0)
164 return &ogl_texture_list[ogl_texture_list_cur];
165 if (++ogl_texture_list_cur>=OGL_TEXTURE_LIST_SIZE)
166 ogl_texture_list_cur=0;
168 Error("OGL: texture list full!\n");
171 int ogl_texture_stats(void){
172 int used=0,usedl4a4=0,usedrgba=0,databytes=0,truebytes=0,datatexel=0,truetexel=0,i;
173 int prio0=0,prio1=0,prio2=0,prio3=0,prioh=0;
176 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
177 t=&ogl_texture_list[i];
180 datatexel+=t->w*t->h;
181 truetexel+=t->tw*t->th;
182 databytes+=t->bytesu;
184 if (t->prio<0.299)prio0++;
185 else if (t->prio<0.399)prio1++;
186 else if (t->prio<0.499)prio2++;
187 else if (t->prio<0.599)prio3++;
194 gr_printf(5,GAME_FONT->ft_h*14+3*14,"%i(%i,%i) %iK(%iK wasted)",used,usedrgba,usedl4a4,truebytes/1024,(truebytes-databytes)/1024);
196 // 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));
199 int ogl_mem_target=-1;
200 void ogl_clean_texture_cache(void){
205 if (ogl_mem_target<0){
211 bytes=ogl_texture_stats();
212 while (bytes>ogl_mem_target){
213 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
214 t=&ogl_texture_list[i];
216 if (t->lastrend+f1_0*time<GameTime){
219 if (bytes<ogl_mem_target)
225 Error("not enough mem?");
230 void ogl_bindbmtex(grs_bitmap *bm){
231 if (bm->gltexture==NULL || bm->gltexture->handle<=0)
232 ogl_loadbmtexture(bm);
233 OGL_BINDTEXTURE(bm->gltexture->handle);
234 bm->gltexture->lastrend=GameTime;
235 bm->gltexture->numrend++;
236 //// if (bm->gltexture->numrend==80 || bm->gltexture->numrend==4000 || bm->gltexture->numrend==80000){
237 // if (bm->gltexture->numrend==100){
238 // bm->gltexture->prio+=0.1;
239 //// glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_PRIORITY,bm->gltexture->prio);
240 // glPrioritizeTextures(1,&bm->gltexture->handle,&bm->gltexture->prio);
243 //gltexture MUST be bound first
244 void ogl_texwrap(ogl_texture *gltexture,int state){
245 if (gltexture->wrapstate!=state || gltexture->numrend<1){
246 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, state);
247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, state);
248 gltexture->wrapstate=state;
252 //crude texture precaching
253 //handles: powerups, walls, weapons, polymodels, etc.
254 //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.
255 //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
258 void ogl_cache_polymodel_textures(int model_num){
259 polymodel *po=&Polygon_models[model_num];
261 for (i=0;i<po->n_textures;i++) {
262 // texture_list_index[i] = ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]];
263 ogl_loadbmtexture(&GameBitmaps[ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]].index]);
266 void ogl_cache_vclip_textures(vclip *vc){
268 for (i=0;i<vc->num_frames;i++){
269 PIGGY_PAGE_IN(vc->frames[i]);
270 ogl_loadbmtexture(&GameBitmaps[vc->frames[i].index]);
273 #define ogl_cache_vclipn_textures(i) ogl_cache_vclip_textures(&Vclip[i])
274 void ogl_cache_weapon_textures(weapon_info *w){
275 ogl_cache_vclipn_textures(w->flash_vclip);
276 ogl_cache_vclipn_textures(w->robot_hit_vclip);
277 ogl_cache_vclipn_textures(w->wall_hit_vclip);
278 if (w->render_type==WEAPON_RENDER_VCLIP)
279 ogl_cache_vclipn_textures(w->weapon_vclip);
280 else if (w->render_type==WEAPON_RENDER_POLYMODEL)
281 ogl_cache_polymodel_textures(w->model_num);
283 void ogl_cache_level_textures(void){
291 ogl_reset_texture_stats_internal();//loading a new lev should reset textures
293 for (i=0,ec=Effects;i<Num_effects;i++,ec++) {
294 if ((Effects[i].changing_wall_texture == -1) && (Effects[i].changing_object_texture==-1) )
296 if (ec->vc.num_frames>max_efx)
297 max_efx=ec->vc.num_frames;
299 glmprintf((0,"max_efx:%i\n",max_efx));
300 for (ef=0;ef<max_efx;ef++){
301 for (i=0,ec=Effects;i<Num_effects;i++,ec++) {
302 if ((Effects[i].changing_wall_texture == -1) && (Effects[i].changing_object_texture==-1) )
304 // if (ec->vc.num_frames>max_efx)
305 // max_efx=ec->vc.num_frames;
308 do_special_effects();
310 for (seg=0;seg<Num_segments;seg++){
311 for (side=0;side<MAX_SIDES_PER_SEGMENT;side++){
312 sidep=&Segments[seg].sides[side];
313 tmap1=sidep->tmap_num;
314 tmap2=sidep->tmap_num2;
315 if (tmap1<0 || tmap1>=NumTextures){
316 glmprintf((0,"ogl_cache_level_textures %i %i %i %i\n",seg,side,tmap1,NumTextures));
320 PIGGY_PAGE_IN(Textures[tmap1]);
321 bm = &GameBitmaps[Textures[tmap1].index];
323 PIGGY_PAGE_IN(Textures[tmap2&0x3FFF]);
324 bm2 = &GameBitmaps[Textures[tmap2&0x3FFF].index];
325 if (ogl_alttexmerge==0 || (bm2->bm_flags & BM_FLAG_SUPER_TRANSPARENT))
326 bm = texmerge_get_cached_bitmap( tmap1, tmap2 );
328 ogl_loadbmtexture(bm2);
330 // 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));
332 ogl_loadbmtexture(bm);
335 glmprintf((0,"finished ef:%i\n",ef));
337 reset_special_effects();
338 init_special_effects();
341 //always have lasers and concs
342 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[LASER_INDEX]]);
343 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[CONCUSSION_INDEX]]);
344 for (i=0;i<Highest_object_index;i++){
345 if(Objects[i].render_type==RT_POWERUP){
346 ogl_cache_vclipn_textures(Objects[i].rtype.vclip_info.vclip_num);
347 switch (Objects[i].id){
349 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[LASER_INDEX]]);
353 case POW_VULCAN_WEAPON:
354 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[VULCAN_INDEX]]);
356 case POW_SPREADFIRE_WEAPON:
357 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[SPREADFIRE_INDEX]]);
359 case POW_PLASMA_WEAPON:
360 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[PLASMA_INDEX]]);
362 case POW_FUSION_WEAPON:
363 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[FUSION_INDEX]]);
365 /* case POW_MISSILE_1:
367 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[CONCUSSION_INDEX]]);
369 case POW_PROXIMITY_WEAPON:
370 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[PROXIMITY_INDEX]]);
372 case POW_HOMING_AMMO_1:
373 case POW_HOMING_AMMO_4:
374 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[HOMING_INDEX]]);
376 case POW_SMARTBOMB_WEAPON:
377 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[SMART_INDEX]]);
379 case POW_MEGA_WEAPON:
380 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[MEGA_INDEX]]);
384 else if(Objects[i].render_type==RT_POLYOBJ){
385 ogl_cache_polymodel_textures(Objects[i].rtype.pobj_info.model_num);
389 glmprintf((0,"finished caching\n"));
392 int r_polyc,r_tpolyc,r_bitmapc,r_ubitmapc,r_ubitbltc,r_upixelc;
394 #define f2glf(x) (f2fl(x))
396 bool g3_draw_line(g3s_point *p0,g3s_point *p1)
399 c=grd_curcanv->cv_color;
400 OGL_DISABLE(TEXTURE_2D);
401 glColor3f(PAL2Tr(c),PAL2Tg(c),PAL2Tb(c));
403 glVertex3f(f2glf(p0->p3_vec.x),f2glf(p0->p3_vec.y),-f2glf(p0->p3_vec.z));
404 glVertex3f(f2glf(p1->p3_vec.x),f2glf(p1->p3_vec.y),-f2glf(p1->p3_vec.z));
408 void ogl_drawcircle2(int nsides,int type,float xsc,float xo,float ysc,float yo){
412 for (i=0; i<nsides; i++) {
413 ang = 2.0*M_PI*i/nsides;
414 glVertex2f(cosf(ang)*xsc+xo,sinf(ang)*ysc+yo);
418 void ogl_drawcircle(int nsides,int type){
422 for (i=0; i<nsides; i++) {
423 ang = 2.0*M_PI*i/nsides;
424 glVertex2f(cosf(ang),sinf(ang));
428 int circle_list_init(int nsides,int type,int mode) {
429 int hand=glGenLists(1);
430 glNewList(hand, mode);
431 /* draw a unit radius circle in xy plane centered on origin */
432 ogl_drawcircle(nsides,type);
436 float bright_g[4]={ 32.0/256, 252.0/256, 32.0/256};
437 float dark_g[4]={ 32.0/256, 148.0/256, 32.0/256};
438 float darker_g[4]={ 32.0/256, 128.0/256, 32.0/256};
439 void ogl_draw_reticle(int cross,int primary,int secondary){
440 float scale=(float)Canvas_height/(float)grd_curscreen->sc_h;
442 // glTranslatef(0.5,0.5,0);
443 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);
444 glScalef(scale/320.0,scale/200.0,scale);//the positions are based upon the standard reticle at 320x200 res.
446 OGL_DISABLE(TEXTURE_2D);
448 if (!cross_lh[cross]){
449 cross_lh[cross]=glGenLists(1);
450 glNewList(cross_lh[cross], GL_COMPILE_AND_EXECUTE);
453 glColor3fv(darker_g);
454 glVertex2f(-4.0,4.0);
456 glColor3fv(bright_g);
459 glVertex2f(-2.0,2.0);
463 glVertex2f(-3.0,-2.0);
465 glColor3fv(bright_g);
466 glVertex2f(-2.0,-1.0);
469 glColor3fv(darker_g);
472 glColor3fv(bright_g);
479 glVertex2f(3.0,-2.0);
481 glColor3fv(bright_g);
482 glVertex2f(2.0,-1.0);
487 glCallList(cross_lh[cross]);
489 // if (Canvas_height>200)
490 // glLineWidth(Canvas_height/(float)200);
491 if (!primary_lh[primary]){
492 primary_lh[primary]=glGenLists(1);
493 glNewList(primary_lh[primary], GL_COMPILE_AND_EXECUTE);
498 glVertex2f(-14.0,-8.0);
499 glVertex2f(-8.0,-5.0);
501 glVertex2f(14.0,-8.0);
502 glVertex2f(8.0,-5.0);
507 glColor3fv(bright_g);
509 ogl_drawcircle2(6,GL_POLYGON,1.5,-7.0,1.5,-5.0);
511 ogl_drawcircle2(6,GL_POLYGON,1.5,7.0,1.5,-5.0);
515 glColor3fv(bright_g);
517 ogl_drawcircle2(4,GL_POLYGON,1.0,-14.0,1.0,-8.0);
519 ogl_drawcircle2(4,GL_POLYGON,1.0,14.0,1.0,-8.0);
523 glCallList(primary_lh[primary]);
524 // if (Canvas_height>200)
527 if (!secondary_lh[secondary]){
528 secondary_lh[secondary]=glGenLists(1);
529 glNewList(secondary_lh[secondary], GL_COMPILE_AND_EXECUTE);
533 glColor3fv(darker_g);
535 glColor3fv(bright_g);
536 ogl_drawcircle2(8,GL_LINE_LOOP,2.0,-10.0,2.0,-1.0);
539 glColor3fv(darker_g);
541 glColor3fv(bright_g);
542 ogl_drawcircle2(8,GL_LINE_LOOP,2.0,10.0,2.0,-1.0);
544 //bottom/middle secondary
546 glColor3fv(darker_g);
548 glColor3fv(bright_g);
549 ogl_drawcircle2(8,GL_LINE_LOOP,2.0,0.0,2.0,-7.0);
553 glCallList(secondary_lh[secondary]);
557 int g3_draw_sphere(g3s_point *pnt,fix rad){
559 c=grd_curcanv->cv_color;
560 OGL_DISABLE(TEXTURE_2D);
561 // glPointSize(f2glf(rad));
562 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
563 // glBegin(GL_POINTS);
564 // glVertex3f(f2glf(pnt->p3_vec.x),f2glf(pnt->p3_vec.y),-f2glf(pnt->p3_vec.z));
567 glTranslatef(f2glf(pnt->p3_vec.x),f2glf(pnt->p3_vec.y),-f2glf(pnt->p3_vec.z));
568 glScalef(f2glf(rad),f2glf(rad),f2glf(rad));
569 if (!sphereh) sphereh=circle_list_init(20,GL_POLYGON,GL_COMPILE_AND_EXECUTE);
570 else glCallList(sphereh);
575 int gr_ucircle(fix xc1,fix yc1,fix r1){//dunno if this really works, radar doesn't seem to.. hm..
577 c=grd_curcanv->cv_color;
578 OGL_DISABLE(TEXTURE_2D);
579 // glPointSize(f2glf(rad));
580 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
581 // glBegin(GL_POINTS);
582 // glVertex3f(f2glf(pnt->p3_vec.x),f2glf(pnt->p3_vec.y),-f2glf(pnt->p3_vec.z));
585 glmprintf((0,"circle: %f(%i),%f(%i),%f\n",(f2fl(xc1)+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,f2i(xc1),(f2fl(yc1)+grd_curcanv->cv_bitmap.bm_y)/(float)last_height,f2i(yc1),f2fl(r1)));
587 (f2fl(xc1)+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,
588 1.0-(f2fl(yc1)+grd_curcanv->cv_bitmap.bm_y)/(float)last_height,0);
589 glScalef(f2fl(r1),f2fl(r1),f2fl(r1));
591 if (!circleh5) circleh5=circle_list_init(5,GL_LINE_LOOP,GL_COMPILE_AND_EXECUTE);
592 else glCallList(circleh5);
594 if (!circleh10) circleh10=circle_list_init(10,GL_LINE_LOOP,GL_COMPILE_AND_EXECUTE);
595 else glCallList(circleh10);
600 int gr_circle(fix xc1,fix yc1,fix r1){
601 return gr_ucircle(xc1,yc1,r1);
604 bool g3_draw_poly(int nv,g3s_point **pointlist)
608 c=grd_curcanv->cv_color;
609 // 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);
610 OGL_DISABLE(TEXTURE_2D);
611 glBegin(GL_TRIANGLE_FAN);
612 glColor3f(PAL2Tr(c),PAL2Tg(c),PAL2Tb(c));
614 // glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
615 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
621 void gr_upoly_tmap(int nverts, int *vert ){
622 mprintf((0,"gr_upoly_tmap: unhandled\n"));//should never get called
624 void draw_tmap_flat(grs_bitmap *bm,int nv,g3s_point **vertlist){
625 mprintf((0,"draw_tmap_flat: unhandled\n"));//should never get called
627 extern void (*tmap_drawer_ptr)(grs_bitmap *bm,int nv,g3s_point **vertlist);
628 bool g3_draw_tmap(int nv,g3s_point **pointlist,g3s_uvl *uvl_list,grs_bitmap *bm)
632 if (tmap_drawer_ptr==draw_tmap_flat){
633 /* fix average_light=0;
636 average_light += uvl_list[i].l;*/
637 OGL_DISABLE(TEXTURE_2D);
638 // glmprintf((0,"Gr_scanline_darkening_level=%i %f\n",Gr_scanline_darkening_level,Gr_scanline_darkening_level/(float)NUM_LIGHTING_LEVELS));
639 glColor4f(0,0,0,1.0-(Gr_scanline_darkening_level/(float)NUM_LIGHTING_LEVELS));
640 //glColor4f(0,0,0,f2fl(average_light/nv));
641 glBegin(GL_TRIANGLE_FAN);
643 // glColor4f(0,0,0,f2fl(uvl_list[c].l));
644 // glTexCoord2f(f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
645 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
648 }else if (tmap_drawer_ptr==draw_tmap){
650 /* if (bm->bm_w !=64||bm->bm_h!=64)
651 printf("g3_draw_tmap w %i h %i\n",bm->bm_w,bm->bm_h);*/
652 OGL_ENABLE(TEXTURE_2D);
654 ogl_texwrap(bm->gltexture,GL_REPEAT);
655 glBegin(GL_TRIANGLE_FAN);
657 if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
660 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
661 l=f2fl(uvl_list[c].l);
664 glTexCoord2f(f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
665 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
666 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
670 mprintf((0,"g3_draw_tmap: unhandled tmap_drawer %p\n",tmap_drawer_ptr));
674 bool g3_draw_tmap_2(int nv,g3s_point **pointlist,g3s_uvl *uvl_list,grs_bitmap *bmbot,grs_bitmap *bm,int orient)
676 #if (defined(GL_ARB_multitexture) || defined(GL_SGIS_multitexture))
677 if (ogl_arb_multitexture_ok || ogl_sgis_multitexture_ok){
682 /* if (bm->bm_w !=64||bm->bm_h!=64)
683 printf("g3_draw_tmap w %i h %i\n",bm->bm_w,bm->bm_h);*/
684 if (ogl_arb_multitexture_ok){
685 #ifdef GL_ARB_multitexture
686 glActiveTextureARB(GL_TEXTURE0_ARB);
688 }else if (ogl_sgis_multitexture_ok){
689 #ifdef GL_SGIS_multitexture
690 glSelectTextureSGIS(GL_TEXTURE0_SGIS);
693 ogl_bindbmtex(bmbot);
694 OGL_ENABLE(TEXTURE_2D);
695 glEnable(GL_TEXTURE_2D);
696 ogl_texwrap(bmbot->gltexture,GL_REPEAT);
697 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
699 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
701 if (ogl_arb_multitexture_ok){
702 #ifdef GL_ARB_multitexture
703 glActiveTextureARB(GL_TEXTURE1_ARB);
705 }else if (ogl_sgis_multitexture_ok){
706 #ifdef GL_SGIS_multitexture
707 glSelectTextureSGIS(GL_TEXTURE1_SGIS);
711 // OGL_ENABLE(TEXTURE_2D);
712 glEnable(GL_TEXTURE_2D);
713 ogl_texwrap(bm->gltexture,GL_REPEAT);
714 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
717 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
719 glBegin(GL_TRIANGLE_FAN);
723 u1=1.0-f2glf(uvl_list[c].v);
724 v1=f2glf(uvl_list[c].u);
727 u1=1.0-f2glf(uvl_list[c].u);
728 v1=1.0-f2glf(uvl_list[c].v);
731 u1=f2glf(uvl_list[c].v);
732 v1=1.0-f2glf(uvl_list[c].u);
735 u1=f2glf(uvl_list[c].u);
736 v1=f2glf(uvl_list[c].v);
739 if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
742 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
743 l=f2fl(uvl_list[c].l);
746 // glTexCoord2f(f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
747 if (ogl_arb_multitexture_ok){
748 #ifdef GL_ARB_multitexture
749 glMultiTexCoord2fARB(GL_TEXTURE0_ARB,f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
751 }else if (ogl_sgis_multitexture_ok){
752 #ifdef GL_SGIS_multitexture
753 glMultiTexCoord2fSGIS(GL_TEXTURE0_SGIS,f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
756 if (ogl_arb_multitexture_ok){
757 #ifdef GL_ARB_multitexture
758 glMultiTexCoord2fARB(GL_TEXTURE1_ARB,u1,v1);
760 }else if (ogl_sgis_multitexture_ok){
761 #ifdef GL_SGIS_multitexture
762 glMultiTexCoord2fSGIS(GL_TEXTURE1_SGIS,u1,v1);
765 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
766 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
767 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
770 /* if (ogl_arb_multitexture_ok){
771 #ifdef GL_ARB_multitexture
772 glActiveTextureARB(GL_TEXTURE1_ARB);
774 }else if (ogl_sgis_multitexture_ok){
775 #ifdef GL_SGIS_multitexture
776 glSelectTextureSGIS(GL_TEXTURE1_SGIS);
779 // OGL_ENABLE(TEXTURE_2D);*/
780 glDisable(GL_TEXTURE_2D);
781 if (ogl_arb_multitexture_ok){
782 #ifdef GL_ARB_multitexture
783 glActiveTextureARB(GL_TEXTURE0_ARB);
785 }else if (ogl_sgis_multitexture_ok){
786 #ifdef GL_SGIS_multitexture
787 glSelectTextureSGIS(GL_TEXTURE0_SGIS);
796 g3_draw_tmap(nv,pointlist,uvl_list,bmbot);//draw the bottom texture first.. could be optimized with multitexturing..
799 /* if (bm->bm_w !=64||bm->bm_h!=64)
800 printf("g3_draw_tmap w %i h %i\n",bm->bm_w,bm->bm_h);*/
801 OGL_ENABLE(TEXTURE_2D);
803 ogl_texwrap(bm->gltexture,GL_REPEAT);
804 glBegin(GL_TRIANGLE_FAN);
808 u1=1.0-f2glf(uvl_list[c].v);
809 v1=f2glf(uvl_list[c].u);
812 u1=1.0-f2glf(uvl_list[c].u);
813 v1=1.0-f2glf(uvl_list[c].v);
816 u1=f2glf(uvl_list[c].v);
817 v1=1.0-f2glf(uvl_list[c].u);
820 u1=f2glf(uvl_list[c].u);
821 v1=f2glf(uvl_list[c].v);
824 if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
827 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
828 l=f2fl(uvl_list[c].l);
832 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
833 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
834 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
841 bool g3_draw_bitmap(vms_vector *pos,fix width,fix height,grs_bitmap *bm, int orientation)
844 vms_vector pv,v1;//,v2;
848 // printf("g3_draw_bitmap: %f,%f,%f - ",f2glf(pos->x),f2glf(pos->y),-f2glf(pos->z));
849 // printf("(%f,%f,%f) ",f2glf(View_position.x),f2glf(View_position.y),-f2glf(View_position.z));
851 OGL_ENABLE(TEXTURE_2D);
853 ogl_texwrap(bm->gltexture,GL_CLAMP);
856 glColor3f(1.0,1.0,1.0);
857 width = fixmul(width,Matrix_scale.x);
858 height = fixmul(height,Matrix_scale.y);
860 // g3_rotate_point(&p[i],pos);
861 vm_vec_sub(&v1,pos,&View_position);
862 vm_vec_rotate(&pv,&v1,&View_matrix);
863 // printf(" %f,%f,%f->",f2glf(pv.x),f2glf(pv.y),-f2glf(pv.z));
866 glTexCoord2f(0.0, 0.0);
871 glTexCoord2f(bm->gltexture->u, 0.0);
876 glTexCoord2f(bm->gltexture->u, bm->gltexture->v);
881 glTexCoord2f(0.0, bm->gltexture->v);
886 // vm_vec_rotate(&v2,&v1,&View_matrix);
887 // vm_vec_sub(&v1,&v2,&pv);
888 //vm_vec_sub(&v1,&pv,&v2);
889 // vm_vec_sub(&v2,&pv,&v1);
890 glVertex3f(f2glf(pv.x),f2glf(pv.y),-f2glf(pv.z));
891 // printf("%f,%f,%f ",f2glf(v1.x),f2glf(v1.y),-f2glf(v1.z));
899 bool ogl_ubitmapm_c(int x, int y,grs_bitmap *bm,int c)
904 x+=grd_curcanv->cv_bitmap.bm_x;
905 y+=grd_curcanv->cv_bitmap.bm_y;
906 xo=x/(float)last_width;
907 xf=(bm->bm_w+x)/(float)last_width;
908 yo=1.0-y/(float)last_height;
909 yf=1.0-(bm->bm_h+y)/(float)last_height;
911 // printf("g3_draw_bitmap: %f,%f,%f - ",f2glf(pos->x),f2glf(pos->y),-f2glf(pos->z));
912 // printf("(%f,%f,%f) ",f2glf(View_position.x),f2glf(View_position.y),-f2glf(View_position.z));
914 /* glEnABLE(ALPHA_TEST);
915 glAlphaFunc(GL_GREATER,0.0);*/
917 OGL_ENABLE(TEXTURE_2D);
919 ogl_texwrap(bm->gltexture,GL_CLAMP);
923 if (bm->bm_w==bm->gltexture->w)
926 u2=(bm->bm_w+bm->bm_x)/(float)bm->gltexture->tw;
928 u1=bm->bm_x/(float)bm->gltexture->tw;
929 u2=(bm->bm_w+bm->bm_x)/(float)bm->gltexture->tw;
933 if (bm->bm_h==bm->gltexture->h)
936 v2=(bm->bm_h+bm->bm_y)/(float)bm->gltexture->th;
938 v1=bm->bm_y/(float)bm->gltexture->th;
939 v2=(bm->bm_h+bm->bm_y)/(float)bm->gltexture->th;
944 glColor3f(1.0,1.0,1.0);
946 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
947 glTexCoord2f(u1, v1); glVertex2f(xo, yo);
948 glTexCoord2f(u2, v1); glVertex2f(xf, yo);
949 glTexCoord2f(u2, v2); glVertex2f(xf, yf);
950 glTexCoord2f(u1, v2); glVertex2f(xo, yf);
952 // glDisABLE(ALPHA_TEST);
956 bool ogl_ubitmapm(int x, int y,grs_bitmap *bm){
957 return ogl_ubitmapm_c(x,y,bm,-1);
958 // return ogl_ubitblt(bm->bm_w,bm->bm_h,x,y,0,0,bm,NULL);
961 //also upsidedown, currently.
962 bool ogl_ubitblt(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest)
964 GLfloat xo,yo;//,xs,ys;
965 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));
970 xo=dx/(float)last_width;
971 // xo=dx/(float)grd_curscreen->sc_w;
972 // xs=w/(float)last_width;
973 //yo=1.0-dy/(float)last_height;
974 yo=1.0-(dy+h)/(float)last_height;
975 // ys=h/(float)last_height;
977 // OGL_ENABLE(TEXTURE_2D);
979 OGL_DISABLE(TEXTURE_2D);
980 glRasterPos2f(xo,yo);
981 ogl_filltexbuf(src->bm_data,texbuf,src->bm_w,w,h,sx,sy,w,h,GL_RGBA);
982 glDrawPixels(w,h,GL_RGBA,GL_UNSIGNED_BYTE,texbuf);
988 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)
991 GLfloat u1,v1;//,u2,v2;
993 // unsigned char *oldpal;
996 ogl_init_texture(&tex);
998 tex.prio=0.0;tex.wantmip=0;
999 tex.lw=src->bm_rowsize;
1001 /* if (w==src->bm_w && sx==0){
1004 u1=sx/(float)src->bm_w*src->glu;
1005 u2=w/(float)src->bm_w*src->glu+u1;
1007 if (h==src->bm_h && sy==0){
1010 v1=sy/(float)src->bm_h*src->glv;
1011 v2=h/(float)src->bm_h*src->glv+v1;
1017 xo=dx/(float)last_width;
1018 xs=dw/(float)last_width;
1019 yo=1.0-dy/(float)last_height;
1020 ys=dh/(float)last_height;
1022 OGL_ENABLE(TEXTURE_2D);
1025 ogl_pal=gr_current_pal;
1026 ogl_loadtexture(src->bm_data,sx,sy,&tex);
1029 OGL_BINDTEXTURE(tex.handle);
1031 ogl_texwrap(&tex,GL_CLAMP);
1034 glColor3f(1.0,1.0,1.0);
1035 glTexCoord2f(u1, v1); glVertex2f(xo, yo);
1036 glTexCoord2f(tex.u, v1); glVertex2f(xo+xs, yo);
1037 glTexCoord2f(tex.u, tex.v); glVertex2f(xo+xs, yo-ys);
1038 glTexCoord2f(u1, tex.v); glVertex2f(xo, yo-ys);
1040 ogl_freetexture(&tex);
1043 bool ogl_ubitblt(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1044 return ogl_ubitblt_i(w,h,dx,dy,w,h,sx,sy,src,dest);
1047 bool ogl_ubitblt_tolinear(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1049 unsigned char *d,*s;
1053 w1=grd_curscreen->sc_w;h1=grd_curscreen->sc_h;
1054 if (w1*h1*3>OGLTEXBUFSIZE)
1055 Error("ogl_ubitblt_tolinear: screen res larger than OGLTEXBUFSIZE\n");
1057 if (ogl_readpixels_ok>0){
1058 OGL_DISABLE(TEXTURE_2D);
1059 glReadBuffer(GL_FRONT);
1060 glReadPixels(0,0,w1,h1,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1061 // glReadPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1062 // glReadPixels(sx,sy,w+sx,h+sy,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1064 memset(texbuf,0,w1*h1*3);
1068 d=dest->bm_data+dx+(dy+i)*dest->bm_rowsize;
1069 s=texbuf+((h1-(i+sy+1))*w1+sx)*3;
1071 *d=gr_find_closest_color(s[0]/4,s[1]/4,s[2]/4);
1078 unsigned char *d,*s,*e;
1079 if (w*h*3>OGLTEXBUFSIZE)
1080 Error("ogl_ubitblt_tolinear: size larger than OGLTEXBUFSIZE\n");
1083 #if 1//also seems to cause a mess. need to look into it a bit more..
1084 if (ogl_readpixels_ok>0){
1085 OGL_DISABLE(TEXTURE_2D);
1086 glReadBuffer(GL_FRONT);
1087 // glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1088 glReadPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1091 memset(texbuf,0,w*h*3);
1092 // d=dest->bm_data+dx+(dy+i)*dest->bm_rowsize;
1093 d=dest->bm_data+dx+dy*dest->bm_rowsize;
1095 s=texbuf+w*(h-(i+1))*3;
1097 if (s<texbuf){Error("blah1\n");}
1098 if (d<dest->bm_data){Error("blah3\n");}
1099 // d=dest->bm_data+(i*dest->bm_rowsize);
1103 if (s>texbuf+w*h*3-3){Error("blah2\n");}
1104 if (d>dest->bm_data+dest->bm_rowsize*(h+dy)+dx ){Error("blah4\n");}
1105 *d=gr_find_closest_color(s[0]/4,s[1]/4,s[2]/4);
1111 d+=dest->bm_rowsize;
1113 glmprintf((0,"c=%i w*h=%i\n",c,w*h));
1118 bool ogl_ubitblt_copy(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1119 #if 0 //just seems to cause a mess.
1120 GLfloat xo,yo;//,xs,ys;
1125 // xo=dx/(float)last_width;
1126 xo=dx/(float)grd_curscreen->sc_w;
1127 // yo=1.0-(dy+h)/(float)last_height;
1128 yo=1.0-(dy+h)/(float)grd_curscreen->sc_h;
1131 OGL_DISABLE(TEXTURE_2D);
1132 glReadBuffer(GL_FRONT);
1133 glRasterPos2f(xo,yo);
1134 // glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1135 glCopyPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_COLOR);
1141 void ogl_start_frame(void){
1142 r_polyc=0;r_tpolyc=0;r_bitmapc=0;r_ubitmapc=0;r_ubitbltc=0;r_upixelc=0;
1143 // gl_badtexture=500;
1145 OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,Canvas_width,Canvas_height);
1146 glClearColor(0.0, 0.0, 0.0, 0.0);
1147 // glEnable(GL_ALPHA_TEST);
1148 // glAlphaFunc(GL_GREATER,0.01);
1149 glShadeModel(GL_SMOOTH);
1150 glMatrixMode(GL_PROJECTION);
1151 glLoadIdentity();//clear matrix
1152 //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),1.0,1000000.0);
1153 //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),0.01,1000000.0);
1154 gluPerspective(90.0,1.0,0.01,1000000.0);
1155 //gluPerspective(90.0,(GLfloat)(Canvas_width*3)/(GLfloat)(Canvas_height*4),0.01,1000000.0);
1156 // gluPerspective(75.0,(GLfloat)Canvas_width/(GLfloat)Canvas_height,1.0,1000000.0);
1157 glMatrixMode(GL_MODELVIEW);
1158 glLoadIdentity();//clear matrix
1160 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1161 // glDisABLE(DITHER);
1162 // glScalef(1.0,1.0,-1.0);
1163 // glScalef(1.0,1.0,-1.0);
1166 // glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1167 // OGL_TEXENV(GL_TEXTURE_ENV_MODE,GL_MODULATE);
1168 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_texmagfilt);
1169 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_texminfilt);
1170 // OGL_TEXPARAM(GL_TEXTURE_MAG_FILTER,GL_texmagfilt);
1171 // OGL_TEXPARAM(GL_TEXTURE_MIN_FILTER,GL_texminfilt);
1172 // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1173 // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1176 void merge_textures_stats(void);
1178 void ogl_end_frame(void){
1179 // OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,);
1180 OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
1182 // merge_textures_stats();
1183 // ogl_texture_stats();
1185 // glViewport(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
1186 glMatrixMode(GL_PROJECTION);
1187 glLoadIdentity();//clear matrix
1188 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
1189 glMatrixMode(GL_MODELVIEW);
1190 glLoadIdentity();//clear matrix
1191 // glDisABLE(BLEND);
1192 //glDisABLE(ALPHA_TEST);
1193 //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),1.0,1000000.0);
1194 // ogl_swap_buffers();//platform specific code
1195 // glClear(GL_COLOR_BUFFER_BIT);
1198 void ogl_swap_buffers(void){
1199 ogl_clean_texture_cache();
1200 if (gr_renderstats){
1201 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);
1202 // 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
1205 ogl_swap_buffers_internal();
1206 glClear(GL_COLOR_BUFFER_BIT);
1209 int tex_format_supported(int iformat,int format){
1212 if (!ogl_intensity4_ok) return 0; break;
1213 case GL_LUMINANCE4_ALPHA4:
1214 if (!ogl_luminance4_alpha4_ok) return 0; break;
1216 if (!ogl_rgba2_ok) return 0; break;
1218 if (ogl_gettexlevelparam_ok){
1219 GLint internalFormat;
1220 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, iformat, 64, 64, 0,
1221 format, GL_UNSIGNED_BYTE, texbuf);//NULL?
1222 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0,
1223 GL_TEXTURE_INTERNAL_FORMAT,
1225 return (internalFormat==iformat);
1230 //little hack to find the largest or equal multiple of 2 for a given number
1233 for (i=2;i<=4096;i*=2)
1238 //GLubyte texbuf[512*512*4];
1239 GLubyte texbuf[OGLTEXBUFSIZE];
1240 void ogl_filltexbuf(unsigned char *data,GLubyte *texp,int truewidth,int width,int height,int dxo,int dyo,int twidth,int theight,int type)
1242 // GLushort *tex=(GLushort *)texp;
1244 if (twidth*theight*4>sizeof(texbuf))//shouldn't happen, descent never uses textures that big.
1245 Error("texture toobig %i %i",twidth,theight);
1248 for (y=0;y<theight;y++){
1249 i=dxo+truewidth*(y+dyo);
1250 for (x=0;x<twidth;x++){
1251 if (x<width && y<height)
1254 c=255;//fill the pad space with transparancy
1260 case GL_LUMINANCE_ALPHA:
1268 (*(texp++))=0;//transparent pixel
1274 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. :)
1277 case GL_LUMINANCE_ALPHA:
1282 //(*(texp++))=gr_palette[c*3]*4;
1283 //(*(texp++))=gr_palette[c*3+1]*4;
1284 //(*(texp++))=gr_palette[c*3+2]*4;
1285 (*(texp++))=ogl_pal[c*3]*4;
1286 (*(texp++))=ogl_pal[c*3+1]*4;
1287 (*(texp++))=ogl_pal[c*3+2]*4;
1288 (*(texp++))=255;//not transparent
1289 // (*(tex++))=(ogl_pal[c*3]>>1) + ((ogl_pal[c*3+1]>>1)<<5) + ((ogl_pal[c*3+2]>>1)<<10) + (1<<15);
1296 int tex_format_verify(ogl_texture *tex){
1297 while (!tex_format_supported(tex->internalformat,tex->format)){
1298 glmprintf((0,"tex format %x not supported",tex->internalformat));
1299 switch (tex->internalformat){
1301 if (ogl_luminance4_alpha4_ok){
1302 tex->internalformat=GL_LUMINANCE4_ALPHA4;
1303 tex->format=GL_LUMINANCE_ALPHA;
1305 }//note how it will fall through here if the statement is false
1306 case GL_LUMINANCE4_ALPHA4:
1308 tex->internalformat=GL_RGBA2;
1309 tex->format=GL_RGBA;
1311 }//note how it will fall through here if the statement is false
1313 tex->internalformat=ogl_rgba_format;
1314 tex->format=GL_RGBA;
1317 mprintf((0,"...no tex format to fall back on\n"));
1320 glmprintf((0,"...falling back to %x\n",tex->internalformat));
1324 void tex_set_size1(ogl_texture *tex,int dbits,int bits,int w, int h){
1326 if (tex->tw!=w || tex->th!=h){
1327 u=(tex->w/(float)tex->tw*w) * (tex->h/(float)tex->th*h);
1328 glmprintf((0,"shrunken texture?\n"));
1331 if (bits<=0){//the beta nvidia GLX server. doesn't ever return any bit sizes, so just use some assumptions.
1332 tex->bytes=((float)w*h*dbits)/8.0;
1333 tex->bytesu=((float)u*dbits)/8.0;
1335 tex->bytes=((float)w*h*bits)/8.0;
1336 tex->bytesu=((float)u*bits)/8.0;
1338 glmprintf((0,"tex_set_size1: %ix%i, %ib(%i) %iB\n",w,h,bits,dbits,tex->bytes));
1340 void tex_set_size(ogl_texture *tex){
1343 if (ogl_gettexlevelparam_ok){
1345 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&w);
1346 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&h);
1347 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_LUMINANCE_SIZE,&t);a+=t;
1348 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_INTENSITY_SIZE,&t);a+=t;
1349 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_RED_SIZE,&t);a+=t;
1350 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_GREEN_SIZE,&t);a+=t;
1351 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_BLUE_SIZE,&t);a+=t;
1352 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_ALPHA_SIZE,&t);a+=t;
1357 switch (tex->format){
1361 case GL_LUMINANCE_ALPHA:
1368 Error("tex_set_size unknown texformat\n");
1371 tex_set_size1(tex,bi,a,w,h);
1373 //loads a palettized bitmap into a ogl RGBA texture.
1374 //Sizes and pads dimensions to multiples of 2 if necessary.
1375 //In theory this could be a problem for repeating textures, but all real
1376 //textures (not sprites, etc) in descent are 64x64, so we are ok.
1377 //stores OpenGL textured id in *texid and u/v values required to get only the real data in *u/*v
1378 void ogl_loadtexture(unsigned char * data, int dxo,int dyo, ogl_texture *tex){
1379 //void ogl_loadtexture(unsigned char * data, int width, int height,int dxo,int dyo, int *texid,float *u,float *v,char domipmap,float prio){
1380 // int internalformat=GL_RGBA;
1381 // int format=GL_RGBA;
1383 tex->tw=pow2ize(tex->w);tex->th=pow2ize(tex->h);//calculate smallest texture size that can accomodate us (must be multiples of 2)
1384 // tex->tw=tex->w;tex->th=tex->h;//feeling lucky?
1386 if(gr_badtexture>0) return;
1388 if (tex_format_verify(tex))
1391 //calculate u/v values that would make the resulting texture correctly sized
1392 tex->u=(float)tex->w/(float)tex->tw;
1393 tex->v=(float)tex->h/(float)tex->th;
1395 // if (width!=twidth || height!=theight)
1396 // glmprintf((0,"sizing %ix%i texture up to %ix%i\n",width,height,twidth,theight));
1397 ogl_filltexbuf(data,texbuf,tex->lw,tex->w,tex->h,dxo,dyo,tex->tw,tex->th,tex->format);
1399 // Generate OpenGL texture IDs.
1400 glGenTextures(1, &tex->handle);
1403 glPrioritizeTextures(1,&tex->handle,&tex->prio);
1405 // Give our data to OpenGL.
1407 OGL_BINDTEXTURE(tex->handle);
1409 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1411 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_texmagfilt);
1412 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_texminfilt);
1414 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1415 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1417 // domipmap=0;//mipmaps aren't used in GL_NEAREST anyway, and making the mipmaps is pretty slow
1418 //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.
1420 if (tex->wantmip && GL_needmipmaps)
1421 gluBuild2DMipmaps( GL_TEXTURE_2D, tex->internalformat, tex->tw,
1422 tex->th, tex->format, GL_UNSIGNED_BYTE, texbuf);
1424 glTexImage2D(GL_TEXTURE_2D, 0, tex->internalformat,
1425 tex->tw, tex->th, 0, tex->format, // RGBA textures.
1426 GL_UNSIGNED_BYTE, // imageData is a GLubyte pointer.
1432 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));
1435 unsigned char decodebuf[512*512];
1436 void ogl_loadbmtexture_m(grs_bitmap *bm,int domipmap){
1438 while (bm->bm_parent)
1441 if (bm->gltexture==NULL){
1442 ogl_init_texture(bm->gltexture=ogl_get_free_texture());
1443 bm->gltexture->lw=bm->bm_w;
1444 bm->gltexture->w=bm->bm_w;
1445 bm->gltexture->h=bm->bm_h;
1446 bm->gltexture->wantmip=domipmap;
1449 if (bm->gltexture->handle>0)
1451 if (bm->gltexture->w==0){
1452 bm->gltexture->lw=bm->bm_w;
1453 bm->gltexture->w=bm->bm_w;
1454 bm->gltexture->h=bm->bm_h;
1457 if (bm->bm_flags & BM_FLAG_RLE){
1458 unsigned char * dbits;
1459 unsigned char * sbits;
1461 sbits = &bm->bm_data[4 + bm->bm_h];
1464 for (i=0; i < bm->bm_h; i++ ) {
1465 gr_rle_decode(sbits,dbits);
1466 sbits += (int)bm->bm_data[4+i];
1471 ogl_loadtexture(buf,0,0,bm->gltexture);
1473 void ogl_loadbmtexture(grs_bitmap *bm){
1474 ogl_loadbmtexture_m(bm,1);
1476 void ogl_freetexture(ogl_texture *gltexture){
1477 if (gltexture->handle>0){
1479 glmprintf((0,"ogl_freetexture(%p):%i (last rend %is) (%i left)\n",gltexture,gltexture->handle,(GameTime-gltexture->lastrend)/f1_0,r_texcount));
1480 glDeleteTextures( 1, &gltexture->handle );
1481 // gltexture->handle=0;
1482 ogl_init_texture(gltexture);
1485 void ogl_freebmtexture(grs_bitmap *bm){
1487 ogl_freetexture(bm->gltexture);
1490 // glmprintf((0,"ogl_freebmtexture(%p,%p):%i (%i left)\n",bm->bm_data,&bm->gltexture,bm->gltexture,r_texcount));
1491 // glDeleteTextures( 1, &bm->gltexture );
1492 // bm->gltexture=-1;