basic generation + render + refactor
This commit is contained in:
parent
54780baed3
commit
f0430ea354
4 changed files with 57 additions and 34 deletions
52
src/main.zig
52
src/main.zig
|
|
@ -7,6 +7,7 @@ const v3 = raylib_helper.v3;
|
|||
const chunks = @import("world/chunk.zig");
|
||||
const generation = @import("world/generation.zig");
|
||||
const render = @import("world/render.zig");
|
||||
const ModelData = render.ModelData;
|
||||
|
||||
const TILE_TEXTURE_RESOLUTION = 16;
|
||||
|
||||
|
|
@ -82,26 +83,32 @@ pub fn main() !void {
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
var list_models = std.ArrayList(raylib.ChunkModel).init(a7r);
|
||||
defer list_models.deinit();
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < 1000) : (i += 1) {
|
||||
// generate chunk
|
||||
const randInt: i32 = @intCast(i * 145343 % 4539);
|
||||
const chunk = try generation.generate_world(a7r, randInt, @divTrunc(randInt, 2), 0.1);
|
||||
defer chunk.deinit();
|
||||
|
||||
// generate model
|
||||
const model = try render.chunk_to_model(chunk, texture, shader, tile_rows, tile_columns);
|
||||
|
||||
// store model
|
||||
try list_models.append(model);
|
||||
// generate array list of chunk pointers
|
||||
var pointer_list_models = try std.ArrayList(?*ModelData).initCapacity(a7r, 1000);
|
||||
defer {
|
||||
// Free each model stored
|
||||
for (pointer_list_models.items) |maybe_ptr| {
|
||||
if (maybe_ptr) |ptr| {
|
||||
a7r.destroy(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
pointer_list_models.deinit();
|
||||
}
|
||||
const seed = 564553;
|
||||
inline for (0..10) |x| for (0..3) |y| for (0..10) |z| {
|
||||
const position = v3.new(@floatFromInt(x), @floatFromInt(y), @floatFromInt(z));
|
||||
const chunk = try generation.generate_chunk(a7r, position, seed, @divTrunc(seed, 2), 0.1);
|
||||
defer chunk.deinit();
|
||||
|
||||
const model = try render.chunk_to_model(chunk, texture, shader, tile_rows, tile_columns);
|
||||
|
||||
// Allocate memory for the model on the heap
|
||||
const model_ptr = try a7r.create(ModelData);
|
||||
model_ptr.* = model;
|
||||
|
||||
try pointer_list_models.append(model_ptr);
|
||||
};
|
||||
|
||||
// if (benchmark_chunk_meshing) {
|
||||
// var tmp: u64 = 0;
|
||||
|
|
@ -148,12 +155,11 @@ pub fn main() !void {
|
|||
defer raylib.EndDrawing();
|
||||
|
||||
// draw models of chunks
|
||||
inline for (0..10) |x| for (0..10) |y| for (0..10) |z| {
|
||||
const chunk_pos_x: u64 = @intCast(x);
|
||||
const chunk_pos_y: u64 = @intCast(y);
|
||||
const chunk_pos_z: u64 = @intCast(z);
|
||||
try render.draw_model(list_models.items[chunk_pos_x * 100 + chunk_pos_y * 10 + chunk_pos_z], camera, shader, chunk_pos_x * 2, chunk_pos_y * 2, chunk_pos_z * 2);
|
||||
};
|
||||
for (pointer_list_models.items) |model_data_pointer| {
|
||||
if (model_data_pointer != null) {
|
||||
try render.draw_model(model_data_pointer.?.*, camera, shader);
|
||||
}
|
||||
}
|
||||
|
||||
raylib.DrawFPS(10, 10);
|
||||
try drawCameraPosition(camera, 10, 30);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
const std = @import("std");
|
||||
const raylib_helper = @import("../lib_helpers/raylib_helper.zig");
|
||||
const raylib = raylib_helper.raylib;
|
||||
const Vector3 = raylib.Vector3;
|
||||
const v3 = raylib_helper.v3;
|
||||
const A7r = std.mem.Allocator;
|
||||
const comptimePrint = std.fmt.comptimePrint;
|
||||
|
|
@ -52,10 +53,12 @@ comptime {
|
|||
pub const Chunk = struct {
|
||||
tiles: []u32,
|
||||
a7r: A7r,
|
||||
position: Vector3,
|
||||
|
||||
pub fn init(a7r: A7r) !Chunk {
|
||||
pub fn init(a7r: A7r, position: Vector3) !Chunk {
|
||||
const self = Chunk{
|
||||
.a7r = a7r,
|
||||
.position = position,
|
||||
.tiles = try a7r.alloc(u32, 32 * 32 * 32),
|
||||
};
|
||||
@memset(self.tiles, 0);
|
||||
|
|
|
|||
|
|
@ -2,11 +2,15 @@ const std = @import("std");
|
|||
const chunks = @import("../world/chunk.zig");
|
||||
const znoise = @import("znoise");
|
||||
const A7r = std.mem.Allocator;
|
||||
//const Chunk = chunks.Chunk;
|
||||
const Chunk = chunks.Chunk;
|
||||
|
||||
const raylib_helper = @import("../lib_helpers/raylib_helper.zig");
|
||||
const raylib = raylib_helper.raylib;
|
||||
const Vector3 = raylib.Vector3;
|
||||
|
||||
|
||||
pub fn generate_world(a7r: A7r, seed: i32, tile_seed: i32, frequency: f32) !chunks.Chunk {
|
||||
var chunk = try chunks.Chunk.init(a7r);
|
||||
pub fn generate_chunk(a7r: A7r, position: Vector3, seed: i32, tile_seed: i32, frequency: f32) !Chunk {
|
||||
var chunk = try chunks.Chunk.init(a7r, position);
|
||||
|
||||
const height_generator = znoise.FnlGenerator{ .seed = seed };
|
||||
const tile_type_generator = znoise.FnlGenerator{ .seed = tile_seed, .frequency = frequency };
|
||||
|
|
@ -14,10 +18,11 @@ pub fn generate_world(a7r: A7r, seed: i32, tile_seed: i32, frequency: f32) !chun
|
|||
const x: u5 = @intCast(raw_x);
|
||||
const y: u5 = @intCast(raw_y);
|
||||
const z: u5 = @intCast(raw_z);
|
||||
const xf: f32 = @floatFromInt(raw_x);
|
||||
const yf: f32 = @floatFromInt(raw_y);
|
||||
const zf: f32 = @floatFromInt(raw_z);
|
||||
const height: f32 = height_generator.noise2(xf, zf) * 80;
|
||||
const xf: f32 = position.x * 32 + @as(f32, @floatFromInt(raw_x));
|
||||
const yf: f32 = position.y * 32 + @as(f32, @floatFromInt(raw_y));
|
||||
const zf: f32 = position.z * 32 + @as(f32, @floatFromInt(raw_z));
|
||||
const height: f32 = height_generator.noise2(xf, zf) * 50 + 50;
|
||||
//std.debug.print("x: {} y: {} z: {}", .{position.x, position.y, position.z});
|
||||
const tile_type: u32 = if (tile_type_generator.noise3(xf, yf, zf) > 0) 1 else 2;
|
||||
if (height >= yf) chunk.setTile(x, y, z, tile_type);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -8,25 +8,34 @@ const Texture2D = raylib.Texture2D;
|
|||
const Shader = raylib.Shader;
|
||||
const Camera3D = raylib.Camera3D;
|
||||
const v3 = raylib_helper.v3;
|
||||
const Vector3 = raylib.Vector3;
|
||||
|
||||
// wasnt allowed to add position and data to model struct so need to make shit up on top of it
|
||||
pub const ModelData = struct {
|
||||
model: ChunkModel,
|
||||
position: Vector3,
|
||||
};
|
||||
|
||||
pub fn chunk_to_model(chunk: Chunk, texture: Texture2D, shader: Shader, tile_rows: u32, tile_columns: u32) !ChunkModel {
|
||||
pub fn chunk_to_model(chunk: Chunk, texture: Texture2D, shader: Shader, tile_rows: u32, tile_columns: u32) !ModelData {
|
||||
const model = raylib.LoadChunkModelFromMesh(try chunk.createMesh(tile_rows, tile_columns));
|
||||
model.materials[0].maps[raylib.MATERIAL_MAP_DIFFUSE].texture = texture;
|
||||
model.materials[0].shader = shader;
|
||||
|
||||
return model;
|
||||
const model_data: ModelData = .{
|
||||
.model = model,
|
||||
.position = chunk.position,
|
||||
};
|
||||
|
||||
return model_data;
|
||||
}
|
||||
|
||||
pub fn draw_model(model: ChunkModel, camera: Camera3D, shader: Shader, chunk_pos_x: u64, chunk_pos_y: u64, chunk_pos_z: u64) !void {
|
||||
|
||||
const model_position = v3.new(@floatFromInt(chunk_pos_x*16), @floatFromInt(chunk_pos_y*16), @floatFromInt(chunk_pos_z*16));
|
||||
pub fn draw_model(model_data: ModelData, camera: Camera3D, shader: Shader) !void {
|
||||
|
||||
{
|
||||
raylib.BeginMode3D(camera);
|
||||
defer raylib.EndMode3D();
|
||||
raylib.BeginShaderMode(shader);
|
||||
defer raylib.EndShaderMode();
|
||||
raylib.DrawChunkModel(model, model_position, 0.5, raylib.WHITE);
|
||||
raylib.DrawChunkModel(model_data.model, v3.scl(model_data.position, 16.0), 0.5, raylib.WHITE);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue