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