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