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