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