OSX OGL fixes
[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__) || defined(__MACOSX__)
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
899 bool ogl_ubitmapm_c(int x, int y,grs_bitmap *bm,int c)
900 {
901         GLfloat xo,yo,xf,yf;
902         GLfloat u1,u2,v1,v2;
903         r_ubitmapc++;
904         x+=grd_curcanv->cv_bitmap.bm_x;
905         y+=grd_curcanv->cv_bitmap.bm_y;
906         xo=x/(float)last_width;
907         xf=(bm->bm_w+x)/(float)last_width;
908         yo=1.0-y/(float)last_height;
909         yf=1.0-(bm->bm_h+y)/(float)last_height;
910
911 //      printf("g3_draw_bitmap: %f,%f,%f - ",f2glf(pos->x),f2glf(pos->y),-f2glf(pos->z));
912 //      printf("(%f,%f,%f) ",f2glf(View_position.x),f2glf(View_position.y),-f2glf(View_position.z));
913
914 /*              glEnABLE(ALPHA_TEST);
915         glAlphaFunc(GL_GREATER,0.0);*/
916
917         OGL_ENABLE(TEXTURE_2D);
918         ogl_bindbmtex(bm);
919         ogl_texwrap(bm->gltexture,GL_CLAMP);
920         
921         if (bm->bm_x==0){
922                 u1=0;
923                 if (bm->bm_w==bm->gltexture->w)
924                         u2=bm->gltexture->u;
925                 else
926                         u2=(bm->bm_w+bm->bm_x)/(float)bm->gltexture->tw;
927         }else {
928                 u1=bm->bm_x/(float)bm->gltexture->tw;
929                 u2=(bm->bm_w+bm->bm_x)/(float)bm->gltexture->tw;
930         }
931         if (bm->bm_y==0){
932                 v1=0;
933                 if (bm->bm_h==bm->gltexture->h)
934                         v2=bm->gltexture->v;
935                 else
936                         v2=(bm->bm_h+bm->bm_y)/(float)bm->gltexture->th;
937         }else{
938                 v1=bm->bm_y/(float)bm->gltexture->th;
939                 v2=(bm->bm_h+bm->bm_y)/(float)bm->gltexture->th;
940         }
941
942         glBegin(GL_QUADS);
943         if (c<0)
944                 glColor3f(1.0,1.0,1.0);
945         else
946                 glColor3f(CPAL2Tr(c),CPAL2Tg(c),CPAL2Tb(c));
947         glTexCoord2f(u1, v1); glVertex2f(xo, yo);
948         glTexCoord2f(u2, v1); glVertex2f(xf, yo);
949         glTexCoord2f(u2, v2); glVertex2f(xf, yf);
950         glTexCoord2f(u1, v2); glVertex2f(xo, yf);
951         glEnd();
952 //      glDisABLE(ALPHA_TEST);
953         
954         return 0;
955 }
956 bool ogl_ubitmapm(int x, int y,grs_bitmap *bm){
957         return ogl_ubitmapm_c(x,y,bm,-1);
958 //      return ogl_ubitblt(bm->bm_w,bm->bm_h,x,y,0,0,bm,NULL);
959 }
960 #if 0
961 //also upsidedown, currently.
962 bool ogl_ubitblt(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest)
963 {
964         GLfloat xo,yo;//,xs,ys;
965         glmprintf((0,"ogl_ubitblt(w=%i,h=%i,dx=%i,dy=%i,sx=%i,sy=%i,src=%p,dest=%p\n",w,h, dx, dy,sx, sy,  src,dest));
966         
967         dx+=dest->bm_x;
968         dy+=dest->bm_y;
969         
970         xo=dx/(float)last_width;
971 //      xo=dx/(float)grd_curscreen->sc_w;
972 //      xs=w/(float)last_width;
973         //yo=1.0-dy/(float)last_height;
974         yo=1.0-(dy+h)/(float)last_height;
975 //      ys=h/(float)last_height;
976         
977 //      OGL_ENABLE(TEXTURE_2D);
978         
979         OGL_DISABLE(TEXTURE_2D);
980         glRasterPos2f(xo,yo);
981         ogl_filltexbuf(src->bm_data,texbuf,src->bm_w,w,h,sx,sy,w,h,GL_RGBA);
982         glDrawPixels(w,h,GL_RGBA,GL_UNSIGNED_BYTE,texbuf);
983         glRasterPos2f(0,0);
984         
985         return 0;
986 }
987 #else
988 bool ogl_ubitblt_i(int dw,int dh,int dx,int dy, int sw, int sh, int sx, int sy, grs_bitmap * src, grs_bitmap * dest)
989 {
990         GLfloat xo,yo,xs,ys;
991         GLfloat u1,v1;//,u2,v2;
992         ogl_texture tex;
993 //      unsigned char *oldpal;
994         r_ubitbltc++;
995
996         ogl_init_texture(&tex);
997         tex.w=sw;tex.h=sh;
998         tex.prio=0.0;tex.wantmip=0;
999         tex.lw=src->bm_rowsize;
1000
1001 /*      if (w==src->bm_w && sx==0){
1002                 u1=0;u2=src->glu;
1003         }else{
1004                 u1=sx/(float)src->bm_w*src->glu;
1005                 u2=w/(float)src->bm_w*src->glu+u1;
1006         }
1007         if (h==src->bm_h && sy==0){
1008                 v1=0;v2=src->glv;
1009         }else{
1010                 v1=sy/(float)src->bm_h*src->glv;
1011                 v2=h/(float)src->bm_h*src->glv+v1;
1012         }*/
1013         u1=v1=0;
1014         
1015         dx+=dest->bm_x;
1016         dy+=dest->bm_y;
1017         xo=dx/(float)last_width;
1018         xs=dw/(float)last_width;
1019         yo=1.0-dy/(float)last_height;
1020         ys=dh/(float)last_height;
1021         
1022         OGL_ENABLE(TEXTURE_2D);
1023         
1024 //      oldpal=ogl_pal;
1025         ogl_pal=gr_current_pal;
1026         ogl_loadtexture(src->bm_data,sx,sy,&tex);
1027 //      ogl_pal=oldpal;
1028         ogl_pal=gr_palette;
1029         OGL_BINDTEXTURE(tex.handle);
1030         
1031         ogl_texwrap(&tex,GL_CLAMP);
1032
1033         glBegin(GL_QUADS);
1034         glColor3f(1.0,1.0,1.0);
1035         glTexCoord2f(u1, v1); glVertex2f(xo, yo);
1036         glTexCoord2f(tex.u, v1); glVertex2f(xo+xs, yo);
1037         glTexCoord2f(tex.u, tex.v); glVertex2f(xo+xs, yo-ys);
1038         glTexCoord2f(u1, tex.v); glVertex2f(xo, yo-ys);
1039         glEnd();
1040         ogl_freetexture(&tex);
1041         return 0;
1042 }
1043 bool ogl_ubitblt(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1044         return ogl_ubitblt_i(w,h,dx,dy,w,h,sx,sy,src,dest);
1045 }
1046 #endif
1047 bool ogl_ubitblt_tolinear(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1048 #if 1
1049         unsigned char *d,*s;
1050         int i,j;
1051         int w1,h1;
1052 //      w1=w;h1=h;
1053         w1=grd_curscreen->sc_w;h1=grd_curscreen->sc_h;
1054         if (w1*h1*3>OGLTEXBUFSIZE)
1055                 Error("ogl_ubitblt_tolinear: screen res larger than OGLTEXBUFSIZE\n");
1056
1057         if (ogl_readpixels_ok>0){
1058                 OGL_DISABLE(TEXTURE_2D);
1059                 glReadBuffer(GL_FRONT);
1060                 glReadPixels(0,0,w1,h1,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1061 //              glReadPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1062 //              glReadPixels(sx,sy,w+sx,h+sy,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1063         }else
1064                 memset(texbuf,0,w1*h1*3);
1065         sx+=src->bm_x;
1066         sy+=src->bm_y;
1067         for (i=0;i<h;i++){
1068                 d=dest->bm_data+dx+(dy+i)*dest->bm_rowsize;
1069                 s=texbuf+((h1-(i+sy+1))*w1+sx)*3;
1070                 for (j=0;j<w;j++){
1071                         *d=gr_find_closest_color(s[0]/4,s[1]/4,s[2]/4);
1072                         s+=3;
1073                         d++;
1074                 }
1075         }
1076 #else
1077         int i,j,c=0;
1078         unsigned char *d,*s,*e;
1079         if (w*h*3>OGLTEXBUFSIZE)
1080                 Error("ogl_ubitblt_tolinear: size larger than OGLTEXBUFSIZE\n");
1081         sx+=src->bm_x;
1082         sy+=src->bm_y;
1083 #if 1//also seems to cause a mess.  need to look into it a bit more..
1084         if (ogl_readpixels_ok>0){
1085                 OGL_DISABLE(TEXTURE_2D);
1086                 glReadBuffer(GL_FRONT);
1087 //              glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1088                 glReadPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1089         }else
1090 #endif
1091                 memset(texbuf,0,w*h*3);
1092 //      d=dest->bm_data+dx+(dy+i)*dest->bm_rowsize;
1093         d=dest->bm_data+dx+dy*dest->bm_rowsize;
1094         for (i=0;i<h;i++){
1095                 s=texbuf+w*(h-(i+1))*3;
1096 //              s=texbuf+w*i*3;
1097                 if (s<texbuf){Error("blah1\n");}
1098                 if (d<dest->bm_data){Error("blah3\n");}
1099 //              d=dest->bm_data+(i*dest->bm_rowsize);
1100
1101                 e=d;
1102                 for (j=0;j<w;j++){
1103                         if (s>texbuf+w*h*3-3){Error("blah2\n");}
1104                         if (d>dest->bm_data+dest->bm_rowsize*(h+dy)+dx  ){Error("blah4\n");}
1105                         *d=gr_find_closest_color(s[0]/4,s[1]/4,s[2]/4);
1106                         s+=3;
1107                         d++;
1108                         c++;
1109                 }
1110                 d=e;
1111                 d+=dest->bm_rowsize;
1112         }
1113         glmprintf((0,"c=%i w*h=%i\n",c,w*h));
1114 #endif
1115         return 0;
1116 }
1117
1118 bool ogl_ubitblt_copy(int w,int h,int dx,int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest){
1119 #if 0 //just seems to cause a mess.
1120         GLfloat xo,yo;//,xs,ys;
1121         
1122         dx+=dest->bm_x;
1123         dy+=dest->bm_y;
1124         
1125 //      xo=dx/(float)last_width;
1126         xo=dx/(float)grd_curscreen->sc_w;
1127 //      yo=1.0-(dy+h)/(float)last_height;
1128         yo=1.0-(dy+h)/(float)grd_curscreen->sc_h;
1129         sx+=src->bm_x;
1130         sy+=src->bm_y;
1131         OGL_DISABLE(TEXTURE_2D);
1132         glReadBuffer(GL_FRONT);
1133         glRasterPos2f(xo,yo);
1134 //      glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,texbuf);
1135         glCopyPixels(sx,grd_curscreen->sc_h-(sy+h),w,h,GL_COLOR);
1136         glRasterPos2f(0,0);
1137 #endif
1138         return 0;
1139 }
1140
1141 void ogl_start_frame(void){
1142         r_polyc=0;r_tpolyc=0;r_bitmapc=0;r_ubitmapc=0;r_ubitbltc=0;r_upixelc=0;
1143 //      gl_badtexture=500;
1144
1145         OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,Canvas_width,Canvas_height);
1146         glClearColor(0.0, 0.0, 0.0, 0.0);
1147 //      glEnable(GL_ALPHA_TEST);
1148 //      glAlphaFunc(GL_GREATER,0.01);
1149         glShadeModel(GL_SMOOTH);
1150         glMatrixMode(GL_PROJECTION);
1151         glLoadIdentity();//clear matrix
1152         //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),1.0,1000000.0);
1153         //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),0.01,1000000.0);
1154         gluPerspective(90.0,1.0,0.01,1000000.0);
1155         //gluPerspective(90.0,(GLfloat)(Canvas_width*3)/(GLfloat)(Canvas_height*4),0.01,1000000.0);
1156 //      gluPerspective(75.0,(GLfloat)Canvas_width/(GLfloat)Canvas_height,1.0,1000000.0);
1157         glMatrixMode(GL_MODELVIEW);
1158         glLoadIdentity();//clear matrix
1159         glEnable(GL_BLEND);
1160         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1161 //      glDisABLE(DITHER);
1162 //      glScalef(1.0,1.0,-1.0);
1163 //      glScalef(1.0,1.0,-1.0);
1164 //      glPushMatrix();
1165         
1166 //      glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1167 //      OGL_TEXENV(GL_TEXTURE_ENV_MODE,GL_MODULATE);
1168 //      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_texmagfilt);
1169 //      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_texminfilt);
1170 //      OGL_TEXPARAM(GL_TEXTURE_MAG_FILTER,GL_texmagfilt);
1171 //      OGL_TEXPARAM(GL_TEXTURE_MIN_FILTER,GL_texminfilt);
1172 //      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1173 //      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1174 }
1175 #ifndef NMONO
1176 void merge_textures_stats(void);
1177 #endif
1178 void ogl_end_frame(void){
1179 //      OGL_VIEWPORT(grd_curcanv->cv_bitmap.bm_x,grd_curcanv->cv_bitmap.bm_y,);
1180         OGL_VIEWPORT(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
1181 #ifndef NMONO
1182 //      merge_textures_stats();
1183 //      ogl_texture_stats();
1184 #endif
1185 //      glViewport(0,0,grd_curscreen->sc_w,grd_curscreen->sc_h);
1186         glMatrixMode(GL_PROJECTION);
1187         glLoadIdentity();//clear matrix
1188         glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
1189         glMatrixMode(GL_MODELVIEW);
1190         glLoadIdentity();//clear matrix
1191 //      glDisABLE(BLEND);
1192         //glDisABLE(ALPHA_TEST);
1193         //gluPerspective(90.0,(GLfloat)(grd_curscreen->sc_w*3)/(GLfloat)(grd_curscreen->sc_h*4),1.0,1000000.0);
1194 //      ogl_swap_buffers();//platform specific code
1195 //      glClear(GL_COLOR_BUFFER_BIT);
1196  
1197 }
1198 void ogl_swap_buffers(void){
1199         ogl_clean_texture_cache();
1200         if (gr_renderstats){
1201                 gr_printf(5,GAME_FONT->ft_h*13+3*13,"%i flat %i tex %i sprites %i bitmaps",r_polyc,r_tpolyc,r_bitmapc,r_ubitmapc);
1202 //      glmprintf((0,"ogl_end_frame: %i polys, %i tmaps, %i sprites, %i bitmaps, %i bitblts, %i pixels\n",r_polyc,r_tpolyc,r_bitmapc,r_ubitmapc,r_ubitbltc,r_upixelc));//we need to do it here because some things get drawn after end_frame
1203         }
1204         ogl_do_palfx();
1205         ogl_swap_buffers_internal();
1206         glClear(GL_COLOR_BUFFER_BIT);
1207 }
1208
1209 int tex_format_supported(int iformat,int format){
1210         switch (iformat){
1211                 case GL_INTENSITY4:
1212                         if (!ogl_intensity4_ok) return 0; break;
1213                 case GL_LUMINANCE4_ALPHA4:
1214                         if (!ogl_luminance4_alpha4_ok) return 0; break;
1215                 case GL_RGBA2:
1216                         if (!ogl_rgba2_ok) return 0; break;
1217         }
1218         if (ogl_gettexlevelparam_ok){
1219                 GLint internalFormat;
1220                 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, iformat, 64, 64, 0,
1221                                 format, GL_UNSIGNED_BYTE, texbuf);//NULL?
1222                 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0,
1223                                 GL_TEXTURE_INTERNAL_FORMAT,
1224                                 &internalFormat);
1225                 return (internalFormat==iformat);
1226         }else
1227                 return 1;
1228 }
1229
1230 //little hack to find the largest or equal multiple of 2 for a given number
1231 int pow2ize(int x){
1232         int i;
1233         for (i=2;i<=4096;i*=2)
1234                 if (x<=i) return i;
1235         return i;
1236 }
1237
1238 //GLubyte texbuf[512*512*4];
1239 GLubyte texbuf[OGLTEXBUFSIZE];
1240 void ogl_filltexbuf(unsigned char *data,GLubyte *texp,int truewidth,int width,int height,int dxo,int dyo,int twidth,int theight,int type)
1241 {
1242 //      GLushort *tex=(GLushort *)texp;
1243         int x,y,c,i;
1244         if (twidth*theight*4>sizeof(texbuf))//shouldn't happen, descent never uses textures that big.
1245                 Error("texture toobig %i %i",twidth,theight);
1246
1247         i=0;
1248         for (y=0;y<theight;y++){
1249                 i=dxo+truewidth*(y+dyo);
1250                 for (x=0;x<twidth;x++){
1251                         if (x<width && y<height)
1252                                 c=data[i++];
1253                         else
1254                                 c=255;//fill the pad space with transparancy
1255                         if (c==255){
1256                                 switch (type){
1257                                         case GL_LUMINANCE:
1258                                                 (*(texp++))=0;
1259                                                 break;
1260                                         case GL_LUMINANCE_ALPHA:
1261                                                 (*(texp++))=0;
1262                                                 (*(texp++))=0;
1263                                                 break;
1264                                         case GL_RGBA:
1265                                                 (*(texp++))=0;
1266                                                 (*(texp++))=0;
1267                                                 (*(texp++))=0;
1268                                                 (*(texp++))=0;//transparent pixel
1269                                                 break;
1270                                 }
1271 //                              (*(tex++))=0;
1272                         }else{
1273                                 switch (type){
1274                                         case GL_LUMINANCE://these could prolly be done to make the intensity based upon the intensity of the resulting color, but its not needed for anything (yet?) so no point. :)
1275                                                 (*(texp++))=255;
1276                                                 break;
1277                                         case GL_LUMINANCE_ALPHA:
1278                                                 (*(texp++))=255;
1279                                                 (*(texp++))=255;
1280                                                 break;
1281                                         case GL_RGBA:
1282                                                 //(*(texp++))=gr_palette[c*3]*4;
1283                                                 //(*(texp++))=gr_palette[c*3+1]*4;
1284                                                 //(*(texp++))=gr_palette[c*3+2]*4;
1285                                                 (*(texp++))=ogl_pal[c*3]*4;
1286                                                 (*(texp++))=ogl_pal[c*3+1]*4;
1287                                                 (*(texp++))=ogl_pal[c*3+2]*4;
1288                                                 (*(texp++))=255;//not transparent
1289                                                 //                              (*(tex++))=(ogl_pal[c*3]>>1) + ((ogl_pal[c*3+1]>>1)<<5) + ((ogl_pal[c*3+2]>>1)<<10) + (1<<15);
1290                                                 break;
1291                                 }
1292                         }
1293                 }
1294         }
1295 }
1296 int tex_format_verify(ogl_texture *tex){
1297         while (!tex_format_supported(tex->internalformat,tex->format)){
1298                 glmprintf((0,"tex format %x not supported",tex->internalformat));
1299                 switch (tex->internalformat){
1300                         case GL_INTENSITY4:
1301                                 if (ogl_luminance4_alpha4_ok){
1302                                         tex->internalformat=GL_LUMINANCE4_ALPHA4;
1303                                         tex->format=GL_LUMINANCE_ALPHA;
1304                                         break;
1305                                 }//note how it will fall through here if the statement is false
1306                         case GL_LUMINANCE4_ALPHA4:
1307                                 if (ogl_rgba2_ok){
1308                                         tex->internalformat=GL_RGBA2;
1309                                         tex->format=GL_RGBA;
1310                                         break;
1311                                 }//note how it will fall through here if the statement is false
1312                         case GL_RGBA2:
1313                                 tex->internalformat=ogl_rgba_format;
1314                                 tex->format=GL_RGBA;
1315                                 break;
1316                         default:
1317                                 mprintf((0,"...no tex format to fall back on\n"));
1318                                 return 1;
1319                 }
1320                 glmprintf((0,"...falling back to %x\n",tex->internalformat));
1321         }
1322         return 0;
1323 }
1324 void tex_set_size1(ogl_texture *tex,int dbits,int bits,int w, int h){
1325         int u;
1326         if (tex->tw!=w || tex->th!=h){
1327                 u=(tex->w/(float)tex->tw*w) * (tex->h/(float)tex->th*h);
1328                 glmprintf((0,"shrunken texture?\n"));
1329         }else
1330                 u=tex->w*tex->h;
1331         if (bits<=0){//the beta nvidia GLX server. doesn't ever return any bit sizes, so just use some assumptions.
1332                 tex->bytes=((float)w*h*dbits)/8.0;
1333                 tex->bytesu=((float)u*dbits)/8.0;
1334         }else{
1335                 tex->bytes=((float)w*h*bits)/8.0;
1336                 tex->bytesu=((float)u*bits)/8.0;
1337         }
1338         glmprintf((0,"tex_set_size1: %ix%i, %ib(%i) %iB\n",w,h,bits,dbits,tex->bytes));
1339 }
1340 void tex_set_size(ogl_texture *tex){
1341         GLint w,h;
1342         int bi=16,a=0;
1343         if (ogl_gettexlevelparam_ok){
1344                 GLint t;
1345                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&w);
1346                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&h);
1347                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_LUMINANCE_SIZE,&t);a+=t;
1348                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_INTENSITY_SIZE,&t);a+=t;
1349                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_RED_SIZE,&t);a+=t;
1350                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_GREEN_SIZE,&t);a+=t;
1351                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_BLUE_SIZE,&t);a+=t;
1352                 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_ALPHA_SIZE,&t);a+=t;
1353         }else{
1354                 w=tex->tw;
1355                 h=tex->th;
1356         }
1357         switch (tex->format){
1358                 case GL_LUMINANCE:
1359                         bi=8;
1360                         break;
1361                 case GL_LUMINANCE_ALPHA:
1362                         bi=8;
1363                         break;
1364                 case GL_RGBA:
1365                         bi=16;
1366                         break;
1367                 default:
1368                         Error("tex_set_size unknown texformat\n");
1369                         break;
1370         }
1371         tex_set_size1(tex,bi,a,w,h);
1372 }
1373 //loads a palettized bitmap into a ogl RGBA texture.
1374 //Sizes and pads dimensions to multiples of 2 if necessary.
1375 //In theory this could be a problem for repeating textures, but all real
1376 //textures (not sprites, etc) in descent are 64x64, so we are ok.
1377 //stores OpenGL textured id in *texid and u/v values required to get only the real data in *u/*v
1378 void ogl_loadtexture(unsigned char * data, int dxo,int dyo, ogl_texture *tex){
1379 //void ogl_loadtexture(unsigned char * data, int width, int height,int dxo,int dyo, int *texid,float *u,float *v,char domipmap,float prio){
1380 //      int internalformat=GL_RGBA;
1381 //      int format=GL_RGBA;
1382         //int filltype=0;
1383         tex->tw=pow2ize(tex->w);tex->th=pow2ize(tex->h);//calculate smallest texture size that can accomodate us (must be multiples of 2)
1384 //      tex->tw=tex->w;tex->th=tex->h;//feeling lucky?
1385         
1386         if(gr_badtexture>0) return;
1387
1388         if (tex_format_verify(tex))
1389                 return;
1390         
1391         //calculate u/v values that would make the resulting texture correctly sized
1392         tex->u=(float)tex->w/(float)tex->tw;
1393         tex->v=(float)tex->h/(float)tex->th;
1394
1395         //      if (width!=twidth || height!=theight)
1396         //              glmprintf((0,"sizing %ix%i texture up to %ix%i\n",width,height,twidth,theight));
1397         ogl_filltexbuf(data,texbuf,tex->lw,tex->w,tex->h,dxo,dyo,tex->tw,tex->th,tex->format);
1398
1399         // Generate OpenGL texture IDs.
1400         glGenTextures(1, &tex->handle);
1401
1402         //set priority
1403         glPrioritizeTextures(1,&tex->handle,&tex->prio);
1404         
1405         // Give our data to OpenGL.
1406
1407         OGL_BINDTEXTURE(tex->handle);
1408
1409         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1410         if (tex->wantmip){
1411                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_texmagfilt);
1412                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_texminfilt);
1413         }else{
1414                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1415                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1416         }
1417 //      domipmap=0;//mipmaps aren't used in GL_NEAREST anyway, and making the mipmaps is pretty slow
1418         //however, if texturing mode becomes an ingame option, they would need to be made regardless, so it could switch to them later.  OTOH, texturing mode could just be made a command line arg.
1419
1420         if (tex->wantmip && GL_needmipmaps)
1421                 gluBuild2DMipmaps( GL_TEXTURE_2D, tex->internalformat, tex->tw,
1422                                 tex->th, tex->format, GL_UNSIGNED_BYTE, texbuf);
1423         else
1424                 glTexImage2D(GL_TEXTURE_2D, 0, tex->internalformat,
1425                         tex->tw, tex->th, 0, tex->format, // RGBA textures.
1426                         GL_UNSIGNED_BYTE, // imageData is a GLubyte pointer.
1427                         texbuf);
1428         
1429         tex_set_size(tex);
1430
1431         r_texcount++; 
1432         glmprintf((0,"ogl_loadtexture(%p,%i,%i,%ix%i,%p):%i u=%f v=%f b=%i bu=%i (%i)\n",data,tex->tw,tex->th,dxo,dyo,tex,tex->handle,tex->u,tex->v,tex->bytes,tex->bytesu,r_texcount));
1433
1434 }
1435 unsigned char decodebuf[512*512];
1436 void ogl_loadbmtexture_m(grs_bitmap *bm,int domipmap){
1437         unsigned char *buf;
1438         while (bm->bm_parent)
1439                 bm=bm->bm_parent;
1440         buf=bm->bm_data;
1441         if (bm->gltexture==NULL){
1442                 ogl_init_texture(bm->gltexture=ogl_get_free_texture());
1443                 bm->gltexture->lw=bm->bm_w;
1444                 bm->gltexture->w=bm->bm_w;
1445                 bm->gltexture->h=bm->bm_h;
1446                 bm->gltexture->wantmip=domipmap;
1447         }
1448         else {
1449                 if (bm->gltexture->handle>0)
1450                         return;
1451                 if (bm->gltexture->w==0){
1452                         bm->gltexture->lw=bm->bm_w;
1453                         bm->gltexture->w=bm->bm_w;
1454                         bm->gltexture->h=bm->bm_h;
1455                 }
1456         }
1457         if (bm->bm_flags & BM_FLAG_RLE){
1458                 unsigned char * dbits;
1459                 unsigned char * sbits;
1460                 int i;
1461                 sbits = &bm->bm_data[4 + bm->bm_h];
1462                 dbits = decodebuf;
1463
1464                 for (i=0; i < bm->bm_h; i++ )    {
1465                         gr_rle_decode(sbits,dbits);
1466                         sbits += (int)bm->bm_data[4+i];
1467                         dbits += bm->bm_w;
1468                 }
1469                 buf=decodebuf;
1470         }
1471         ogl_loadtexture(buf,0,0,bm->gltexture);
1472 }
1473 void ogl_loadbmtexture(grs_bitmap *bm){
1474         ogl_loadbmtexture_m(bm,1);
1475 }
1476 void ogl_freetexture(ogl_texture *gltexture){
1477         if (gltexture->handle>0){
1478                 r_texcount--;
1479                 glmprintf((0,"ogl_freetexture(%p):%i (last rend %is) (%i left)\n",gltexture,gltexture->handle,(GameTime-gltexture->lastrend)/f1_0,r_texcount));
1480                 glDeleteTextures( 1, &gltexture->handle );
1481 //              gltexture->handle=0;
1482                 ogl_init_texture(gltexture);
1483         }
1484 }
1485 void ogl_freebmtexture(grs_bitmap *bm){
1486         if (bm->gltexture){
1487                 ogl_freetexture(bm->gltexture);
1488                 bm->gltexture=NULL;
1489 //              r_texcount--;
1490 //              glmprintf((0,"ogl_freebmtexture(%p,%p):%i (%i left)\n",bm->bm_data,&bm->gltexture,bm->gltexture,r_texcount));
1491 //              glDeleteTextures( 1, &bm->gltexture );
1492 //              bm->gltexture=-1;
1493         }
1494 }