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