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