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];
vertex_ptr.position = position;
vertex_ptr.color = color;
- vertex_ptr.texture = .{ 0, 0 };
+ vertex_ptr.texture = texture;
},
}
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);
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);
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); },
#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();
}
}
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 {