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