]> icculus.org git repositories - btb/d2x.git/blob - arch/ogl/ogl.c
OS X stuff
[btb/d2x.git] / arch / ogl / ogl.c
1 /* $ Id: $ */
2 /*
3  *
4  * Graphics support functions for OpenGL.
5  *
6  *
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <conf.h>
11 #endif
12
13 //#include <stdio.h>
14 #ifdef __WINDOWS__
15 #include <windows.h>
16 #include <stddef.h>
17 #endif
18 #ifdef __MACOSX__
19 #include <OpenGL/gl.h>
20 #include <OpenGL/glu.h>
21 #else
22 #include <GL/gl.h>
23 #include <GL/glu.h>
24 #endif
25 #include <string.h>
26 #include <math.h>
27
28 #include "ogl_init.h"
29 #include "3d.h"
30 #include "piggy.h"
31 #include "../../3d/globvars.h"
32 #include "error.h"
33 #include "texmap.h"
34 #include "palette.h"
35 #include "rle.h"
36 #include "mono.h"
37
38 #include "segment.h"
39 #include "textures.h"
40 #include "texmerge.h"
41 #include "effects.h"
42 #include "weapon.h"
43 #include "powerup.h"
44 #include "polyobj.h"
45 #include "gamefont.h"
46
47 //change to 1 for lots of spew.
48 #if 0
49 #define glmprintf(a) mprintf(a)
50 #else
51 #define glmprintf(a)
52 #endif
53
54 #ifndef M_PI
55 #define M_PI 3.14159
56 #endif
57
58 #if defined(__WINDOWS__) || defined(__MINGW32__)
59 #define cosf(a) cos(a)
60 #define sinf(a) sin(a)
61 #endif
62
63 unsigned char *ogl_pal=gr_palette;
64
65 int GL_texmagfilt=GL_NEAREST;
66 int GL_texminfilt=GL_NEAREST;
67 int GL_needmipmaps=0;
68
69 int last_width=-1,last_height=-1;
70 int GL_TEXTURE_2D_enabled=-1;
71 int GL_texclamp_enabled=-1;
72
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;
79 int ogl_rgba2_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;
84 #endif
85 #ifdef GL_SGIS_multitexture
86 int ogl_sgis_multitexture_ok=0;
87 #endif
88
89 int sphereh=0;
90 int circleh5=0;
91 int circleh10=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};
95 /*int lastbound=-1;
96
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);
101
102
103 ogl_texture ogl_texture_list[OGL_TEXTURE_LIST_SIZE];
104 int ogl_texture_list_cur;
105
106 void ogl_init_texture_stats(ogl_texture* t){
107         t->prio=0.3;//default prio
108         t->lastrend=0;
109         t->numrend=0;
110 }
111 void ogl_init_texture(ogl_texture* t){
112         t->handle=0;
113         t->internalformat=ogl_rgba_format;
114         t->format=GL_RGBA;
115         t->wrapstate=-1;
116         t->w=t->h=0;
117         ogl_init_texture_stats(t);
118 }
119 void ogl_reset_texture_stats_internal(void){
120         int i;
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]);
124                 }
125 }
126 void ogl_init_texture_list_internal(void){
127         int i;
128         ogl_texture_list_cur=0;
129         for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++)
130                 ogl_init_texture(&ogl_texture_list[i]);
131 }
132 void ogl_smash_texture_list_internal(void){
133         int i;
134         sphereh=0;
135         circleh5=0;
136         circleh10=0;
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;
144                 }
145                 ogl_texture_list[i].wrapstate=-1;
146         }
147 }
148 void ogl_vivify_texture_list_internal(void){
149 /*
150    int i;
151         ogl_texture* t;
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);
156         }
157 */
158 }
159
160 ogl_texture* ogl_get_free_texture(void){
161         int i;
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;
167         }
168         Error("OGL: texture list full!\n");
169 //      return NULL;
170 }
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;
174 //      int grabbed=0;
175         ogl_texture* t;
176         for (i=0;i<OGL_TEXTURE_LIST_SIZE;i++){
177                 t=&ogl_texture_list[i];
178                 if (t->handle>0){
179                         used++;
180                         datatexel+=t->w*t->h;
181                         truetexel+=t->tw*t->th;
182                         databytes+=t->bytesu;
183                         truebytes+=t->bytes;
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++;
188                         else prioh++;
189                 }
190 //              else if(t->w!=0)
191 //                      grabbed++;
192         }
193         if (gr_renderstats){
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);
195         }
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));
197         return truebytes;
198 }
199 int ogl_mem_target=-1;
200 void ogl_clean_texture_cache(void){
201         ogl_texture* t;
202         int i,bytes;
203         int time=120;
204         
205         if (ogl_mem_target<0){
206                 if (gr_renderstats)
207                         ogl_texture_stats();
208                 return;
209         }
210         
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];
215                         if (t->handle>0){
216                                 if (t->lastrend+f1_0*time<GameTime){
217                                         ogl_freetexture(t);
218                                         bytes-=t->bytes;
219                                         if (bytes<ogl_mem_target)
220                                                 return;
221                                 }
222                         }
223                 }
224                 if (time==0)
225                         Error("not enough mem?");
226                 time=time/2;
227         }
228         
229 }
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);
241 //      }
242 }
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;
249         }
250 }
251
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
256 //TODO: doors
257
258 void ogl_cache_polymodel_textures(int model_num){
259         polymodel *po=&Polygon_models[model_num];
260         int i;
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]);
264         }
265 }
266 void ogl_cache_vclip_textures(vclip *vc){
267         int i;
268         for (i=0;i<vc->num_frames;i++){
269                 PIGGY_PAGE_IN(vc->frames[i]);
270                 ogl_loadbmtexture(&GameBitmaps[vc->frames[i].index]);
271         }
272 }
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);
282 }
283 void ogl_cache_level_textures(void){
284         int seg,side,i;
285         eclip *ec;
286         short tmap1,tmap2;
287         grs_bitmap *bm,*bm2;
288         struct side *sidep;
289         int max_efx=0,ef;
290         
291         ogl_reset_texture_stats_internal();//loading a new lev should reset textures
292         
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) )
295                         continue;
296                 if (ec->vc.num_frames>max_efx)
297                         max_efx=ec->vc.num_frames;
298         }
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) )
303                                 continue;
304 //                      if (ec->vc.num_frames>max_efx)
305 //                              max_efx=ec->vc.num_frames;
306                         ec->time_left=-1;
307                 }
308                 do_special_effects();
309
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));
317                                         //                              tmap1=0;
318                                         continue;
319                                 }
320                                 PIGGY_PAGE_IN(Textures[tmap1]);
321                                 bm = &GameBitmaps[Textures[tmap1].index];
322                                 if (tmap2 != 0){
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 );
327                                         else {
328                                                 ogl_loadbmtexture(bm2);
329                                         }
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));
331                                 }
332                                 ogl_loadbmtexture(bm);
333                         }
334                 }
335                 glmprintf((0,"finished ef:%i\n",ef));
336         }
337         reset_special_effects();
338         init_special_effects();
339         {
340 //              int laserlev=1;
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){
348 /*                                      case POW_LASER:
349                                                 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[LASER_INDEX]]);
350 //                                              if (laserlev<4)
351 //                                                      laserlev++;
352                                                 break;*/
353                                         case POW_VULCAN_WEAPON:
354                                                 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[VULCAN_INDEX]]);
355                                                 break;
356                                         case POW_SPREADFIRE_WEAPON:
357                                                 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[SPREADFIRE_INDEX]]);
358                                                 break;
359                                         case POW_PLASMA_WEAPON:
360                                                 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[PLASMA_INDEX]]);
361                                                 break;
362                                         case POW_FUSION_WEAPON:
363                                                 ogl_cache_weapon_textures(&Weapon_info[Primary_weapon_to_weapon_info[FUSION_INDEX]]);
364                                                 break;
365 /*                                      case POW_MISSILE_1:
366                                         case POW_MISSILE_4:
367                                                 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[CONCUSSION_INDEX]]);
368                                                 break;*/
369                                         case POW_PROXIMITY_WEAPON:
370                                                 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[PROXIMITY_INDEX]]);
371                                                 break;
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]]);
375                                                 break;
376                                         case POW_SMARTBOMB_WEAPON:
377                                                 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[SMART_INDEX]]);
378                                                 break;
379                                         case POW_MEGA_WEAPON:
380                                                 ogl_cache_weapon_textures(&Weapon_info[Secondary_weapon_to_weapon_info[MEGA_INDEX]]);
381                                                 break;
382                                 }
383                         }
384                         else if(Objects[i].render_type==RT_POLYOBJ){
385                                 ogl_cache_polymodel_textures(Objects[i].rtype.pobj_info.model_num);
386                         }
387                 }
388         }
389         glmprintf((0,"finished caching\n"));
390 }
391
392 int r_polyc,r_tpolyc,r_bitmapc,r_ubitmapc,r_ubitbltc,r_upixelc;
393 int r_texcount=0;
394 #define f2glf(x) (f2fl(x))
395
396 bool g3_draw_line(g3s_point *p0,g3s_point *p1)
397 {
398         int c;
399         c=grd_curcanv->cv_color;
400         OGL_DISABLE(TEXTURE_2D);
401         glColor3f(PAL2Tr(c),PAL2Tg(c),PAL2Tb(c));
402         glBegin(GL_LINES);
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));
405         glEnd();
406         return 1;
407 }
408 void ogl_drawcircle2(int nsides,int type,float xsc,float xo,float ysc,float yo){
409         int i;
410         float ang;
411         glBegin(type);
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);
415         }
416         glEnd();
417 }
418 void ogl_drawcircle(int nsides,int type){
419         int i;
420         float ang;
421         glBegin(type);
422         for (i=0; i<nsides; i++) {
423                 ang = 2.0*M_PI*i/nsides;
424                 glVertex2f(cosf(ang),sinf(ang));
425         }
426         glEnd();
427 }
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);
433         glEndList();
434         return hand;
435 }
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;
441         glPushMatrix();
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.
445         
446         OGL_DISABLE(TEXTURE_2D);
447
448         if (!cross_lh[cross]){
449                 cross_lh[cross]=glGenLists(1);
450                 glNewList(cross_lh[cross], GL_COMPILE_AND_EXECUTE);
451                 glBegin(GL_LINES);
452                 //cross top left
453                 glColor3fv(darker_g);
454                 glVertex2f(-4.0,4.0);
455                 if (cross)
456                         glColor3fv(bright_g);
457                 else
458                         glColor3fv(dark_g);
459                 glVertex2f(-2.0,2.0);
460
461                 //cross bottom left
462                 glColor3fv(dark_g);
463                 glVertex2f(-3.0,-2.0);
464                 if (cross)
465                         glColor3fv(bright_g);
466                 glVertex2f(-2.0,-1.0);
467
468                 //cross top right
469                 glColor3fv(darker_g);
470                 glVertex2f(4.0,4.0);
471                 if (cross)
472                         glColor3fv(bright_g);
473                 else
474                         glColor3fv(dark_g);
475                 glVertex2f(2.0,2.0);
476
477                 //cross bottom right
478                 glColor3fv(dark_g);
479                 glVertex2f(3.0,-2.0);
480                 if (cross)
481                         glColor3fv(bright_g);
482                 glVertex2f(2.0,-1.0);
483
484                 glEnd();
485                 glEndList();
486         }else
487                 glCallList(cross_lh[cross]);
488
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);
494
495                 glColor3fv(dark_g);
496                 glBegin(GL_LINES);
497                 //left primary bar
498                 glVertex2f(-14.0,-8.0);
499                 glVertex2f(-8.0,-5.0);
500                 //right primary bar
501                 glVertex2f(14.0,-8.0);
502                 glVertex2f(8.0,-5.0);
503                 glEnd();
504                 if (primary==0)
505                         glColor3fv(dark_g);
506                 else
507                         glColor3fv(bright_g);
508                 //left upper
509                 ogl_drawcircle2(6,GL_POLYGON,1.5,-7.0,1.5,-5.0);
510                 //right upper
511                 ogl_drawcircle2(6,GL_POLYGON,1.5,7.0,1.5,-5.0);
512                 if (primary!=2)
513                         glColor3fv(dark_g);
514                 else
515                         glColor3fv(bright_g);
516                 //left lower
517                 ogl_drawcircle2(4,GL_POLYGON,1.0,-14.0,1.0,-8.0);
518                 //right lower
519                 ogl_drawcircle2(4,GL_POLYGON,1.0,14.0,1.0,-8.0);
520
521                 glEndList();
522         }else
523                 glCallList(primary_lh[primary]);
524 //      if (Canvas_height>200)
525 //              glLineWidth(1);
526
527         if (!secondary_lh[secondary]){
528                 secondary_lh[secondary]=glGenLists(1);
529                 glNewList(secondary_lh[secondary], GL_COMPILE_AND_EXECUTE);
530                 if (secondary<=2){
531                         //left secondary
532                         if (secondary!=1)
533                                 glColor3fv(darker_g);
534                         else
535                                 glColor3fv(bright_g);
536                         ogl_drawcircle2(8,GL_LINE_LOOP,2.0,-10.0,2.0,-1.0);
537                         //right secondary
538                         if (secondary!=2)
539                                 glColor3fv(darker_g);
540                         else
541                                 glColor3fv(bright_g);
542                         ogl_drawcircle2(8,GL_LINE_LOOP,2.0,10.0,2.0,-1.0);
543                 }else{
544                         //bottom/middle secondary
545                         if (secondary!=4)
546                                 glColor3fv(darker_g);
547                         else
548                                 glColor3fv(bright_g);
549                         ogl_drawcircle2(8,GL_LINE_LOOP,2.0,0.0,2.0,-7.0);
550                 }
551                 glEndList();
552         }else
553                 glCallList(secondary_lh[secondary]);
554
555         glPopMatrix();
556 }
557 int g3_draw_sphere(g3s_point *pnt,fix rad){
558         int c;
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));
565 //      glEnd();
566         glPushMatrix();
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);
571         glPopMatrix();
572         return 0;
573
574 }
575 int gr_ucircle(fix xc1,fix yc1,fix r1){//dunno if this really works, radar doesn't seem to.. hm..
576         int c;
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));
583 //      glEnd();
584         glPushMatrix();
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)));
586         glTranslatef(
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));
590         if (r1<=i2f(5)){
591                 if (!circleh5) circleh5=circle_list_init(5,GL_LINE_LOOP,GL_COMPILE_AND_EXECUTE);
592                 else glCallList(circleh5);
593         }else{
594                 if (!circleh10) circleh10=circle_list_init(10,GL_LINE_LOOP,GL_COMPILE_AND_EXECUTE);
595                 else glCallList(circleh10);
596         }
597         glPopMatrix();
598         return 0;
599 }
600 int gr_circle(fix xc1,fix yc1,fix r1){
601         return gr_ucircle(xc1,yc1,r1);
602 }
603
604 bool g3_draw_poly(int nv,g3s_point **pointlist)
605 {
606         int c;
607         r_polyc++;
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));
613         for (c=0;c<nv;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));
616         }
617         glEnd();
618         return 0;
619 }
620
621 void gr_upoly_tmap(int nverts, int *vert ){
622                 mprintf((0,"gr_upoly_tmap: unhandled\n"));//should never get called
623 }
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
626 }
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)
629 {
630         int c;
631         float l;
632         if (tmap_drawer_ptr==draw_tmap_flat){
633 /*              fix average_light=0;
634                 int i;
635                 for (i=0; i<nv; i++)
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);
642                 for (c=0;c<nv;c++){
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));
646                 }
647                 glEnd();
648         }else if (tmap_drawer_ptr==draw_tmap){
649                 r_tpolyc++;
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);
653                 ogl_bindbmtex(bm);
654                 ogl_texwrap(bm->gltexture,GL_REPEAT);
655                 glBegin(GL_TRIANGLE_FAN);
656                 for (c=0;c<nv;c++){
657                         if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
658                                 l=1.0;
659                         }else{
660                                 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
661                                 l=f2fl(uvl_list[c].l);
662                         }
663                         glColor3f(l,l,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));
667                 }
668                 glEnd();
669         }else{
670                 mprintf((0,"g3_draw_tmap: unhandled tmap_drawer %p\n",tmap_drawer_ptr));
671         }
672         return 0;
673 }
674 bool g3_draw_tmap_2(int nv,g3s_point **pointlist,g3s_uvl *uvl_list,grs_bitmap *bmbot,grs_bitmap *bm,int orient)
675 {
676 #if (defined(GL_ARB_multitexture) || defined(GL_SGIS_multitexture))
677         if (ogl_arb_multitexture_ok || ogl_sgis_multitexture_ok){
678                 int c;
679                 float l,u1,v1;
680
681                 r_tpolyc+=2;
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);
687 #endif
688                 }else if (ogl_sgis_multitexture_ok){
689 #ifdef GL_SGIS_multitexture
690                         glSelectTextureSGIS(GL_TEXTURE0_SGIS);
691 #endif
692                 }
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);
698                 glEnable(GL_BLEND);
699                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
700
701                 if (ogl_arb_multitexture_ok){
702 #ifdef GL_ARB_multitexture
703                         glActiveTextureARB(GL_TEXTURE1_ARB);
704 #endif
705                 }else if (ogl_sgis_multitexture_ok){
706 #ifdef GL_SGIS_multitexture
707                         glSelectTextureSGIS(GL_TEXTURE1_SGIS);
708 #endif
709                 }
710                 ogl_bindbmtex(bm);
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);
715                 
716                 glEnable(GL_BLEND);
717                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
718
719                 glBegin(GL_TRIANGLE_FAN);
720                 for (c=0;c<nv;c++){
721                         switch(orient){
722                                 case 1:
723                                         u1=1.0-f2glf(uvl_list[c].v);
724                                         v1=f2glf(uvl_list[c].u);
725                                         break;
726                                 case 2:
727                                         u1=1.0-f2glf(uvl_list[c].u);
728                                         v1=1.0-f2glf(uvl_list[c].v);
729                                         break;
730                                 case 3:
731                                         u1=f2glf(uvl_list[c].v);
732                                         v1=1.0-f2glf(uvl_list[c].u);
733                                         break;
734                                 default:
735                                         u1=f2glf(uvl_list[c].u);
736                                         v1=f2glf(uvl_list[c].v);
737                                         break;
738                         }
739                         if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
740                                 l=1.0;
741                         }else{
742                                 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
743                                 l=f2fl(uvl_list[c].l);
744                         }
745                         glColor3f(l,l,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));
750 #endif
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));
754 #endif
755                         }
756                         if (ogl_arb_multitexture_ok){
757 #ifdef GL_ARB_multitexture
758                                 glMultiTexCoord2fARB(GL_TEXTURE1_ARB,u1,v1);
759 #endif
760                         }else if (ogl_sgis_multitexture_ok){
761 #ifdef GL_SGIS_multitexture
762                                 glMultiTexCoord2fSGIS(GL_TEXTURE1_SGIS,u1,v1);
763 #endif
764                         }
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));
768                 }
769                 glEnd();
770 /*              if (ogl_arb_multitexture_ok){
771 #ifdef GL_ARB_multitexture
772                         glActiveTextureARB(GL_TEXTURE1_ARB);
773 #endif
774                 }else if (ogl_sgis_multitexture_ok){
775 #ifdef GL_SGIS_multitexture
776                         glSelectTextureSGIS(GL_TEXTURE1_SGIS);
777 #endif
778                 }
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);
784 #endif
785                 }else if (ogl_sgis_multitexture_ok){
786 #ifdef GL_SGIS_multitexture
787                         glSelectTextureSGIS(GL_TEXTURE0_SGIS);
788 #endif
789                 }
790         }else
791 #endif
792         {
793                 int c;
794                 float l,u1,v1;
795
796                 g3_draw_tmap(nv,pointlist,uvl_list,bmbot);//draw the bottom texture first.. could be optimized with multitexturing..
797
798                 r_tpolyc++;
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);
802                 ogl_bindbmtex(bm);
803                 ogl_texwrap(bm->gltexture,GL_REPEAT);
804                 glBegin(GL_TRIANGLE_FAN);
805                 for (c=0;c<nv;c++){
806                         switch(orient){
807                                 case 1:
808                                         u1=1.0-f2glf(uvl_list[c].v);
809                                         v1=f2glf(uvl_list[c].u);
810                                         break;
811                                 case 2:
812                                         u1=1.0-f2glf(uvl_list[c].u);
813                                         v1=1.0-f2glf(uvl_list[c].v);
814                                         break;
815                                 case 3:
816                                         u1=f2glf(uvl_list[c].v);
817                                         v1=1.0-f2glf(uvl_list[c].u);
818                                         break;
819                                 default:
820                                         u1=f2glf(uvl_list[c].u);
821                                         v1=f2glf(uvl_list[c].v);
822                                         break;
823                         }
824                         if (bm->bm_flags&BM_FLAG_NO_LIGHTING){
825                                 l=1.0;
826                         }else{
827                                 //l=f2fl(uvl_list[c].l)+gr_palette_gamma/63.0;
828                                 l=f2fl(uvl_list[c].l);
829                         }
830                         glColor3f(l,l,l);
831                         glTexCoord2f(u1,v1);
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));
835                 }
836                 glEnd();
837         }
838         return 0;
839 }
840
841 bool g3_draw_bitmap(vms_vector *pos,fix width,fix height,grs_bitmap *bm, int orientation)
842 {
843         //float l=1.0;
844         vms_vector pv,v1;//,v2;
845         int i;
846         r_bitmapc++;
847         v1.z=0;
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));
850
851         OGL_ENABLE(TEXTURE_2D);
852         ogl_bindbmtex(bm);
853         ogl_texwrap(bm->gltexture,GL_CLAMP);
854
855         glBegin(GL_QUADS);
856         glColor3f(1.0,1.0,1.0);
857     width = fixmul(width,Matrix_scale.x);       
858     height = fixmul(height,Matrix_scale.y);     
859         for (i=0;i<4;i++){
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));
864                 switch (i){
865                         case 0:
866                                 glTexCoord2f(0.0, 0.0);
867                                 pv.x+=-width;
868                                 pv.y+=height;
869                                 break;
870                         case 1:
871                                 glTexCoord2f(bm->gltexture->u, 0.0);
872                                 pv.x+=width;
873                                 pv.y+=height;
874                                 break;
875                         case 2:
876                                 glTexCoord2f(bm->gltexture->u, bm->gltexture->v);
877                                 pv.x+=width;
878                                 pv.y+=-height;
879                                 break;
880                         case 3:
881                                 glTexCoord2f(0.0, bm->gltexture->v);
882                                 pv.x+=-width;
883                                 pv.y+=-height;
884                                 break;
885                 }
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));
892         }
893         glEnd();
894 //      printf("\n");
895
896         return 0;
897 }
898 bool ogl_ubitmapm_c(int x, int y,grs_bitmap *bm,int c)
899 {
900         GLfloat xo,yo,xf,yf;
901         GLfloat u1,u2,v1,v2;
902         r_ubitmapc++;
903         x+=grd_curcanv->cv_bitmap.bm_x;
904         y+=grd_curcanv->cv_bitmap.bm_y;
905         xo=x/(float)last_width;
906         xf=(bm->bm_w+x)/(float)last_width;
907         yo=1.0-y/(float)last_height;
908         yf=1.0-(bm->bm_h+y)/(float)last_height;
909
910 //      printf("g3_draw_bitmap: %f,%f,%f - ",f2glf(pos->x),f2glf(pos->y),-f2glf(pos->z));
911 //      printf("(%f,%f,%f) ",f2glf(View_position.x),f2glf(View_position.y),-f2glf(View_position.z));
912
913 /*              glEnABLE(ALPHA_TEST);
914         glAlphaFunc(GL_GREATER,0.0);*/
915
916         OGL_ENABLE(TEXTURE_2D);
917         ogl_bindbmtex(bm);
918         ogl_texwrap(bm->gltexture,GL_CLAMP);
919         
920         if (bm->bm_x==0){
921                 u1=0;
922                 if (bm->bm_w==bm->gltexture->w)
923                         u2=bm->gltexture->u;
924                 else
925                         u2=(bm->bm_w+bm->bm_x)/(float)bm->gltexture->tw;
926         }else {
927                 u1=bm->bm_x/(float)bm->gltexture->tw;
928                 u2=(bm->bm_w+bm->bm_x)/(float)bm->gltexture->tw;
929         }
930         if (bm->bm_y==0){
931                 v1=0;
932                 if (bm->bm_h==bm->gltexture->h)
933                         v2=bm->gltexture->v;
934                 else
935                         v2=(bm->bm_h+bm->bm_y)/(float)bm->gltexture->th;
936         }else{
937                 v1=bm->bm_y/(float)bm->gltexture->th;
938                 v2=(bm->bm_h+bm->bm_y)/(float)bm->gltexture->th;
939         }
940
941         glBegin(GL_QUADS);
942         if (c<0)
943                 glColor3f(1.0,1.0,1.0);
944         else
945                 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
946         glTexCoord2f(u1, v1); glVertex2f(xo, yo);
947         glTexCoord2f(u2, v1); glVertex2f(xf, yo);
948         glTexCoord2f(u2, v2); glVertex2f(xf, yf);
949         glTexCoord2f(u1, v2); glVertex2f(xo, yf);
950         glEnd();
951 //      glDisABLE(ALPHA_TEST);
952         
953         return 0;
954 }
955 bool ogl_ubitmapm(int x, int y,grs_bitmap *bm){
956         return ogl_ubitmapm_c(x,y,bm,-1);
957 //      return ogl_ubitblt(bm->bm_w,bm->bm_h,x,y,0,0,bm,NULL);
958 }
959 #if 0
960 //also upsidedown, currently.
961 bool ogl_ubitblt(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest)
962 {
963         GLfloat xo,yo;//,xs,ys;
964         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));
965         
966         dx+=dest->bm_x;
967         dy+=dest->bm_y;
968         
969         xo=dx/(float)last_width;
970 //      xo=dx/(float)grd_curscreen->sc_w;
971 //      xs=w/(float)last_width;
972         //yo=1.0-dy/(float)last_height;
973         yo=1.0-(dy+h)/(float)last_height;
974 //      ys=h/(float)last_height;
975         
976 //      OGL_ENABLE(TEXTURE_2D);
977         
978         OGL_DISABLE(TEXTURE_2D);
979         glRasterPos2f(xo,yo);
980         ogl_filltexbuf(src->bm_data,texbuf,src->bm_w,w,h,sx,sy,w,h,GL_RGBA);
981         glDrawPixels(w,h,GL_RGBA,GL_UNSIGNED_BYTE,texbuf);
982         glRasterPos2f(0,0);
983         
984         return 0;
985 }
986 #else
987 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)
988 {
989         GLfloat xo,yo,xs,ys;
990         GLfloat u1,v1;//,u2,v2;
991         ogl_texture tex;
992 //      unsigned char *oldpal;
993         r_ubitbltc++;
994
995         ogl_init_texture(&tex);
996         tex.w=sw;tex.h=sh;
997         tex.prio=0.0;tex.wantmip=0;
998         tex.lw=src->bm_rowsize;
999
1000 /*      if (w==src->bm_w && sx==0){
1001                 u1=0;u2=src->glu;
1002         }else{
1003                 u1=sx/(float)src->bm_w*src->glu;
1004                 u2=w/(float)src->bm_w*src->glu+u1;
1005         }
1006         if (h==src->bm_h && sy==0){
1007                 v1=0;v2=src->glv;
1008         }else{
1009                 v1=sy/(float)src->bm_h*src->glv;
1010                 v2=h/(float)src->bm_h*src->glv+v1;
1011         }*/
1012         u1=v1=0;
1013         
1014         dx+=dest->bm_x;
1015         dy+=dest->bm_y;
1016         xo=dx/(float)last_width;
1017         xs=dw/(float)last_width;
1018         yo=1.0-dy/(float)last_height;
1019         ys=dh/(float)last_height;
1020         
1021         OGL_ENABLE(TEXTURE_2D);
1022         
1023 //      oldpal=ogl_pal;
1024         ogl_pal=gr_current_pal;
1025         ogl_loadtexture(src->bm_data,sx,sy,&tex);
1026 //      ogl_pal=oldpal;
1027         ogl_pal=gr_palette;
1028         OGL_BINDTEXTURE(tex.handle);
1029         
1030         ogl_texwrap(&tex,GL_CLAMP);
1031
1032         glBegin(GL_QUADS);
1033         glColor3f(1.0,1.0,1.0);
1034         glTexCoord2f(u1, v1); glVertex2f(xo, yo);
1035         glTexCoord2f(tex.u, v1); glVertex2f(xo+xs, yo);
1036         glTexCoord2f(tex.u, tex.v); glVertex2f(xo+xs, yo-ys);
1037         glTexCoord2f(u1, tex.v); glVertex2f(xo, yo-ys);
1038         glEnd();
1039         ogl_freetexture(&tex);
1040         return 0;
1041 }
1042 bool ogl_ubitblt(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1043         return ogl_ubitblt_i(w,h,dx,dy,w,h,sx,sy,src,dest);
1044 }
1045 #endif
1046 bool ogl_ubitblt_tolinear(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1047 #if 1
1048         unsigned char *d,*s;
1049         int i,j;
1050         int w1,h1;
1051 //      w1=w;h1=h;
1052         w1=grd_curscreen->sc_w;h1=grd_curscreen->sc_h;
1053         if (w1*h1*3>OGLTEXBUFSIZE)
1054                 Error("ogl_ubitblt_tolinear: screen res larger than OGLTEXBUFSIZE\n");
1055
1056         if (ogl_readpixels_ok>0){
1057                 OGL_DISABLE(TEXTURE_2D);
1058                 glReadBuffer(GL_FRONT);
1059                 glReadPixels(0,0,w1,h1,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1060 //              glReadPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1061 //              glReadPixels(sx,sy,w+sx,h+sy,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1062         }else
1063                 memset(texbuf,0,w1*h1*3);
1064         sx+=src->bm_x;
1065         sy+=src->bm_y;
1066         for (i=0;i<h;i++){
1067                 d=dest->bm_data+dx+(dy+i)*dest->bm_rowsize;
1068                 s=texbuf+((h1-(i+sy+1))*w1+sx)*3;
1069                 for (j=0;j<w;j++){
1070                         *d=gr_find_closest_color(s[0]/4,s[1]/4,s[2]/4);
1071                         s+=3;
1072                         d++;
1073                 }
1074         }
1075 #else
1076         int i,j,c=0;
1077         unsigned char *d,*s,*e;
1078         if (w*h*3>OGLTEXBUFSIZE)
1079                 Error("ogl_ubitblt_tolinear: size larger than OGLTEXBUFSIZE\n");
1080         sx+=src->bm_x;
1081         sy+=src->bm_y;
1082 #if 1//also seems to cause a mess.  need to look into it a bit more..
1083         if (ogl_readpixels_ok>0){
1084                 OGL_DISABLE(TEXTURE_2D);
1085                 glReadBuffer(GL_FRONT);
1086 //              glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1087                 glReadPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1088         }else
1089 #endif
1090                 memset(texbuf,0,w*h*3);
1091 //      d=dest->bm_data+dx+(dy+i)*dest->bm_rowsize;
1092         d=dest->bm_data+dx+dy*dest->bm_rowsize;
1093         for (i=0;i<h;i++){
1094                 s=texbuf+w*(h-(i+1))*3;
1095 //              s=texbuf+w*i*3;
1096                 if (s<texbuf){Error("blah1\n");}
1097                 if (d<dest->bm_data){Error("blah3\n");}
1098 //              d=dest->bm_data+(i*dest->bm_rowsize);
1099
1100                 e=d;
1101                 for (j=0;j<w;j++){
1102                         if (s>texbuf+w*h*3-3){Error("blah2\n");}
1103                         if (d>dest->bm_data+dest->bm_rowsize*(h+dy)+dx  ){Error("blah4\n");}
1104                         *d=gr_find_closest_color(s[0]/4,s[1]/4,s[2]/4);
1105                         s+=3;
1106                         d++;
1107                         c++;
1108                 }
1109                 d=e;
1110                 d+=dest->bm_rowsize;
1111         }
1112         glmprintf((0,"c=%i w*h=%i\n",c,w*h));
1113 #endif
1114         return 0;
1115 }
1116
1117 bool ogl_ubitblt_copy(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1118 #if 0 //just seems to cause a mess.
1119         GLfloat xo,yo;//,xs,ys;
1120         
1121         dx+=dest->bm_x;
1122         dy+=dest->bm_y;
1123         
1124 //      xo=dx/(float)last_width;
1125         xo=dx/(float)grd_curscreen->sc_w;
1126 //      yo=1.0-(dy+h)/(float)last_height;
1127         yo=1.0-(dy+h)/(float)grd_curscreen->sc_h;
1128         sx+=src->bm_x;
1129         sy+=src->bm_y;
1130         OGL_DISABLE(TEXTURE_2D);
1131         glReadBuffer(GL_FRONT);
1132         glRasterPos2f(xo,yo);
1133 //      glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1134         glCopyPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_COLOR);
1135         glRasterPos2f(0,0);
1136 #endif
1137         return 0;
1138 }
1139
1140 void ogl_start_frame(void){
1141         r_polyc=0;r_tpolyc=0;r_bitmapc=0;r_ubitmapc=0;r_ubitbltc=0;r_upixelc=0;
1142 //      gl_badtexture=500;
1143
1144         OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,Canvas_width,Canvas_height);
1145         glClearColor(0.0, 0.0, 0.0, 0.0);
1146 //      glEnable(GL_ALPHA_TEST);
1147 //      glAlphaFunc(GL_GREATER,0.01);
1148         glShadeModel(GL_SMOOTH);
1149         glMatrixMode(GL_PROJECTION);
1150         glLoadIdentity();//clear matrix
1151         //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),1.0,1000000.0);
1152         //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),0.01,1000000.0);
1153         gluPerspective(90.0,1.0,0.01,1000000.0);
1154         //gluPerspective(90.0,(GLfloat)(Canvas_width*3)/(GLfloat)(Canvas_height*4),0.01,1000000.0);
1155 //      gluPerspective(75.0,(GLfloat)Canvas_width/(GLfloat)Canvas_height,1.0,1000000.0);
1156         glMatrixMode(GL_MODELVIEW);
1157         glLoadIdentity();//clear matrix
1158         glEnable(GL_BLEND);
1159         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1160 //      glDisABLE(DITHER);
1161 //      glScalef(1.0,1.0,-1.0);
1162 //      glScalef(1.0,1.0,-1.0);
1163 //      glPushMatrix();
1164         
1165 //      glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1166 //      OGL_TEXENV(GL_TEXTURE_ENV_MODE,GL_MODULATE);
1167 //      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_texmagfilt);
1168 //      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_texminfilt);
1169 //      OGL_TEXPARAM(GL_TEXTURE_MAG_FILTER,GL_texmagfilt);
1170 //      OGL_TEXPARAM(GL_TEXTURE_MIN_FILTER,GL_texminfilt);
1171 //      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1172 //      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1173 }
1174 #ifndef NMONO
1175 void merge_textures_stats(void);
1176 #endif
1177 void ogl_end_frame(void){
1178 //      OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,);
1179         OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
1180 #ifndef NMONO
1181 //      merge_textures_stats();
1182 //      ogl_texture_stats();
1183 #endif
1184 //      glViewport(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
1185         glMatrixMode(GL_PROJECTION);
1186         glLoadIdentity();//clear matrix
1187         glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
1188         glMatrixMode(GL_MODELVIEW);
1189         glLoadIdentity();//clear matrix
1190 //      glDisABLE(BLEND);
1191         //glDisABLE(ALPHA_TEST);
1192         //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),1.0,1000000.0);
1193 //      ogl_swap_buffers();//platform specific code
1194 //      glClear(GL_COLOR_BUFFER_BIT);
1195  
1196 }
1197 void ogl_swap_buffers(void){
1198         ogl_clean_texture_cache();
1199         if (gr_renderstats){
1200                 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);
1201 //      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
1202         }
1203         ogl_do_palfx();
1204         ogl_swap_buffers_internal();
1205         glClear(GL_COLOR_BUFFER_BIT);
1206 }
1207
1208 int tex_format_supported(int iformat,int format){
1209         switch (iformat){
1210                 case GL_INTENSITY4:
1211                         if (!ogl_intensity4_ok) return 0; break;
1212                 case GL_LUMINANCE4_ALPHA4:
1213                         if (!ogl_luminance4_alpha4_ok) return 0; break;
1214                 case GL_RGBA2:
1215                         if (!ogl_rgba2_ok) return 0; break;
1216         }
1217         if (ogl_gettexlevelparam_ok){
1218                 int internalFormat;
1219                 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, iformat, 64, 64, 0,
1220                                 format, GL_UNSIGNED_BYTE, texbuf);//NULL?
1221                 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0,
1222                                 GL_TEXTURE_INTERNAL_FORMAT,
1223                                 &internalFormat);
1224                 return (internalFormat==iformat);
1225         }else
1226                 return 1;
1227 }
1228
1229 //little hack to find the largest or equal multiple of 2 for a given number
1230 int pow2ize(int x){
1231         int i;
1232         for (i=2;i<=4096;i*=2)
1233                 if (x<=i) return i;
1234         return i;
1235 }
1236
1237 //GLubyte texbuf[512*512*4];
1238 GLubyte texbuf[OGLTEXBUFSIZE];
1239 void ogl_filltexbuf(unsigned char *data,GLubyte *texp,int truewidth,int width,int height,int dxo,int dyo,int twidth,int theight,int type)
1240 {
1241 //      GLushort *tex=(GLushort *)texp;
1242         int x,y,c,i;
1243         if (twidth*theight*4>sizeof(texbuf))//shouldn't happen, descent never uses textures that big.
1244                 Error("texture toobig %i %i",twidth,theight);
1245
1246         i=0;
1247         for (y=0;y<theight;y++){
1248                 i=dxo+truewidth*(y+dyo);
1249                 for (x=0;x<twidth;x++){
1250                         if (x<width && y<height)
1251                                 c=data[i++];
1252                         else
1253                                 c=255;//fill the pad space with transparancy
1254                         if (c==255){
1255                                 switch (type){
1256                                         case GL_LUMINANCE:
1257                                                 (*(texp++))=0;
1258                                                 break;
1259                                         case GL_LUMINANCE_ALPHA:
1260                                                 (*(texp++))=0;
1261                                                 (*(texp++))=0;
1262                                                 break;
1263                                         case GL_RGBA:
1264                                                 (*(texp++))=0;
1265                                                 (*(texp++))=0;
1266                                                 (*(texp++))=0;
1267                                                 (*(texp++))=0;//transparent pixel
1268                                                 break;
1269                                 }
1270 //                              (*(tex++))=0;
1271                         }else{
1272                                 switch (type){
1273                                         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. :)
1274                                                 (*(texp++))=255;
1275                                                 break;
1276                                         case GL_LUMINANCE_ALPHA:
1277                                                 (*(texp++))=255;
1278                                                 (*(texp++))=255;
1279                                                 break;
1280                                         case GL_RGBA:
1281                                                 //(*(texp++))=gr_palette[c*3]*4;
1282                                                 //(*(texp++))=gr_palette[c*3+1]*4;
1283                                                 //(*(texp++))=gr_palette[c*3+2]*4;
1284                                                 (*(texp++))=ogl_pal[c*3]*4;
1285                                                 (*(texp++))=ogl_pal[c*3+1]*4;
1286                                                 (*(texp++))=ogl_pal[c*3+2]*4;
1287                                                 (*(texp++))=255;//not transparent
1288                                                 //                              (*(tex++))=(ogl_pal[c*3]>>1) + ((ogl_pal[c*3+1]>>1)<<5) + ((ogl_pal[c*3+2]>>1)<<10) + (1<<15);
1289                                                 break;
1290                                 }
1291                         }
1292                 }
1293         }
1294 }
1295 int tex_format_verify(ogl_texture *tex){
1296         while (!tex_format_supported(tex->internalformat,tex->format)){
1297                 glmprintf((0,"tex format %x not supported",tex->internalformat));
1298                 switch (tex->internalformat){
1299                         case GL_INTENSITY4:
1300                                 if (ogl_luminance4_alpha4_ok){
1301                                         tex->internalformat=GL_LUMINANCE4_ALPHA4;
1302                                         tex->format=GL_LUMINANCE_ALPHA;
1303                                         break;
1304                                 }//note how it will fall through here if the statement is false
1305                         case GL_LUMINANCE4_ALPHA4:
1306                                 if (ogl_rgba2_ok){
1307                                         tex->internalformat=GL_RGBA2;
1308                                         tex->format=GL_RGBA;
1309                                         break;
1310                                 }//note how it will fall through here if the statement is false
1311                         case GL_RGBA2:
1312                                 tex->internalformat=ogl_rgba_format;
1313                                 tex->format=GL_RGBA;
1314                                 break;
1315                         default:
1316                                 mprintf((0,"...no tex format to fall back on\n"));
1317                                 return 1;
1318                 }
1319                 glmprintf((0,"...falling back to %x\n",tex->internalformat));
1320         }
1321         return 0;
1322 }
1323 void tex_set_size1(ogl_texture *tex,int dbits,int bits,int w, int h){
1324         int u;
1325         if (tex->tw!=w || tex->th!=h){
1326                 u=(tex->w/(float)tex->tw*w) * (tex->h/(float)tex->th*h);
1327                 glmprintf((0,"shrunken texture?\n"));
1328         }else
1329                 u=tex->w*tex->h;
1330         if (bits<=0){//the beta nvidia GLX server. doesn't ever return any bit sizes, so just use some assumptions.
1331                 tex->bytes=((float)w*h*dbits)/8.0;
1332                 tex->bytesu=((float)u*dbits)/8.0;
1333         }else{
1334                 tex->bytes=((float)w*h*bits)/8.0;
1335                 tex->bytesu=((float)u*bits)/8.0;
1336         }
1337         glmprintf((0,"tex_set_size1: %ix%i, %ib(%i) %iB\n",w,h,bits,dbits,tex->bytes));
1338 }
1339 void tex_set_size(ogl_texture *tex){
1340         int w,h,bi=16,a=0;
1341         if (ogl_gettexlevelparam_ok){
1342                 int t;
1343                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&w);
1344                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&h);
1345                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_LUMINANCE_SIZE,&t);a+=t;
1346                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_INTENSITY_SIZE,&t);a+=t;
1347                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_RED_SIZE,&t);a+=t;
1348                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_GREEN_SIZE,&t);a+=t;
1349                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_BLUE_SIZE,&t);a+=t;
1350                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_ALPHA_SIZE,&t);a+=t;
1351         }else{
1352                 w=tex->tw;
1353                 h=tex->th;
1354         }
1355         switch (tex->format){
1356                 case GL_LUMINANCE:
1357                         bi=8;
1358                         break;
1359                 case GL_LUMINANCE_ALPHA:
1360                         bi=8;
1361                         break;
1362                 case GL_RGBA:
1363                         bi=16;
1364                         break;
1365                 default:
1366                         Error("tex_set_size unknown texformat\n");
1367                         break;
1368         }
1369         tex_set_size1(tex,bi,a,w,h);
1370 }
1371 //loads a palettized bitmap into a ogl RGBA texture.
1372 //Sizes and pads dimensions to multiples of 2 if necessary.
1373 //In theory this could be a problem for repeating textures, but all real
1374 //textures (not sprites, etc) in descent are 64x64, so we are ok.
1375 //stores OpenGL textured id in *texid and u/v values required to get only the real data in *u/*v
1376 void ogl_loadtexture(unsigned char * data, int dxo,int dyo, ogl_texture *tex){
1377 //void ogl_loadtexture(unsigned char * data, int width, int height,int dxo,int dyo, int *texid,float *u,float *v,char domipmap,float prio){
1378 //      int internalformat=GL_RGBA;
1379 //      int format=GL_RGBA;
1380         //int filltype=0;
1381         tex->tw=pow2ize(tex->w);tex->th=pow2ize(tex->h);//calculate smallest texture size that can accomodate us (must be multiples of 2)
1382 //      tex->tw=tex->w;tex->th=tex->h;//feeling lucky?
1383         
1384         if(gr_badtexture>0) return;
1385
1386         if (tex_format_verify(tex))
1387                 return;
1388         
1389         //calculate u/v values that would make the resulting texture correctly sized
1390         tex->u=(float)tex->w/(float)tex->tw;
1391         tex->v=(float)tex->h/(float)tex->th;
1392
1393         //      if (width!=twidth || height!=theight)
1394         //              glmprintf((0,"sizing %ix%i texture up to %ix%i\n",width,height,twidth,theight));
1395         ogl_filltexbuf(data,texbuf,tex->lw,tex->w,tex->h,dxo,dyo,tex->tw,tex->th,tex->format);
1396
1397         // Generate OpenGL texture IDs.
1398         glGenTextures(1, &tex->handle);
1399
1400         //set priority
1401         glPrioritizeTextures(1,&tex->handle,&tex->prio);
1402         
1403         // Give our data to OpenGL.
1404
1405         OGL_BINDTEXTURE(tex->handle);
1406
1407         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1408         if (tex->wantmip){
1409                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_texmagfilt);
1410                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_texminfilt);
1411         }else{
1412                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1413                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1414         }
1415 //      domipmap=0;//mipmaps aren't used in GL_NEAREST anyway, and making the mipmaps is pretty slow
1416         //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.
1417
1418         if (tex->wantmip && GL_needmipmaps)
1419                 gluBuild2DMipmaps( GL_TEXTURE_2D, tex->internalformat, tex->tw,
1420                                 tex->th, tex->format, GL_UNSIGNED_BYTE, texbuf);
1421         else
1422                 glTexImage2D(GL_TEXTURE_2D, 0, tex->internalformat,
1423                         tex->tw, tex->th, 0, tex->format, // RGBA textures.
1424                         GL_UNSIGNED_BYTE, // imageData is a GLubyte pointer.
1425                         texbuf);
1426         
1427         tex_set_size(tex);
1428
1429         r_texcount++; 
1430         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));
1431
1432 }
1433 unsigned char decodebuf[512*512];
1434 void ogl_loadbmtexture_m(grs_bitmap *bm,int domipmap){
1435         unsigned char *buf;
1436         while (bm->bm_parent)
1437                 bm=bm->bm_parent;
1438         buf=bm->bm_data;
1439         if (bm->gltexture==NULL){
1440                 ogl_init_texture(bm->gltexture=ogl_get_free_texture());
1441                 bm->gltexture->lw=bm->bm_w;
1442                 bm->gltexture->w=bm->bm_w;
1443                 bm->gltexture->h=bm->bm_h;
1444                 bm->gltexture->wantmip=domipmap;
1445         }
1446         else {
1447                 if (bm->gltexture->handle>0)
1448                         return;
1449                 if (bm->gltexture->w==0){
1450                         bm->gltexture->lw=bm->bm_w;
1451                         bm->gltexture->w=bm->bm_w;
1452                         bm->gltexture->h=bm->bm_h;
1453                 }
1454         }
1455         if (bm->bm_flags & BM_FLAG_RLE){
1456                 unsigned char * dbits;
1457                 unsigned char * sbits;
1458                 int i;
1459                 sbits = &bm->bm_data[4 + bm->bm_h];
1460                 dbits = decodebuf;
1461
1462                 for (i=0; i < bm->bm_h; i++ )    {
1463                         gr_rle_decode(sbits,dbits);
1464                         sbits += (int)bm->bm_data[4+i];
1465                         dbits += bm->bm_w;
1466                 }
1467                 buf=decodebuf;
1468         }
1469         ogl_loadtexture(buf,0,0,bm->gltexture);
1470 }
1471 void ogl_loadbmtexture(grs_bitmap *bm){
1472         ogl_loadbmtexture_m(bm,1);
1473 }
1474 void ogl_freetexture(ogl_texture *gltexture){
1475         if (gltexture->handle>0){
1476                 r_texcount--;
1477                 glmprintf((0,"ogl_freetexture(%p):%i (last rend %is) (%i left)\n",gltexture,gltexture->handle,(GameTime-gltexture->lastrend)/f1_0,r_texcount));
1478                 glDeleteTextures( 1, &gltexture->handle );
1479 //              gltexture->handle=0;
1480                 ogl_init_texture(gltexture);
1481         }
1482 }
1483 void ogl_freebmtexture(grs_bitmap *bm){
1484         if (bm->gltexture){
1485                 ogl_freetexture(bm->gltexture);
1486                 bm->gltexture=NULL;
1487 //              r_texcount--;
1488 //              glmprintf((0,"ogl_freebmtexture(%p,%p):%i (%i left)\n",bm->bm_data,&bm->gltexture,bm->gltexture,r_texcount));
1489 //              glDeleteTextures( 1, &bm->gltexture );
1490 //              bm->gltexture=-1;
1491         }
1492 }