summaryrefslogtreecommitdiff
path: root/src/render
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
parent31b77ed2e0c037e5718b9ecd06d3941600cc8815 (diff)
2d batch rendering
Diffstat (limited to 'src/render')
-rw-r--r--src/render/gl_2d.cpp63
-rw-r--r--src/render/gl_2d_font.cpp67
-rw-r--r--src/render/gl_2d_font.h3
-rw-r--r--src/render/gl_batch.h37
4 files changed, 83 insertions, 87 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 ) {
diff --git a/src/render/gl_2d_font.cpp b/src/render/gl_2d_font.cpp
index 10ae71a..6533dce 100644
--- a/src/render/gl_2d_font.cpp
+++ b/src/render/gl_2d_font.cpp
@@ -145,7 +145,6 @@ void gl_font_calc_vertices_uvs(
F32 _scale,
VERTEX* vertices,
U16* indices,
- VEC2* coords,
CLR clr
) {
U32 len = (U32)strlen( text );
@@ -199,13 +198,12 @@ void gl_font_calc_vertices_uvs(
}
}
-void gl_font_draw( GL_FONT* font, GL_SHADER_PROGRAM* shader, VEC2 origin, const char* text, CLR clr, F32 _scale ) {
+void gl_font_draw( GL_FONT* font, GL_SHADER_PROGRAM* shader, VEC2 pos, const char* text, CLR clr, F32 _scale ) {
U32 len = strlen( text );
VERTEX* vertices = (VERTEX*)malloc( sizeof(VERTEX) * 6 * len );
U16* indices = (U16*)malloc( sizeof(U16) * 6 * len );
- VEC2* coords = (VEC2*)malloc( sizeof(VEC2) * 6 * len );
- gl_font_calc_vertices_uvs( font, origin, text, _scale, vertices, indices, coords, clr );
+ gl_font_calc_vertices_uvs( font, pos, text, _scale, vertices, indices, clr );
glUseProgram( shader->id );
glBindBuffer( GL_ARRAY_BUFFER, shader->gl->vbuffer );
@@ -242,75 +240,20 @@ void gl_font_draw( GL_FONT* font, GL_SHADER_PROGRAM* shader, VEC2 origin, const
free( vertices );
free( indices );
- free( coords );
}
-void gl_font_textured(
- GL_FONT* font,
- GL_SHADER_PROGRAM* shader,
- VEC2 origin,
- const char* text,
- GL_TEX2D* tex,
- CLR clr,
- F32 _scale
-) {
+void gl_font_draw( GL_FONT* font, GL_BATCH2D* batch, VEC2 pos, const char* text, CLR clr, F32 _scale ) {
U32 len = strlen( text );
- struct FONT_CUSTOM_VERTEX {
- VERTEX v;
- VEC2 uv2;
- };
-
- FONT_CUSTOM_VERTEX* custom_vertices = (FONT_CUSTOM_VERTEX*)malloc( sizeof(FONT_CUSTOM_VERTEX) * 6 * len );
VERTEX* vertices = (VERTEX*)malloc( sizeof(VERTEX) * 6 * len );
U16* indices = (U16*)malloc( sizeof(U16) * 6 * len );
- VEC2* coords = (VEC2*)malloc( sizeof(VEC2) * 6 * len );
-
- gl_font_calc_vertices_uvs( font, origin, text, _scale, vertices, indices, coords, clr );
- VEC2 dim = gl_font_dim( font, text, _scale );
-
- for( U32 i = 0; i < len * 6; ++i ) {
- custom_vertices[i].v = vertices[i];
- custom_vertices[i].uv2 = {
- (vertices[i].pos.x - origin.x) / dim.x,
- (vertices[i].pos.y - origin.y - vertices[0].pos.y) / dim.y
- };
- }
- glUseProgram( shader->id );
-
- glBindBuffer( GL_ARRAY_BUFFER, shader->gl->vbuffer );
- glBufferData( GL_ARRAY_BUFFER, sizeof(FONT_CUSTOM_VERTEX) * 6 * len, custom_vertices, GL_STATIC_DRAW );
- I32 position = glGetAttribLocation( shader->id, "in_pos" );
- glEnableVertexAttribArray( position );
- glVertexAttribPointer( position, 2, GL_FLOAT, 0, sizeof(FONT_CUSTOM_VERTEX), 0 );
-
- I32 texcoord = glGetAttribLocation( shader->id, "in_texcoord" );
- glEnableVertexAttribArray( texcoord );
- glVertexAttribPointer( texcoord, 2, GL_FLOAT, 0, sizeof(FONT_CUSTOM_VERTEX), (void*)(sizeof(VEC2)) );
-
- I32 texcoord2 = glGetAttribLocation( shader->id, "in_texcoord2" );
- glEnableVertexAttribArray( texcoord2 );
- glVertexAttribPointer( texcoord2, 2, GL_FLOAT, 0, sizeof(FONT_CUSTOM_VERTEX), (void*)(sizeof(VERTEX)) );
-
- glBindBuffer( GL_ARRAY_BUFFER, 0 );
-
- I32 color = glGetUniformLocation( shader->id, "in_color" );
- glUniform4fv( color, 1, (F32*)&clr );
-
- glActiveTexture( GL_TEXTURE0 );
- glUniform1iv( glGetUniformLocation( shader->id, "in_sampler" ), 0, 0 );
- glBindTexture( GL_TEXTURE_2D, font->atlas->id );
-
- glDrawElements( GL_TRIANGLES, len * 6, GL_UNSIGNED_SHORT, indices );
- glBindTexture( GL_TEXTURE_2D, 0 );
+ gl_font_calc_vertices_uvs( font, pos, text, _scale, vertices, indices, clr );
+ gl_batch_insert( batch, vertices, len * 6, font->atlas, GL_TRIANGLES );
free( vertices );
free( indices );
- free( coords );
- free( custom_vertices );
}
-
VEC2 gl_font_dim( GL_FONT* font, const char* text, F32 _scale ) {
U32 len = strlen( text );
diff --git a/src/render/gl_2d_font.h b/src/render/gl_2d_font.h
index 71154c0..d0c9a71 100644
--- a/src/render/gl_2d_font.h
+++ b/src/render/gl_2d_font.h
@@ -2,7 +2,7 @@
#include <ft2build.h>
#include FT_FREETYPE_H
-#include "gl.h"
+#include "gl_2d.h"
#include "../util/thread.h"
extern THREAD_MUTEX font_mutex;
@@ -38,6 +38,7 @@ GL_FONT* gl_font_create( GL_DATA* gl, const char* path, I32 size );
void gl_font_destroy( GL_DATA* gl, GL_FONT* font );
void gl_font_draw( GL_FONT* font, GL_SHADER_PROGRAM* shader, VEC2 pos, const char* text, CLR color, F32 scale = 1.0f );
+void gl_font_draw( GL_FONT* font, GL_BATCH2D* batch, VEC2 pos, const char* text, CLR color, F32 scale = 1.0f );
void gl_font_textured(
GL_FONT* font,
GL_SHADER_PROGRAM* shader,
diff --git a/src/render/gl_batch.h b/src/render/gl_batch.h
index 12b512f..04f72a7 100644
--- a/src/render/gl_batch.h
+++ b/src/render/gl_batch.h
@@ -79,12 +79,16 @@ inline GL_BATCH<VERTEX>* gl_batch_create(
template <typename VERTEX>
inline void gl_batch_destroy( GL_BATCH<VERTEX>* batch ) {
- glDeleteBuffers( &batch->vbuffer );
+ glDeleteBuffers( 1, &batch->vbuffer );
+ for( auto& it : batch->calls )
+ it.textures.clear();
delete batch;
}
template <typename VERTEX>
inline void gl_batch_empty( GL_BATCH<VERTEX>* batch ) {
+ for( auto& it : batch->calls )
+ it.textures.clear();
batch->calls.clear();
}
@@ -119,6 +123,19 @@ inline void gl_batch_draw( GL_BATCH<VERTEX>* batch ) {
gl_set_clip( batch->gl, clip_start, clip_dim );
}
+inline U8 gl_batch_is_allowed_primitive( U16 primitive ) {
+ switch( primitive ) {
+ case GL_TRIANGLE_FAN:
+ case GL_TRIANGLE_STRIP:
+ case GL_QUAD_STRIP:
+ case GL_LINE_STRIP:
+ case GL_LINE_LOOP:
+ return 0;
+ default:
+ return 1;
+ }
+}
+
template <typename VERTEX>
inline GL_BATCH_CALL<VERTEX>* gl_batch_get_call( GL_BATCH<VERTEX>* batch, GL_TEX2D* tex, U16 primitive, I32* texid ) {
U32 n = batch->calls.size;
@@ -127,7 +144,7 @@ inline GL_BATCH_CALL<VERTEX>* gl_batch_get_call( GL_BATCH<VERTEX>* batch, GL_TEX
VEC2 clip_start = batch->clip_start;
VEC2 clip_dim = batch->clip_dim;
- if( n ) {
+ if( gl_batch_is_allowed_primitive( primitive ) && n ) {
GL_BATCH_CALL<VERTEX>* last = &batch->calls.data[n - 1];
if( last->primitive == primitive
&& vp_start == last->clip_start && vp_dim == last->viewport_dim
@@ -162,8 +179,11 @@ inline GL_BATCH_CALL<VERTEX>* gl_batch_get_call( GL_BATCH<VERTEX>* batch, GL_TEX
.textures = {},
};
- *texid = 0;
- call.textures.push( tex );
+ if( tex ) {
+ *texid = 0;
+ call.textures.push( tex );
+ } else *texid = SAMPLER_ID_NONE;
+
return batch->calls.push( call );
}
@@ -191,8 +211,11 @@ inline void gl_batch_insert(
call = gl_batch_get_call( batch, tex, primitive, &texid );
- for( U32 i = 0; i < count; ++i ) {
+ for( U32 i = 0; i < count; ++i )
vertices[i].sampler = texid;
- call->vertices.push( vertices[i] );
- }
+
+ if( call->vertices.capacity < call->vertices.size + count )
+ call->vertices.reserve( (call->vertices.size + count) * 4 );
+ memcpy( &call->vertices.data[call->vertices.size], vertices, sizeof( VERTEX ) * count );
+ call->vertices.size += count;
}