1 /* $Id: ogl.c,v 1.15 2004-05-16 00:45:25 schaffner Exp $ */
4 * Graphics support functions for OpenGL.
18 #if defined(__APPLE__) && defined(__MACH__)
19 #include <OpenGL/gl.h>
20 #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;
70 int last_width=-1,last_height=-1;
71 int GL_TEXTURE_2D_enabled=-1;
72 int GL_texclamp_enabled=-1;
74 extern int gr_renderstats;
75 extern int gr_badtexture;
76 int ogl_alttexmerge=1;//merge textures by just printing the seperate textures?
77 int ogl_rgba_format=4;
78 int ogl_intensity4_ok=1;
79 int ogl_luminance4_alpha4_ok=1;
81 int ogl_readpixels_ok=1;
82 int ogl_gettexlevelparam_ok=1;
83 #ifdef GL_ARB_multitexture
84 int ogl_arb_multitexture_ok=0;
86 #ifdef GL_SGIS_multitexture
87 int ogl_sgis_multitexture_ok=0;
93 int cross_lh[2]={0,0};
94 int primary_lh[3]={0,0,0};
95 int secondary_lh[5]={0,0,0,0,0};
98 #define OGL_BINDTEXTURE(a) if(gr_badtexture>0) glBindTexture(GL_TEXTURE_2D, 0);\
99 else if(a!=lastbound) {glBindTexture(GL_TEXTURE_2D, a);lastbound=a;}*/
100 #define OGL_BINDTEXTURE(a) if(gr_badtexture>0) glBindTexture(GL_TEXTURE_2D, 0);\
101 else glBindTexture(GL_TEXTURE_2D, a);
104 ogl_texture ogl_texture_list[OGL_TEXTURE_LIST_SIZE];
105 int ogl_texture_list_cur;
107 /* some function prototypes */
109 //#define OGLTEXBUFSIZE (1024*1024*4)
110 #define OGLTEXBUFSIZE (2048*2048*4)
111 extern GLubyte texbuf[OGLTEXBUFSIZE];
112 //void ogl_filltexbuf(unsigned char *data,GLubyte *texp,int width,int height,int twidth,int theight);
113 void ogl_filltexbuf(unsigned char *data,GLubyte *texp,int truewidth,int width,int height,int dxo,int dyo,int twidth,int theight,int type);
114 void ogl_loadbmtexture(grs_bitmap *bm);
115 //void ogl_loadtexture(unsigned char * data, int width, int height,int dxo,intdyo , int *texid,float *u,float *v,char domipmap,float prio);
116 void ogl_loadtexture(unsigned char * data, int dxo,int dyo, ogl_texture *tex);
117 void ogl_freetexture(ogl_texture *gltexture);
118 void ogl_do_palfx(void);
120 void ogl_init_texture_stats(ogl_texture* t){
121 t->prio=0.3;//default prio
125 void ogl_init_texture(ogl_texture* t){
127 t->internalformat=ogl_rgba_format;
131 ogl_init_texture_stats(t);
133 void ogl_reset_texture_stats_internal(void){
135 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++)
136 if (ogl_texture_list[i].handle>0){
137 ogl_init_texture_stats(&ogl_texture_list[i]);
140 void ogl_init_texture_list_internal(void){
142 ogl_texture_list_cur=0;
143 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++)
144 ogl_init_texture(&ogl_texture_list[i]);
146 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=-1;
162 void ogl_vivify_texture_list_internal(void){
166 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
167 t=&ogl_texture_list[i];
168 if (t->w>0){//erk, realised this can't be done since we'd need the texture bm_data too. hmmm.
169 ogl_loadbmtexture(t);
174 ogl_texture* ogl_get_free_texture(void){
176 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
177 if (ogl_texture_list[ogl_texture_list_cur].handle<=0 && ogl_texture_list[ogl_texture_list_cur].w==0)
178 return &ogl_texture_list[ogl_texture_list_cur];
179 if (++ogl_texture_list_cur>=OGL_TEXTURE_LIST_SIZE)
180 ogl_texture_list_cur=0;
182 Error("OGL: texture list full!\n");
185 int ogl_texture_stats(void){
186 int used=0,usedl4a4=0,usedrgba=0,databytes=0,truebytes=0,datatexel=0,truetexel=0,i;
187 int prio0=0,prio1=0,prio2=0,prio3=0,prioh=0;
190 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
191 t=&ogl_texture_list[i];
194 datatexel+=t->w*t->h;
195 truetexel+=t->tw*t->th;
196 databytes+=t->bytesu;
198 if (t->prio<0.299)prio0++;
199 else if (t->prio<0.399)prio1++;
200 else if (t->prio<0.499)prio2++;
201 else if (t->prio<0.599)prio3++;
208 gr_printf(5,GAME_FONT->ft_h*14+3*14,"%i(%i,%i) %iK(%iK wasted)",used,usedrgba,usedl4a4,truebytes/1024,(truebytes-databytes)/1024);
210 // 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));
213 int ogl_mem_target=-1;
214 void ogl_clean_texture_cache(void){
219 if (ogl_mem_target<0){
225 bytes=ogl_texture_stats();
226 while (bytes>ogl_mem_target){
227 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
228 t=&ogl_texture_list[i];
230 if (t->lastrend+f1_0*time<GameTime){
233 if (bytes<ogl_mem_target)
239 Error("not enough mem?");
244 void ogl_bindbmtex(grs_bitmap *bm){
245 if (bm->gltexture==NULL || bm->gltexture->handle<=0)
246 ogl_loadbmtexture(bm);
247 OGL_BINDTEXTURE(bm->gltexture->handle);
248 bm->gltexture->lastrend=GameTime;
249 bm->gltexture->numrend++;
250 //// if (bm->gltexture->numrend==80 || bm->gltexture->numrend==4000 || bm->gltexture->numrend==80000){
251 // if (bm->gltexture->numrend==100){
252 // bm->gltexture->prio+=0.1;
253 //// glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_PRIORITY,bm->gltexture->prio);
254 // glPrioritizeTextures(1,&bm->gltexture->handle,&bm->gltexture->prio);
257 //gltexture MUST be bound first
258 void ogl_texwrap(ogl_texture *gltexture,int state){
259 if (gltexture->wrapstate!=state || gltexture->numrend<1){
260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, state);
261 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, state);
262 gltexture->wrapstate=state;
266 //crude texture precaching
267 //handles: powerups, walls, weapons, polymodels, etc.
268 //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.
269 //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
272 void ogl_cache_polymodel_textures(int model_num){
273 polymodel *po=&Polygon_models[model_num];
275 for (i=0;i<po->n_textures;i++) {
276 // texture_list_index[i] = ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]];
277 ogl_loadbmtexture(&GameBitmaps[ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]].index]);
280 void ogl_cache_vclip_textures(vclip *vc){
282 for (i=0;i<vc->num_frames;i++){
283 PIGGY_PAGE_IN(vc->frames[i]);
284 ogl_loadbmtexture(&GameBitmaps[vc->frames[i].index]);
287 #define ogl_cache_vclipn_textures(i) ogl_cache_vclip_textures(&Vclip[i])
288 void ogl_cache_weapon_textures(weapon_info *w){
289 ogl_cache_vclipn_textures(w->flash_vclip);
290 ogl_cache_vclipn_textures(w->robot_hit_vclip);
291 ogl_cache_vclipn_textures(w->wall_hit_vclip);
292 if (w->render_type==WEAPON_RENDER_VCLIP)
293 ogl_cache_vclipn_textures(w->weapon_vclip);
294 else if (w->render_type==WEAPON_RENDER_POLYMODEL)
295 ogl_cache_polymodel_textures(w->model_num);
297 void ogl_cache_level_textures(void){
305 ogl_reset_texture_stats_internal();//loading a new lev should reset textures
307 for (i=0,ec=Effects;i<Num_effects;i++,ec++) {
308 if ((Effects[i].changing_wall_texture == -1) && (Effects[i].changing_object_texture==-1) )
310 if (ec->vc.num_frames>max_efx)
311 max_efx=ec->vc.num_frames;
313 glmprintf((0,"max_efx:%i\n",max_efx));
314 for (ef=0;ef<max_efx;ef++){
315 for (i=0,ec=Effects;i<Num_effects;i++,ec++) {
316 if ((Effects[i].changing_wall_texture == -1) && (Effects[i].changing_object_texture==-1) )
318 // if (ec->vc.num_frames>max_efx)
319 // max_efx=ec->vc.num_frames;
322 do_special_effects();
324 for (seg=0;seg<Num_segments;seg++){
325 for (side=0;side<MAX_SIDES_PER_SEGMENT;side++){
326 sidep=&Segments[seg].sides[side];
327 tmap1=sidep->tmap_num;
328 tmap2=sidep->tmap_num2;
329 if (tmap1<0 || tmap1>=NumTextures){
330 glmprintf((0,"ogl_cache_level_textures %i %i %i %i\n",seg,side,tmap1,NumTextures));
334 PIGGY_PAGE_IN(Textures[tmap1]);
335 bm = &GameBitmaps[Textures[tmap1].index];
337 PIGGY_PAGE_IN(Textures[tmap2&0x3FFF]);
338 bm2 = &GameBitmaps[Textures[tmap2&0x3FFF].index];
339 if (ogl_alttexmerge==0 || (bm2->bm_flags & BM_FLAG_SUPER_TRANSPARENT))
340 bm = texmerge_get_cached_bitmap( tmap1, tmap2 );
342 ogl_loadbmtexture(bm2);
344 // 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));
346 ogl_loadbmtexture(bm);
349 glmprintf((0,"finished ef:%i\n",ef));
351 reset_special_effects();
352 init_special_effects();
355 //always have lasers and concs
356 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[LASER_INDEX]]);
357 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[CONCUSSION_INDEX]]);
358 for (i=0;i<Highest_object_index;i++){
359 if(Objects[i].render_type==RT_POWERUP){
360 ogl_cache_vclipn_textures(Objects[i].rtype.vclip_info.vclip_num);
361 switch (Objects[i].id){
363 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[LASER_INDEX]]);
367 case POW_VULCAN_WEAPON:
368 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[VULCAN_INDEX]]);
370 case POW_SPREADFIRE_WEAPON:
371 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[SPREADFIRE_INDEX]]);
373 case POW_PLASMA_WEAPON:
374 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[PLASMA_INDEX]]);
376 case POW_FUSION_WEAPON:
377 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[FUSION_INDEX]]);
379 /* case POW_MISSILE_1:
381 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[CONCUSSION_INDEX]]);
383 case POW_PROXIMITY_WEAPON:
384 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[PROXIMITY_INDEX]]);
386 case POW_HOMING_AMMO_1:
387 case POW_HOMING_AMMO_4:
388 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[HOMING_INDEX]]);
390 case POW_SMARTBOMB_WEAPON:
391 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[SMART_INDEX]]);
393 case POW_MEGA_WEAPON:
394 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[MEGA_INDEX]]);
398 else if(Objects[i].render_type==RT_POLYOBJ){
399 ogl_cache_polymodel_textures(Objects[i].rtype.pobj_info.model_num);
403 glmprintf((0,"finished caching\n"));
406 int r_polyc,r_tpolyc,r_bitmapc,r_ubitmapc,r_ubitbltc,r_upixelc;
408 #define f2glf(x) (f2fl(x))
410 bool g3_draw_line(g3s_point *p0,g3s_point *p1)
413 c=grd_curcanv->cv_color;
414 OGL_DISABLE(TEXTURE_2D);
415 glColor3f(PAL2Tr(c),PAL2Tg(c),PAL2Tb(c));
417 glVertex3f(f2glf(p0->p3_vec.x),f2glf(p0->p3_vec.y),-f2glf(p0->p3_vec.z));
418 glVertex3f(f2glf(p1->p3_vec.x),f2glf(p1->p3_vec.y),-f2glf(p1->p3_vec.z));
422 void ogl_drawcircle2(int nsides,int type,float xsc,float xo,float ysc,float yo){
426 for (i=0; i<nsides; i++) {
427 ang = 2.0*M_PI*i/nsides;
428 glVertex2f(cosf(ang)*xsc+xo,sinf(ang)*ysc+yo);
432 void ogl_drawcircle(int nsides,int type){
436 for (i=0; i<nsides; i++) {
437 ang = 2.0*M_PI*i/nsides;
438 glVertex2f(cosf(ang),sinf(ang));
442 int circle_list_init(int nsides,int type,int mode) {
443 int hand=glGenLists(1);
444 glNewList(hand, mode);
445 /* draw a unit radius circle in xy plane centered on origin */
446 ogl_drawcircle(nsides,type);
450 float bright_g[4]={ 32.0/256, 252.0/256, 32.0/256};
451 float dark_g[4]={ 32.0/256, 148.0/256, 32.0/256};
452 float darker_g[4]={ 32.0/256, 128.0/256, 32.0/256};
453 void ogl_draw_reticle(int cross,int primary,int secondary){
454 float scale=(float)Canvas_height/(float)grd_curscreen->sc_h;
456 // glTranslatef(0.5,0.5,0);
457 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);
458 glScalef(scale/320.0,scale/200.0,scale);//the positions are based upon the standard reticle at 320x200 res.
460 OGL_DISABLE(TEXTURE_2D);
462 if (!cross_lh[cross]){
463 cross_lh[cross]=glGenLists(1);
464 glNewList(cross_lh[cross], GL_COMPILE_AND_EXECUTE);
467 glColor3fv(darker_g);
468 glVertex2f(-4.0,4.0);
470 glColor3fv(bright_g);
473 glVertex2f(-2.0,2.0);
477 glVertex2f(-3.0,-2.0);
479 glColor3fv(bright_g);
480 glVertex2f(-2.0,-1.0);
483 glColor3fv(darker_g);
486 glColor3fv(bright_g);
493 glVertex2f(3.0,-2.0);
495 glColor3fv(bright_g);
496 glVertex2f(2.0,-1.0);
501 glCallList(cross_lh[cross]);
503 // if (Canvas_height>200)
504 // glLineWidth(Canvas_height/(float)200);
505 if (!primary_lh[primary]){
506 primary_lh[primary]=glGenLists(1);
507 glNewList(primary_lh[primary], GL_COMPILE_AND_EXECUTE);
512 glVertex2f(-14.0,-8.0);
513 glVertex2f(-8.0,-5.0);
515 glVertex2f(14.0,-8.0);
516 glVertex2f(8.0,-5.0);
521 glColor3fv(bright_g);
523 ogl_drawcircle2(6,GL_POLYGON,1.5,-7.0,1.5,-5.0);
525 ogl_drawcircle2(6,GL_POLYGON,1.5,7.0,1.5,-5.0);
529 glColor3fv(bright_g);
531 ogl_drawcircle2(4,GL_POLYGON,1.0,-14.0,1.0,-8.0);
533 ogl_drawcircle2(4,GL_POLYGON,1.0,14.0,1.0,-8.0);
537 glCallList(primary_lh[primary]);
538 // if (Canvas_height>200)
541 if (!secondary_lh[secondary]){
542 secondary_lh[secondary]=glGenLists(1);
543 glNewList(secondary_lh[secondary], GL_COMPILE_AND_EXECUTE);
547 glColor3fv(darker_g);
549 glColor3fv(bright_g);
550 ogl_drawcircle2(8,GL_LINE_LOOP,2.0,-10.0,2.0,-1.0);
553 glColor3fv(darker_g);
555 glColor3fv(bright_g);
556 ogl_drawcircle2(8,GL_LINE_LOOP,2.0,10.0,2.0,-1.0);
558 //bottom/middle secondary
560 glColor3fv(darker_g);
562 glColor3fv(bright_g);
563 ogl_drawcircle2(8,GL_LINE_LOOP,2.0,0.0,2.0,-7.0);
567 glCallList(secondary_lh[secondary]);
571 int g3_draw_sphere(g3s_point *pnt,fix rad){
573 c=grd_curcanv->cv_color;
574 OGL_DISABLE(TEXTURE_2D);
575 // glPointSize(f2glf(rad));
576 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
577 // glBegin(GL_POINTS);
578 // glVertex3f(f2glf(pnt->p3_vec.x),f2glf(pnt->p3_vec.y),-f2glf(pnt->p3_vec.z));
581 glTranslatef(f2glf(pnt->p3_vec.x),f2glf(pnt->p3_vec.y),-f2glf(pnt->p3_vec.z));
582 glScalef(f2glf(rad),f2glf(rad),f2glf(rad));
583 if (!sphereh) sphereh=circle_list_init(20,GL_POLYGON,GL_COMPILE_AND_EXECUTE);
584 else glCallList(sphereh);
589 int gr_ucircle(fix xc1,fix yc1,fix r1){//dunno if this really works, radar doesn't seem to.. hm..
591 c=grd_curcanv->cv_color;
592 OGL_DISABLE(TEXTURE_2D);
593 // glPointSize(f2glf(rad));
594 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
595 // glBegin(GL_POINTS);
596 // glVertex3f(f2glf(pnt->p3_vec.x),f2glf(pnt->p3_vec.y),-f2glf(pnt->p3_vec.z));
599 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)));
601 (f2fl(xc1)+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,
602 1.0-(f2fl(yc1)+grd_curcanv->cv_bitmap.bm_y)/(float)last_height,0);
603 glScalef(f2fl(r1),f2fl(r1),f2fl(r1));
605 if (!circleh5) circleh5=circle_list_init(5,GL_LINE_LOOP,GL_COMPILE_AND_EXECUTE);
606 else glCallList(circleh5);
608 if (!circleh10) circleh10=circle_list_init(10,GL_LINE_LOOP,GL_COMPILE_AND_EXECUTE);
609 else glCallList(circleh10);
614 int gr_circle(fix xc1,fix yc1,fix r1){
615 return gr_ucircle(xc1,yc1,r1);
618 bool g3_draw_poly(int nv,g3s_point **pointlist)
622 c=grd_curcanv->cv_color;
623 // 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);
624 OGL_DISABLE(TEXTURE_2D);
625 if (Gr_scanline_darkening_level >= GR_FADE_LEVELS)
626 glColor3f(PAL2Tr(c), PAL2Tg(c), PAL2Tb(c));
628 glColor4f(PAL2Tr(c), PAL2Tg(c), PAL2Tb(c), 1.0 - (float)Gr_scanline_darkening_level / ((float)GR_FADE_LEVELS - 1.0));
629 glBegin(GL_TRIANGLE_FAN);
631 // glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
632 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
638 void gr_upoly_tmap(int nverts, int *vert ){
639 mprintf((0,"gr_upoly_tmap: unhandled\n"));//should never get called
641 void draw_tmap_flat(grs_bitmap *bm,int nv,g3s_point **vertlist){
642 mprintf((0,"draw_tmap_flat: unhandled\n"));//should never get called
644 extern void (*tmap_drawer_ptr)(grs_bitmap *bm,int nv,g3s_point **vertlist);
645 bool g3_draw_tmap(int nv,g3s_point **pointlist,g3s_uvl *uvl_list,grs_bitmap *bm)
649 if (tmap_drawer_ptr==draw_tmap_flat){
650 /* fix average_light=0;
653 average_light += uvl_list[i].l;*/
654 OGL_DISABLE(TEXTURE_2D);
655 // glmprintf((0,"Gr_scanline_darkening_level=%i %f\n",Gr_scanline_darkening_level,Gr_scanline_darkening_level/(float)NUM_LIGHTING_LEVELS));
656 glColor4f(0,0,0,1.0-(Gr_scanline_darkening_level/(float)NUM_LIGHTING_LEVELS));
657 //glColor4f(0,0,0,f2fl(average_light/nv));
658 glBegin(GL_TRIANGLE_FAN);
660 // glColor4f(0,0,0,f2fl(uvl_list[c].l));
661 // glTexCoord2f(f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
662 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
665 }else if (tmap_drawer_ptr==draw_tmap){
667 /* if (bm->bm_w !=64||bm->bm_h!=64)
668 printf("g3_draw_tmap w %i h %i\n",bm->bm_w,bm->bm_h);*/
669 OGL_ENABLE(TEXTURE_2D);
671 ogl_texwrap(bm->gltexture,GL_REPEAT);
672 glBegin(GL_TRIANGLE_FAN);
674 if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
677 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
678 l=f2fl(uvl_list[c].l);
681 glTexCoord2f(f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
682 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
683 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
687 mprintf((0,"g3_draw_tmap: unhandled tmap_drawer %p\n",tmap_drawer_ptr));
691 bool g3_draw_tmap_2(int nv,g3s_point **pointlist,g3s_uvl *uvl_list,grs_bitmap *bmbot,grs_bitmap *bm,int orient)
693 #if (defined(GL_ARB_multitexture) || defined(GL_SGIS_multitexture))
694 if (ogl_arb_multitexture_ok || ogl_sgis_multitexture_ok){
699 /* if (bm->bm_w !=64||bm->bm_h!=64)
700 printf("g3_draw_tmap w %i h %i\n",bm->bm_w,bm->bm_h);*/
701 if (ogl_arb_multitexture_ok){
702 #ifdef GL_ARB_multitexture
703 glActiveTextureARB(GL_TEXTURE0_ARB);
705 }else if (ogl_sgis_multitexture_ok){
706 #ifdef GL_SGIS_multitexture
707 glSelectTextureSGIS(GL_TEXTURE0_SGIS);
710 ogl_bindbmtex(bmbot);
711 OGL_ENABLE(TEXTURE_2D);
712 glEnable(GL_TEXTURE_2D);
713 ogl_texwrap(bmbot->gltexture,GL_REPEAT);
714 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
716 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
718 if (ogl_arb_multitexture_ok){
719 #ifdef GL_ARB_multitexture
720 glActiveTextureARB(GL_TEXTURE1_ARB);
722 }else if (ogl_sgis_multitexture_ok){
723 #ifdef GL_SGIS_multitexture
724 glSelectTextureSGIS(GL_TEXTURE1_SGIS);
728 // OGL_ENABLE(TEXTURE_2D);
729 glEnable(GL_TEXTURE_2D);
730 ogl_texwrap(bm->gltexture,GL_REPEAT);
731 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
734 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
736 glBegin(GL_TRIANGLE_FAN);
740 u1=1.0-f2glf(uvl_list[c].v);
741 v1=f2glf(uvl_list[c].u);
744 u1=1.0-f2glf(uvl_list[c].u);
745 v1=1.0-f2glf(uvl_list[c].v);
748 u1=f2glf(uvl_list[c].v);
749 v1=1.0-f2glf(uvl_list[c].u);
752 u1=f2glf(uvl_list[c].u);
753 v1=f2glf(uvl_list[c].v);
756 if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
759 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
760 l=f2fl(uvl_list[c].l);
763 // glTexCoord2f(f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
764 if (ogl_arb_multitexture_ok){
765 #ifdef GL_ARB_multitexture
766 glMultiTexCoord2fARB(GL_TEXTURE0_ARB,f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
768 }else if (ogl_sgis_multitexture_ok){
769 #ifdef GL_SGIS_multitexture
770 glMultiTexCoord2fSGIS(GL_TEXTURE0_SGIS,f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
773 if (ogl_arb_multitexture_ok){
774 #ifdef GL_ARB_multitexture
775 glMultiTexCoord2fARB(GL_TEXTURE1_ARB,u1,v1);
777 }else if (ogl_sgis_multitexture_ok){
778 #ifdef GL_SGIS_multitexture
779 glMultiTexCoord2fSGIS(GL_TEXTURE1_SGIS,u1,v1);
782 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
783 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
784 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
787 /* if (ogl_arb_multitexture_ok){
788 #ifdef GL_ARB_multitexture
789 glActiveTextureARB(GL_TEXTURE1_ARB);
791 }else if (ogl_sgis_multitexture_ok){
792 #ifdef GL_SGIS_multitexture
793 glSelectTextureSGIS(GL_TEXTURE1_SGIS);
796 // OGL_ENABLE(TEXTURE_2D);*/
797 glDisable(GL_TEXTURE_2D);
798 if (ogl_arb_multitexture_ok){
799 #ifdef GL_ARB_multitexture
800 glActiveTextureARB(GL_TEXTURE0_ARB);
802 }else if (ogl_sgis_multitexture_ok){
803 #ifdef GL_SGIS_multitexture
804 glSelectTextureSGIS(GL_TEXTURE0_SGIS);
813 g3_draw_tmap(nv,pointlist,uvl_list,bmbot);//draw the bottom texture first.. could be optimized with multitexturing..
816 /* if (bm->bm_w !=64||bm->bm_h!=64)
817 printf("g3_draw_tmap w %i h %i\n",bm->bm_w,bm->bm_h);*/
818 OGL_ENABLE(TEXTURE_2D);
820 ogl_texwrap(bm->gltexture,GL_REPEAT);
821 glBegin(GL_TRIANGLE_FAN);
825 u1=1.0-f2glf(uvl_list[c].v);
826 v1=f2glf(uvl_list[c].u);
829 u1=1.0-f2glf(uvl_list[c].u);
830 v1=1.0-f2glf(uvl_list[c].v);
833 u1=f2glf(uvl_list[c].v);
834 v1=1.0-f2glf(uvl_list[c].u);
837 u1=f2glf(uvl_list[c].u);
838 v1=f2glf(uvl_list[c].v);
841 if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
844 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
845 l=f2fl(uvl_list[c].l);
849 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
850 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
851 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
858 bool g3_draw_bitmap(vms_vector *pos,fix width,fix height,grs_bitmap *bm, int orientation)
861 vms_vector pv,v1;//,v2;
865 // printf("g3_draw_bitmap: %f,%f,%f - ",f2glf(pos->x),f2glf(pos->y),-f2glf(pos->z));
866 // printf("(%f,%f,%f) ",f2glf(View_position.x),f2glf(View_position.y),-f2glf(View_position.z));
868 OGL_ENABLE(TEXTURE_2D);
870 ogl_texwrap(bm->gltexture,GL_CLAMP);
873 glColor3f(1.0,1.0,1.0);
874 width = fixmul(width,Matrix_scale.x);
875 height = fixmul(height,Matrix_scale.y);
877 // g3_rotate_point(&p[i],pos);
878 vm_vec_sub(&v1,pos,&View_position);
879 vm_vec_rotate(&pv,&v1,&View_matrix);
880 // printf(" %f,%f,%f->",f2glf(pv.x),f2glf(pv.y),-f2glf(pv.z));
883 glTexCoord2f(0.0, 0.0);
888 glTexCoord2f(bm->gltexture->u, 0.0);
893 glTexCoord2f(bm->gltexture->u, bm->gltexture->v);
898 glTexCoord2f(0.0, bm->gltexture->v);
903 // vm_vec_rotate(&v2,&v1,&View_matrix);
904 // vm_vec_sub(&v1,&v2,&pv);
905 //vm_vec_sub(&v1,&pv,&v2);
906 // vm_vec_sub(&v2,&pv,&v1);
907 glVertex3f(f2glf(pv.x),f2glf(pv.y),-f2glf(pv.z));
908 // printf("%f,%f,%f ",f2glf(v1.x),f2glf(v1.y),-f2glf(v1.z));
916 bool ogl_ubitmapm_c(int x, int y,grs_bitmap *bm,int c)
921 x+=grd_curcanv->cv_bitmap.bm_x;
922 y+=grd_curcanv->cv_bitmap.bm_y;
923 xo=x/(float)last_width;
924 xf=(bm->bm_w+x)/(float)last_width;
925 yo=1.0-y/(float)last_height;
926 yf=1.0-(bm->bm_h+y)/(float)last_height;
928 // printf("g3_draw_bitmap: %f,%f,%f - ",f2glf(pos->x),f2glf(pos->y),-f2glf(pos->z));
929 // printf("(%f,%f,%f) ",f2glf(View_position.x),f2glf(View_position.y),-f2glf(View_position.z));
931 /* glEnABLE(ALPHA_TEST);
932 glAlphaFunc(GL_GREATER,0.0);*/
934 OGL_ENABLE(TEXTURE_2D);
936 ogl_texwrap(bm->gltexture,GL_CLAMP);
940 if (bm->bm_w==bm->gltexture->w)
943 u2=(bm->bm_w+bm->bm_x)/(float)bm->gltexture->tw;
945 u1=bm->bm_x/(float)bm->gltexture->tw;
946 u2=(bm->bm_w+bm->bm_x)/(float)bm->gltexture->tw;
950 if (bm->bm_h==bm->gltexture->h)
953 v2=(bm->bm_h+bm->bm_y)/(float)bm->gltexture->th;
955 v1=bm->bm_y/(float)bm->gltexture->th;
956 v2=(bm->bm_h+bm->bm_y)/(float)bm->gltexture->th;
961 glColor3f(1.0,1.0,1.0);
963 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
964 glTexCoord2f(u1, v1); glVertex2f(xo, yo);
965 glTexCoord2f(u2, v1); glVertex2f(xf, yo);
966 glTexCoord2f(u2, v2); glVertex2f(xf, yf);
967 glTexCoord2f(u1, v2); glVertex2f(xo, yf);
969 // glDisABLE(ALPHA_TEST);
973 bool ogl_ubitmapm(int x, int y,grs_bitmap *bm){
974 return ogl_ubitmapm_c(x,y,bm,-1);
975 // return ogl_ubitblt(bm->bm_w,bm->bm_h,x,y,0,0,bm,NULL);
978 //also upsidedown, currently.
979 bool ogl_ubitblt(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest)
981 GLfloat xo,yo;//,xs,ys;
982 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));
987 xo=dx/(float)last_width;
988 // xo=dx/(float)grd_curscreen->sc_w;
989 // xs=w/(float)last_width;
990 //yo=1.0-dy/(float)last_height;
991 yo=1.0-(dy+h)/(float)last_height;
992 // ys=h/(float)last_height;
994 // OGL_ENABLE(TEXTURE_2D);
996 OGL_DISABLE(TEXTURE_2D);
997 glRasterPos2f(xo,yo);
998 ogl_filltexbuf(src->bm_data,texbuf,src->bm_w,w,h,sx,sy,w,h,GL_RGBA);
999 glDrawPixels(w,h,GL_RGBA,GL_UNSIGNED_BYTE,texbuf);
1005 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)
1007 GLfloat xo,yo,xs,ys;
1008 GLfloat u1,v1;//,u2,v2;
1010 // unsigned char *oldpal;
1013 ogl_init_texture(&tex);
1015 tex.prio=0.0;tex.wantmip=0;
1016 tex.lw=src->bm_rowsize;
1018 /* if (w==src->bm_w && sx==0){
1021 u1=sx/(float)src->bm_w*src->glu;
1022 u2=w/(float)src->bm_w*src->glu+u1;
1024 if (h==src->bm_h && sy==0){
1027 v1=sy/(float)src->bm_h*src->glv;
1028 v2=h/(float)src->bm_h*src->glv+v1;
1034 xo=dx/(float)last_width;
1035 xs=dw/(float)last_width;
1036 yo=1.0-dy/(float)last_height;
1037 ys=dh/(float)last_height;
1039 OGL_ENABLE(TEXTURE_2D);
1042 ogl_pal=gr_current_pal;
1043 ogl_loadtexture(src->bm_data,sx,sy,&tex);
1046 OGL_BINDTEXTURE(tex.handle);
1048 ogl_texwrap(&tex,GL_CLAMP);
1051 glColor3f(1.0,1.0,1.0);
1052 glTexCoord2f(u1, v1); glVertex2f(xo, yo);
1053 glTexCoord2f(tex.u, v1); glVertex2f(xo+xs, yo);
1054 glTexCoord2f(tex.u, tex.v); glVertex2f(xo+xs, yo-ys);
1055 glTexCoord2f(u1, tex.v); glVertex2f(xo, yo-ys);
1057 ogl_freetexture(&tex);
1060 bool ogl_ubitblt(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1061 return ogl_ubitblt_i(w,h,dx,dy,w,h,sx,sy,src,dest);
1064 bool ogl_ubitblt_tolinear(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1066 unsigned char *d,*s;
1070 w1=grd_curscreen->sc_w;h1=grd_curscreen->sc_h;
1071 if (w1*h1*3>OGLTEXBUFSIZE)
1072 Error("ogl_ubitblt_tolinear: screen res larger than OGLTEXBUFSIZE\n");
1074 if (ogl_readpixels_ok>0){
1075 OGL_DISABLE(TEXTURE_2D);
1076 glReadBuffer(GL_FRONT);
1077 glReadPixels(0,0,w1,h1,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1078 // glReadPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1079 // glReadPixels(sx,sy,w+sx,h+sy,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1081 memset(texbuf,0,w1*h1*3);
1085 d=dest->bm_data+dx+(dy+i)*dest->bm_rowsize;
1086 s=texbuf+((h1-(i+sy+1))*w1+sx)*3;
1088 *d=gr_find_closest_color(s[0]/4,s[1]/4,s[2]/4);
1095 unsigned char *d,*s,*e;
1096 if (w*h*3>OGLTEXBUFSIZE)
1097 Error("ogl_ubitblt_tolinear: size larger than OGLTEXBUFSIZE\n");
1100 #if 1//also seems to cause a mess. need to look into it a bit more..
1101 if (ogl_readpixels_ok>0){
1102 OGL_DISABLE(TEXTURE_2D);
1103 glReadBuffer(GL_FRONT);
1104 // glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1105 glReadPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1108 memset(texbuf,0,w*h*3);
1109 // d=dest->bm_data+dx+(dy+i)*dest->bm_rowsize;
1110 d=dest->bm_data+dx+dy*dest->bm_rowsize;
1112 s=texbuf+w*(h-(i+1))*3;
1114 if (s<texbuf){Error("blah1\n");}
1115 if (d<dest->bm_data){Error("blah3\n");}
1116 // d=dest->bm_data+(i*dest->bm_rowsize);
1120 if (s>texbuf+w*h*3-3){Error("blah2\n");}
1121 if (d>dest->bm_data+dest->bm_rowsize*(h+dy)+dx ){Error("blah4\n");}
1122 *d=gr_find_closest_color(s[0]/4,s[1]/4,s[2]/4);
1128 d+=dest->bm_rowsize;
1130 glmprintf((0,"c=%i w*h=%i\n",c,w*h));
1135 bool ogl_ubitblt_copy(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1136 #if 0 //just seems to cause a mess.
1137 GLfloat xo,yo;//,xs,ys;
1142 // xo=dx/(float)last_width;
1143 xo=dx/(float)grd_curscreen->sc_w;
1144 // yo=1.0-(dy+h)/(float)last_height;
1145 yo=1.0-(dy+h)/(float)grd_curscreen->sc_h;
1148 OGL_DISABLE(TEXTURE_2D);
1149 glReadBuffer(GL_FRONT);
1150 glRasterPos2f(xo,yo);
1151 // glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1152 glCopyPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_COLOR);
1158 void ogl_start_frame(void){
1159 r_polyc=0;r_tpolyc=0;r_bitmapc=0;r_ubitmapc=0;r_ubitbltc=0;r_upixelc=0;
1160 // gl_badtexture=500;
1162 OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,Canvas_width,Canvas_height);
1163 glClearColor(0.0, 0.0, 0.0, 0.0);
1164 // glEnable(GL_ALPHA_TEST);
1165 // glAlphaFunc(GL_GREATER,0.01);
1166 glShadeModel(GL_SMOOTH);
1167 glMatrixMode(GL_PROJECTION);
1168 glLoadIdentity();//clear matrix
1169 //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),1.0,1000000.0);
1170 //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),0.01,1000000.0);
1171 gluPerspective(90.0,1.0,0.01,1000000.0);
1172 //gluPerspective(90.0,(GLfloat)(Canvas_width*3)/(GLfloat)(Canvas_height*4),0.01,1000000.0);
1173 // gluPerspective(75.0,(GLfloat)Canvas_width/(GLfloat)Canvas_height,1.0,1000000.0);
1174 glMatrixMode(GL_MODELVIEW);
1175 glLoadIdentity();//clear matrix
1177 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1178 // glDisABLE(DITHER);
1179 // glScalef(1.0,1.0,-1.0);
1180 // glScalef(1.0,1.0,-1.0);
1183 // glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1184 // OGL_TEXENV(GL_TEXTURE_ENV_MODE,GL_MODULATE);
1185 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_texmagfilt);
1186 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_texminfilt);
1187 // OGL_TEXPARAM(GL_TEXTURE_MAG_FILTER,GL_texmagfilt);
1188 // OGL_TEXPARAM(GL_TEXTURE_MIN_FILTER,GL_texminfilt);
1189 // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1190 // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1193 void merge_textures_stats(void);
1195 void ogl_end_frame(void){
1196 // OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,);
1197 OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
1199 // merge_textures_stats();
1200 // ogl_texture_stats();
1202 // glViewport(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
1203 glMatrixMode(GL_PROJECTION);
1204 glLoadIdentity();//clear matrix
1205 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
1206 glMatrixMode(GL_MODELVIEW);
1207 glLoadIdentity();//clear matrix
1208 // glDisABLE(BLEND);
1209 //glDisABLE(ALPHA_TEST);
1210 //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),1.0,1000000.0);
1211 // ogl_swap_buffers();//platform specific code
1212 // glClear(GL_COLOR_BUFFER_BIT);
1215 void ogl_swap_buffers(void){
1216 ogl_clean_texture_cache();
1217 if (gr_renderstats){
1218 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);
1219 // 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
1222 ogl_swap_buffers_internal();
1223 glClear(GL_COLOR_BUFFER_BIT);
1226 int tex_format_supported(int iformat,int format){
1229 if (!ogl_intensity4_ok) return 0; break;
1230 case GL_LUMINANCE4_ALPHA4:
1231 if (!ogl_luminance4_alpha4_ok) return 0; break;
1233 if (!ogl_rgba2_ok) return 0; break;
1235 if (ogl_gettexlevelparam_ok){
1236 GLint internalFormat;
1237 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, iformat, 64, 64, 0,
1238 format, GL_UNSIGNED_BYTE, texbuf);//NULL?
1239 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0,
1240 GL_TEXTURE_INTERNAL_FORMAT,
1242 return (internalFormat==iformat);
1247 //little hack to find the largest or equal multiple of 2 for a given number
1250 for (i=2;i<=4096;i*=2)
1255 //GLubyte texbuf[512*512*4];
1256 GLubyte texbuf[OGLTEXBUFSIZE];
1257 void ogl_filltexbuf(unsigned char *data,GLubyte *texp,int truewidth,int width,int height,int dxo,int dyo,int twidth,int theight,int type)
1259 // GLushort *tex=(GLushort *)texp;
1261 if (twidth*theight*4>sizeof(texbuf))//shouldn't happen, descent never uses textures that big.
1262 Error("texture toobig %i %i",twidth,theight);
1265 for (y=0;y<theight;y++){
1266 i=dxo+truewidth*(y+dyo);
1267 for (x=0;x<twidth;x++){
1268 if (x<width && y<height)
1271 c=255;//fill the pad space with transparancy
1277 case GL_LUMINANCE_ALPHA:
1285 (*(texp++))=0;//transparent pixel
1291 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. :)
1294 case GL_LUMINANCE_ALPHA:
1299 //(*(texp++))=gr_palette[c*3]*4;
1300 //(*(texp++))=gr_palette[c*3+1]*4;
1301 //(*(texp++))=gr_palette[c*3+2]*4;
1302 (*(texp++))=ogl_pal[c*3]*4;
1303 (*(texp++))=ogl_pal[c*3+1]*4;
1304 (*(texp++))=ogl_pal[c*3+2]*4;
1305 (*(texp++))=255;//not transparent
1306 // (*(tex++))=(ogl_pal[c*3]>>1) + ((ogl_pal[c*3+1]>>1)<<5) + ((ogl_pal[c*3+2]>>1)<<10) + (1<<15);
1313 int tex_format_verify(ogl_texture *tex){
1314 while (!tex_format_supported(tex->internalformat,tex->format)){
1315 glmprintf((0,"tex format %x not supported",tex->internalformat));
1316 switch (tex->internalformat){
1318 if (ogl_luminance4_alpha4_ok){
1319 tex->internalformat=GL_LUMINANCE4_ALPHA4;
1320 tex->format=GL_LUMINANCE_ALPHA;
1322 }//note how it will fall through here if the statement is false
1323 case GL_LUMINANCE4_ALPHA4:
1325 tex->internalformat=GL_RGBA2;
1326 tex->format=GL_RGBA;
1328 }//note how it will fall through here if the statement is false
1330 tex->internalformat=ogl_rgba_format;
1331 tex->format=GL_RGBA;
1334 mprintf((0,"...no tex format to fall back on\n"));
1337 glmprintf((0,"...falling back to %x\n",tex->internalformat));
1341 void tex_set_size1(ogl_texture *tex,int dbits,int bits,int w, int h){
1343 if (tex->tw!=w || tex->th!=h){
1344 u=(tex->w/(float)tex->tw*w) * (tex->h/(float)tex->th*h);
1345 glmprintf((0,"shrunken texture?\n"));
1348 if (bits<=0){//the beta nvidia GLX server. doesn't ever return any bit sizes, so just use some assumptions.
1349 tex->bytes=((float)w*h*dbits)/8.0;
1350 tex->bytesu=((float)u*dbits)/8.0;
1352 tex->bytes=((float)w*h*bits)/8.0;
1353 tex->bytesu=((float)u*bits)/8.0;
1355 glmprintf((0,"tex_set_size1: %ix%i, %ib(%i) %iB\n",w,h,bits,dbits,tex->bytes));
1357 void tex_set_size(ogl_texture *tex){
1360 if (ogl_gettexlevelparam_ok){
1362 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&w);
1363 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&h);
1364 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_LUMINANCE_SIZE,&t);a+=t;
1365 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_INTENSITY_SIZE,&t);a+=t;
1366 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_RED_SIZE,&t);a+=t;
1367 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_GREEN_SIZE,&t);a+=t;
1368 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_BLUE_SIZE,&t);a+=t;
1369 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_ALPHA_SIZE,&t);a+=t;
1374 switch (tex->format){
1378 case GL_LUMINANCE_ALPHA:
1385 Error("tex_set_size unknown texformat\n");
1388 tex_set_size1(tex,bi,a,w,h);
1390 //loads a palettized bitmap into a ogl RGBA texture.
1391 //Sizes and pads dimensions to multiples of 2 if necessary.
1392 //In theory this could be a problem for repeating textures, but all real
1393 //textures (not sprites, etc) in descent are 64x64, so we are ok.
1394 //stores OpenGL textured id in *texid and u/v values required to get only the real data in *u/*v
1395 void ogl_loadtexture(unsigned char * data, int dxo,int dyo, ogl_texture *tex)
1397 //void ogl_loadtexture(unsigned char * data, int width, int height,int dxo,int dyo, int *texid,float *u,float *v,char domipmap,float prio){
1398 // int internalformat=GL_RGBA;
1399 // int format=GL_RGBA;
1401 tex->tw=pow2ize(tex->w);tex->th=pow2ize(tex->h);//calculate smallest texture size that can accomodate us (must be multiples of 2)
1402 // tex->tw=tex->w;tex->th=tex->h;//feeling lucky?
1404 if(gr_badtexture>0) return;
1406 #if !(defined(__APPLE__) && defined(__MACH__))
1407 // always fails on OS X, but textures work fine!
1408 if (tex_format_verify(tex))
1412 //calculate u/v values that would make the resulting texture correctly sized
1413 tex->u=(float)tex->w/(float)tex->tw;
1414 tex->v=(float)tex->h/(float)tex->th;
1416 // if (width!=twidth || height!=theight)
1417 // glmprintf((0,"sizing %ix%i texture up to %ix%i\n",width,height,twidth,theight));
1418 ogl_filltexbuf(data,texbuf,tex->lw,tex->w,tex->h,dxo,dyo,tex->tw,tex->th,tex->format);
1420 // Generate OpenGL texture IDs.
1421 glGenTextures(1, &tex->handle);
1424 glPrioritizeTextures(1,&tex->handle,&tex->prio);
1426 // Give our data to OpenGL.
1428 OGL_BINDTEXTURE(tex->handle);
1430 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1432 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_texmagfilt);
1433 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_texminfilt);
1435 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1436 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1438 // domipmap=0;//mipmaps aren't used in GL_NEAREST anyway, and making the mipmaps is pretty slow
1439 //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.
1441 if (tex->wantmip && GL_needmipmaps)
1442 gluBuild2DMipmaps( GL_TEXTURE_2D, tex->internalformat, tex->tw,
1443 tex->th, tex->format, GL_UNSIGNED_BYTE, texbuf);
1445 glTexImage2D(GL_TEXTURE_2D, 0, tex->internalformat,
1446 tex->tw, tex->th, 0, tex->format, // RGBA textures.
1447 GL_UNSIGNED_BYTE, // imageData is a GLubyte pointer.
1453 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));
1457 unsigned char decodebuf[512*512];
1459 void ogl_loadbmtexture_m(grs_bitmap *bm,int domipmap)
1462 while (bm->bm_parent)
1465 if (bm->gltexture==NULL){
1466 ogl_init_texture(bm->gltexture=ogl_get_free_texture());
1467 bm->gltexture->lw=bm->bm_w;
1468 bm->gltexture->w=bm->bm_w;
1469 bm->gltexture->h=bm->bm_h;
1470 bm->gltexture->wantmip=domipmap;
1473 if (bm->gltexture->handle>0)
1475 if (bm->gltexture->w==0){
1476 bm->gltexture->lw=bm->bm_w;
1477 bm->gltexture->w=bm->bm_w;
1478 bm->gltexture->h=bm->bm_h;
1481 if (bm->bm_flags & BM_FLAG_RLE){
1482 unsigned char * dbits;
1483 unsigned char * sbits;
1487 if (bm->bm_flags & BM_FLAG_RLE_BIG)
1490 sbits = &bm->bm_data[4 + (bm->bm_h * data_offset)];
1493 for (i=0; i < bm->bm_h; i++ ) {
1494 gr_rle_decode(sbits,dbits);
1495 if ( bm->bm_flags & BM_FLAG_RLE_BIG )
1496 sbits += (int)INTEL_SHORT(*((short *)&(bm->bm_data[4+(i*data_offset)])));
1498 sbits += (int)bm->bm_data[4+i];
1503 ogl_loadtexture(buf,0,0,bm->gltexture);
1506 void ogl_loadbmtexture(grs_bitmap *bm)
1508 ogl_loadbmtexture_m(bm,1);
1511 void ogl_freetexture(ogl_texture *gltexture)
1513 if (gltexture->handle>0) {
1515 glmprintf((0,"ogl_freetexture(%p):%i (last rend %is) (%i left)\n",gltexture,gltexture->handle,(GameTime-gltexture->lastrend)/f1_0,r_texcount));
1516 glDeleteTextures( 1, &gltexture->handle );
1517 // gltexture->handle=0;
1518 ogl_init_texture(gltexture);
1521 void ogl_freebmtexture(grs_bitmap *bm){
1523 ogl_freetexture(bm->gltexture);
1526 // glmprintf((0,"ogl_freebmtexture(%p,%p):%i (%i left)\n",bm->bm_data,&bm->gltexture,bm->gltexture,r_texcount));
1527 // glDeleteTextures( 1, &bm->gltexture );
1528 // bm->gltexture=-1;