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