basics of rendering the world texture
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 22 Apr 2021 16:02:36 +0000 (11:02 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 22 Apr 2021 16:02:36 +0000 (11:02 -0500)
lib/immediate_renderer.onyx
site/js/webgl2.js
site/sand_toy.wasm
src/sand_toy.onyx

index e37a0caac93a6501c579de4c33e8e0c53af568a9..470a16cfd989393ed97772589e571df87b373ce4 100644 (file)
@@ -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);
index 61d565a14c3c268cfb786eaefe08f620edf72268..bd5339936379059d152f471dea94bb1e7f0dce32 100644 (file)
@@ -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); },
index ed634df6c1f57a6b6bf3f6c377235f4bbfa24dbe..ad6c39bad00b1cfb75777e5912e18f5a93c6d4c0 100644 (file)
Binary files a/site/sand_toy.wasm and b/site/sand_toy.wasm differ
index 62134b0b930dc0220b10292f53e08831c390b293..aa75920dde1f480be4f31a3ab9ff8801fc2fafeb 100644 (file)
@@ -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 {