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