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