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