more microui

This commit is contained in:
2026-03-25 17:48:46 +05:45
parent 5bc235f1ab
commit 00ae6cbc4e
5 changed files with 239 additions and 42 deletions
+114 -7
View File
@@ -1346,6 +1346,7 @@ Draw :: struct
solid_circles : SolidCircle,
solid_capsules : SolidCapsules,
polygons : SolidPolygon,
textures : Texture,
drawCounters : bool,
//regular_font : im.Font,
frame_buffer : u32,
@@ -1435,11 +1436,33 @@ draw_flush :: proc(draw : ^Draw) {
lines_flush(&draw.lines, &draw.cam)
points_flush(&draw.points, &draw.cam)
circle_flush(&draw.circles, &draw.cam)
textures_flush(draw)
text_flush(draw)
check_opengl()
}
Rect :: struct {
x, y, w, h : f32,
}
TextureData :: struct
{
texture_id : u32, //opengl texture id
width, height : i32,
src_rect : Rect, // in pixel format
dst_rect : Rect, // in pixel format
}
//Contains multiple textures
Texture :: struct
{
textures : [dynamic]TextureData,
vao, vbo, ebo: u32,
uniforms : gl.Uniforms,
program_id : u32,
}
/*
Create texture and render texture
@@ -1449,7 +1472,51 @@ draw_flush :: proc(draw : ^Draw) {
render_texture :: gets a texture_id and rect and displays the texture (generic but convinent for micorui)
*/
create_texutre :: proc(data: []u8, width, height: i32) -> u32
textures_create :: proc(texture: ^Texture)
{
texture.program_id, _ = gl.load_shaders_source(#load("shaders/texture.vs"), #load("shaders/texture.fs"))
gl.UseProgram(texture.program_id)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
texture.uniforms = gl.get_uniforms_from_program(texture.program_id)
vertices : []f32 = {
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 0.0, 0.0,
1.0, 0.0, 1.0, 0.0,
1.0, 1.0, 1.0, 1.0,
}
indices := []u32 {
0, 1, 2,
0, 2, 3,
}
gl.GenVertexArrays(1, &texture.vao)
gl.GenBuffers(1, &texture.vbo)
gl.GenBuffers(1, &texture.ebo)
gl.BindVertexArray(texture.vao)
gl.BindBuffer(gl.ARRAY_BUFFER, texture.vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(vertices) * size_of(f32), raw_data(vertices), gl.DYNAMIC_DRAW)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, texture.ebo)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(indices) * size_of(u32), raw_data(indices), gl.DYNAMIC_DRAW)
gl.VertexAttribPointer(0, 2, gl.FLOAT, false, 4 * size_of(f32), 0)
gl.EnableVertexAttribArray(0)
gl.VertexAttribPointer(1, 2, gl.FLOAT, false, 4 * size_of(f32), 2 * size_of(f32))
gl.EnableVertexAttribArray(1)
//gl.BindBuffer(gl.ARRAY_BUFFER, 0)
//gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0)
gl.UseProgram(0)
}
create_texture :: proc(data: []u8, width, height: i32) -> u32
{
texture : u32
@@ -1457,16 +1524,55 @@ create_texutre :: proc(data: []u8, width, height: i32) -> u32
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA8, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, raw_data(data))
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.BindTexture(gl.TEXTURE_2D, 0)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.BindTexture( gl.TEXTURE_2D, 0)
return texture
}
textures_flush :: proc(d: ^Draw)
{
texture := &d.textures
gl.UseProgram(texture.program_id)
gl.Uniform2f(texture.uniforms["u_resolution"].location, f32(d.cam.width), f32(d.cam.height))
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.ActiveTexture(gl.TEXTURE0)
for &t in &texture.textures
{
vertices := []f32{
0.0, 1.0, t.src_rect.x , (t.src_rect.y+ f32(t.src_rect.h)),
0.0, 0.0, t.src_rect.x , (t.src_rect.y ),
1.0, 0.0, (t.src_rect.x + f32(t.src_rect.w)), (t.src_rect.y ),
1.0, 1.0, (t.src_rect.x + f32(t.src_rect.w)), (t.src_rect.y+ f32(t.src_rect.h))
}
gl.BindBuffer(gl.ARRAY_BUFFER, texture.vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(vertices) * size_of(f32), raw_data(vertices), gl.DYNAMIC_DRAW)
gl.Uniform2f(texture.uniforms["u_position"].location, t.dst_rect.x, t.dst_rect.y)
gl.Uniform2f(texture.uniforms["u_size"].location, t.dst_rect.w, t.dst_rect.h)
gl.BindTexture(gl.TEXTURE_2D, t.texture_id)
gl.Uniform1i(texture.uniforms["ourTexture"].location, 0)
gl.BindVertexArray(texture.vao)
gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, nil)
}
gl.Disable(gl.BLEND)
gl.UseProgram(0)
check_opengl()
clear(&texture.textures)
}
@@ -1481,6 +1587,7 @@ draw_create :: proc(draw : ^Draw, camera : ^Camera, font_path : string = "")
circle_create(&draw.circles)
solid_circle_create(&draw.solid_circles)
solid_polygon_create(&draw.polygons)
textures_create(&draw.textures)
{
draw.glyph.atlas_width = draw.cam.width
+1 -1
View File
@@ -18,7 +18,7 @@ void main(){
vec2 ndc;
ndc.x = (screen_pos.x / u_resolution.x) * 2.0 - 1.0;
ndc.y = 1.0 - (screen_pos.y / u_resolution.x) * 2.0;
ndc.y = 1.0 - (screen_pos.y / u_resolution.y) * 2.0;
gl_Position = vec4(ndc, 0.0, 1.0);
TexCoord = aTexCoord;