alpha blending experiments

main
Allen Webster 2023-04-26 21:23:39 -07:00
parent 7c4ca1dc6e
commit 97d8c7430e
1 changed files with 92 additions and 1 deletions

View File

@ -42,6 +42,7 @@ typedef double F64;
#define U64FromPtr(ptr) (U64)(ptr)
#define PtrFromU64(ptr) (void*)(x)
#define ArrayCount(a) ((sizeof(a))/(sizeof(*a)))
#define Member(T,m) ((T*)0)->m
#define PtrOffsetOf(T,m) (void*)(&Member(T,m))
#define OffsetOf(T,m) U64FromPtr(&Member(T,m))
@ -105,9 +106,13 @@ typedef unsigned char GLboolean;
#define GL_COLOR_BUFFER_BIT 0x00004000
#define GL_TRIANGLES 0x0004
#define GL_SRC_ALPHA 0x0302
#define GL_ONE_MINUS_SRC_ALPHA 0x0303
#define GL_BLEND 0x0BE2
#define GL_TEXTURE_2D 0x0DE1
#define GL_UNSIGNED_BYTE 0x1401
@ -449,6 +454,12 @@ opengl_render_init(void){
canvas_texture, 0);
}
//- enable alpha blend
{
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
}
//- test3 texture
{
U8 test3_img[16*3] = {0};
@ -539,6 +550,17 @@ opengl_draw_texture_geometry(Vertex *v, U64 count, U32 texture){
////////////////////////////////
//- Main
typedef struct Rect{
F32 x0;
F32 y0;
F32 x1;
F32 y1;
F32 r;
F32 g;
F32 b;
F32 a;
} Rect;
int
WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
@ -579,6 +601,7 @@ WinMain(HINSTANCE hInstance,
F32 test2_y = graphics_h - 60.f;
F32 test3_y = graphics_h - 110.f;
F32 test4_y = graphics_h - 160.f;
F32 test5_y = graphics_h - 340.f;
// render to canvas frame buffer
{
@ -779,6 +802,74 @@ WinMain(HINSTANCE hInstance,
}
}
// 5: transparency blending
{
Rect rect[20] = {0};
U32 recti = 0;
F32 top_y = test5_y;
F32 bot_y = top_y - 100.f;
rect[recti].x0 = grad_x0;
rect[recti].y0 = bot_y;
rect[recti].x1 = grad_x1;
rect[recti].y1 = top_y;
rect[recti].r = 1.f;
rect[recti].g = 1.f;
rect[recti].b = 1.f;
rect[recti].a = 1.f;
recti += 1;
for (U32 i = 0; i < 3; i += 1){
rect[recti].x0 = grad_x0;
rect[recti].y0 = top_y - 10.f - 30.f*i;
rect[recti].x1 = grad_x1;
rect[recti].y1 = top_y - 30.f - 30.f*i;
rect[recti].r = (i == 0)?1.f:0.f;
rect[recti].g = (i == 1)?1.f:0.f;
rect[recti].b = (i == 2)?1.f:0.f;
rect[recti].a = 1.f;
recti += 1;
}
for (U32 j = 0; j < 2; j += 1){
for (U32 i = 0; i < 3; i += 1){
rect[recti].x0 = grad_x0 + 10.f + 30.f*i + 100.f*j;
rect[recti].y0 = bot_y;
rect[recti].x1 = grad_x0 + 30.f + 30.f*i + 100.f*j;
rect[recti].y1 = top_y;
rect[recti].r = (i == 0)?1.f:0.f;
rect[recti].g = (i == 1)?1.f:0.f;
rect[recti].b = (i == 2)?1.f:0.f;
rect[recti].a = (j == 0)?0.5f:0.75f;
recti += 1;
}
}
for (U32 j = 0; j < recti; j += 1){
Vertex v[6] = {0};
v[0].px = rect[j].x0; v[0].py = rect[j].y0;
v[1].px = rect[j].x0; v[1].py = rect[j].y1;
v[2].px = rect[j].x1; v[2].py = rect[j].y0;
v[3].px = rect[j].x0; v[3].py = rect[j].y1;
v[4].px = rect[j].x1; v[4].py = rect[j].y0;
v[5].px = rect[j].x1; v[5].py = rect[j].y1;
for (U32 k = 0; k < 6; k += 1){
v[k].cr = rect[j].r;
v[k].cg = rect[j].g;
v[k].cb = rect[j].b;
v[k].ca = rect[j].a;
}
if (test_idx == 0){
opengl_draw_basic_geometry(v, 6);
}
else{
opengl_draw_srgb_in_geometry(v, 6);
}
}
}
}
}