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