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