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