-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
144 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const sokol = @import("sokol"); | ||
const sg = sokol.gfx; | ||
|
||
pub const Morph = struct {}; | ||
pub const Skin = struct {}; | ||
|
||
pub const Deform = @This(); | ||
|
||
bind: sg.Bindings = sg.Bindings{}, | ||
morph: ?Morph = null, | ||
skin: ?Skin = null, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const stb = @import("stb/stb.zig"); | ||
|
||
pub const Image = @This(); | ||
width: u32, | ||
height: u32, | ||
channels: u32, | ||
pixels: []const u8, | ||
|
||
pub const white = Image{ | ||
.width = 2, | ||
.height = 2, | ||
.channels = 4, | ||
.pixels = &.{ | ||
255, 255, 255, 255, | ||
255, 255, 255, 255, | ||
255, 255, 255, 255, | ||
255, 255, 255, 255, | ||
}, | ||
}; | ||
|
||
pub fn init(data: []const u8) ?@This() { | ||
var img_width: c_int = undefined; | ||
var img_height: c_int = undefined; | ||
var num_channels: c_int = undefined; | ||
const desired_channels = 4; | ||
const pixels = stb.image.stbi_load_from_memory( | ||
@ptrCast(&data[0]), | ||
@intCast(data.len), | ||
&img_width, | ||
&img_height, | ||
&num_channels, | ||
desired_channels, | ||
) orelse { | ||
return null; | ||
}; | ||
return .{ | ||
.width = @intCast(img_width), | ||
.height = @intCast(img_height), | ||
.channels = @intCast(num_channels), | ||
.pixels = pixels[0..@intCast(img_width * img_height * num_channels)], | ||
}; | ||
} | ||
|
||
pub fn deinit(self: @This()) void { | ||
stb.image.stbi_image_free(&self.pixels[0]); | ||
} | ||
|
||
pub fn byteLength(self: @This()) u32 { | ||
return self.width * self.height * 4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const sokol = @import("sokol"); | ||
const sg = sokol.gfx; | ||
const shader = @import("gltf.glsl.zig"); | ||
const Image = @import("Image.zig"); | ||
|
||
pub const Texture = @This(); | ||
fs: sg.StageBindings = sg.StageBindings{}, | ||
|
||
pub fn init(image: Image, _sampler: ?sg.SamplerDesc) @This() { | ||
// init sokol | ||
var texture = Texture{}; | ||
texture.fs.images[shader.SLOT_colorTexture2D] = sg.allocImage(); | ||
texture.fs.samplers[shader.SLOT_colorTextureSmp] = sg.allocSampler(); | ||
sg.initSampler( | ||
texture.fs.samplers[shader.SLOT_colorTextureSmp], | ||
if (_sampler) |sampler| | ||
sampler | ||
else | ||
sg.SamplerDesc{ | ||
.wrap_u = .REPEAT, | ||
.wrap_v = .REPEAT, | ||
.min_filter = .LINEAR, | ||
.mag_filter = .LINEAR, | ||
.compare = .NEVER, | ||
}, | ||
); | ||
|
||
// initialize the sokol-gfx texture | ||
var img_desc = sg.ImageDesc{ | ||
.width = @intCast(image.width), | ||
.height = @intCast(image.height), | ||
// set pixel_format to RGBA8 for WebGL | ||
.pixel_format = .RGBA8, | ||
}; | ||
img_desc.data.subimage[0][0] = .{ | ||
.ptr = &image.pixels[0], | ||
.size = image.byteLength(), | ||
}; | ||
sg.initImage(texture.fs.images[shader.SLOT_colorTexture2D], img_desc); | ||
|
||
return texture; | ||
} |