From: Brendan Hansen Date: Thu, 22 Apr 2021 16:02:36 +0000 (-0500) Subject: basics of rendering the world texture X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=4b7b53fd862a6057c5cb4b1e69867627979c7728;p=sand-toy.git basics of rendering the world texture --- diff --git a/lib/immediate_renderer.onyx b/lib/immediate_renderer.onyx index e37a0ca..470a16c 100644 --- a/lib/immediate_renderer.onyx +++ b/lib/immediate_renderer.onyx @@ -111,10 +111,10 @@ Immediate_Renderer :: struct { push_vertex :: proc { // If a color is not provided, the previous color is used. (use ir: ^Immediate_Renderer, position: Vector2) { - push_vertex(ir, position, color = verticies[vertex_count - 1].color); + push_vertex(ir, position, texture = .{ 0, 0 }, color = verticies[vertex_count - 1].color); }, - (use ir: ^Immediate_Renderer, position: Vector2, color: Color4) { + (use ir: ^Immediate_Renderer, position: Vector2, color: Color4, texture: Vector2 = .{0,0}) { if vertex_count >= verticies.count do ir->flush(); vertex_ptr := ^verticies[vertex_count]; @@ -122,7 +122,7 @@ Immediate_Renderer :: struct { vertex_ptr.position = position; vertex_ptr.color = color; - vertex_ptr.texture = .{ 0, 0 }; + vertex_ptr.texture = texture; }, } @@ -136,7 +136,17 @@ Immediate_Renderer :: struct { push_vertex(ir, .{ position.x, position.y + size.y }); } - // NOTE: Calling set_texture without a paramter will disabled textured rendering. + textured_quad :: (use ir: ^Immediate_Renderer, position: Vector2, size: Vector2, texture_position: Vector2, texture_size: Vector2, color: Color4 = .{1,1,1}) { + push_vertex(ir, .{ position.x, position.y }, color, .{ texture_position.x, texture_position.y }); + push_vertex(ir, .{ position.x + size.x, position.y }, color, .{ texture_position.x + texture_size.x, texture_position.y }); + push_vertex(ir, .{ position.x + size.x, position.y + size.y }, color, .{ texture_position.x + texture_size.x, texture_position.y + texture_size.y }); + + push_vertex(ir, .{ position.x, position.y }, color, .{ texture_position.x, texture_position.y }); + push_vertex(ir, .{ position.x + size.x, position.y + size.y }, color, .{ texture_position.x + texture_size.x, texture_position.y + texture_size.y }); + push_vertex(ir, .{ position.x, position.y + size.y }, color, .{ texture_position.x, texture_position.y + texture_size.y }); + } + + // NOTE: Calling set_texture without a parameter will disable textured rendering. set_texture :: (use ir: ^Immediate_Renderer, texture_id: i32 = -1) { if vertex_count > 0 do flush(ir); @@ -174,6 +184,10 @@ immediate_quad :: (position: Vector2, size: Vector2, color: Color4 = .{1,1,1}) { immediate_renderer->quad(position, size, color); } +immediate_textured_quad :: (position: Vector2, size: Vector2, texture_position: Vector2, texture_size: Vector2, color: Color4 = .{1,1,1}) { + immediate_renderer->textured_quad(position, size, texture_position, texture_size, color); +} + immediate_flush :: () do immediate_renderer->flush(); immediate_set_texture :: (texture_id: i32 = -1) do immediate_renderer->set_texture(texture_id); diff --git a/site/js/webgl2.js b/site/js/webgl2.js index 61d565a..bd53399 100644 --- a/site/js/webgl2.js +++ b/site/js/webgl2.js @@ -249,7 +249,7 @@ window.ONYX_MODULES.push({ stencilOp(fail, zfail, mask) { gl.stencilOp(fail, zfail, mask); }, stencilOpSeparate(face, fail, zfail, zpass) { gl.stencilOpSeparate(face, fail, zfail, zpass); }, texImage2D(target, level, internalforamt, width, height, border, format, type, pixels, pixelslen) { - const data = new DataView(window.ONYX_MEMORY.buffer, pixels, pixelslen); + const data = new Uint8Array(window.ONYX_MEMORY.buffer, pixels, pixelslen); gl.texImage2D(target, level, internalforamt, width, height, border, format, type, data); }, texParameterf(target, pname, param) { gl.texParameterf(target, pname, param); }, diff --git a/site/sand_toy.wasm b/site/sand_toy.wasm index ed634df..ad6c39b 100644 Binary files a/site/sand_toy.wasm and b/site/sand_toy.wasm differ diff --git a/src/sand_toy.onyx b/src/sand_toy.onyx index 62134b0..aa75920 100644 --- a/src/sand_toy.onyx +++ b/src/sand_toy.onyx @@ -3,10 +3,28 @@ use package core #private_file events :: package js_events #private_file gl :: package gl + +world_texture : gl.GLTexture +world_texture_data : [] u8 +world_width := 256 +world_height := 256 + init :: () { events.init(); gl.init("simulation-canvas"); + + world_texture = gl.createTexture(); + world_texture_data = memory.make_slice(u8, world_width * world_height * 3); + + gl.bindTexture(gl.TEXTURE_2D, world_texture); + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, world_width, world_height, 0, gl.RGB, gl.UNSIGNED_BYTE, world_texture_data); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); + 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, -1); + imgui.immediate_renderer_init(); } @@ -36,18 +54,25 @@ poll_events :: () { } update :: () { + world_texture_data[(100 * world_width + 50) * 3 + 0] = 255; + world_texture_data[(100 * world_width + 50) * 3 + 2] = 255; } draw :: () { gl.clearColor(0, 0, 0, 1); gl.clear(gl.COLOR_BUFFER_BIT); - use imgui; + use imgui { immediate_set_texture, immediate_textured_quad, immediate_flush }; - immediate_set_texture(); - immediate_quad(.{ -1, -1 }, .{ 2, 2 }, color=.{ 1, 0, 0 }); + gl.bindTexture(gl.TEXTURE_2D, world_texture); + gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, world_width, world_height, gl.RGB, gl.UNSIGNED_BYTE, world_texture_data); + + immediate_set_texture(0); + immediate_textured_quad(.{ -1, -1 }, .{ 2, 2 }, .{ 0, 0 }, .{ 1, 1 }, color=.{ 1, 1, 1 }); immediate_flush(); + + gl.bindTexture(gl.TEXTURE_2D, -1); } loop :: () -> void #export {