2 * $Source: /cvs/cvsroot/d2x/video/ogl.c,v $
5 * $Date: 2001-10-09 03:00:08 $
7 * Graphics support functions for OpenGL.
9 * $Log: not supported by cvs2svn $
10 * Revision 1.5 2001/10/09 02:38:52 bradleyb
11 * re-imported d1x version of this file
13 * Revision 1.4 2001/01/31 16:31:14 bradleyb
14 * OpenGL support under Cygwin/SDL
16 * Revision 1.3 2001/01/29 13:47:52 bradleyb
17 * Fixed build, some minor cleanups.
38 #include "../3d/globvars.h"
55 //change to 1 for lots of spew.
57 #define glmprintf(a) mprintf(a)
67 #define cosf(a) cos(a)
68 #define sinf(a) sin(a)
71 unsigned char *ogl_pal=gr_palette;
73 int GL_texmagfilt=GL_NEAREST;
74 int GL_texminfilt=GL_NEAREST;
77 int last_width=-1,last_height=-1;
78 int GL_TEXTURE_2D_enabled=-1;
79 int GL_texclamp_enabled=-1;
81 extern int gr_renderstats;
82 extern int gr_badtexture;
83 int ogl_alttexmerge=1;//merge textures by just printing the seperate textures?
84 int ogl_rgba_format=4;
85 int ogl_intensity4_ok=1;
86 int ogl_luminance4_alpha4_ok=1;
88 int ogl_readpixels_ok=1;
89 int ogl_gettexlevelparam_ok=1;
90 #ifdef GL_ARB_multitexture
91 int ogl_arb_multitexture_ok=0;
93 #ifdef GL_SGIS_multitexture
94 int ogl_sgis_multitexture_ok=0;
100 int cross_lh[2]={0,0};
101 int primary_lh[3]={0,0,0};
102 int secondary_lh[5]={0,0,0,0,0};
105 #define OGL_BINDTEXTURE(a) if(gr_badtexture>0) glBindTexture(GL_TEXTURE_2D, 0);\
106 else if(a!=lastbound) {glBindTexture(GL_TEXTURE_2D, a);lastbound=a;}*/
107 #define OGL_BINDTEXTURE(a) if(gr_badtexture>0) glBindTexture(GL_TEXTURE_2D, 0);\
108 else glBindTexture(GL_TEXTURE_2D, a);
111 ogl_texture ogl_texture_list[OGL_TEXTURE_LIST_SIZE];
112 int ogl_texture_list_cur;
114 void ogl_init_texture_stats(ogl_texture* t){
115 t->prio=0.3;//default prio
119 void ogl_init_texture(ogl_texture* t){
121 t->internalformat=ogl_rgba_format;
125 ogl_init_texture_stats(t);
127 void ogl_reset_texture_stats_internal(void){
129 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++)
130 if (ogl_texture_list[i].handle>0){
131 ogl_init_texture_stats(&ogl_texture_list[i]);
134 void ogl_init_texture_list_internal(void){
136 ogl_texture_list_cur=0;
137 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++)
138 ogl_init_texture(&ogl_texture_list[i]);
140 void ogl_smash_texture_list_internal(void){
145 memset(cross_lh,0,sizeof(cross_lh));
146 memset(primary_lh,0,sizeof(primary_lh));
147 memset(secondary_lh,0,sizeof(secondary_lh));
148 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
149 if (ogl_texture_list[i].handle>0){
150 glDeleteTextures( 1, &ogl_texture_list[i].handle );
151 ogl_texture_list[i].handle=0;
153 ogl_texture_list[i].wrapstate=-1;
156 void ogl_vivify_texture_list_internal(void){
160 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
161 t=&ogl_texture_list[i];
162 if (t->w>0){//erk, realised this can't be done since we'd need the texture bm_data too. hmmm.
163 ogl_loadbmtexture(t);
168 ogl_texture* ogl_get_free_texture(void){
170 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
171 if (ogl_texture_list[ogl_texture_list_cur].handle<=0 && ogl_texture_list[ogl_texture_list_cur].w==0)
172 return &ogl_texture_list[ogl_texture_list_cur];
173 if (++ogl_texture_list_cur>=OGL_TEXTURE_LIST_SIZE)
174 ogl_texture_list_cur=0;
176 Error("OGL: texture list full!\n");
179 int ogl_texture_stats(void){
180 int used=0,usedl4a4=0,usedrgba=0,databytes=0,truebytes=0,datatexel=0,truetexel=0,i;
181 int prio0=0,prio1=0,prio2=0,prio3=0,prioh=0;
184 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
185 t=&ogl_texture_list[i];
188 datatexel+=t->w*t->h;
189 truetexel+=t->tw*t->th;
190 databytes+=t->bytesu;
192 if (t->prio<0.299)prio0++;
193 else if (t->prio<0.399)prio1++;
194 else if (t->prio<0.499)prio2++;
195 else if (t->prio<0.599)prio3++;
202 gr_printf(5,GAME_FONT->ft_h*14+3*14,"%i(%i,%i) %iK(%iK wasted)",used,usedrgba,usedl4a4,truebytes/1024,(truebytes-databytes)/1024);
204 // 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));
207 int ogl_mem_target=-1;
208 void ogl_clean_texture_cache(void){
213 if (ogl_mem_target<0){
219 bytes=ogl_texture_stats();
220 while (bytes>ogl_mem_target){
221 for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
222 t=&ogl_texture_list[i];
224 if (t->lastrend+f1_0*time<GameTime){
227 if (bytes<ogl_mem_target)
233 Error("not enough mem?");
238 void ogl_bindbmtex(grs_bitmap *bm){
239 if (bm->gltexture==NULL || bm->gltexture->handle<=0)
240 ogl_loadbmtexture(bm);
241 OGL_BINDTEXTURE(bm->gltexture->handle);
242 bm->gltexture->lastrend=GameTime;
243 bm->gltexture->numrend++;
244 //// if (bm->gltexture->numrend==80 || bm->gltexture->numrend==4000 || bm->gltexture->numrend==80000){
245 // if (bm->gltexture->numrend==100){
246 // bm->gltexture->prio+=0.1;
247 //// glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_PRIORITY,bm->gltexture->prio);
248 // glPrioritizeTextures(1,&bm->gltexture->handle,&bm->gltexture->prio);
251 //gltexture MUST be bound first
252 void ogl_texwrap(ogl_texture *gltexture,int state){
253 if (gltexture->wrapstate!=state || gltexture->numrend<1){
254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, state);
255 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, state);
256 gltexture->wrapstate=state;
260 //crude texture precaching
261 //handles: powerups, walls, weapons, polymodels, etc.
262 //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.
263 //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
266 void ogl_cache_polymodel_textures(int model_num){
267 polymodel *po=&Polygon_models[model_num];
269 for (i=0;i<po->n_textures;i++) {
270 // texture_list_index[i] = ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]];
271 ogl_loadbmtexture(&GameBitmaps[ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]].index]);
274 void ogl_cache_vclip_textures(vclip *vc){
276 for (i=0;i<vc->num_frames;i++){
277 PIGGY_PAGE_IN(vc->frames[i]);
278 ogl_loadbmtexture(&GameBitmaps[vc->frames[i].index]);
281 #define ogl_cache_vclipn_textures(i) ogl_cache_vclip_textures(&Vclip[i])
282 void ogl_cache_weapon_textures(weapon_info *w){
283 ogl_cache_vclipn_textures(w->flash_vclip);
284 ogl_cache_vclipn_textures(w->robot_hit_vclip);
285 ogl_cache_vclipn_textures(w->wall_hit_vclip);
286 if (w->render_type==WEAPON_RENDER_VCLIP)
287 ogl_cache_vclipn_textures(w->weapon_vclip);
288 else if (w->render_type==WEAPON_RENDER_POLYMODEL)
289 ogl_cache_polymodel_textures(w->model_num);
291 void ogl_cache_level_textures(void){
299 ogl_reset_texture_stats_internal();//loading a new lev should reset textures
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;
307 glmprintf((0,"max_efx:%i\n",max_efx));
308 for (ef=0;ef<max_efx;ef++){
309 for (i=0,ec=Effects;i<Num_effects;i++,ec++) {
310 if ((Effects[i].changing_wall_texture == -1) && (Effects[i].changing_object_texture==-1) )
312 // if (ec->vc.num_frames>max_efx)
313 // max_efx=ec->vc.num_frames;
316 do_special_effects();
318 for (seg=0;seg<Num_segments;seg++){
319 for (side=0;side<MAX_SIDES_PER_SEGMENT;side++){
320 sidep=&Segments[seg].sides[side];
321 tmap1=sidep->tmap_num;
322 tmap2=sidep->tmap_num2;
323 if (tmap1<0 || tmap1>=NumTextures){
324 glmprintf((0,"ogl_cache_level_textures %i %i %i %i\n",seg,side,tmap1,NumTextures));
328 PIGGY_PAGE_IN(Textures[tmap1]);
329 bm = &GameBitmaps[Textures[tmap1].index];
331 PIGGY_PAGE_IN(Textures[tmap2&0x3FFF]);
332 bm2 = &GameBitmaps[Textures[tmap2&0x3FFF].index];
333 if (ogl_alttexmerge==0 || (bm2->bm_flags & BM_FLAG_SUPER_TRANSPARENT))
334 bm = texmerge_get_cached_bitmap( tmap1, tmap2 );
336 ogl_loadbmtexture(bm2);
338 // 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));
340 ogl_loadbmtexture(bm);
343 glmprintf((0,"finished ef:%i\n",ef));
345 reset_special_effects();
346 init_special_effects();
349 //always have lasers and concs
350 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[LASER_INDEX]]);
351 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[CONCUSSION_INDEX]]);
352 for (i=0;i<Highest_object_index;i++){
353 if(Objects[i].render_type==RT_POWERUP){
354 ogl_cache_vclipn_textures(Objects[i].rtype.vclip_info.vclip_num);
355 switch (Objects[i].id){
357 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[LASER_INDEX]]);
361 case POW_VULCAN_WEAPON:
362 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[VULCAN_INDEX]]);
364 case POW_SPREADFIRE_WEAPON:
365 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[SPREADFIRE_INDEX]]);
367 case POW_PLASMA_WEAPON:
368 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[PLASMA_INDEX]]);
370 case POW_FUSION_WEAPON:
371 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[FUSION_INDEX]]);
373 /* case POW_MISSILE_1:
375 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[CONCUSSION_INDEX]]);
377 case POW_PROXIMITY_WEAPON:
378 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[PROXIMITY_INDEX]]);
380 case POW_HOMING_AMMO_1:
381 case POW_HOMING_AMMO_4:
382 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[HOMING_INDEX]]);
384 case POW_SMARTBOMB_WEAPON:
385 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[SMART_INDEX]]);
387 case POW_MEGA_WEAPON:
388 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[MEGA_INDEX]]);
392 else if(Objects[i].render_type==RT_POLYOBJ){
393 ogl_cache_polymodel_textures(Objects[i].rtype.pobj_info.model_num);
397 glmprintf((0,"finished caching\n"));
400 int r_polyc,r_tpolyc,r_bitmapc,r_ubitmapc,r_ubitbltc,r_upixelc;
402 #define f2glf(x) (f2fl(x))
404 bool g3_draw_line(g3s_point *p0,g3s_point *p1)
407 c=grd_curcanv->cv_color;
408 OGL_DISABLE(TEXTURE_2D);
409 glColor3f(PAL2Tr(c),PAL2Tg(c),PAL2Tb(c));
411 glVertex3f(f2glf(p0->p3_vec.x),f2glf(p0->p3_vec.y),-f2glf(p0->p3_vec.z));
412 glVertex3f(f2glf(p1->p3_vec.x),f2glf(p1->p3_vec.y),-f2glf(p1->p3_vec.z));
416 void ogl_drawcircle2(int nsides,int type,float xsc,float xo,float ysc,float yo){
420 for (i=0; i<nsides; i++) {
421 ang = 2.0*M_PI*i/nsides;
422 glVertex2f(cosf(ang)*xsc+xo,sinf(ang)*ysc+yo);
426 void ogl_drawcircle(int nsides,int type){
430 for (i=0; i<nsides; i++) {
431 ang = 2.0*M_PI*i/nsides;
432 glVertex2f(cosf(ang),sinf(ang));
436 int circle_list_init(int nsides,int type,int mode) {
437 int hand=glGenLists(1);
438 glNewList(hand, mode);
439 /* draw a unit radius circle in xy plane centered on origin */
440 ogl_drawcircle(nsides,type);
444 float bright_g[4]={ 32.0/256, 252.0/256, 32.0/256};
445 float dark_g[4]={ 32.0/256, 148.0/256, 32.0/256};
446 float darker_g[4]={ 32.0/256, 128.0/256, 32.0/256};
447 void ogl_draw_reticle(int cross,int primary,int secondary){
448 float scale=(float)Canvas_height/(float)grd_curscreen->sc_h;
450 // glTranslatef(0.5,0.5,0);
451 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);
452 glScalef(scale/320.0,scale/200.0,scale);//the positions are based upon the standard reticle at 320x200 res.
454 OGL_DISABLE(TEXTURE_2D);
456 if (!cross_lh[cross]){
457 cross_lh[cross]=glGenLists(1);
458 glNewList(cross_lh[cross], GL_COMPILE_AND_EXECUTE);
461 glColor3fv(darker_g);
462 glVertex2f(-4.0,4.0);
464 glColor3fv(bright_g);
467 glVertex2f(-2.0,2.0);
471 glVertex2f(-3.0,-2.0);
473 glColor3fv(bright_g);
474 glVertex2f(-2.0,-1.0);
477 glColor3fv(darker_g);
480 glColor3fv(bright_g);
487 glVertex2f(3.0,-2.0);
489 glColor3fv(bright_g);
490 glVertex2f(2.0,-1.0);
495 glCallList(cross_lh[cross]);
497 // if (Canvas_height>200)
498 // glLineWidth(Canvas_height/(float)200);
499 if (!primary_lh[primary]){
500 primary_lh[primary]=glGenLists(1);
501 glNewList(primary_lh[primary], GL_COMPILE_AND_EXECUTE);
506 glVertex2f(-14.0,-8.0);
507 glVertex2f(-8.0,-5.0);
509 glVertex2f(14.0,-8.0);
510 glVertex2f(8.0,-5.0);
515 glColor3fv(bright_g);
517 ogl_drawcircle2(6,GL_POLYGON,1.5,-7.0,1.5,-5.0);
519 ogl_drawcircle2(6,GL_POLYGON,1.5,7.0,1.5,-5.0);
523 glColor3fv(bright_g);
525 ogl_drawcircle2(4,GL_POLYGON,1.0,-14.0,1.0,-8.0);
527 ogl_drawcircle2(4,GL_POLYGON,1.0,14.0,1.0,-8.0);
531 glCallList(primary_lh[primary]);
532 // if (Canvas_height>200)
535 if (!secondary_lh[secondary]){
536 secondary_lh[secondary]=glGenLists(1);
537 glNewList(secondary_lh[secondary], GL_COMPILE_AND_EXECUTE);
541 glColor3fv(darker_g);
543 glColor3fv(bright_g);
544 ogl_drawcircle2(8,GL_LINE_LOOP,2.0,-10.0,2.0,-1.0);
547 glColor3fv(darker_g);
549 glColor3fv(bright_g);
550 ogl_drawcircle2(8,GL_LINE_LOOP,2.0,10.0,2.0,-1.0);
552 //bottom/middle secondary
554 glColor3fv(darker_g);
556 glColor3fv(bright_g);
557 ogl_drawcircle2(8,GL_LINE_LOOP,2.0,0.0,2.0,-7.0);
561 glCallList(secondary_lh[secondary]);
565 int g3_draw_sphere(g3s_point *pnt,fix rad){
567 c=grd_curcanv->cv_color;
568 OGL_DISABLE(TEXTURE_2D);
569 // glPointSize(f2glf(rad));
570 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
571 // glBegin(GL_POINTS);
572 // glVertex3f(f2glf(pnt->p3_vec.x),f2glf(pnt->p3_vec.y),-f2glf(pnt->p3_vec.z));
575 glTranslatef(f2glf(pnt->p3_vec.x),f2glf(pnt->p3_vec.y),-f2glf(pnt->p3_vec.z));
576 glScalef(f2glf(rad),f2glf(rad),f2glf(rad));
577 if (!sphereh) sphereh=circle_list_init(20,GL_POLYGON,GL_COMPILE_AND_EXECUTE);
578 else glCallList(sphereh);
583 int gr_ucircle(fix xc1,fix yc1,fix r1){//dunno if this really works, radar doesn't seem to.. hm..
585 c=grd_curcanv->cv_color;
586 OGL_DISABLE(TEXTURE_2D);
587 // glPointSize(f2glf(rad));
588 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
589 // glBegin(GL_POINTS);
590 // glVertex3f(f2glf(pnt->p3_vec.x),f2glf(pnt->p3_vec.y),-f2glf(pnt->p3_vec.z));
593 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)));
595 (f2fl(xc1)+grd_curcanv->cv_bitmap.bm_x)/(float)last_width,
596 1.0-(f2fl(yc1)+grd_curcanv->cv_bitmap.bm_y)/(float)last_height,0);
597 glScalef(f2fl(r1),f2fl(r1),f2fl(r1));
599 if (!circleh5) circleh5=circle_list_init(5,GL_LINE_LOOP,GL_COMPILE_AND_EXECUTE);
600 else glCallList(circleh5);
602 if (!circleh10) circleh10=circle_list_init(10,GL_LINE_LOOP,GL_COMPILE_AND_EXECUTE);
603 else glCallList(circleh10);
608 int gr_circle(fix xc1,fix yc1,fix r1){
609 return gr_ucircle(xc1,yc1,r1);
612 bool g3_draw_poly(int nv,g3s_point **pointlist)
616 c=grd_curcanv->cv_color;
617 // 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);
618 OGL_DISABLE(TEXTURE_2D);
619 glBegin(GL_TRIANGLE_FAN);
620 glColor3f(PAL2Tr(c),PAL2Tg(c),PAL2Tb(c));
622 // glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
623 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
629 void gr_upoly_tmap(int nverts, int *vert ){
630 mprintf((0,"gr_upoly_tmap: unhandled\n"));//should never get called
632 void draw_tmap_flat(grs_bitmap *bm,int nv,g3s_point **vertlist){
633 mprintf((0,"draw_tmap_flat: unhandled\n"));//should never get called
635 extern void (*tmap_drawer_ptr)(grs_bitmap *bm,int nv,g3s_point **vertlist);
636 bool g3_draw_tmap(int nv,g3s_point **pointlist,g3s_uvl *uvl_list,grs_bitmap *bm)
640 if (tmap_drawer_ptr==draw_tmap_flat){
641 /* fix average_light=0;
644 average_light += uvl_list[i].l;*/
645 OGL_DISABLE(TEXTURE_2D);
646 // glmprintf((0,"Gr_scanline_darkening_level=%i %f\n",Gr_scanline_darkening_level,Gr_scanline_darkening_level/(float)NUM_LIGHTING_LEVELS));
647 glColor4f(0,0,0,1.0-(Gr_scanline_darkening_level/(float)NUM_LIGHTING_LEVELS));
648 //glColor4f(0,0,0,f2fl(average_light/nv));
649 glBegin(GL_TRIANGLE_FAN);
651 // glColor4f(0,0,0,f2fl(uvl_list[c].l));
652 // glTexCoord2f(f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
653 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
656 }else if (tmap_drawer_ptr==draw_tmap){
658 /* if (bm->bm_w !=64||bm->bm_h!=64)
659 printf("g3_draw_tmap w %i h %i\n",bm->bm_w,bm->bm_h);*/
660 OGL_ENABLE(TEXTURE_2D);
662 ogl_texwrap(bm->gltexture,GL_REPEAT);
663 glBegin(GL_TRIANGLE_FAN);
665 if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
668 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
669 l=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));
674 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
678 mprintf((0,"g3_draw_tmap: unhandled tmap_drawer %p\n",tmap_drawer_ptr));
682 bool g3_draw_tmap_2(int nv,g3s_point **pointlist,g3s_uvl *uvl_list,grs_bitmap *bmbot,grs_bitmap *bm,int orient)
684 #if (defined(GL_ARB_multitexture) || defined(GL_SGIS_multitexture))
685 if (ogl_arb_multitexture_ok || ogl_sgis_multitexture_ok){
690 /* if (bm->bm_w !=64||bm->bm_h!=64)
691 printf("g3_draw_tmap w %i h %i\n",bm->bm_w,bm->bm_h);*/
692 if (ogl_arb_multitexture_ok){
693 #ifdef GL_ARB_multitexture
694 glActiveTextureARB(GL_TEXTURE0_ARB);
696 }else if (ogl_sgis_multitexture_ok){
697 #ifdef GL_SGIS_multitexture
698 glSelectTextureSGIS(GL_TEXTURE0_SGIS);
701 ogl_bindbmtex(bmbot);
702 OGL_ENABLE(TEXTURE_2D);
703 glEnable(GL_TEXTURE_2D);
704 ogl_texwrap(bmbot->gltexture,GL_REPEAT);
705 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
707 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
709 if (ogl_arb_multitexture_ok){
710 #ifdef GL_ARB_multitexture
711 glActiveTextureARB(GL_TEXTURE1_ARB);
713 }else if (ogl_sgis_multitexture_ok){
714 #ifdef GL_SGIS_multitexture
715 glSelectTextureSGIS(GL_TEXTURE1_SGIS);
719 // OGL_ENABLE(TEXTURE_2D);
720 glEnable(GL_TEXTURE_2D);
721 ogl_texwrap(bm->gltexture,GL_REPEAT);
722 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
725 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
727 glBegin(GL_TRIANGLE_FAN);
731 u1=1.0-f2glf(uvl_list[c].v);
732 v1=f2glf(uvl_list[c].u);
735 u1=1.0-f2glf(uvl_list[c].u);
736 v1=1.0-f2glf(uvl_list[c].v);
739 u1=f2glf(uvl_list[c].v);
740 v1=1.0-f2glf(uvl_list[c].u);
743 u1=f2glf(uvl_list[c].u);
744 v1=f2glf(uvl_list[c].v);
747 if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
750 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
751 l=f2fl(uvl_list[c].l);
754 // glTexCoord2f(f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
755 if (ogl_arb_multitexture_ok){
756 #ifdef GL_ARB_multitexture
757 glMultiTexCoord2fARB(GL_TEXTURE0_ARB,f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
759 }else if (ogl_sgis_multitexture_ok){
760 #ifdef GL_SGIS_multitexture
761 glMultiTexCoord2fSGIS(GL_TEXTURE0_SGIS,f2glf(uvl_list[c].u),f2glf(uvl_list[c].v));
764 if (ogl_arb_multitexture_ok){
765 #ifdef GL_ARB_multitexture
766 glMultiTexCoord2fARB(GL_TEXTURE1_ARB,u1,v1);
768 }else if (ogl_sgis_multitexture_ok){
769 #ifdef GL_SGIS_multitexture
770 glMultiTexCoord2fSGIS(GL_TEXTURE1_SGIS,u1,v1);
773 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
774 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
775 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
778 /* if (ogl_arb_multitexture_ok){
779 #ifdef GL_ARB_multitexture
780 glActiveTextureARB(GL_TEXTURE1_ARB);
782 }else if (ogl_sgis_multitexture_ok){
783 #ifdef GL_SGIS_multitexture
784 glSelectTextureSGIS(GL_TEXTURE1_SGIS);
787 // OGL_ENABLE(TEXTURE_2D);*/
788 glDisable(GL_TEXTURE_2D);
789 if (ogl_arb_multitexture_ok){
790 #ifdef GL_ARB_multitexture
791 glActiveTextureARB(GL_TEXTURE0_ARB);
793 }else if (ogl_sgis_multitexture_ok){
794 #ifdef GL_SGIS_multitexture
795 glSelectTextureSGIS(GL_TEXTURE0_SGIS);
804 g3_draw_tmap(nv,pointlist,uvl_list,bmbot);//draw the bottom texture first.. could be optimized with multitexturing..
807 /* if (bm->bm_w !=64||bm->bm_h!=64)
808 printf("g3_draw_tmap w %i h %i\n",bm->bm_w,bm->bm_h);*/
809 OGL_ENABLE(TEXTURE_2D);
811 ogl_texwrap(bm->gltexture,GL_REPEAT);
812 glBegin(GL_TRIANGLE_FAN);
816 u1=1.0-f2glf(uvl_list[c].v);
817 v1=f2glf(uvl_list[c].u);
820 u1=1.0-f2glf(uvl_list[c].u);
821 v1=1.0-f2glf(uvl_list[c].v);
824 u1=f2glf(uvl_list[c].v);
825 v1=1.0-f2glf(uvl_list[c].u);
828 u1=f2glf(uvl_list[c].u);
829 v1=f2glf(uvl_list[c].v);
832 if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
835 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
836 l=f2fl(uvl_list[c].l);
840 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
841 //glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),f2glf(pointlist[c]->p3_vec.z));
842 glVertex3f(f2glf(pointlist[c]->p3_vec.x),f2glf(pointlist[c]->p3_vec.y),-f2glf(pointlist[c]->p3_vec.z));
849 bool g3_draw_bitmap(vms_vector *pos,fix width,fix height,grs_bitmap *bm, int orientation)
852 vms_vector pv,v1;//,v2;
856 // printf("g3_draw_bitmap: %f,%f,%f - ",f2glf(pos->x),f2glf(pos->y),-f2glf(pos->z));
857 // printf("(%f,%f,%f) ",f2glf(View_position.x),f2glf(View_position.y),-f2glf(View_position.z));
859 OGL_ENABLE(TEXTURE_2D);
861 ogl_texwrap(bm->gltexture,GL_CLAMP);
864 glColor3f(1.0,1.0,1.0);
865 width = fixmul(width,Matrix_scale.x);
866 height = fixmul(height,Matrix_scale.y);
868 // g3_rotate_point(&p[i],pos);
869 vm_vec_sub(&v1,pos,&View_position);
870 vm_vec_rotate(&pv,&v1,&View_matrix);
871 // printf(" %f,%f,%f->",f2glf(pv.x),f2glf(pv.y),-f2glf(pv.z));
874 glTexCoord2f(0.0, 0.0);
879 glTexCoord2f(bm->gltexture->u, 0.0);
884 glTexCoord2f(bm->gltexture->u, bm->gltexture->v);
889 glTexCoord2f(0.0, bm->gltexture->v);
894 // vm_vec_rotate(&v2,&v1,&View_matrix);
895 // vm_vec_sub(&v1,&v2,&pv);
896 //vm_vec_sub(&v1,&pv,&v2);
897 // vm_vec_sub(&v2,&pv,&v1);
898 glVertex3f(f2glf(pv.x),f2glf(pv.y),-f2glf(pv.z));
899 // printf("%f,%f,%f ",f2glf(v1.x),f2glf(v1.y),-f2glf(v1.z));
906 bool ogl_ubitmapm_c(int x, int y,grs_bitmap *bm,int c)
911 x+=grd_curcanv->cv_bitmap.bm_x;
912 y+=grd_curcanv->cv_bitmap.bm_y;
913 xo=x/(float)last_width;
914 xf=(bm->bm_w+x)/(float)last_width;
915 yo=1.0-y/(float)last_height;
916 yf=1.0-(bm->bm_h+y)/(float)last_height;
918 // printf("g3_draw_bitmap: %f,%f,%f - ",f2glf(pos->x),f2glf(pos->y),-f2glf(pos->z));
919 // printf("(%f,%f,%f) ",f2glf(View_position.x),f2glf(View_position.y),-f2glf(View_position.z));
921 /* glEnABLE(ALPHA_TEST);
922 glAlphaFunc(GL_GREATER,0.0);*/
924 OGL_ENABLE(TEXTURE_2D);
926 ogl_texwrap(bm->gltexture,GL_CLAMP);
930 if (bm->bm_w==bm->gltexture->w)
933 u2=(bm->bm_w+bm->bm_x)/(float)bm->gltexture->tw;
935 u1=bm->bm_x/(float)bm->gltexture->tw;
936 u2=(bm->bm_w+bm->bm_x)/(float)bm->gltexture->tw;
940 if (bm->bm_h==bm->gltexture->h)
943 v2=(bm->bm_h+bm->bm_y)/(float)bm->gltexture->th;
945 v1=bm->bm_y/(float)bm->gltexture->th;
946 v2=(bm->bm_h+bm->bm_y)/(float)bm->gltexture->th;
951 glColor3f(1.0,1.0,1.0);
953 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
954 glTexCoord2f(u1, v1); glVertex2f(xo, yo);
955 glTexCoord2f(u2, v1); glVertex2f(xf, yo);
956 glTexCoord2f(u2, v2); glVertex2f(xf, yf);
957 glTexCoord2f(u1, v2); glVertex2f(xo, yf);
959 // glDisABLE(ALPHA_TEST);
963 bool ogl_ubitmapm(int x, int y,grs_bitmap *bm){
964 return ogl_ubitmapm_c(x,y,bm,-1);
965 // return ogl_ubitblt(bm->bm_w,bm->bm_h,x,y,0,0,bm,NULL);
968 //also upsidedown, currently.
969 bool ogl_ubitblt(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest)
971 GLfloat xo,yo;//,xs,ys;
972 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));
977 xo=dx/(float)last_width;
978 // xo=dx/(float)grd_curscreen->sc_w;
979 // xs=w/(float)last_width;
980 //yo=1.0-dy/(float)last_height;
981 yo=1.0-(dy+h)/(float)last_height;
982 // ys=h/(float)last_height;
984 // OGL_ENABLE(TEXTURE_2D);
986 OGL_DISABLE(TEXTURE_2D);
987 glRasterPos2f(xo,yo);
988 ogl_filltexbuf(src->bm_data,texbuf,src->bm_w,w,h,sx,sy,w,h,GL_RGBA);
989 glDrawPixels(w,h,GL_RGBA,GL_UNSIGNED_BYTE,texbuf);
995 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)
998 GLfloat u1,v1;//,u2,v2;
1000 // unsigned char *oldpal;
1003 ogl_init_texture(&tex);
1005 tex.prio=0.0;tex.wantmip=0;
1006 tex.lw=src->bm_rowsize;
1008 /* if (w==src->bm_w && sx==0){
1011 u1=sx/(float)src->bm_w*src->glu;
1012 u2=w/(float)src->bm_w*src->glu+u1;
1014 if (h==src->bm_h && sy==0){
1017 v1=sy/(float)src->bm_h*src->glv;
1018 v2=h/(float)src->bm_h*src->glv+v1;
1024 xo=dx/(float)last_width;
1025 xs=dw/(float)last_width;
1026 yo=1.0-dy/(float)last_height;
1027 ys=dh/(float)last_height;
1029 OGL_ENABLE(TEXTURE_2D);
1032 ogl_pal=gr_current_pal;
1033 ogl_loadtexture(src->bm_data,sx,sy,&tex);
1036 OGL_BINDTEXTURE(tex.handle);
1038 ogl_texwrap(&tex,GL_CLAMP);
1041 glColor3f(1.0,1.0,1.0);
1042 glTexCoord2f(u1, v1); glVertex2f(xo, yo);
1043 glTexCoord2f(tex.u, v1); glVertex2f(xo+xs, yo);
1044 glTexCoord2f(tex.u, tex.v); glVertex2f(xo+xs, yo-ys);
1045 glTexCoord2f(u1, tex.v); glVertex2f(xo, yo-ys);
1047 ogl_freetexture(&tex);
1050 bool ogl_ubitblt(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1051 return ogl_ubitblt_i(w,h,dx,dy,w,h,sx,sy,src,dest);
1054 bool ogl_ubitblt_tolinear(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1056 unsigned char *d,*s;
1060 w1=grd_curscreen->sc_w;h1=grd_curscreen->sc_h;
1061 if (w1*h1*3>OGLTEXBUFSIZE)
1062 Error("ogl_ubitblt_tolinear: screen res larger than OGLTEXBUFSIZE\n");
1064 if (ogl_readpixels_ok>0){
1065 OGL_DISABLE(TEXTURE_2D);
1066 glReadBuffer(GL_FRONT);
1067 glReadPixels(0,0,w1,h1,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1068 // glReadPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1069 // glReadPixels(sx,sy,w+sx,h+sy,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1071 memset(texbuf,0,w1*h1*3);
1075 d=dest->bm_data+dx+(dy+i)*dest->bm_rowsize;
1076 s=texbuf+((h1-(i+sy+1))*w1+sx)*3;
1078 *d=gr_find_closest_color(s[0]/4,s[1]/4,s[2]/4);
1085 unsigned char *d,*s,*e;
1086 if (w*h*3>OGLTEXBUFSIZE)
1087 Error("ogl_ubitblt_tolinear: size larger than OGLTEXBUFSIZE\n");
1090 #if 1//also seems to cause a mess. need to look into it a bit more..
1091 if (ogl_readpixels_ok>0){
1092 OGL_DISABLE(TEXTURE_2D);
1093 glReadBuffer(GL_FRONT);
1094 // glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1095 glReadPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1098 memset(texbuf,0,w*h*3);
1099 // d=dest->bm_data+dx+(dy+i)*dest->bm_rowsize;
1100 d=dest->bm_data+dx+dy*dest->bm_rowsize;
1102 s=texbuf+w*(h-(i+1))*3;
1104 if (s<texbuf){Error("blah1\n");}
1105 if (d<dest->bm_data){Error("blah3\n");}
1106 // d=dest->bm_data+(i*dest->bm_rowsize);
1110 if (s>texbuf+w*h*3-3){Error("blah2\n");}
1111 if (d>dest->bm_data+dest->bm_rowsize*(h+dy)+dx ){Error("blah4\n");}
1112 *d=gr_find_closest_color(s[0]/4,s[1]/4,s[2]/4);
1118 d+=dest->bm_rowsize;
1120 glmprintf((0,"c=%i w*h=%i\n",c,w*h));
1125 bool ogl_ubitblt_copy(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1126 #if 0 //just seems to cause a mess.
1127 GLfloat xo,yo;//,xs,ys;
1132 // xo=dx/(float)last_width;
1133 xo=dx/(float)grd_curscreen->sc_w;
1134 // yo=1.0-(dy+h)/(float)last_height;
1135 yo=1.0-(dy+h)/(float)grd_curscreen->sc_h;
1138 OGL_DISABLE(TEXTURE_2D);
1139 glReadBuffer(GL_FRONT);
1140 glRasterPos2f(xo,yo);
1141 // glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1142 glCopyPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_COLOR);
1148 void ogl_start_frame(void){
1149 r_polyc=0;r_tpolyc=0;r_bitmapc=0;r_ubitmapc=0;r_ubitbltc=0;r_upixelc=0;
1150 // gl_badtexture=500;
1152 OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,Canvas_width,Canvas_height);
1153 glClearColor(0.0, 0.0, 0.0, 0.0);
1154 // glEnable(GL_ALPHA_TEST);
1155 // glAlphaFunc(GL_GREATER,0.01);
1156 glShadeModel(GL_SMOOTH);
1157 glMatrixMode(GL_PROJECTION);
1158 glLoadIdentity();//clear matrix
1159 //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),1.0,1000000.0);
1160 //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),0.01,1000000.0);
1161 gluPerspective(90.0,1.0,0.01,1000000.0);
1162 //gluPerspective(90.0,(GLfloat)(Canvas_width*3)/(GLfloat)(Canvas_height*4),0.01,1000000.0);
1163 // gluPerspective(75.0,(GLfloat)Canvas_width/(GLfloat)Canvas_height,1.0,1000000.0);
1164 glMatrixMode(GL_MODELVIEW);
1165 glLoadIdentity();//clear matrix
1167 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1168 // glDisABLE(DITHER);
1169 // glScalef(1.0,1.0,-1.0);
1170 // glScalef(1.0,1.0,-1.0);
1173 // glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1174 // OGL_TEXENV(GL_TEXTURE_ENV_MODE,GL_MODULATE);
1175 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_texmagfilt);
1176 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_texminfilt);
1177 // OGL_TEXPARAM(GL_TEXTURE_MAG_FILTER,GL_texmagfilt);
1178 // OGL_TEXPARAM(GL_TEXTURE_MIN_FILTER,GL_texminfilt);
1179 // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1180 // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1183 void merge_textures_stats(void);
1185 void ogl_end_frame(void){
1186 // OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,);
1187 OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
1189 // merge_textures_stats();
1190 // ogl_texture_stats();
1192 // glViewport(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
1193 glMatrixMode(GL_PROJECTION);
1194 glLoadIdentity();//clear matrix
1195 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
1196 glMatrixMode(GL_MODELVIEW);
1197 glLoadIdentity();//clear matrix
1198 // glDisABLE(BLEND);
1199 //glDisABLE(ALPHA_TEST);
1200 //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),1.0,1000000.0);
1201 // ogl_swap_buffers();//platform specific code
1202 // glClear(GL_COLOR_BUFFER_BIT);
1205 void ogl_swap_buffers(void){
1206 ogl_clean_texture_cache();
1207 if (gr_renderstats){
1208 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);
1209 // 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
1212 ogl_swap_buffers_internal();
1213 glClear(GL_COLOR_BUFFER_BIT);
1216 int tex_format_supported(int iformat,int format){
1219 if (!ogl_intensity4_ok) return 0; break;
1220 case GL_LUMINANCE4_ALPHA4:
1221 if (!ogl_luminance4_alpha4_ok) return 0; break;
1223 if (!ogl_rgba2_ok) return 0; break;
1225 if (ogl_gettexlevelparam_ok){
1227 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, iformat, 64, 64, 0,
1228 format, GL_UNSIGNED_BYTE, texbuf);//NULL?
1229 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0,
1230 GL_TEXTURE_INTERNAL_FORMAT,
1232 return (internalFormat==iformat);
1237 //little hack to find the largest or equal multiple of 2 for a given number
1240 for (i=2;i<=4096;i*=2)
1245 //GLubyte texbuf[512*512*4];
1246 GLubyte texbuf[OGLTEXBUFSIZE];
1247 void ogl_filltexbuf(unsigned char *data,GLubyte *texp,int truewidth,int width,int height,int dxo,int dyo,int twidth,int theight,int type)
1249 // GLushort *tex=(GLushort *)texp;
1251 if (twidth*theight*4>sizeof(texbuf))//shouldn't happen, descent never uses textures that big.
1252 Error("texture toobig %i %i",twidth,theight);
1255 for (y=0;y<theight;y++){
1256 i=dxo+truewidth*(y+dyo);
1257 for (x=0;x<twidth;x++){
1258 if (x<width && y<height)
1261 c=255;//fill the pad space with transparancy
1267 case GL_LUMINANCE_ALPHA:
1275 (*(texp++))=0;//transparent pixel
1281 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. :)
1284 case GL_LUMINANCE_ALPHA:
1289 //(*(texp++))=gr_palette[c*3]*4;
1290 //(*(texp++))=gr_palette[c*3+1]*4;
1291 //(*(texp++))=gr_palette[c*3+2]*4;
1292 (*(texp++))=ogl_pal[c*3]*4;
1293 (*(texp++))=ogl_pal[c*3+1]*4;
1294 (*(texp++))=ogl_pal[c*3+2]*4;
1295 (*(texp++))=255;//not transparent
1296 // (*(tex++))=(ogl_pal[c*3]>>1) + ((ogl_pal[c*3+1]>>1)<<5) + ((ogl_pal[c*3+2]>>1)<<10) + (1<<15);
1303 int tex_format_verify(ogl_texture *tex){
1304 while (!tex_format_supported(tex->internalformat,tex->format)){
1305 glmprintf((0,"tex format %x not supported",tex->internalformat));
1306 switch (tex->internalformat){
1308 if (ogl_luminance4_alpha4_ok){
1309 tex->internalformat=GL_LUMINANCE4_ALPHA4;
1310 tex->format=GL_LUMINANCE_ALPHA;
1312 }//note how it will fall through here if the statement is false
1313 case GL_LUMINANCE4_ALPHA4:
1315 tex->internalformat=GL_RGBA2;
1316 tex->format=GL_RGBA;
1318 }//note how it will fall through here if the statement is false
1320 tex->internalformat=ogl_rgba_format;
1321 tex->format=GL_RGBA;
1324 mprintf((0,"...no tex format to fall back on\n"));
1327 glmprintf((0,"...falling back to %x\n",tex->internalformat));
1331 void tex_set_size1(ogl_texture *tex,int dbits,int bits,int w, int h){
1333 if (tex->tw!=w || tex->th!=h){
1334 u=(tex->w/(float)tex->tw*w) * (tex->h/(float)tex->th*h);
1335 glmprintf((0,"shrunken texture?\n"));
1338 if (bits<=0){//the beta nvidia GLX server. doesn't ever return any bit sizes, so just use some assumptions.
1339 tex->bytes=((float)w*h*dbits)/8.0;
1340 tex->bytesu=((float)u*dbits)/8.0;
1342 tex->bytes=((float)w*h*bits)/8.0;
1343 tex->bytesu=((float)u*bits)/8.0;
1345 glmprintf((0,"tex_set_size1: %ix%i, %ib(%i) %iB\n",w,h,bits,dbits,tex->bytes));
1347 void tex_set_size(ogl_texture *tex){
1349 if (ogl_gettexlevelparam_ok){
1351 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&w);
1352 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&h);
1353 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_LUMINANCE_SIZE,&t);a+=t;
1354 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_INTENSITY_SIZE,&t);a+=t;
1355 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_RED_SIZE,&t);a+=t;
1356 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_GREEN_SIZE,&t);a+=t;
1357 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_BLUE_SIZE,&t);a+=t;
1358 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_ALPHA_SIZE,&t);a+=t;
1363 switch (tex->format){
1367 case GL_LUMINANCE_ALPHA:
1374 Error("tex_set_size unknown texformat\n");
1377 tex_set_size1(tex,bi,a,w,h);
1379 //loads a palettized bitmap into a ogl RGBA texture.
1380 //Sizes and pads dimensions to multiples of 2 if necessary.
1381 //In theory this could be a problem for repeating textures, but all real
1382 //textures (not sprites, etc) in descent are 64x64, so we are ok.
1383 //stores OpenGL textured id in *texid and u/v values required to get only the real data in *u/*v
1384 void ogl_loadtexture(unsigned char * data, int dxo,int dyo, ogl_texture *tex){
1385 //void ogl_loadtexture(unsigned char * data, int width, int height,int dxo,int dyo, int *texid,float *u,float *v,char domipmap,float prio){
1386 // int internalformat=GL_RGBA;
1387 // int format=GL_RGBA;
1389 tex->tw=pow2ize(tex->w);tex->th=pow2ize(tex->h);//calculate smallest texture size that can accomodate us (must be multiples of 2)
1390 // tex->tw=tex->w;tex->th=tex->h;//feeling lucky?
1392 if(gr_badtexture>0) return;
1394 if (tex_format_verify(tex))
1397 //calculate u/v values that would make the resulting texture correctly sized
1398 tex->u=(float)tex->w/(float)tex->tw;
1399 tex->v=(float)tex->h/(float)tex->th;
1401 // if (width!=twidth || height!=theight)
1402 // glmprintf((0,"sizing %ix%i texture up to %ix%i\n",width,height,twidth,theight));
1403 ogl_filltexbuf(data,texbuf,tex->lw,tex->w,tex->h,dxo,dyo,tex->tw,tex->th,tex->format);
1405 // Generate OpenGL texture IDs.
1406 glGenTextures(1, &tex->handle);
1409 glPrioritizeTextures(1,&tex->handle,&tex->prio);
1411 // Give our data to OpenGL.
1413 OGL_BINDTEXTURE(tex->handle);
1415 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1417 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_texmagfilt);
1418 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_texminfilt);
1420 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1421 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1423 // domipmap=0;//mipmaps aren't used in GL_NEAREST anyway, and making the mipmaps is pretty slow
1424 //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.
1426 if (tex->wantmip && GL_needmipmaps)
1427 gluBuild2DMipmaps( GL_TEXTURE_2D, tex->internalformat, tex->tw,
1428 tex->th, tex->format, GL_UNSIGNED_BYTE, texbuf);
1430 glTexImage2D(GL_TEXTURE_2D, 0, tex->internalformat,
1431 tex->tw, tex->th, 0, tex->format, // RGBA textures.
1432 GL_UNSIGNED_BYTE, // imageData is a GLubyte pointer.
1438 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));
1441 unsigned char decodebuf[512*512];
1442 void ogl_loadbmtexture_m(grs_bitmap *bm,int domipmap){
1444 while (bm->bm_parent)
1447 if (bm->gltexture==NULL){
1448 ogl_init_texture(bm->gltexture=ogl_get_free_texture());
1449 bm->gltexture->lw=bm->bm_w;
1450 bm->gltexture->w=bm->bm_w;
1451 bm->gltexture->h=bm->bm_h;
1452 bm->gltexture->wantmip=domipmap;
1455 if (bm->gltexture->handle>0)
1457 if (bm->gltexture->w==0){
1458 bm->gltexture->lw=bm->bm_w;
1459 bm->gltexture->w=bm->bm_w;
1460 bm->gltexture->h=bm->bm_h;
1463 if (bm->bm_flags & BM_FLAG_RLE){
1464 unsigned char * dbits;
1465 unsigned char * sbits;
1467 sbits = &bm->bm_data[4 + bm->bm_h];
1470 for (i=0; i < bm->bm_h; i++ ) {
1471 gr_rle_decode(sbits,dbits);
1472 sbits += (int)bm->bm_data[4+i];
1477 ogl_loadtexture(buf,0,0,bm->gltexture);
1479 void ogl_loadbmtexture(grs_bitmap *bm){
1480 ogl_loadbmtexture_m(bm,1);
1482 void ogl_freetexture(ogl_texture *gltexture){
1483 if (gltexture->handle>0){
1485 glmprintf((0,"ogl_freetexture(%p):%i (last rend %is) (%i left)\n",gltexture,gltexture->handle,(GameTime-gltexture->lastrend)/f1_0,r_texcount));
1486 glDeleteTextures( 1, &gltexture->handle );
1487 // gltexture->handle=0;
1488 ogl_init_texture(gltexture);
1491 void ogl_freebmtexture(grs_bitmap *bm){
1493 ogl_freetexture(bm->gltexture);
1496 // glmprintf((0,"ogl_freebmtexture(%p,%p):%i (%i left)\n",bm->bm_data,&bm->gltexture,bm->gltexture,r_texcount));
1497 // glDeleteTextures( 1, &bm->gltexture );
1498 // bm->gltexture=-1;