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