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