]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/grgl1render.cpp
minor cleanup and performance improvements
[taylor/freespace2.git] / src / graphics / grgl1render.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 #include "pstypes.h"
10 #include "2d.h"
11 #include "gropengl.h"
12 #include "grgl1.h"
13 #include "gropenglinternal.h"
14 #include "bmpman.h"
15 #include "grinternal.h"
16 #include "3d.h"
17 #include "neb.h"
18 #include "line.h"
19 #include "palman.h"
20
21
22 extern int OGL_fog_mode;
23
24 #define NEBULA_COLORS 20
25
26
27 static void opengl1_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
28 {
29         int saved_zbuf;
30         vertex v[4];
31         vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
32
33         saved_zbuf = gr_zbuffer_get();
34
35         // start the frame, no zbuffering, no culling
36         g3_start_frame(1);
37         gr_zbuffer_set(GR_ZBUFF_NONE);
38         gr_set_cull(0);
39
40         // stuff coords
41         v[0].sx = i2fl(x);
42         v[0].sy = i2fl(y);
43         v[0].sw = 0.0f;
44         v[0].u = 0.0f;
45         v[0].v = 0.0f;
46         v[0].flags = PF_PROJECTED;
47         v[0].codes = 0;
48         v[0].r = (ubyte)r;
49         v[0].g = (ubyte)g;
50         v[0].b = (ubyte)b;
51         v[0].a = (ubyte)a;
52
53         v[1].sx = i2fl(x + w);
54         v[1].sy = i2fl(y);
55         v[1].sw = 0.0f;
56         v[1].u = 0.0f;
57         v[1].v = 0.0f;
58         v[1].flags = PF_PROJECTED;
59         v[1].codes = 0;
60         v[1].r = (ubyte)r;
61         v[1].g = (ubyte)g;
62         v[1].b = (ubyte)b;
63         v[1].a = (ubyte)a;
64
65         v[2].sx = i2fl(x + w);
66         v[2].sy = i2fl(y + h);
67         v[2].sw = 0.0f;
68         v[2].u = 0.0f;
69         v[2].v = 0.0f;
70         v[2].flags = PF_PROJECTED;
71         v[2].codes = 0;
72         v[2].r = (ubyte)r;
73         v[2].g = (ubyte)g;
74         v[2].b = (ubyte)b;
75         v[2].a = (ubyte)a;
76
77         v[3].sx = i2fl(x);
78         v[3].sy = i2fl(y + h);
79         v[3].sw = 0.0f;
80         v[3].u = 0.0f;
81         v[3].v = 0.0f;
82         v[3].flags = PF_PROJECTED;
83         v[3].codes = 0;
84         v[3].r = (ubyte)r;
85         v[3].g = (ubyte)g;
86         v[3].b = (ubyte)b;
87         v[3].a = (ubyte)a;
88
89         // draw the polys
90         g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
91
92         g3_end_frame();
93
94         // restore zbuffer and culling
95         gr_zbuffer_set(saved_zbuf);
96         gr_set_cull(1);
97 }
98
99 static void opengl1_aabitmap_ex_internal(int x,int y,int w,int h,int sx,int sy)
100 {
101         if ( (w < 1) || (h < 1) ) {
102                 return;
103         }
104
105         if ( !gr_screen.current_color.is_alphacolor ) {
106                 return;
107         }
108
109         float u_scale, v_scale;
110
111         if ( !opengl1_tcache_set(gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP,
112                         &u_scale, &v_scale, 0, -1, -1, 0) )
113         {
114                 // Couldn't set texture
115                 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
116                 return;
117         }
118
119         opengl1_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
120
121         float u0, u1, v0, v1;
122         float x1, x2, y1, y2;
123         int bw, bh;
124
125         bm_get_info( gr_screen.current_bitmap, &bw, &bh );
126
127         u0 = u_scale*i2fl(sx)/i2fl(bw);
128         v0 = v_scale*i2fl(sy)/i2fl(bh);
129
130         u1 = u_scale*i2fl(sx+w)/i2fl(bw);
131         v1 = v_scale*i2fl(sy+h)/i2fl(bh);
132
133         x1 = i2fl(x+gr_screen.offset_x);
134         y1 = i2fl(y+gr_screen.offset_y);
135         x2 = i2fl(x+w+gr_screen.offset_x);
136         y2 = i2fl(y+h+gr_screen.offset_y);
137
138         glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green,
139                         gr_screen.current_color.blue,gr_screen.current_color.alpha);
140
141         opengl_alloc_render_buffer(4);
142
143         render_buffer[0].x = x1;
144         render_buffer[0].y = y1;
145         render_buffer[0].u = u0;
146         render_buffer[0].v = v0;
147
148         render_buffer[1].x = x1;
149         render_buffer[1].y = y2;
150         render_buffer[1].u = u0;
151         render_buffer[1].v = v1;
152
153         render_buffer[2].x = x2;
154         render_buffer[2].y = y1;
155         render_buffer[2].u = u1;
156         render_buffer[2].v = v0;
157
158         render_buffer[3].x = x2;
159         render_buffer[3].y = y2;
160         render_buffer[3].u = u1;
161         render_buffer[3].v = v1;
162
163         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
164         glEnableClientState(GL_VERTEX_ARRAY);
165
166         glTexCoordPointer(2, GL_FLOAT, sizeof(rb_t), &render_buffer[0].u);
167         glVertexPointer(2, GL_FLOAT, sizeof(rb_t), &render_buffer[0].x);
168
169         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
170
171         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
172         glDisableClientState(GL_VERTEX_ARRAY);
173 }
174
175 static void opengl1_stuff_fog_value(float z, float *f_val)
176 {
177         float f_float;
178
179         if ( !f_val ) {
180                 return;
181         }
182
183         f_float = 1.0f - ((gr_screen.fog_far - z) / (gr_screen.fog_far - gr_screen.fog_near));
184
185         if (f_float < 0.0f) {
186                 f_float = 0.0f;
187         } else if (f_float > 1.0f) {
188                 f_float = 1.0f;
189         }
190
191         *f_val = f_float;
192 }
193
194 static void opengl1_tmapper_internal( int nv, vertex ** verts, uint flags, int is_scaler )
195 {
196         int i;
197         float u_scale = 1.0f, v_scale = 1.0f;
198
199         gr_texture_source texture_source = TEXTURE_SOURCE_NONE;
200         gr_alpha_blend alpha_blend = ALPHA_BLEND_ALPHA_BLEND_ALPHA;
201         gr_zbuffer_type zbuffer_type = ZBUFFER_TYPE_NONE;
202
203         if (Gr_zbuffering) {
204                 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
205                         zbuffer_type = ZBUFFER_TYPE_READ;
206                 } else {
207                         zbuffer_type = ZBUFFER_TYPE_FULL;
208                 }
209         }
210
211         int tmap_type = TCACHE_TYPE_NORMAL;
212
213         ubyte r = 255, g = 255, b = 255, a = 255;
214
215         if ( !(flags & TMAP_FLAG_TEXTURED) ) {
216                 r = gr_screen.current_color.red;
217                 g = gr_screen.current_color.green;
218                 b = gr_screen.current_color.blue;
219         }
220
221         if (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) {
222                 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
223
224                 // Blend with screen pixel using src*alpha+dst
225
226                 if (gr_screen.current_alpha < 1.0f) {
227                         r = ubyte((r * gr_screen.current_alpha) + 0.5f);
228                         g = ubyte((g * gr_screen.current_alpha) + 0.5f);
229                         b = ubyte((b * gr_screen.current_alpha) + 0.5f);
230                 }
231         }
232
233         if (flags & TMAP_FLAG_BITMAP_SECTION) {
234                 SDL_assert( !(flags & TMAP_FLAG_BITMAP_INTERFACE) );
235                 tmap_type = TCACHE_TYPE_BITMAP_SECTION;
236         } else if (flags & TMAP_FLAG_BITMAP_INTERFACE) {
237                 SDL_assert( !(flags & TMAP_FLAG_BITMAP_SECTION) );
238                 tmap_type = TCACHE_TYPE_BITMAP_INTERFACE;
239         }
240
241         if (flags & TMAP_FLAG_TEXTURED) {
242                 if ( !opengl1_tcache_set(gr_screen.current_bitmap, tmap_type, &u_scale,
243                                 &v_scale, 0, gr_screen.current_bitmap_sx, gr_screen.current_bitmap_sy, 0) )
244                 {
245                         mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
246                         return;
247                 }
248
249                 // use non-filtered textures for bitmap sections and UI graphics
250                 switch (tmap_type) {
251                         case TCACHE_TYPE_BITMAP_INTERFACE:
252                         case TCACHE_TYPE_BITMAP_SECTION:
253                                 texture_source = TEXTURE_SOURCE_NO_FILTERING;
254                                 break;
255
256                         default:
257                                 texture_source = TEXTURE_SOURCE_DECAL;
258                                 break;
259                 }
260         }
261
262
263         opengl1_set_state( texture_source, alpha_blend, zbuffer_type );
264
265         float ox = gr_screen.offset_x * 16.0f;
266         float oy = gr_screen.offset_y * 16.0f;
267
268         float fr = 1.0f, fg = 1.0f, fb = 1.0f;
269
270         if (flags & TMAP_FLAG_PIXEL_FOG) {
271                 int r, g, b;
272                 int ra, ga, ba;
273                 float sx, sy;
274
275                 ra = ga = ba = 0;
276
277                 /* argh */
278                 for (i=nv-1;i>=0;i--)   // DDOI - change polygon winding
279                 {
280                         vertex * va = verts[i];
281
282                         sx = (va->sx * 16.0f + ox) / 16.0f;
283                         sy = (va->sy * 16.0f + oy) / 16.0f;
284
285                         neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
286
287                         ra += r;
288                         ga += g;
289                         ba += b;
290                 }
291
292                 ra /= nv;
293                 ga /= nv;
294                 ba /= nv;
295
296                 gr_fog_set(GR_FOGMODE_FOG, ra, ga, ba, -1.0f, -1.0f);
297
298                 fr = ra / 255.0f;
299                 fg = ga / 255.0f;
300                 fb = ba / 255.0f;
301         }
302
303         opengl_alloc_render_buffer(nv);
304
305         int rb_offset = 0;
306
307         float sx, sy, sz = 0.99f, rhw = 1.0f;
308
309         for (i = nv-1; i >= 0; i--) {
310                 vertex *va = verts[i];
311
312                 if ( Gr_zbuffering || (flags & TMAP_FLAG_NEBULA) ) {
313                         sz = 1.0f - 1.0f / (1.0f + va->z / (32768.0f / 256.0f));
314
315                         if ( sz > 0.98f ) {
316                                 sz = 0.98f;
317                         }
318                 }
319
320                 if (flags & TMAP_FLAG_CORRECT) {
321                         rhw = 1.0f / va->sw;
322                 }
323
324                 if (flags & TMAP_FLAG_ALPHA) {
325                         a = verts[i]->a;
326                 }
327
328                 if (flags & TMAP_FLAG_NEBULA ) {
329                         int pal = (verts[i]->b*(NEBULA_COLORS-1))/255;
330                         r = gr_palette[pal*3+0];
331                         g = gr_palette[pal*3+1];
332                         b = gr_palette[pal*3+2];
333                 } else if ( (flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD) )   {
334                         r = g = b = Gr_gamma_lookup[verts[i]->b];
335                 } else if ( (flags & TMAP_FLAG_RGB)  && (flags & TMAP_FLAG_GOURAUD) )   {
336                         // Make 0.75 be 256.0f
337                         r = Gr_gamma_lookup[verts[i]->r];
338                         g = Gr_gamma_lookup[verts[i]->g];
339                         b = Gr_gamma_lookup[verts[i]->b];
340                 }
341
342                 render_buffer[rb_offset].r = r;
343                 render_buffer[rb_offset].g = g;
344                 render_buffer[rb_offset].b = b;
345                 render_buffer[rb_offset].a = a;
346
347                 if ( (flags & TMAP_FLAG_PIXEL_FOG) && (OGL_fog_mode == 1) ) {
348                         float f_val;
349
350                         opengl1_stuff_fog_value(va->z, &f_val);
351
352                         render_buffer[rb_offset].sr = (ubyte)(((fr * f_val) * 255.0f) + 0.5f);
353                         render_buffer[rb_offset].sg = (ubyte)(((fg * f_val) * 255.0f) + 0.5f);
354                         render_buffer[rb_offset].sb = (ubyte)(((fb * f_val) * 255.0f) + 0.5f);
355                 }
356
357                 sx = (va->sx * 16.0f + ox) / 16.0f;
358                 sy = (va->sy * 16.0f + oy) / 16.0f;
359
360                 if (flags & TMAP_FLAG_TEXTURED) {
361                         render_buffer[rb_offset].u = va->u * u_scale;
362                         render_buffer[rb_offset].v = va->v * v_scale;
363                 }
364
365                 render_buffer[rb_offset].x = sx * rhw;
366                 render_buffer[rb_offset].y = sy * rhw;
367                 render_buffer[rb_offset].z = -sz * rhw;
368                 render_buffer[rb_offset].w = rhw;
369
370                 ++rb_offset;
371         }
372
373         if (flags & TMAP_FLAG_TEXTURED) {
374                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
375                 glTexCoordPointer(2, GL_FLOAT, sizeof(rb_t), &render_buffer[0].u);
376         }
377
378         if ( (gr_screen.current_fog_mode != GR_FOGMODE_NONE) && (OGL_fog_mode == 1) ) {
379                 glEnableClientState(GL_SECONDARY_COLOR_ARRAY);
380                 vglSecondaryColorPointer(3, GL_UNSIGNED_BYTE, sizeof(rb_t), &render_buffer[0].sr);
381         }
382
383         glEnableClientState(GL_COLOR_ARRAY);
384         glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(rb_t), &render_buffer[0].r);
385
386         glEnableClientState(GL_VERTEX_ARRAY);
387         glVertexPointer(4, GL_FLOAT, sizeof(rb_t), &render_buffer[0].x);
388
389         glDrawArrays(GL_TRIANGLE_FAN, 0, rb_offset);
390
391         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
392         glDisableClientState(GL_SECONDARY_COLOR_ARRAY);
393         glDisableClientState(GL_COLOR_ARRAY);
394         glDisableClientState(GL_VERTEX_ARRAY);
395 }
396
397 void gr_opengl1_rect(int x,int y,int w,int h)
398 {
399         opengl1_rect_internal(x, y, w, h, gr_screen.current_color.red,
400                         gr_screen.current_color.green, gr_screen.current_color.blue,
401                         gr_screen.current_color.alpha);
402 }
403
404 void gr_opengl1_shade(int x,int y,int w,int h)
405 {
406         int r,g,b,a;
407
408         float shade1 = 1.0f;
409         float shade2 = 6.0f;
410
411         r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
412         if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
413         g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
414         if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
415         b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
416         if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
417         a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
418         if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
419
420         opengl1_rect_internal(x, y, w, h, r, g, b, a);
421 }
422
423 void gr_opengl1_aabitmap_ex(int x,int y,int w,int h,int sx,int sy)
424 {
425         int reclip;
426         #ifndef NDEBUG
427         int count = 0;
428         #endif
429
430         int dx1=x, dx2=x+w-1;
431         int dy1=y, dy2=y+h-1;
432
433         int bw, bh;
434         bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
435
436         do {
437                 reclip = 0;
438                 #ifndef NDEBUG
439                         if ( count > 1 ) Int3();
440                         count++;
441                 #endif
442
443                 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
444                 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
445                 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
446                 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
447                 if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
448                 if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
449
450                 if ( sx < 0 ) {
451                         dx1 -= sx;
452                         sx = 0;
453                         reclip = 1;
454                 }
455
456                 if ( sy < 0 ) {
457                         dy1 -= sy;
458                         sy = 0;
459                         reclip = 1;
460                 }
461
462                 w = dx2-dx1+1;
463                 h = dy2-dy1+1;
464
465                 if ( sx + w > bw ) {
466                         w = bw - sx;
467                         dx2 = dx1 + w - 1;
468                 }
469
470                 if ( sy + h > bh ) {
471                         h = bh - sy;
472                         dy2 = dy1 + h - 1;
473                 }
474
475                 if ( w < 1 ) return;            // clipped away!
476                 if ( h < 1 ) return;            // clipped away!
477
478         } while (reclip);
479
480         // Make sure clipping algorithm works
481         #ifndef NDEBUG
482                 SDL_assert( w > 0 );
483                 SDL_assert( h > 0 );
484                 SDL_assert( w == (dx2-dx1+1) );
485                 SDL_assert( h == (dy2-dy1+1) );
486                 SDL_assert( sx >= 0 );
487                 SDL_assert( sy >= 0 );
488                 SDL_assert( sx+w <= bw );
489                 SDL_assert( sy+h <= bh );
490                 SDL_assert( dx2 >= dx1 );
491                 SDL_assert( dy2 >= dy1 );
492                 SDL_assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
493                 SDL_assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
494                 SDL_assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
495                 SDL_assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
496         #endif
497
498         // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
499         opengl1_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
500 }
501
502 void gr_opengl1_aabitmap(int x, int y)
503 {
504         int w, h;
505
506         bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
507         int dx1=x, dx2=x+w-1;
508         int dy1=y, dy2=y+h-1;
509         int sx=0, sy=0;
510
511         if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
512         if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
513         if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
514         if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
515         if ( dx2 > gr_screen.clip_right )       { dx2 = gr_screen.clip_right; }
516         if ( dy2 > gr_screen.clip_bottom )      { dy2 = gr_screen.clip_bottom; }
517
518         if ( sx < 0 ) return;
519         if ( sy < 0 ) return;
520         if ( sx >= w ) return;
521         if ( sy >= h ) return;
522
523         // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
524         gr_opengl1_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
525 }
526
527 void gr_opengl1_string( int sx, int sy, const char *s )
528 {
529         int width, spacing, letter;
530         int x, y;
531         int rb_offset;
532         float u_scale, v_scale;
533         float u0, u1, v0, v1;
534         float x1, x2, y1, y2;
535         int bw, bh;
536         float fbw, fbh;
537
538         if ( !Current_font )    {
539                 return;
540         }
541
542         gr_set_bitmap(Current_font->bitmap_id, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
543
544         if ( !opengl1_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 0, -1, -1, 0 ) )  {
545                 // Couldn't set texture
546                 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
547                 return;
548         }
549
550         bm_get_info( gr_screen.current_bitmap, &bw, &bh );
551
552         fbw = 1.0f / i2fl(bw);
553         fbh = 1.0f / i2fl(bh);
554
555         opengl1_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
556
557         // don't want to create a super huge buffer size (i.e. credits text)
558         const int alocsize = 320;       // 80 characters max per render call
559         opengl_alloc_render_buffer(alocsize);
560
561         glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green,
562                         gr_screen.current_color.blue, gr_screen.current_color.alpha);
563
564         glEnableClientState(GL_VERTEX_ARRAY);
565         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
566
567         glVertexPointer(2, GL_FLOAT, sizeof(rb_t), &render_buffer[0].x);
568         glTexCoordPointer(2, GL_FLOAT, sizeof(rb_t), &render_buffer[0].u);
569
570         y = sy;
571
572         if (sx==0x8000) {                       //centered
573                 x = get_centered_x(s);
574         } else {
575                 x = sx;
576         }
577
578         spacing = 0;
579         rb_offset = 0;
580
581         while (*s)      {
582                 x += spacing;
583
584                 while (*s== '\n' )      {
585                         s++;
586                         y += Current_font->h;
587                         if (sx==0x8000) {                       //centered
588                                 x = get_centered_x(s);
589                         } else {
590                                 x = sx;
591                         }
592                 }
593                 if (*s == 0 ) break;
594
595                 letter = get_char_width(s[0],s[1],&width,&spacing);
596                 s++;
597
598                 //not in font, draw as space
599                 if (letter<0)   {
600                         continue;
601                 }
602
603                 int xd, yd, xc, yc;
604                 int wc, hc;
605
606                 // Check if this character is totally clipped
607                 if ( x + width < gr_screen.clip_left ) continue;
608                 if ( y + Current_font->h < gr_screen.clip_top ) continue;
609                 if ( x > gr_screen.clip_right ) continue;
610                 if ( y > gr_screen.clip_bottom ) continue;
611
612                 xd = yd = 0;
613                 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
614                 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
615                 xc = x+xd;
616                 yc = y+yd;
617
618                 wc = width - xd; hc = Current_font->h - yd;
619                 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
620                 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
621
622                 if ( wc < 1 ) continue;
623                 if ( hc < 1 ) continue;
624
625                 float u = i2fl(Current_font->bm_u[letter] + xd);
626                 float v = i2fl(Current_font->bm_v[letter] + yd);
627
628                 x1 = i2fl(xc + gr_screen.offset_x);
629                 y1 = i2fl(yc + gr_screen.offset_y);
630                 x2 = x1 + i2fl(wc);
631                 y2 = y1 + i2fl(hc);
632
633                 u0 = u_scale * (u * fbw);
634                 v0 = v_scale * (v * fbh);
635
636                 u1 = u_scale * ((u+i2fl(wc)) * fbw);
637                 v1 = v_scale * ((v+i2fl(hc)) * fbh);
638
639                 // maybe go ahead and draw
640                 if (rb_offset == alocsize) {
641                         glDrawArrays(GL_TRIANGLE_STRIP, 0, rb_offset);
642                         rb_offset = 0;
643                 }
644
645                 render_buffer[rb_offset].x = x1;
646                 render_buffer[rb_offset].y = y1;
647                 render_buffer[rb_offset].u = u0;
648                 render_buffer[rb_offset].v = v0;
649                 ++rb_offset;
650
651                 render_buffer[rb_offset].x = x1;
652                 render_buffer[rb_offset].y = y2;
653                 render_buffer[rb_offset].u = u0;
654                 render_buffer[rb_offset].v = v1;
655                 ++rb_offset;
656
657                 render_buffer[rb_offset].x = x2;
658                 render_buffer[rb_offset].y = y1;
659                 render_buffer[rb_offset].u = u1;
660                 render_buffer[rb_offset].v = v0;
661                 ++rb_offset;
662
663                 render_buffer[rb_offset].x = x2;
664                 render_buffer[rb_offset].y = y2;
665                 render_buffer[rb_offset].u = u1;
666                 render_buffer[rb_offset].v = v1;
667                 ++rb_offset;
668         }
669
670         if (rb_offset) {
671                 glDrawArrays(GL_TRIANGLE_STRIP, 0, rb_offset);
672         }
673
674         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
675         glDisableClientState(GL_VERTEX_ARRAY);
676 }
677
678 void gr_opengl1_line(int x1,int y1,int x2,int y2)
679 {
680         opengl1_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
681
682         INT_CLIPLINE(x1,y1,x2,y2,gr_screen.clip_left,gr_screen.clip_top,
683                         gr_screen.clip_right,gr_screen.clip_bottom,return,void(),void());
684
685         float sx1, sy1;
686         float sx2, sy2;
687
688         sx1 = i2fl(x1 + gr_screen.offset_x)+0.5f;
689         sy1 = i2fl(y1 + gr_screen.offset_y)+0.5f;
690         sx2 = i2fl(x2 + gr_screen.offset_x)+0.5f;
691         sy2 = i2fl(y2 + gr_screen.offset_y)+0.5f;
692
693         opengl_alloc_render_buffer(2);
694
695         if ( x1 == x2 && y1 == y2 ) {
696                 render_buffer[0].x = sx1;
697                 render_buffer[0].y = sy1;
698                 render_buffer[0].z = -0.99f;
699
700                 glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green,
701                                 gr_screen.current_color.blue, gr_screen.current_color.alpha);
702
703                 glEnableClientState(GL_VERTEX_ARRAY);
704                 glVertexPointer(3, GL_FLOAT, sizeof(rb_t), &render_buffer[0].x);
705
706                 glDrawArrays(GL_POINTS, 0, 1);
707
708                 glDisableClientState(GL_VERTEX_ARRAY);
709
710                 return;
711         }
712
713         if ( x1 == x2 ) {
714                 if ( sy1 < sy2 )    {
715                         sy2 += 0.5f;
716                 } else {
717                         sy1 += 0.5f;
718                 }
719         } else if ( y1 == y2 )  {
720                 if ( sx1 < sx2 )    {
721                         sx2 += 0.5f;
722                 } else {
723                         sx1 += 0.5f;
724                 }
725         }
726
727         render_buffer[0].x = sx2;
728         render_buffer[0].y = sy2;
729         render_buffer[0].z = -0.99f;
730
731         render_buffer[1].x = sx1;
732         render_buffer[1].y = sy1;
733         render_buffer[1].z = -0.99f;
734
735         glColor4ub(gr_screen.current_color.red, gr_screen.current_color.green,
736                         gr_screen.current_color.blue, gr_screen.current_color.alpha);
737
738         glEnableClientState(GL_VERTEX_ARRAY);
739         glVertexPointer(3, GL_FLOAT, sizeof(rb_t), &render_buffer[0].x);
740
741         glDrawArrays(GL_LINES, 0, 2);
742
743         glDisableClientState(GL_VERTEX_ARRAY);
744 }
745
746 void gr_opengl1_aaline(vertex *v1, vertex *v2)
747 {
748         gr_opengl1_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
749 }
750
751 void gr_opengl1_gradient(int x1,int y1,int x2,int y2)
752 {
753         int swapped=0;
754
755         if ( !gr_screen.current_color.is_alphacolor )   {
756                 gr_line( x1, y1, x2, y2 );
757                 return;
758         }
759
760         INT_CLIPLINE(x1,y1,x2,y2,gr_screen.clip_left,gr_screen.clip_top,
761                         gr_screen.clip_right,gr_screen.clip_bottom,return,void(),swapped=1);
762
763         opengl1_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
764
765         int aa = swapped ? 0 : gr_screen.current_color.alpha;
766         int ba = swapped ? gr_screen.current_color.alpha : 0;
767
768         float sx1, sy1;
769         float sx2, sy2;
770
771         sx1 = i2fl(x1 + gr_screen.offset_x)+0.5f;
772         sy1 = i2fl(y1 + gr_screen.offset_y)+0.5f;
773         sx2 = i2fl(x2 + gr_screen.offset_x)+0.5f;
774         sy2 = i2fl(y2 + gr_screen.offset_y)+0.5f;
775
776         if ( x1 == x2 ) {
777                 if ( sy1 < sy2 )    {
778                         sy2 += 0.5f;
779                 } else {
780                         sy1 += 0.5f;
781                 }
782         } else if ( y1 == y2 )  {
783                 if ( sx1 < sx2 )    {
784                         sx2 += 0.5f;
785                 } else {
786                         sx1 += 0.5f;
787                 }
788         }
789
790         opengl_alloc_render_buffer(2);
791
792         render_buffer[0].r = gr_screen.current_color.red;
793         render_buffer[0].g = gr_screen.current_color.green;
794         render_buffer[0].b = gr_screen.current_color.blue;
795         render_buffer[0].a = (ubyte)ba;
796         render_buffer[0].x = sx2;
797         render_buffer[0].y = sy2;
798         render_buffer[0].z = -0.99f;
799
800         render_buffer[1].r = gr_screen.current_color.red;
801         render_buffer[1].g = gr_screen.current_color.green;
802         render_buffer[1].b = gr_screen.current_color.blue;
803         render_buffer[1].a = (ubyte)aa;
804         render_buffer[1].x = sx1;
805         render_buffer[1].y = sy1;
806         render_buffer[1].z = -0.99f;
807
808         glEnableClientState(GL_COLOR_ARRAY);
809         glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(rb_t), &render_buffer[0].r);
810
811         glEnableClientState(GL_VERTEX_ARRAY);
812         glVertexPointer(3, GL_FLOAT, sizeof(rb_t), &render_buffer[0].x);
813
814         glDrawArrays(GL_LINES, 0, 2);
815
816         glDisableClientState(GL_COLOR_ARRAY);
817         glDisableClientState(GL_VERTEX_ARRAY);
818 }
819
820 void gr_opengl1_circle( int xc, int yc, int d )
821 {
822         int p,x, y, r;
823
824         r = d/2;
825         p=3-d;
826         x=0;
827         y=r;
828
829         // Big clip
830         if ( (xc+r) < gr_screen.clip_left ) return;
831         if ( (xc-r) > gr_screen.clip_right ) return;
832         if ( (yc+r) < gr_screen.clip_top ) return;
833         if ( (yc-r) > gr_screen.clip_bottom ) return;
834
835         while(x<y)      {
836                 // Draw the first octant
837                 gr_opengl1_line( xc-y, yc-x, xc+y, yc-x );
838                 gr_opengl1_line( xc-y, yc+x, xc+y, yc+x );
839
840                 if (p<0)
841                         p=p+(x<<2)+6;
842                 else    {
843                         // Draw the second octant
844                         gr_opengl1_line( xc-x, yc-y, xc+x, yc-y );
845                         gr_opengl1_line( xc-x, yc+y, xc+x, yc+y );
846
847                         p=p+((x-y)<<2)+10;
848                         y--;
849                 }
850                 x++;
851         }
852         if(x==y) {
853                 gr_opengl1_line( xc-x, yc-y, xc+x, yc-y );
854                 gr_opengl1_line( xc-x, yc+y, xc+x, yc+y );
855         }
856         return;
857 }
858
859 void gr_opengl1_pixel(int x, int y)
860 {
861         gr_line(x,y,x,y);
862 }
863
864
865 // cross fade
866 void gr_opengl1_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
867 {
868         gr_set_bitmap(bmap1, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 1.0f - pct );
869         gr_bitmap(x1, y1);
870
871         gr_set_bitmap(bmap2, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, pct );
872         gr_bitmap(x2, y2);
873 }
874
875 void gr_opengl1_flash(int r, int g, int b)
876 {
877         CAP(r,0,255);
878         CAP(g,0,255);
879         CAP(b,0,255);
880
881         if ( r || g || b ) {
882                 opengl1_set_state( TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE );
883
884                 float x1, x2, y1, y2;
885                 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
886                 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
887                 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
888                 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
889
890                 glColor4ub((GLubyte)r, (GLubyte)g, (GLubyte)b, 255);
891
892                 opengl_alloc_render_buffer(4);
893
894                 render_buffer[0].x = x1;
895                 render_buffer[0].y = y1;
896                 render_buffer[0].z = -0.99f;
897
898                 render_buffer[1].x = x1;
899                 render_buffer[1].y = y2;
900                 render_buffer[1].z = -0.99f;
901
902                 render_buffer[2].x = x2;
903                 render_buffer[2].y = y1;
904                 render_buffer[2].z = -0.99f;
905
906                 render_buffer[3].x = x2;
907                 render_buffer[3].y = y2;
908                 render_buffer[3].z = -0.99f;
909
910                 glEnableClientState(GL_VERTEX_ARRAY);
911                 glVertexPointer(3, GL_FLOAT, sizeof(rb_t), &render_buffer[0].x);
912
913                 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
914
915                 glDisableClientState(GL_VERTEX_ARRAY);
916         }
917 }
918
919 void gr_opengl1_tmapper( int nverts, vertex **verts, uint flags )
920 {
921         opengl1_tmapper_internal( nverts, verts, flags, 0 );
922 }
923
924 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
925
926 void gr_opengl1_scaler(vertex *va, vertex *vb )
927 {
928         float x0, y0, x1, y1;
929         float u0, v0, u1, v1;
930         float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
931         float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
932         float xmin, xmax, ymin, ymax;
933         int dx0, dy0, dx1, dy1;
934
935         //============= CLIP IT =====================
936
937         x0 = va->sx; y0 = va->sy;
938         x1 = vb->sx; y1 = vb->sy;
939
940         xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
941         xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
942
943         u0 = va->u; v0 = va->v;
944         u1 = vb->u; v1 = vb->v;
945
946         // Check for obviously offscreen bitmaps...
947         if ( (y1<=y0) || (x1<=x0) ) return;
948         if ( (x1<xmin ) || (x0>xmax) ) return;
949         if ( (y1<ymin ) || (y0>ymax) ) return;
950
951         clipped_u0 = u0; clipped_v0 = v0;
952         clipped_u1 = u1; clipped_v1 = v1;
953
954         clipped_x0 = x0; clipped_y0 = y0;
955         clipped_x1 = x1; clipped_y1 = y1;
956
957         // Clip the left, moving u0 right as necessary
958         if ( x0 < xmin )        {
959                 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
960                 clipped_x0 = xmin;
961         }
962
963         // Clip the right, moving u1 left as necessary
964         if ( x1 > xmax )        {
965                 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
966                 clipped_x1 = xmax;
967         }
968
969         // Clip the top, moving v0 down as necessary
970         if ( y0 < ymin )        {
971                 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
972                 clipped_y0 = ymin;
973         }
974
975         // Clip the bottom, moving v1 up as necessary
976         if ( y1 > ymax )        {
977                 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
978                 clipped_y1 = ymax;
979         }
980
981         dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
982         dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
983
984         if (dx1<=dx0) return;
985         if (dy1<=dy0) return;
986
987         //============= DRAW IT =====================
988
989         vertex v[4];
990         vertex *vl[4];
991
992         vl[0] = &v[0];
993         v[0].sx = clipped_x0;
994         v[0].sy = clipped_y0;
995         v[0].sw = va->sw;
996         v[0].z = va->z;
997         v[0].u = clipped_u0;
998         v[0].v = clipped_v0;
999
1000         vl[1] = &v[1];
1001         v[1].sx = clipped_x1;
1002         v[1].sy = clipped_y0;
1003         v[1].sw = va->sw;
1004         v[1].z = va->z;
1005         v[1].u = clipped_u1;
1006         v[1].v = clipped_v0;
1007
1008         vl[2] = &v[2];
1009         v[2].sx = clipped_x1;
1010         v[2].sy = clipped_y1;
1011         v[2].sw = va->sw;
1012         v[2].z = va->z;
1013         v[2].u = clipped_u1;
1014         v[2].v = clipped_v1;
1015
1016         vl[3] = &v[3];
1017         v[3].sx = clipped_x0;
1018         v[3].sy = clipped_y1;
1019         v[3].sw = va->sw;
1020         v[3].z = va->z;
1021         v[3].u = clipped_u0;
1022         v[3].v = clipped_v1;
1023
1024         opengl1_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );
1025 }