2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
9 #include "SDL_opengles2.h"
14 #include "gropenglinternal.h"
17 #include "grinternal.h"
24 #define NEBULA_COLORS 20
33 static void opengl2_tmapper_internal(int nv, vertex **verts, uint flags, int is_scaler)
36 float u_scale = 1.0f, v_scale = 1.0f;
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;
43 if ( is_scaler || (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) ) {
44 zbuffer_type = ZBUFFER_TYPE_READ;
46 zbuffer_type = ZBUFFER_TYPE_FULL;
50 int tmap_type = TCACHE_TYPE_NORMAL;
52 ubyte r = 255, g = 255, b = 255, a = 255;
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;
60 if (gr_screen.current_alphablend_mode == GR_ALPHABLEND_FILTER) {
61 alpha_blend = ALPHA_BLEND_ALPHA_ADDITIVE;
63 // Blend with screen pixel using src*alpha+dst
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);
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;
80 if (flags & TMAP_FLAG_TEXTURED) {
81 if ( !opengl2_tcache_set(gr_screen.current_bitmap, tmap_type, &u_scale,
84 mprintf(( "Not rendering a texture because it didn't fit in VRAM!\n" ));
88 // use non-filtered textures for bitmap sections and UI graphics
90 case TCACHE_TYPE_BITMAP_INTERFACE:
91 case TCACHE_TYPE_BITMAP_SECTION:
92 texture_source = TEXTURE_SOURCE_NO_FILTERING;
96 texture_source = TEXTURE_SOURCE_DECAL;
102 opengl2_set_state( texture_source, alpha_blend, zbuffer_type );
104 float ox = gr_screen.offset_x * 16.0f;
105 float oy = gr_screen.offset_y * 16.0f;
107 if (flags & TMAP_FLAG_PIXEL_FOG) {
114 for (i = nv-1; i >= 0; i--) {
115 vertex * va = verts[i];
117 sx = (va->sx * 16.0f + ox) / 16.0f;
118 sy = (va->sy * 16.0f + oy) / 16.0f;
120 neb2_get_pixel((int)sx, (int)sy, &r, &g, &b);
131 gr_opengl2_fog_set(GR_FOGMODE_FOG, ra, ga, ba, gr_screen.fog_near, gr_screen.fog_far);
134 opengl_alloc_render_buffer(nv);
138 float sx, sy, sz = 0.99f, rhw = 1.0f;
140 bool bZval = (Gr_zbuffering || (flags & TMAP_FLAG_NEBULA));
141 bool bCorrect = (flags & TMAP_FLAG_CORRECT);
142 bool bAlpha = (flags & TMAP_FLAG_ALPHA);
143 bool bNebula = (flags & TMAP_FLAG_NEBULA);
144 bool bRamp = ((flags & TMAP_FLAG_RAMP) && (flags & TMAP_FLAG_GOURAUD));
145 bool bRGB = ((flags & TMAP_FLAG_RGB) && (flags & TMAP_FLAG_GOURAUD));
146 bool bTextured = (flags & TMAP_FLAG_TEXTURED);
147 bool bFog = (flags & TMAP_FLAG_PIXEL_FOG);
149 for (i = nv-1; i >= 0; i--) {
150 vertex *va = verts[i];
153 sz = 1.0f - 1.0f / (1.0f + va->z / (32768.0f / 256.0f));
169 // Make 0.75 be 256.0f
170 r = Gr_gamma_lookup[va->r];
171 g = Gr_gamma_lookup[va->g];
172 b = Gr_gamma_lookup[va->b];
173 } else if (bNebula) {
174 int pal = (va->b*(NEBULA_COLORS-1))/255;
175 r = gr_palette[pal*3+0];
176 g = gr_palette[pal*3+1];
177 b = gr_palette[pal*3+2];
179 r = g = b = Gr_gamma_lookup[va->b];
182 render_buffer[rb_offset].r = r;
183 render_buffer[rb_offset].g = g;
184 render_buffer[rb_offset].b = b;
185 render_buffer[rb_offset].a = a;
190 opengl_stuff_fog_value(va->z, &f_val);
192 render_buffer[rb_offset].sr = gr_screen.current_fog_color.red;
193 render_buffer[rb_offset].sg = gr_screen.current_fog_color.green;
194 render_buffer[rb_offset].sb = gr_screen.current_fog_color.blue;
195 render_buffer[rb_offset].sa = (ubyte)(f_val * 255.0f);
198 sx = (va->sx * 16.0f + ox) / 16.0f;
199 sy = (va->sy * 16.0f + oy) / 16.0f;
202 render_buffer[rb_offset].u = va->u * u_scale;
203 render_buffer[rb_offset].v = va->v * v_scale;
206 render_buffer[rb_offset].x = sx * rhw;
207 render_buffer[rb_offset].y = sy * rhw;
208 render_buffer[rb_offset].z = -sz * rhw;
209 render_buffer[rb_offset].w = rhw;
214 sdr_prog_t program = PROG_COLOR;
216 if (flags & TMAP_FLAG_TEXTURED) {
220 if (flags & TMAP_FLAG_PIXEL_FOG) {
221 // fog versions of color/tex shaders should one higher than non-fog version
222 program = (sdr_prog_t)((int)program + 1);
225 opengl2_shader_use(program);
227 if (flags & TMAP_FLAG_TEXTURED) {
228 glVertexAttribPointer(SDRI_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(rb_t), &render_buffer[0].u);
229 glEnableVertexAttribArray(SDRI_TEXCOORD);
232 if (flags & TMAP_FLAG_PIXEL_FOG) {
233 glVertexAttribPointer(SDRI_SEC_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(rb_t), &render_buffer[0].sr);
234 glEnableVertexAttribArray(SDRI_SEC_COLOR);
237 glVertexAttribPointer(SDRI_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(rb_t), &render_buffer[0].r);
238 glEnableVertexAttribArray(SDRI_COLOR);
240 glVertexAttribPointer(SDRI_POSITION, 4, GL_FLOAT, GL_FALSE, sizeof(rb_t), &render_buffer[0].x);
241 glEnableVertexAttribArray(SDRI_POSITION);
243 glDrawArrays(GL_TRIANGLE_FAN, 0, rb_offset);
245 glDisableVertexAttribArray(SDRI_COLOR);
246 glDisableVertexAttribArray(SDRI_SEC_COLOR);
247 glDisableVertexAttribArray(SDRI_POSITION);
248 glDisableVertexAttribArray(SDRI_TEXCOORD);
251 void opengl2_rect_internal(int x, int y, int w, int h, int r, int g, int b, int a)
255 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
257 saved_zbuf = gr_zbuffer_get();
259 // start the frame, no zbuffering, no culling
261 gr_zbuffer_set(GR_ZBUFF_NONE);
262 gr_opengl_set_cull(0);
270 v[0].flags = PF_PROJECTED;
277 v[1].sx = i2fl(x + w);
282 v[1].flags = PF_PROJECTED;
289 v[2].sx = i2fl(x + w);
290 v[2].sy = i2fl(y + h);
294 v[2].flags = PF_PROJECTED;
302 v[3].sy = i2fl(y + h);
306 v[3].flags = PF_PROJECTED;
314 g3_draw_poly_constant_sw(4, verts, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB | TMAP_FLAG_ALPHA, 0.1f);
318 // restore zbuffer and culling
319 gr_zbuffer_set(saved_zbuf);
320 gr_opengl_set_cull(1);
323 void opengl2_aabitmap_ex_internal(int x, int y, int w, int h, int sx, int sy)
325 if ( (w < 1) || (h < 1) ) {
329 if ( !gr_screen.current_color.is_alphacolor ) {
333 float u_scale, v_scale;
335 if ( !opengl2_tcache_set(gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP,
336 &u_scale, &v_scale, 0) )
338 // Couldn't set texture
339 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
343 opengl2_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
345 float u0, u1, v0, v1;
346 float x1, x2, y1, y2;
349 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
351 u0 = u_scale*i2fl(sx)/i2fl(bw);
352 v0 = v_scale*i2fl(sy)/i2fl(bh);
354 u1 = u_scale*i2fl(sx+w)/i2fl(bw);
355 v1 = v_scale*i2fl(sy+h)/i2fl(bh);
357 x1 = i2fl(x+gr_screen.offset_x);
358 y1 = i2fl(y+gr_screen.offset_y);
359 x2 = i2fl(x+w+gr_screen.offset_x);
360 y2 = i2fl(y+h+gr_screen.offset_y);
363 opengl_alloc_render_buffer(4);
365 render_buffer[0].x = x1;
366 render_buffer[0].y = y1;
367 render_buffer[0].u = u0;
368 render_buffer[0].v = v0;
370 render_buffer[1].x = x1;
371 render_buffer[1].y = y2;
372 render_buffer[1].u = u0;
373 render_buffer[1].v = v1;
375 render_buffer[2].x = x2;
376 render_buffer[2].y = y1;
377 render_buffer[2].u = u1;
378 render_buffer[2].v = v0;
380 render_buffer[3].x = x2;
381 render_buffer[3].y = y2;
382 render_buffer[3].u = u1;
383 render_buffer[3].v = v1;
385 opengl2_shader_use(PROG_AABITMAP);
388 gr_get_colorf(&r, &g, &b, &a);
389 glVertexAttrib4f(SDRI_COLOR, r, g, b, a);
391 glVertexAttribPointer(SDRI_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(rb_t), &render_buffer[0].x);
392 glEnableVertexAttribArray(SDRI_POSITION);
394 glVertexAttribPointer(SDRI_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(rb_t), &render_buffer[0].u);
395 glEnableVertexAttribArray(SDRI_TEXCOORD);
397 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
399 glDisableVertexAttribArray(SDRI_POSITION);
400 glDisableVertexAttribArray(SDRI_TEXCOORD);
403 void gr_opengl2_rect(int x, int y, int w, int h)
405 opengl2_rect_internal(x, y, w, h, gr_screen.current_color.red,
406 gr_screen.current_color.green, gr_screen.current_color.blue,
407 gr_screen.current_color.alpha);
410 void gr_opengl2_shade(int x, int y, int w, int h)
417 r = fl2i(gr_screen.current_shader.r*255.0f*shade1);
419 g = fl2i(gr_screen.current_shader.g*255.0f*shade1);
421 b = fl2i(gr_screen.current_shader.b*255.0f*shade1);
423 a = fl2i(gr_screen.current_shader.c*255.0f*shade2);
426 opengl2_rect_internal(x, y, w, h, r, g, b, a);
429 void gr_opengl2_aabitmap_ex(int x, int y, int w, int h, int sx, int sy)
436 int dx1=x, dx2=x+w-1;
437 int dy1=y, dy2=y+h-1;
440 bm_get_info( gr_screen.current_bitmap, &bw, &bh, NULL );
445 if ( count > 1 ) Int3();
449 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
450 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
451 if ( dx1 < gr_screen.clip_left ) { sx += gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
452 if ( dy1 < gr_screen.clip_top ) { sy += gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
453 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
454 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
481 if ( w < 1 ) return; // clipped away!
482 if ( h < 1 ) return; // clipped away!
486 // Make sure clipping algorithm works
490 SDL_assert( w == (dx2-dx1+1) );
491 SDL_assert( h == (dy2-dy1+1) );
492 SDL_assert( sx >= 0 );
493 SDL_assert( sy >= 0 );
494 SDL_assert( sx+w <= bw );
495 SDL_assert( sy+h <= bh );
496 SDL_assert( dx2 >= dx1 );
497 SDL_assert( dy2 >= dy1 );
498 SDL_assert( (dx1 >= gr_screen.clip_left ) && (dx1 <= gr_screen.clip_right) );
499 SDL_assert( (dx2 >= gr_screen.clip_left ) && (dx2 <= gr_screen.clip_right) );
500 SDL_assert( (dy1 >= gr_screen.clip_top ) && (dy1 <= gr_screen.clip_bottom) );
501 SDL_assert( (dy2 >= gr_screen.clip_top ) && (dy2 <= gr_screen.clip_bottom) );
504 // We now have dx1,dy1 and dx2,dy2 and sx, sy all set validly within clip regions.
505 opengl2_aabitmap_ex_internal(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
508 void gr_opengl2_aabitmap(int x, int y)
512 bm_get_info( gr_screen.current_bitmap, &w, &h, NULL );
513 int dx1=x, dx2=x+w-1;
514 int dy1=y, dy2=y+h-1;
517 if ((dx1 > gr_screen.clip_right ) || (dx2 < gr_screen.clip_left)) return;
518 if ((dy1 > gr_screen.clip_bottom ) || (dy2 < gr_screen.clip_top)) return;
519 if ( dx1 < gr_screen.clip_left ) { sx = gr_screen.clip_left-dx1; dx1 = gr_screen.clip_left; }
520 if ( dy1 < gr_screen.clip_top ) { sy = gr_screen.clip_top-dy1; dy1 = gr_screen.clip_top; }
521 if ( dx2 > gr_screen.clip_right ) { dx2 = gr_screen.clip_right; }
522 if ( dy2 > gr_screen.clip_bottom ) { dy2 = gr_screen.clip_bottom; }
524 if ( sx < 0 ) return;
525 if ( sy < 0 ) return;
526 if ( sx >= w ) return;
527 if ( sy >= h ) return;
529 // Draw bitmap bm[sx,sy] into (dx1,dy1)-(dx2,dy2)
530 gr_opengl2_aabitmap_ex(dx1,dy1,dx2-dx1+1,dy2-dy1+1,sx,sy);
533 void opengl2_error_check(const char *name, int lno)
535 GLenum error = GL_NO_ERROR;
538 error = glGetError();
540 if (error != GL_NO_ERROR) {
541 nprintf(("Warning", "!!DEBUG!! OpenGL Error: %d at %s:%d\n", error, name, lno));
543 } while (error != GL_NO_ERROR);
547 void gr_opengl2_string(int sx, int sy, const char *s)
549 int width, spacing, letter;
552 float u_scale, v_scale;
553 float u0, u1, v0, v1;
554 float x1, x2, y1, y2;
558 if ( !Current_font ) {
562 gr_set_bitmap(Current_font->bitmap_id, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
564 if ( !opengl2_tcache_set( gr_screen.current_bitmap, TCACHE_TYPE_AABITMAP, &u_scale, &v_scale, 0 ) ) {
565 // Couldn't set texture
566 mprintf(( "WARNING: Error setting aabitmap texture!\n" ));
570 bm_get_info( gr_screen.current_bitmap, &bw, &bh );
572 fbw = 1.0f / i2fl(bw);
573 fbh = 1.0f / i2fl(bh);
575 opengl2_set_state( TEXTURE_SOURCE_NO_FILTERING, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE );
577 // don't want to create a super huge buffer size (i.e. credits text)
578 const int alocsize = 320; // 80 characters max per render call
579 opengl_alloc_render_buffer(alocsize);
581 opengl2_shader_use(PROG_AABITMAP);
583 glVertexAttribPointer(SDRI_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(rb_t), &render_buffer[0].x);
584 glEnableVertexAttribArray(SDRI_POSITION);
587 gr_get_colorf(&r, &g, &b, &a);
588 glVertexAttrib4f(SDRI_COLOR, r, g, b, a);
590 glVertexAttribPointer(SDRI_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(rb_t), &render_buffer[0].u);
591 glEnableVertexAttribArray(SDRI_TEXCOORD);
596 if (sx==0x8000) { //centered
597 x = get_centered_x(s);
610 y += Current_font->h;
611 if (sx==0x8000) { //centered
612 x = get_centered_x(s);
619 letter = get_char_width(s[0],s[1],&width,&spacing);
622 //not in font, draw as space
630 // Check if this character is totally clipped
631 if ( x + width < gr_screen.clip_left ) continue;
632 if ( y + Current_font->h < gr_screen.clip_top ) continue;
633 if ( x > gr_screen.clip_right ) continue;
634 if ( y > gr_screen.clip_bottom ) continue;
637 if ( x < gr_screen.clip_left ) xd = gr_screen.clip_left - x;
638 if ( y < gr_screen.clip_top ) yd = gr_screen.clip_top - y;
642 wc = width - xd; hc = Current_font->h - yd;
643 if ( xc + wc > gr_screen.clip_right ) wc = gr_screen.clip_right - xc;
644 if ( yc + hc > gr_screen.clip_bottom ) hc = gr_screen.clip_bottom - yc;
646 if ( wc < 1 ) continue;
647 if ( hc < 1 ) continue;
649 float u = i2fl(Current_font->bm_u[letter] + xd);
650 float v = i2fl(Current_font->bm_v[letter] + yd);
652 x1 = i2fl(xc + gr_screen.offset_x);
653 y1 = i2fl(yc + gr_screen.offset_y);
657 u0 = u_scale * (u * fbw);
658 v0 = v_scale * (v * fbh);
660 u1 = u_scale * ((u+i2fl(wc)) * fbw);
661 v1 = v_scale * ((v+i2fl(hc)) * fbh);
663 // maybe go ahead and draw
664 if (rb_offset == alocsize) {
665 glDrawArrays(GL_TRIANGLE_STRIP, 0, rb_offset);
669 render_buffer[rb_offset].x = x1;
670 render_buffer[rb_offset].y = y1;
671 render_buffer[rb_offset].u = u0;
672 render_buffer[rb_offset].v = v0;
675 render_buffer[rb_offset].x = x1;
676 render_buffer[rb_offset].y = y2;
677 render_buffer[rb_offset].u = u0;
678 render_buffer[rb_offset].v = v1;
681 render_buffer[rb_offset].x = x2;
682 render_buffer[rb_offset].y = y1;
683 render_buffer[rb_offset].u = u1;
684 render_buffer[rb_offset].v = v0;
687 render_buffer[rb_offset].x = x2;
688 render_buffer[rb_offset].y = y2;
689 render_buffer[rb_offset].u = u1;
690 render_buffer[rb_offset].v = v1;
695 glDrawArrays(GL_TRIANGLE_STRIP, 0, rb_offset);
698 glDisableVertexAttribArray(SDRI_POSITION);
699 glDisableVertexAttribArray(SDRI_TEXCOORD);
702 void gr_opengl2_line(int x1, int y1, int x2, int y2)
704 opengl2_set_state(TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE);
706 INT_CLIPLINE(x1, y1, x2, y2, gr_screen.clip_left, gr_screen.clip_top,
707 gr_screen.clip_right, gr_screen.clip_bottom, return, void(), void());
712 sx1 = i2fl(x1 + gr_screen.offset_x) + 0.5f;
713 sy1 = i2fl(y1 + gr_screen.offset_y) + 0.5f;
714 sx2 = i2fl(x2 + gr_screen.offset_x) + 0.5f;
715 sy2 = i2fl(y2 + gr_screen.offset_y) + 0.5f;
717 opengl_alloc_render_buffer(2);
719 opengl2_shader_use(PROG_COLOR);
721 glVertexAttribPointer(SDRI_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(rb_t), &render_buffer[0].x);
722 glEnableVertexAttribArray(SDRI_POSITION);
725 gr_get_colorf(&r, &g, &b, &a);
726 glVertexAttrib4f(SDRI_COLOR, r, g, b, a);
728 if ( (x1 == x2) && (y1 == y2) ) {
729 render_buffer[0].x = sx1;
730 render_buffer[0].y = sy1;
731 render_buffer[0].z = -0.99f;
733 glDrawArrays(GL_POINTS, 0, 1);
735 glDisableVertexAttribArray(SDRI_POSITION);
746 } else if (y1 == y2) {
754 render_buffer[0].x = sx2;
755 render_buffer[0].y = sy2;
756 render_buffer[0].z = -0.99f;
758 render_buffer[1].x = sx1;
759 render_buffer[1].y = sy1;
760 render_buffer[1].z = -0.99f;
762 glDrawArrays(GL_LINES, 0, 2);
764 glDisableVertexAttribArray(SDRI_POSITION);
767 void gr_opengl2_aaline(vertex *v1, vertex *v2)
769 gr_opengl2_line( fl2i(v1->sx), fl2i(v1->sy), fl2i(v2->sx), fl2i(v2->sy) );
772 void gr_opengl2_gradient(int x1, int y1, int x2, int y2)
776 if ( !gr_screen.current_color.is_alphacolor ) {
777 gr_opengl2_line(x1, y1, x2, y2);
781 INT_CLIPLINE(x1, y1, x2, y2, gr_screen.clip_left, gr_screen.clip_top,
782 gr_screen.clip_right, gr_screen.clip_bottom, return, void(), swapped=1);
784 opengl2_set_state(TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_BLEND_ALPHA, ZBUFFER_TYPE_NONE);
786 ubyte aa = swapped ? 0 : gr_screen.current_color.alpha;
787 ubyte ba = swapped ? gr_screen.current_color.alpha : 0;
792 sx1 = i2fl(x1 + gr_screen.offset_x) + 0.5f;
793 sy1 = i2fl(y1 + gr_screen.offset_y) + 0.5f;
794 sx2 = i2fl(x2 + gr_screen.offset_x) + 0.5f;
795 sy2 = i2fl(y2 + gr_screen.offset_y) + 0.5f;
803 } else if (y1 == y2) {
811 opengl_alloc_render_buffer(2);
813 render_buffer[0].r = gr_screen.current_color.red;
814 render_buffer[0].g = gr_screen.current_color.green;
815 render_buffer[0].b = gr_screen.current_color.blue;
816 render_buffer[0].a = ba;
817 render_buffer[0].x = sx2;
818 render_buffer[0].y = sy2;
819 render_buffer[0].z = -0.99f;
821 render_buffer[1].r = gr_screen.current_color.red;
822 render_buffer[1].g = gr_screen.current_color.green;
823 render_buffer[1].b = gr_screen.current_color.blue;
824 render_buffer[1].a = aa;
825 render_buffer[1].x = sx1;
826 render_buffer[1].y = sy1;
827 render_buffer[1].z = -0.99f;
829 opengl2_shader_use(PROG_COLOR);
831 glVertexAttribPointer(SDRI_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(rb_t), &render_buffer[0].r);
832 glEnableVertexAttribArray(SDRI_COLOR);
834 glVertexAttribPointer(SDRI_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(rb_t), &render_buffer[0].x);
835 glEnableVertexAttribArray(SDRI_POSITION);
837 glDrawArrays(GL_LINES, 0, 2);
839 glDisableVertexAttribArray(SDRI_COLOR);
840 glDisableVertexAttribArray(SDRI_POSITION);
843 void gr_opengl2_circle(int xc, int yc, int d)
853 if ( (xc+r) < gr_screen.clip_left ) return;
854 if ( (xc-r) > gr_screen.clip_right ) return;
855 if ( (yc+r) < gr_screen.clip_top ) return;
856 if ( (yc-r) > gr_screen.clip_bottom ) return;
859 // Draw the first octant
860 gr_opengl2_line( xc-y, yc-x, xc+y, yc-x );
861 gr_opengl2_line( xc-y, yc+x, xc+y, yc+x );
866 // Draw the second octant
867 gr_opengl2_line( xc-x, yc-y, xc+x, yc-y );
868 gr_opengl2_line( xc-x, yc+y, xc+x, yc+y );
876 gr_opengl2_line( xc-x, yc-y, xc+x, yc-y );
877 gr_opengl2_line( xc-x, yc+y, xc+x, yc+y );
881 void gr_opengl2_pixel(int x, int y)
883 gr_opengl2_line(x,y,x,y);
886 void gr_opengl2_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
888 gr_set_bitmap(bmap1, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 1.0f - pct );
891 gr_set_bitmap(bmap2, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, pct );
895 void gr_opengl2_flash(int r, int g, int b)
902 opengl2_set_state(TEXTURE_SOURCE_NONE, ALPHA_BLEND_ALPHA_ADDITIVE, ZBUFFER_TYPE_NONE);
904 float x1, x2, y1, y2;
905 x1 = i2fl(gr_screen.clip_left+gr_screen.offset_x);
906 y1 = i2fl(gr_screen.clip_top+gr_screen.offset_y);
907 x2 = i2fl(gr_screen.clip_right+gr_screen.offset_x);
908 y2 = i2fl(gr_screen.clip_bottom+gr_screen.offset_y);
910 opengl_alloc_render_buffer(4);
912 render_buffer[0].x = x1;
913 render_buffer[0].y = y1;
914 render_buffer[0].z = -0.99f;
916 render_buffer[1].x = x1;
917 render_buffer[1].y = y2;
918 render_buffer[1].z = -0.99f;
920 render_buffer[2].x = x2;
921 render_buffer[2].y = y1;
922 render_buffer[2].z = -0.99f;
924 render_buffer[3].x = x2;
925 render_buffer[3].y = y2;
926 render_buffer[3].z = -0.99f;
928 opengl2_shader_use(PROG_COLOR);
930 glVertexAttrib4f(SDRI_COLOR, r / 255.0f, g / 255.0f, b / 255.0f, 1.0f);
932 glVertexAttribPointer(SDRI_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(rb_t), &render_buffer[0].x);
933 glEnableVertexAttribArray(SDRI_POSITION);
935 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
937 glDisableVertexAttribArray(SDRI_POSITION);
941 void gr_opengl2_tmapper(int nverts, vertex **verts, uint flags)
943 opengl2_tmapper_internal(nverts, verts, flags, 0);
946 #define FIND_SCALED_NUM(x,x0,x1,y0,y1) (((((x)-(x0))*((y1)-(y0)))/((x1)-(x0)))+(y0))
948 void gr_opengl2_scaler(vertex *va, vertex *vb)
950 float x0, y0, x1, y1;
951 float u0, v0, u1, v1;
952 float clipped_x0, clipped_y0, clipped_x1, clipped_y1;
953 float clipped_u0, clipped_v0, clipped_u1, clipped_v1;
954 float xmin, xmax, ymin, ymax;
955 int dx0, dy0, dx1, dy1;
957 //============= CLIP IT =====================
959 x0 = va->sx; y0 = va->sy;
960 x1 = vb->sx; y1 = vb->sy;
962 xmin = i2fl(gr_screen.clip_left); ymin = i2fl(gr_screen.clip_top);
963 xmax = i2fl(gr_screen.clip_right); ymax = i2fl(gr_screen.clip_bottom);
965 u0 = va->u; v0 = va->v;
966 u1 = vb->u; v1 = vb->v;
968 // Check for obviously offscreen bitmaps...
969 if ( (y1<=y0) || (x1<=x0) ) return;
970 if ( (x1<xmin ) || (x0>xmax) ) return;
971 if ( (y1<ymin ) || (y0>ymax) ) return;
973 clipped_u0 = u0; clipped_v0 = v0;
974 clipped_u1 = u1; clipped_v1 = v1;
976 clipped_x0 = x0; clipped_y0 = y0;
977 clipped_x1 = x1; clipped_y1 = y1;
979 // Clip the left, moving u0 right as necessary
981 clipped_u0 = FIND_SCALED_NUM(xmin,x0,x1,u0,u1);
985 // Clip the right, moving u1 left as necessary
987 clipped_u1 = FIND_SCALED_NUM(xmax,x0,x1,u0,u1);
991 // Clip the top, moving v0 down as necessary
993 clipped_v0 = FIND_SCALED_NUM(ymin,y0,y1,v0,v1);
997 // Clip the bottom, moving v1 up as necessary
999 clipped_v1 = FIND_SCALED_NUM(ymax,y0,y1,v0,v1);
1003 dx0 = fl2i(clipped_x0); dx1 = fl2i(clipped_x1);
1004 dy0 = fl2i(clipped_y0); dy1 = fl2i(clipped_y1);
1006 if (dx1<=dx0) return;
1007 if (dy1<=dy0) return;
1009 //============= DRAW IT =====================
1015 v[0].sx = clipped_x0;
1016 v[0].sy = clipped_y0;
1019 v[0].u = clipped_u0;
1020 v[0].v = clipped_v0;
1023 v[1].sx = clipped_x1;
1024 v[1].sy = clipped_y0;
1027 v[1].u = clipped_u1;
1028 v[1].v = clipped_v0;
1031 v[2].sx = clipped_x1;
1032 v[2].sy = clipped_y1;
1035 v[2].u = clipped_u1;
1036 v[2].v = clipped_v1;
1039 v[3].sx = clipped_x0;
1040 v[3].sy = clipped_y1;
1043 v[3].u = clipped_u0;
1044 v[3].v = clipped_v1;
1046 opengl2_tmapper_internal( 4, vl, TMAP_FLAG_TEXTURED, 1 );