basic generation + render + refactor
This commit is contained in:
parent
54780baed3
commit
f0430ea354
4 changed files with 57 additions and 34 deletions
46
src/main.zig
46
src/main.zig
|
|
@ -7,6 +7,7 @@ const v3 = raylib_helper.v3;
|
||||||
const chunks = @import("world/chunk.zig");
|
const chunks = @import("world/chunk.zig");
|
||||||
const generation = @import("world/generation.zig");
|
const generation = @import("world/generation.zig");
|
||||||
const render = @import("world/render.zig");
|
const render = @import("world/render.zig");
|
||||||
|
const ModelData = render.ModelData;
|
||||||
|
|
||||||
const TILE_TEXTURE_RESOLUTION = 16;
|
const TILE_TEXTURE_RESOLUTION = 16;
|
||||||
|
|
||||||
|
|
@ -82,26 +83,32 @@ pub fn main() !void {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 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();
|
||||||
var list_models = std.ArrayList(raylib.ChunkModel).init(a7r);
|
}
|
||||||
defer list_models.deinit();
|
const seed = 564553;
|
||||||
|
inline for (0..10) |x| for (0..3) |y| for (0..10) |z| {
|
||||||
var i: usize = 0;
|
const position = v3.new(@floatFromInt(x), @floatFromInt(y), @floatFromInt(z));
|
||||||
while (i < 1000) : (i += 1) {
|
const chunk = try generation.generate_chunk(a7r, position, seed, @divTrunc(seed, 2), 0.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();
|
defer chunk.deinit();
|
||||||
|
|
||||||
// generate model
|
|
||||||
const model = try render.chunk_to_model(chunk, texture, shader, tile_rows, tile_columns);
|
const model = try render.chunk_to_model(chunk, texture, shader, tile_rows, tile_columns);
|
||||||
|
|
||||||
// store model
|
// Allocate memory for the model on the heap
|
||||||
try list_models.append(model);
|
const model_ptr = try a7r.create(ModelData);
|
||||||
}
|
model_ptr.* = model;
|
||||||
|
|
||||||
|
|
||||||
|
try pointer_list_models.append(model_ptr);
|
||||||
|
};
|
||||||
|
|
||||||
// if (benchmark_chunk_meshing) {
|
// if (benchmark_chunk_meshing) {
|
||||||
// var tmp: u64 = 0;
|
// var tmp: u64 = 0;
|
||||||
|
|
@ -148,12 +155,11 @@ pub fn main() !void {
|
||||||
defer raylib.EndDrawing();
|
defer raylib.EndDrawing();
|
||||||
|
|
||||||
// draw models of chunks
|
// draw models of chunks
|
||||||
inline for (0..10) |x| for (0..10) |y| for (0..10) |z| {
|
for (pointer_list_models.items) |model_data_pointer| {
|
||||||
const chunk_pos_x: u64 = @intCast(x);
|
if (model_data_pointer != null) {
|
||||||
const chunk_pos_y: u64 = @intCast(y);
|
try render.draw_model(model_data_pointer.?.*, camera, shader);
|
||||||
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);
|
}
|
||||||
};
|
|
||||||
|
|
||||||
raylib.DrawFPS(10, 10);
|
raylib.DrawFPS(10, 10);
|
||||||
try drawCameraPosition(camera, 10, 30);
|
try drawCameraPosition(camera, 10, 30);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const raylib_helper = @import("../lib_helpers/raylib_helper.zig");
|
const raylib_helper = @import("../lib_helpers/raylib_helper.zig");
|
||||||
const raylib = raylib_helper.raylib;
|
const raylib = raylib_helper.raylib;
|
||||||
|
const Vector3 = raylib.Vector3;
|
||||||
const v3 = raylib_helper.v3;
|
const v3 = raylib_helper.v3;
|
||||||
const A7r = std.mem.Allocator;
|
const A7r = std.mem.Allocator;
|
||||||
const comptimePrint = std.fmt.comptimePrint;
|
const comptimePrint = std.fmt.comptimePrint;
|
||||||
|
|
@ -52,10 +53,12 @@ comptime {
|
||||||
pub const Chunk = struct {
|
pub const Chunk = struct {
|
||||||
tiles: []u32,
|
tiles: []u32,
|
||||||
a7r: A7r,
|
a7r: A7r,
|
||||||
|
position: Vector3,
|
||||||
|
|
||||||
pub fn init(a7r: A7r) !Chunk {
|
pub fn init(a7r: A7r, position: Vector3) !Chunk {
|
||||||
const self = Chunk{
|
const self = Chunk{
|
||||||
.a7r = a7r,
|
.a7r = a7r,
|
||||||
|
.position = position,
|
||||||
.tiles = try a7r.alloc(u32, 32 * 32 * 32),
|
.tiles = try a7r.alloc(u32, 32 * 32 * 32),
|
||||||
};
|
};
|
||||||
@memset(self.tiles, 0);
|
@memset(self.tiles, 0);
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,15 @@ const std = @import("std");
|
||||||
const chunks = @import("../world/chunk.zig");
|
const chunks = @import("../world/chunk.zig");
|
||||||
const znoise = @import("znoise");
|
const znoise = @import("znoise");
|
||||||
const A7r = std.mem.Allocator;
|
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 {
|
pub fn generate_chunk(a7r: A7r, position: Vector3, seed: i32, tile_seed: i32, frequency: f32) !Chunk {
|
||||||
var chunk = try chunks.Chunk.init(a7r);
|
var chunk = try chunks.Chunk.init(a7r, position);
|
||||||
|
|
||||||
const height_generator = znoise.FnlGenerator{ .seed = seed };
|
const height_generator = znoise.FnlGenerator{ .seed = seed };
|
||||||
const tile_type_generator = znoise.FnlGenerator{ .seed = tile_seed, .frequency = frequency };
|
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 x: u5 = @intCast(raw_x);
|
||||||
const y: u5 = @intCast(raw_y);
|
const y: u5 = @intCast(raw_y);
|
||||||
const z: u5 = @intCast(raw_z);
|
const z: u5 = @intCast(raw_z);
|
||||||
const xf: f32 = @floatFromInt(raw_x);
|
const xf: f32 = position.x * 32 + @as(f32, @floatFromInt(raw_x));
|
||||||
const yf: f32 = @floatFromInt(raw_y);
|
const yf: f32 = position.y * 32 + @as(f32, @floatFromInt(raw_y));
|
||||||
const zf: f32 = @floatFromInt(raw_z);
|
const zf: f32 = position.z * 32 + @as(f32, @floatFromInt(raw_z));
|
||||||
const height: f32 = height_generator.noise2(xf, zf) * 80;
|
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;
|
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);
|
if (height >= yf) chunk.setTile(x, y, z, tile_type);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -8,25 +8,34 @@ const Texture2D = raylib.Texture2D;
|
||||||
const Shader = raylib.Shader;
|
const Shader = raylib.Shader;
|
||||||
const Camera3D = raylib.Camera3D;
|
const Camera3D = raylib.Camera3D;
|
||||||
const v3 = raylib_helper.v3;
|
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));
|
const model = raylib.LoadChunkModelFromMesh(try chunk.createMesh(tile_rows, tile_columns));
|
||||||
model.materials[0].maps[raylib.MATERIAL_MAP_DIFFUSE].texture = texture;
|
model.materials[0].maps[raylib.MATERIAL_MAP_DIFFUSE].texture = texture;
|
||||||
model.materials[0].shader = shader;
|
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 {
|
pub fn draw_model(model_data: ModelData, camera: Camera3D, shader: Shader) !void {
|
||||||
|
|
||||||
const model_position = v3.new(@floatFromInt(chunk_pos_x*16), @floatFromInt(chunk_pos_y*16), @floatFromInt(chunk_pos_z*16));
|
|
||||||
|
|
||||||
{
|
{
|
||||||
raylib.BeginMode3D(camera);
|
raylib.BeginMode3D(camera);
|
||||||
defer raylib.EndMode3D();
|
defer raylib.EndMode3D();
|
||||||
raylib.BeginShaderMode(shader);
|
raylib.BeginShaderMode(shader);
|
||||||
defer raylib.EndShaderMode();
|
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