implement image atlasing and tiling
This commit is contained in:
parent
5c7f996eb6
commit
e560c0de6b
7 changed files with 95 additions and 33 deletions
|
|
@ -54,7 +54,7 @@ pub const Chunk = struct {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn createMesh(chunk: Chunk) !raylib.Mesh {
|
||||
pub fn createMesh(chunk: Chunk, tile_rows: u32, tile_columns: u32) !raylib.Mesh {
|
||||
var raw_quads = try std.ArrayList(RawQuad).initCapacity(chunk.a7r, 4096);
|
||||
defer raw_quads.deinit();
|
||||
|
||||
|
|
@ -78,14 +78,13 @@ pub const Chunk = struct {
|
|||
const surface = tile_surfaces[y][z];
|
||||
if (surface == 0) continue;
|
||||
var y2 = y + 1;
|
||||
var z2 = z + 1;
|
||||
while (y2 <= 31 and tile_surfaces[y2][z] == surface) : (y2 += 1) {
|
||||
tile_surfaces[y2][z] = 0;
|
||||
}
|
||||
var z2 = z + 1;
|
||||
zloop: while (z2 <= 31) : (z2 += 1) {
|
||||
for (y..y2) |ytmp| if (tile_surfaces[ytmp][z2] != surface) break :zloop;
|
||||
for (y..y2) |ytmp| tile_surfaces[ytmp][z2] = 0;
|
||||
tile_surfaces[y][z2] = 0;
|
||||
}
|
||||
y2 -= 1;
|
||||
z2 -= 1;
|
||||
|
|
@ -118,50 +117,71 @@ pub const Chunk = struct {
|
|||
|
||||
const vertices: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 3)));
|
||||
const texcoords: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 2)));
|
||||
const texcoords2: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 2)));
|
||||
const normals: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 3)));
|
||||
|
||||
for (raw_quads.items, 0..) |raw_quad, i| {
|
||||
if (raw_quad.tile <= 0) continue;
|
||||
const tile = @as(u32, @intCast(raw_quad.tile));
|
||||
|
||||
for (0..6) |j| {
|
||||
normals[18 * i + 3 * j + 0] = raw_quad.normal.x;
|
||||
normals[18 * i + 3 * j + 1] = raw_quad.normal.y;
|
||||
normals[18 * i + 3 * j + 2] = raw_quad.normal.z;
|
||||
}
|
||||
|
||||
const left_uv = @as(f32, @floatFromInt(tile % tile_columns)) / @as(f32, @floatFromInt(tile_columns));
|
||||
const right_uv = @as(f32, @floatFromInt(tile % tile_columns + 1)) / @as(f32, @floatFromInt(tile_columns));
|
||||
const top_uv = @as(f32, @floatFromInt(tile / tile_columns)) / @as(f32, @floatFromInt(tile_rows));
|
||||
const bottom_uv = @as(f32, @floatFromInt(tile / tile_columns + 1)) / @as(f32, @floatFromInt(tile_rows));
|
||||
|
||||
vertices[18 * i + 0] = raw_quad.top_left.x;
|
||||
vertices[18 * i + 1] = raw_quad.top_left.y;
|
||||
vertices[18 * i + 2] = raw_quad.top_left.z;
|
||||
texcoords[12 * i + 0] = 0.0;
|
||||
texcoords[12 * i + 1] = 0.0;
|
||||
texcoords[12 * i + 0] = left_uv;
|
||||
texcoords[12 * i + 1] = top_uv;
|
||||
texcoords2[12 * i + 0] = 0.0;
|
||||
texcoords2[12 * i + 1] = 0.0;
|
||||
|
||||
vertices[18 * i + 3] = raw_quad.top_right.x;
|
||||
vertices[18 * i + 4] = raw_quad.top_right.y;
|
||||
vertices[18 * i + 5] = raw_quad.top_right.z;
|
||||
texcoords[12 * i + 2] = raw_quad.width;
|
||||
texcoords[12 * i + 3] = 0.0;
|
||||
texcoords[12 * i + 2] = right_uv;
|
||||
texcoords[12 * i + 3] = top_uv;
|
||||
texcoords2[12 * i + 2] = raw_quad.width;
|
||||
texcoords2[12 * i + 3] = 0.0;
|
||||
|
||||
vertices[18 * i + 6] = raw_quad.bottom_left.x;
|
||||
vertices[18 * i + 7] = raw_quad.bottom_left.y;
|
||||
vertices[18 * i + 8] = raw_quad.bottom_left.z;
|
||||
texcoords[12 * i + 4] = 0.0;
|
||||
texcoords[12 * i + 5] = raw_quad.height;
|
||||
texcoords[12 * i + 4] = left_uv;
|
||||
texcoords[12 * i + 5] = bottom_uv;
|
||||
texcoords2[12 * i + 4] = 0.0;
|
||||
texcoords2[12 * i + 5] = raw_quad.height;
|
||||
|
||||
vertices[18 * i + 9] = raw_quad.bottom_right.x;
|
||||
vertices[18 * i + 10] = raw_quad.bottom_right.y;
|
||||
vertices[18 * i + 11] = raw_quad.bottom_right.z;
|
||||
texcoords[12 * i + 6] = raw_quad.width;
|
||||
texcoords[12 * i + 7] = raw_quad.height;
|
||||
texcoords[12 * i + 6] = right_uv;
|
||||
texcoords[12 * i + 7] = bottom_uv;
|
||||
texcoords2[12 * i + 6] = raw_quad.width;
|
||||
texcoords2[12 * i + 7] = raw_quad.height;
|
||||
|
||||
vertices[18 * i + 12] = raw_quad.bottom_left.x;
|
||||
vertices[18 * i + 13] = raw_quad.bottom_left.y;
|
||||
vertices[18 * i + 14] = raw_quad.bottom_left.z;
|
||||
texcoords[12 * i + 8] = 0.0;
|
||||
texcoords[12 * i + 9] = raw_quad.height;
|
||||
texcoords[12 * i + 8] = left_uv;
|
||||
texcoords[12 * i + 9] = bottom_uv;
|
||||
texcoords2[12 * i + 8] = 0.0;
|
||||
texcoords2[12 * i + 9] = raw_quad.height;
|
||||
|
||||
vertices[18 * i + 15] = raw_quad.top_right.x;
|
||||
vertices[18 * i + 16] = raw_quad.top_right.y;
|
||||
vertices[18 * i + 17] = raw_quad.top_right.z;
|
||||
texcoords[12 * i + 10] = raw_quad.width;
|
||||
texcoords[12 * i + 11] = 0.0;
|
||||
texcoords[12 * i + 10] = right_uv;
|
||||
texcoords[12 * i + 11] = top_uv;
|
||||
texcoords2[12 * i + 10] = raw_quad.width;
|
||||
texcoords2[12 * i + 11] = 0.0;
|
||||
}
|
||||
|
||||
var mesh = raylib.Mesh{
|
||||
|
|
@ -170,7 +190,7 @@ pub const Chunk = struct {
|
|||
|
||||
.vertices = vertices,
|
||||
.texcoords = texcoords,
|
||||
.texcoords2 = null,
|
||||
.texcoords2 = texcoords2,
|
||||
.normals = normals,
|
||||
.tangents = null,
|
||||
.colors = null,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue