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