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