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