From 853659870bcec233d1d726e05b272acf8f39cef6 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Fri, 28 Apr 2023 16:21:59 -0700 Subject: [PATCH] colored foreground/background anti-aliasing test --- ...standalone_srgb_antialiasing_experiments.c | 192 +++++++++++++++++- 1 file changed, 191 insertions(+), 1 deletion(-) diff --git a/src/standalone_srgb_antialiasing_experiments.c b/src/standalone_srgb_antialiasing_experiments.c index f7ecb04..1928066 100644 --- a/src/standalone_srgb_antialiasing_experiments.c +++ b/src/standalone_srgb_antialiasing_experiments.c @@ -90,8 +90,10 @@ static void opengl_render_init(void); static void opengl_draw_basic_geometry(Vertex *v, U64 count); static void opengl_draw_srgb_in_geometry(Vertex *v, U64 count); -static void opengl_draw_texture_geometry(Vertex *v, U64 count, U32 texture); static void opengl_draw_rect_geometry(Vertex *v, U64 count); +static void opengl_draw_texture_geometry(Vertex *v, U64 count, U32 texture); +static void opengl_draw_texrgb_geometry(Vertex *v, U64 count, U32 texture, + F32 r, F32 g, F32 b, F32 a); @@ -674,6 +676,38 @@ static GLint texture_u_texture = -1; +static char texrgb_vshader[] = +"#version 330\n" +"uniform vec2 u_view_xform;\n" +"layout (location = 0) in vec2 v_p;\n" +"layout (location = 1) in vec2 v_uv;\n" +"out vec2 f_uv;\n" +"void main(){\n" +"vec2 norm_pos = v_p*u_view_xform + vec2(-1.0, -1.0);\n" +"gl_Position = vec4(norm_pos, 0.0, 1.0);\n" +"f_uv = v_uv;\n" +"}\n" +; + +static char texrgb_fshader[] = +"#version 330\n" +"uniform sampler2D u_texture;\n" +"uniform vec4 u_color;\n" +"in vec2 f_uv;\n" +"out vec4 out_color;\n" +"void main(){\n" +"float texture_a = texture(u_texture, f_uv).r;\n" +"out_color = vec4(u_color.rgb, u_color.a*texture_a);\n" +"}\n" +; + +static GLuint texrgb_program = 0; +static GLint texrgb_u_view_xform = -1; +static GLint texrgb_u_texture = -1; +static GLint texrgb_u_color = -1; + + + static GLuint canvas_texture = 0; static GLuint canvas_framebuffer = 0; @@ -697,6 +731,7 @@ static GLuint circle25_texture = 0; #define CIRCLE125_SIDE 300 static GLuint circle125_texture = 0; + static void opengl_render_init(void){ //- setup basic shader program @@ -736,6 +771,17 @@ opengl_render_init(void){ texture_u_texture = glGetUniformLocation(texture_program, "u_texture"); } + //- setup texture rgb shader program + { + GLuint shaders[2] = {0}; + shaders[0] = opengl_helper_make_shader(texrgb_vshader, GL_VERTEX_SHADER); + shaders[1] = opengl_helper_make_shader(texrgb_fshader, GL_FRAGMENT_SHADER); + texrgb_program = opengl_helper_make_program(shaders, 2); + texrgb_u_view_xform = glGetUniformLocation(texrgb_program, "u_view_xform"); + texrgb_u_texture = glGetUniformLocation(texrgb_program, "u_texture"); + texrgb_u_color = glGetUniformLocation(texrgb_program, "u_color"); + } + //- vertex array object { GLuint vao = 0; @@ -955,6 +1001,31 @@ opengl_draw_texture_geometry(Vertex *v, U64 count, U32 texture){ glDrawArrays(GL_TRIANGLES, 0, count); } +static void +opengl_draw_texrgb_geometry(Vertex *v, U64 count, U32 texture, + F32 r, F32 g, F32 b, F32 a){ + glBufferData(GL_ARRAY_BUFFER, sizeof(*v)*count, v, GL_STREAM_DRAW); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture); + + glUseProgram(texrgb_program); + glUniform2f(texrgb_u_view_xform, 2.f/graphics_w, 2.f/graphics_h); + glUniform1i(texrgb_u_texture, 0); + glUniform4f(texrgb_u_color, r, g, b, a); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_FLOAT, 0, + sizeof(Vertex), PtrOffsetOf(Vertex, px)); + + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, 0, + sizeof(Vertex), PtrOffsetOf(Vertex, uvx)); + + glDrawArrays(GL_TRIANGLES, 0, count); +} + + //////////////////////////////// @@ -1018,6 +1089,7 @@ WinMain(HINSTANCE hInstance, F32 test5_y = graphics_h - 340.f; F32 test6_y = graphics_h - 450.f; F32 test7_y = graphics_h - 480.f; + F32 test8_y = graphics_h - 800.f; // render to canvas frame buffer { @@ -1403,6 +1475,124 @@ WinMain(HINSTANCE hInstance, opengl_draw_texture_geometry(v, 6, circle125_texture); } } + + // 8: color on color circles + { + F32 back[][4] = { + {1.f, 0.f, 0.f, 1.f}, + {1.f, 0.f, 0.f, 1.f}, + {0.f, 1.f, 0.f, 1.f}, + {0.f, 1.f, 0.f, 1.f}, + {0.f, 0.f, 1.f, 1.f}, + {0.f, 0.f, 1.f, 1.f}, + }; + F32 fore[][4] = { + {0.f, 1.f, 0.f, 1.f}, + {0.f, 0.f, 1.f, 1.f}, + {1.f, 0.f, 0.f, 1.f}, + {0.f, 0.f, 1.f, 1.f}, + {1.f, 0.f, 0.f, 1.f}, + {0.f, 1.f, 0.f, 1.f}, + }; + + + + F32 y = test8_y; + F32 x = grad_x0; + + for (U32 k = 0; k < 6; k += 1){ + // background + { + F32 x0 = x; + F32 x1 = x + CIRCLE5_SIDE + CIRCLE25_SIDE + 20.f; + F32 y1 = y; + F32 y0 = y - CIRCLE25_SIDE; + + Vertex v[6]; + v[0].px = x0; v[0].py = y0; + v[1].px = x0; v[1].py = y1; + v[2].px = x1; v[2].py = y0; + v[3].px = x0; v[3].py = y1; + v[4].px = x1; v[4].py = y0; + v[5].px = x1; v[5].py = y1; + for (U32 i = 0; i < 6; i += 1){ + v[i].cr = back[k][0]; + v[i].cg = back[k][1]; + v[i].cb = back[k][2]; + v[i].ca = back[k][3]; + } + + if (test_idx == 0){ + opengl_draw_basic_geometry(v, 6); + } + else{ + opengl_draw_srgb_in_geometry(v, 6); + } + } + + // circle5 + { + F32 y1 = y; + F32 y0 = y - CIRCLE5_SIDE; + F32 x0 = x; + F32 x1 = x + CIRCLE5_SIDE; + + x += CIRCLE5_SIDE + 10; + + Vertex v[6]; + v[0].px = x0; v[0].py = y0; + v[0].uvx = 0.f; v[0].uvy = 0.f; + v[1].px = x0; v[1].py = y1; + v[1].uvx = 0.f; v[1].uvy = 1.f; + v[2].px = x1; v[2].py = y0; + v[2].uvx = 1.f; v[2].uvy = 0.f; + v[3].px = x0; v[3].py = y1; + v[3].uvx = 0.f; v[3].uvy = 1.f; + v[4].px = x1; v[4].py = y0; + v[4].uvx = 1.f; v[4].uvy = 0.f; + v[5].px = x1; v[5].py = y1; + v[5].uvx = 1.f; v[5].uvy = 1.f; + + opengl_draw_texrgb_geometry(v, 6, circle5_texture, + fore[k][0], fore[k][1], + fore[k][2], fore[k][3]); + } + + // circle25 + { + F32 y1 = y; + F32 y0 = y - CIRCLE25_SIDE; + F32 x0 = x; + F32 x1 = x + CIRCLE25_SIDE; + + x += CIRCLE25_SIDE + 10; + + Vertex v[6]; + v[0].px = x0; v[0].py = y0; + v[0].uvx = 0.f; v[0].uvy = 0.f; + v[1].px = x0; v[1].py = y1; + v[1].uvx = 0.f; v[1].uvy = 1.f; + v[2].px = x1; v[2].py = y0; + v[2].uvx = 1.f; v[2].uvy = 0.f; + v[3].px = x0; v[3].py = y1; + v[3].uvx = 0.f; v[3].uvy = 1.f; + v[4].px = x1; v[4].py = y0; + v[4].uvx = 1.f; v[4].uvy = 0.f; + v[5].px = x1; v[5].py = y1; + v[5].uvx = 1.f; v[5].uvy = 1.f; + + opengl_draw_texrgb_geometry(v, 6, circle25_texture, + fore[k][0], fore[k][1], + fore[k][2], fore[k][3]); + } + + // move y down after 2 + if ((k + 1) % 2 == 0){ + x = grad_x0; + y -= CIRCLE25_SIDE; + } + } + } } }