summaryrefslogtreecommitdiff
path: root/src/render/gl_2d.cpp
diff options
context:
space:
mode:
authoraura <nw@moneybot.cc>2026-03-10 05:18:33 +0100
committeraura <nw@moneybot.cc>2026-03-10 05:18:33 +0100
commit30151d75dddd651faa1b27148a052ced7d0f190a (patch)
tree30b9374d59ecdd83296f7a2b7aa008340b95daf5 /src/render/gl_2d.cpp
parent31b77ed2e0c037e5718b9ecd06d3941600cc8815 (diff)
2d batch rendering
Diffstat (limited to 'src/render/gl_2d.cpp')
-rw-r--r--src/render/gl_2d.cpp63
1 files changed, 46 insertions, 17 deletions
diff --git a/src/render/gl_2d.cpp b/src/render/gl_2d.cpp
index 506d0da..bc55cae 100644
--- a/src/render/gl_2d.cpp
+++ b/src/render/gl_2d.cpp
@@ -382,12 +382,15 @@ void gl_2d_rect( GL_BATCH2D* batch, VEC2 origin, VEC2 dim, CLR col ) {
VERTEX vertices[] = {
{ .pos = { origin.x , origin.y }, .clr = col },
{ .pos = { origin.x + dim.x, origin.y }, .clr = col },
+ { .pos = { origin.x + dim.x, origin.y }, .clr = col },
+ { .pos = { origin.x + dim.x, origin.y + dim.y }, .clr = col },
{ .pos = { origin.x + dim.x, origin.y + dim.y }, .clr = col },
{ .pos = { origin.x , origin.y + dim.y }, .clr = col },
+ { .pos = { origin.x , origin.y + dim.y }, .clr = col },
{ .pos = { origin.x , origin.y }, .clr = col },
};
- gl_batch_insert( batch, vertices, 5, 0, GL_LINE_STRIP );
+ gl_batch_insert( batch, vertices, 8, 0, GL_LINES );
}
void gl_2d_frect( GL_BATCH2D* batch, VEC2 origin, VEC2 dim, CLR col ) {
@@ -395,19 +398,24 @@ void gl_2d_frect( GL_BATCH2D* batch, VEC2 origin, VEC2 dim, CLR col ) {
{ .pos = { origin.x, origin.y }, .uv = { 0.f, 0.f }, .clr = col },
{ .pos = { origin.x + dim.x, origin.y }, .uv = { 1.f, 0.f }, .clr = col },
{ .pos = { origin.x, origin.y + dim.y }, .uv = { 0.f, 1.f }, .clr = col },
- { .pos = { origin.x + dim.x, origin.y + dim.y }, .uv = { 1.f, 1.f }, .clr = col }
+ { .pos = { origin.x + dim.x, origin.y }, .uv = { 1.f, 0.f }, .clr = col },
+ { .pos = { origin.x + dim.x, origin.y + dim.y }, .uv = { 1.f, 1.f }, .clr = col },
+ { .pos = { origin.x , origin.y + dim.y }, .uv = { 0.f, 1.f }, .clr = col }
};
- gl_batch_insert( batch, vertices, 4, 0, GL_TRIANGLE_STRIP );
+ gl_batch_insert( batch, vertices, 6, 0, GL_TRIANGLES );
}
void gl_2d_circle( GL_BATCH2D* batch, VEC2 origin, F32 radius, CLR col, U32 res ) {
const F32 step = 360.f / (F32)res;
- VERTEX* vertices = (VERTEX*)malloc( sizeof( VERTEX ) * (res + 1) );
- for( U32 i = 0; i < res + 1; ++i ) {
- VEC2 offset = m_radial_offset( step * ( i == res? 0 : i ), radius );
+ LIST<VERTEX> vertices;
+ vertices.reserve( res * 2 );
+ VEC2 prev;
+ for( U32 i = 0; i < res * 2; i += 2 ) {
+ VEC2 offset = m_radial_offset( step * i, radius );
+ VEC2 offset_next = m_radial_offset( step * ( i + 1 ), radius );
- vertices[i] = (VERTEX){
+ vertices.data[i] = (VERTEX){
.pos = {
origin.x + offset.x,
origin.y + offset.y,
@@ -415,17 +423,26 @@ void gl_2d_circle( GL_BATCH2D* batch, VEC2 origin, F32 radius, CLR col, U32 res
.uv = {},
.clr = col
};
+ vertices.data[i+1] = (VERTEX){
+ .pos = {
+ origin.x + offset_next.x,
+ origin.y + offset_next.y,
+ },
+ .uv = {},
+ .clr = col
+ };
}
- gl_batch_insert( batch, vertices, res + 1, 0, GL_LINE_STRIP );
+ gl_batch_insert( batch, vertices.data, vertices.size, 0, GL_LINES );
}
void gl_2d_fcircle( GL_BATCH2D* batch, VEC2 origin, F32 radius, CLR col, U32 res ) {
const F32 step = 360.f / (F32)res;
- VERTEX* vertices = (VERTEX*)malloc( sizeof(VERTEX) * (res * 2) );
- for( U32 i = 0; i < res * 2; i += 2 ) {
+ VERTEX* vertices = (VERTEX*)malloc( sizeof(VERTEX) * (res * 3) );
+ for( U32 i = 0; i < res * 3; i += 3 ) {
VEC2 offset = m_radial_offset( step * i, radius );
+ VEC2 offset_next = m_radial_offset( step * (i + 1), radius );
vertices[i].pos = (VEC2){
origin.x,
origin.y,
@@ -436,17 +453,27 @@ void gl_2d_fcircle( GL_BATCH2D* batch, VEC2 origin, F32 radius, CLR col, U32 res
origin.y + offset.y,
};
+ vertices[i + 2].pos = (VEC2){
+ origin.x + offset_next.x,
+ origin.y + offset_next.y,
+ };
+
vertices[i].uv = (VEC2){ 0.5f, 0.5f };
vertices[i + 1].uv = (VEC2){
0.5f + ( offset.x * 0.5f ) / radius,
0.5f + ( offset.y * 0.5f ) / radius
};
+ vertices[i + 2].uv = (VEC2){
+ 0.5f + ( offset_next.x * 0.5f ) / radius,
+ 0.5f + ( offset_next.y * 0.5f ) / radius
+ };
vertices[i].clr = col;
vertices[i + 1].clr = col;
+ vertices[i + 2].clr = col;
};
- gl_batch_insert( batch, vertices, res * 2, 0, GL_TRIANGLE_STRIP );
+ gl_batch_insert( batch, vertices, res * 3, 0, GL_TRIANGLES );
}
@@ -460,10 +487,12 @@ extern void gl_2d_textured_frect(
F32 rotation
) {
VERTEX vertices[] = {
- { { origin.x, origin.y }, uv? uv[0] : VEC2{ 0.f, 0.f }, col, 0 },
- { { origin.x + dim.x, origin.y }, uv? uv[1] : VEC2{ 1.f, 0.f }, col, 0 },
- { { origin.x, origin.y + dim.y }, uv? uv[2] : VEC2{ 0.f, 1.f }, col, 0 },
- { { origin.x + dim.x, origin.y + dim.y }, uv? uv[3] : VEC2{ 1.f, 1.f }, col, 0 }
+ { .pos = { origin.x, origin.y }, .uv = { 0.f, 0.f }, .clr = col },
+ { .pos = { origin.x + dim.x, origin.y }, .uv = { 1.f, 0.f }, .clr = col },
+ { .pos = { origin.x, origin.y + dim.y }, .uv = { 0.f, 1.f }, .clr = col },
+ { .pos = { origin.x + dim.x, origin.y }, .uv = { 1.f, 0.f }, .clr = col },
+ { .pos = { origin.x + dim.x, origin.y + dim.y }, .uv = { 1.f, 1.f }, .clr = col },
+ { .pos = { origin.x , origin.y + dim.y }, .uv = { 0.f, 1.f }, .clr = col }
};
rotation = remainderf( rotation, 360.f );
@@ -471,7 +500,7 @@ extern void gl_2d_textured_frect(
F32 rad2dg = rotation * (M_PI / 180.f);
// rotate texture coordinates
- for( U32 i = 0; i < 4; ++i ) {
+ for( U32 i = 0; i < 6; ++i ) {
F32 x = vertices[i].uv.x - 0.5f;
F32 y = vertices[i].uv.y - 0.5f;
vertices[i].uv.x = x * cosf( rad2dg ) - y * sinf( rad2dg ) + 0.5f;
@@ -481,7 +510,7 @@ extern void gl_2d_textured_frect(
}
}
- gl_batch_insert( batch, vertices, 4, texture, GL_TRIANGLE_FAN );
+ gl_batch_insert( batch, vertices, 6, texture, GL_TRIANGLES );
}
void gl_2d_polygon( GL_BATCH2D* batch, VERTEX* vertices, U32 vertices_count ) {