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