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