replace raylib with glfw; no full rewrite yet

This commit is contained in:
catangent 2025-07-03 10:31:12 +01:00
parent 6b25507808
commit 57d1417ae9
16 changed files with 77 additions and 41349 deletions

15
.gitmodules vendored
View file

@ -1,6 +1,15 @@
[submodule "znoise"]
path = znoise
url = https://github.com/zig-gamedev/znoise
[submodule "raylib-extended/raylib"]
path = raylib-extended/raylib
url = https://github.com/raysan5/raylib.git
[submodule "zglfw"]
path = zglfw
url = https://github.com/zig-gamedev/zglfw
[submodule "zmath"]
path = zmath
url = https://github.com/zig-gamedev/zmath.git
[submodule "harfbuzz"]
path = harfbuzz
url = https://github.com/hexops/harfbuzz.git
[submodule "mach-freetype"]
path = mach-freetype
url = https://github.com/hexops/mach-freetype.git

View file

@ -4,15 +4,26 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const rl_ext = b.dependency("raylib_extended", .{
const zglfw = b.dependency("zglfw", .{
.target = target,
.optimize = optimize,
});
const zmath = b.dependency("zmath", .{
.target = target,
.optimize = optimize,
});
const znoise = b.dependency("znoise", .{
.target = target,
.optimize = optimize,
});
const mach_freetype = b.dependency("mach_freetype", .{
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{
.name = "voxel_test",
.root_source_file = b.path("src/main.zig"),
@ -20,9 +31,14 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
exe.root_module.addImport("raylib_extended", rl_ext.module("raylib_extended"));
exe.root_module.addImport("znoise", znoise.module("root"));
exe.linkLibrary(znoise.artifact("FastNoiseLite"));
exe.root_module.addImport("zglfw", zglfw.module("root"));
if (target.result.os.tag != .emscripten) {
exe.linkLibrary(zglfw.artifact("glfw"));
}
exe.root_module.addImport("zmath", zmath.module("root"));
exe.root_module.addImport("mach_freetype", mach_freetype.module("root"));
b.installArtifact(exe);
@ -41,9 +57,14 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
exe_unit_tests.root_module.addImport("raylib_extended", rl_ext.module("raylib_extended"));
exe_unit_tests.root_module.addImport("znoise", znoise.module("root"));
exe_unit_tests.linkLibrary(znoise.artifact("FastNoiseLite"));
exe_unit_tests.root_module.addImport("zglfw", zglfw.module("root"));
if (target.result.os.tag != .emscripten) {
exe_unit_tests.linkLibrary(zglfw.artifact("glfw"));
}
exe_unit_tests.root_module.addImport("zmath", zmath.module("root"));
exe_unit_tests.root_module.addImport("mach_freetype", mach_freetype.module("root"));
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);

View file

@ -2,11 +2,13 @@
.name = .voxel_test,
.version = "0.0.0",
.minimum_zig_version = "0.14.0",
.fingerprint = 0x1720c90e411a705,
.fingerprint = 0x1720c90ba1d88f1,
.dependencies = .{
.znoise = .{ .path = "znoise" },
.raylib_extended = .{ .path = "raylib-extended" },
.zglfw = .{ .path = "zglfw" },
.zmath = .{ .path = "zmath" },
.mach_freetype = .{ .path = "mach-freetype" },
},
.paths = .{

1
mach-freetype Submodule

@ -0,0 +1 @@
Subproject commit 359e8721d4b988d6297534164e4976a71c46ec43

View file

@ -1,29 +0,0 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const rl = b.dependency("raylib", .{
.target = target,
.optimize = optimize,
});
const lib = b.addModule("raylib_extended", .{
.root_source_file = b.path("src/raylib_extension.zig"),
});
lib.linkLibrary(rl.artifact("raylib"));
lib.addIncludePath(b.path("raylib/src/external"));
lib.addIncludePath(b.path("raylib/src"));
lib.addIncludePath(b.path("src"));
lib.addCSourceFile(.{
.file = b.path("src/raylib_extension.c"),
.flags = &.{
"-std=gnu99",
"-D_GNU_SOURCE",
"-DGL_SILENCE_DEPRECATION=199309L",
"-fno-sanitize=undefined",
}
});
}

View file

@ -1,19 +0,0 @@
.{
.name = .raylib_extended,
.version = "0.0.0",
.minimum_zig_version = "0.14.0",
.fingerprint = 0x6b1dfd21f0cb5b08,
.dependencies = .{
.raylib = .{ .path = "raylib" },
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
// For example...
//"LICENSE",
//"README.md",
},
}

@ -1 +0,0 @@
Subproject commit 688a81d3334c789493b24617778a334ac786f52e

View file

@ -1,572 +0,0 @@
#include "raylib_extension.h"
// Upload vertex data into a VAO (if supported) and VBO
void UploadChunkMesh(ChunkMesh *mesh, bool dynamic) {
if (mesh->vaoId > 0)
{
// Check if mesh has already been loaded in GPU
printf("CHUNK VAO: [ID %i] Trying to re-load an already loaded mesh\n", mesh->vaoId);
return;
}
mesh->vboId = (unsigned int *)RL_CALLOC(CHUNK_MAX_MESH_VERTEX_BUFFERS, sizeof(unsigned int));
mesh->vaoId = 0; // Vertex Array Object
mesh->vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION] = 0;
mesh->vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD] = 0;
mesh->vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL] = 0;
mesh->vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TILETEXCOORD] = 0;
mesh->vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_METADATA1] = 0;
mesh->vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_AMBIENT_OCCLUSION_SIDES] = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
mesh->vaoId = rlLoadVertexArray();
rlEnableVertexArray(mesh->vaoId);
// NOTE: Vertex attributes must be uploaded considering default locations points and available vertex data
// Enable vertex attributes: position (shader-location = 0)
mesh->vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION] = rlLoadVertexBuffer(mesh->vertices, mesh->vertexCount*3*sizeof(float), dynamic);
rlSetVertexAttribute(CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION, 3, RL_FLOAT, 0, 0, 0);
rlEnableVertexAttribute(CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION);
// Enable vertex attributes: texcoords (shader-location = 1)
mesh->vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD] = rlLoadVertexBuffer(mesh->texcoords, mesh->vertexCount*2*sizeof(float), dynamic);
rlSetVertexAttribute(CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD, 2, RL_FLOAT, 0, 0, 0);
rlEnableVertexAttribute(CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD);
// Enable vertex attributes: normals (shader-location = 2)
mesh->vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL] = rlLoadVertexBuffer(mesh->normals, mesh->vertexCount*3*sizeof(float), dynamic);
rlSetVertexAttribute(CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL, 3, RL_FLOAT, 0, 0, 0);
rlEnableVertexAttribute(CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL);
// Enable vertex attribute: tiletexcoord (shader-location = 3)
mesh->vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TILETEXCOORD] = rlLoadVertexBuffer(mesh->tiletexcoords, mesh->vertexCount*2*sizeof(float), dynamic);
rlSetVertexAttribute(CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TILETEXCOORD, 2, RL_FLOAT, 0, 0, 0);
rlEnableVertexAttribute(CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TILETEXCOORD);
// Enable vertex attribute: metadata1 (shader-location = 4)
mesh->vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_METADATA1] = rlLoadVertexBuffer(mesh->metadata1, mesh->vertexCount*4*sizeof(unsigned int), dynamic);
rlSetVertexAttributeInteger(CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_METADATA1, 4, RL_UNSIGNED_INT, 0, 0);
rlEnableVertexAttribute(CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_METADATA1);
// Enable vertex attribute: ambient_occlusion_sides (shader-location = 5)
mesh->vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_AMBIENT_OCCLUSION_SIDES] = rlLoadVertexBuffer(mesh->ambient_occlusion_sides, mesh->vertexCount*4*sizeof(unsigned int), dynamic);
rlSetVertexAttributeInteger(CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_AMBIENT_OCCLUSION_SIDES, 4, RL_UNSIGNED_INT, 0, 0);
rlEnableVertexAttribute(CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_AMBIENT_OCCLUSION_SIDES);
printf("\nmeow :3c %x\n\n", mesh->ambient_occlusion_sides[292*24]);
if (mesh->vaoId > 0) printf("CHUNK VAO: [ID %i] Mesh uploaded successfully to VRAM (GPU)\n", mesh->vaoId);
else printf("CHUNK VBO: Mesh uploaded successfully to VRAM (GPU)");
rlDisableVertexArray();
#endif
}
// Load model from generated mesh
// WARNING: A shallow copy of mesh is generated, passed by value,
// as long as struct contains pointers to data and some values, we get a copy
// of mesh pointing to same data as original version... be careful!
ChunkModel LoadChunkModelFromMesh(ChunkMesh mesh)
{
ChunkModel model = { 0 };
model.transform = MatrixIdentity();
model.meshCount = 1;
model.meshes = (ChunkMesh *)RL_CALLOC(model.meshCount, sizeof(ChunkMesh));
model.meshes[0] = mesh;
model.materialCount = 1;
model.materials = (Material *)RL_CALLOC(model.materialCount, sizeof(Material));
model.materials[0] = LoadMaterialDefault();
model.meshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int));
model.meshMaterial[0] = 0; // First material index
return model;
}
// Unload model (meshes/materials) from memory (RAM and/or VRAM)
// NOTE: This function takes care of all model elements, for a detailed control
// over them, use UnloadMesh() and UnloadMaterial()
void UnloadChunkModel(ChunkModel model)
{
// Unload meshes
for (int i = 0; i < model.meshCount; i++) UnloadChunkMesh(model.meshes[i]);
// Unload materials maps
// NOTE: As the user could be sharing shaders and textures between models,
// we don't unload the material but just free its maps,
// the user is responsible for freeing models shaders and textures
for (int i = 0; i < model.materialCount; i++) RL_FREE(model.materials[i].maps);
// Unload arrays
RL_FREE(model.meshes);
RL_FREE(model.materials);
RL_FREE(model.meshMaterial);
TRACELOG(LOG_INFO, "CHUNK MODEL: Unloaded model (and meshes) from RAM and VRAM");
}
// Unload mesh from memory (RAM and VRAM)
void UnloadChunkMesh(ChunkMesh mesh)
{
// Unload rlgl mesh vboId data
rlUnloadVertexArray(mesh.vaoId);
if (mesh.vboId != NULL) for (int i = 0; i < CHUNK_MAX_MESH_VERTEX_BUFFERS; i++) rlUnloadVertexBuffer(mesh.vboId[i]);
RL_FREE(mesh.vboId);
RL_FREE(mesh.vertices);
RL_FREE(mesh.texcoords);
RL_FREE(mesh.normals);
RL_FREE(mesh.tiletexcoords);
RL_FREE(mesh.ambient_occlusion_sides);
}
// Draw a model (with texture if set)
void DrawChunkModel(ChunkModel model, Vector3 position, float scale, Color tint)
{
Vector3 vScale = { scale, scale, scale };
Vector3 rotationAxis = { 0.0f, 1.0f, 0.0f };
DrawChunkModelEx(model, position, rotationAxis, 0.0f, vScale, tint);
}
// Draw a model with extended parameters
void DrawChunkModelEx(ChunkModel model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint)
{
// Calculate transformation matrix from function parameters
// Get transform matrix (rotation -> scale -> translation)
Matrix matScale = MatrixScale(scale.x, scale.y, scale.z);
Matrix matRotation = MatrixRotate(rotationAxis, rotationAngle*DEG2RAD);
Matrix matTranslation = MatrixTranslate(position.x, position.y, position.z);
Matrix matTransform = MatrixMultiply(MatrixMultiply(matScale, matRotation), matTranslation);
// Combine model transformation matrix (model.transform) with matrix generated by function parameters (matTransform)
model.transform = MatrixMultiply(model.transform, matTransform);
for (int i = 0; i < model.meshCount; i++)
{
Color color = model.materials[model.meshMaterial[i]].maps[MATERIAL_MAP_DIFFUSE].color;
Color colorTint = WHITE;
colorTint.r = (unsigned char)(((int)color.r*(int)tint.r)/255);
colorTint.g = (unsigned char)(((int)color.g*(int)tint.g)/255);
colorTint.b = (unsigned char)(((int)color.b*(int)tint.b)/255);
colorTint.a = (unsigned char)(((int)color.a*(int)tint.a)/255);
model.materials[model.meshMaterial[i]].maps[MATERIAL_MAP_DIFFUSE].color = colorTint;
DrawChunkMesh(model.meshes[i], model.materials[model.meshMaterial[i]], model.transform);
model.materials[model.meshMaterial[i]].maps[MATERIAL_MAP_DIFFUSE].color = color;
}
}
// Draw a 3d mesh with material and transform
void DrawChunkMesh(ChunkMesh mesh, Material material, Matrix transform)
{
#if defined(GRAPHICS_API_OPENGL_11)
#define GL_VERTEX_ARRAY 0x8074
#define GL_NORMAL_ARRAY 0x8075
#define GL_COLOR_ARRAY 0x8076
#define GL_TEXTURE_COORD_ARRAY 0x8078
rlEnableTexture(material.maps[MATERIAL_MAP_DIFFUSE].texture.id);
rlEnableStatePointer(GL_VERTEX_ARRAY, mesh.vertices);
rlEnableStatePointer(GL_TEXTURE_COORD_ARRAY, mesh.texcoords);
rlEnableStatePointer(GL_NORMAL_ARRAY, mesh.normals);
rlEnableStatePointer(GL_COLOR_ARRAY, mesh.colors);
rlPushMatrix();
rlMultMatrixf(MatrixToFloat(transform));
rlColor4ub(material.maps[MATERIAL_MAP_DIFFUSE].color.r,
material.maps[MATERIAL_MAP_DIFFUSE].color.g,
material.maps[MATERIAL_MAP_DIFFUSE].color.b,
material.maps[MATERIAL_MAP_DIFFUSE].color.a);
if (mesh.indices != NULL) rlDrawVertexArrayElements(0, mesh.triangleCount*3, mesh.indices);
else rlDrawVertexArray(0, mesh.vertexCount);
rlPopMatrix();
rlDisableStatePointer(GL_VERTEX_ARRAY);
rlDisableStatePointer(GL_TEXTURE_COORD_ARRAY);
rlDisableStatePointer(GL_NORMAL_ARRAY);
rlDisableStatePointer(GL_COLOR_ARRAY);
rlDisableTexture();
#endif
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Bind shader program
rlEnableShader(material.shader.id);
// Send required data to shader (matrices, values)
//-----------------------------------------------------
// Upload to shader material.colDiffuse
if (material.shader.locs[CHUNK_SHADER_LOC_COLOR_DIFFUSE] != -1)
{
float values[4] = {
(float)material.maps[MATERIAL_MAP_DIFFUSE].color.r/255.0f,
(float)material.maps[MATERIAL_MAP_DIFFUSE].color.g/255.0f,
(float)material.maps[MATERIAL_MAP_DIFFUSE].color.b/255.0f,
(float)material.maps[MATERIAL_MAP_DIFFUSE].color.a/255.0f
};
rlSetUniform(material.shader.locs[CHUNK_SHADER_LOC_COLOR_DIFFUSE], values, SHADER_UNIFORM_VEC4, 1);
}
// Upload to shader material.colSpecular (if location available)
if (material.shader.locs[CHUNK_SHADER_LOC_COLOR_SPECULAR] != -1)
{
float values[4] = {
(float)material.maps[MATERIAL_MAP_SPECULAR].color.r/255.0f,
(float)material.maps[MATERIAL_MAP_SPECULAR].color.g/255.0f,
(float)material.maps[MATERIAL_MAP_SPECULAR].color.b/255.0f,
(float)material.maps[MATERIAL_MAP_SPECULAR].color.a/255.0f
};
rlSetUniform(material.shader.locs[CHUNK_SHADER_LOC_COLOR_SPECULAR], values, SHADER_UNIFORM_VEC4, 1);
}
// Get a copy of current matrices to work with,
// just in case stereo render is required, and we need to modify them
// NOTE: At this point the modelview matrix just contains the view matrix (camera)
// That's because BeginMode3D() sets it and there is no model-drawing function
// that modifies it, all use rlPushMatrix() and rlPopMatrix()
Matrix matModel = MatrixIdentity();
Matrix matView = rlGetMatrixModelview();
Matrix matModelView = MatrixIdentity();
Matrix matProjection = rlGetMatrixProjection();
// Upload view and projection matrices (if locations available)
if (material.shader.locs[CHUNK_SHADER_LOC_MATRIX_VIEW] != -1) rlSetUniformMatrix(material.shader.locs[CHUNK_SHADER_LOC_MATRIX_VIEW], matView);
if (material.shader.locs[CHUNK_SHADER_LOC_MATRIX_PROJECTION] != -1) rlSetUniformMatrix(material.shader.locs[CHUNK_SHADER_LOC_MATRIX_PROJECTION], matProjection);
// Accumulate several model transformations:
// transform: model transformation provided (includes DrawModel() params combined with model.transform)
// rlGetMatrixTransform(): rlgl internal transform matrix due to push/pop matrix stack
matModel = MatrixMultiply(transform, rlGetMatrixTransform());
// Model transformation matrix is sent to shader uniform location: SHADER_LOC_MATRIX_MODEL
if (material.shader.locs[CHUNK_SHADER_LOC_MATRIX_MODEL] != -1) rlSetUniformMatrix(material.shader.locs[CHUNK_SHADER_LOC_MATRIX_MODEL], matModel);
// Get model-view matrix
matModelView = MatrixMultiply(matModel, matView);
// Upload model normal matrix (if locations available)
if (material.shader.locs[CHUNK_SHADER_LOC_MATRIX_NORMAL] != -1) rlSetUniformMatrix(material.shader.locs[CHUNK_SHADER_LOC_MATRIX_NORMAL], MatrixTranspose(MatrixInvert(matModel)));
//-----------------------------------------------------
// Bind active texture maps (if available)
for (int i = 0; i < CHUNK_MAX_MATERIAL_MAPS; i++)
{
if (material.maps[i].texture.id > 0)
{
// Select current shader texture slot
rlActiveTextureSlot(i);
// Enable texture for active slot
if ((i == MATERIAL_MAP_IRRADIANCE) ||
(i == MATERIAL_MAP_PREFILTER) ||
(i == MATERIAL_MAP_CUBEMAP)) rlEnableTextureCubemap(material.maps[i].texture.id);
else rlEnableTexture(material.maps[i].texture.id);
rlSetUniform(material.shader.locs[CHUNK_SHADER_LOC_MAP_DIFFUSE + i], &i, SHADER_UNIFORM_INT, 1);
}
}
// Try binding vertex array objects (VAO) or use VBOs if not possible
// WARNING: UploadMesh() enables all vertex attributes available in mesh and sets default attribute values
// for shader expected vertex attributes that are not provided by the mesh (i.e. colors)
// This could be a dangerous approach because different meshes with different shaders can enable/disable some attributes
if (!rlEnableVertexArray(mesh.vaoId))
{
// Bind mesh VBO data: vertex position (shader-location = 0)
rlEnableVertexBuffer(mesh.vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION]);
rlSetVertexAttribute(material.shader.locs[CHUNK_SHADER_LOC_VERTEX_POSITION], 3, RL_FLOAT, 0, 0, 0);
rlEnableVertexAttribute(material.shader.locs[CHUNK_SHADER_LOC_VERTEX_POSITION]);
// Bind mesh VBO data: vertex texcoords (shader-location = 1)
rlEnableVertexBuffer(mesh.vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD]);
rlSetVertexAttribute(material.shader.locs[CHUNK_SHADER_LOC_VERTEX_TEXCOORD], 2, RL_FLOAT, 0, 0, 0);
rlEnableVertexAttribute(material.shader.locs[CHUNK_SHADER_LOC_VERTEX_TEXCOORD]);
// Bind mesh VBO data: vertex normal (shader-location = 2)
rlEnableVertexBuffer(mesh.vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL]);
rlSetVertexAttribute(material.shader.locs[CHUNK_SHADER_LOC_VERTEX_NORMAL], 3, RL_FLOAT, 0, 0, 0);
rlEnableVertexAttribute(material.shader.locs[CHUNK_SHADER_LOC_VERTEX_NORMAL]);
// Bind mesh VBO data: vertex tiletexcoords (shader-location = 3)
rlEnableVertexBuffer(mesh.vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TILETEXCOORD]);
rlSetVertexAttribute(material.shader.locs[CHUNK_SHADER_LOC_VERTEX_TILETEXCOORD], 2, RL_FLOAT, 0, 0, 0);
rlEnableVertexAttribute(material.shader.locs[CHUNK_SHADER_LOC_VERTEX_TILETEXCOORD]);
// Bind mesh VBO data: vertex metadata1 (shader-location = 4)
rlEnableVertexBuffer(mesh.vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_METADATA1]);
rlSetVertexAttribute(material.shader.locs[CHUNK_SHADER_LOC_VERTEX_METADATA1], 4, RL_UNSIGNED_INT, 0, 0, 0);
rlEnableVertexAttribute(material.shader.locs[CHUNK_SHADER_LOC_VERTEX_METADATA1]);
// Bind mesh VBO data: vertex ambient occlusion sides (shader-location = 5)
rlEnableVertexBuffer(mesh.vboId[CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_AMBIENT_OCCLUSION_SIDES]);
rlSetVertexAttribute(material.shader.locs[CHUNK_SHADER_LOC_VERTEX_AMBIENT_OCCLUSION_SIDES], 4, RL_UNSIGNED_INT, 0, 0, 0);
rlEnableVertexAttribute(material.shader.locs[CHUNK_SHADER_LOC_VERTEX_AMBIENT_OCCLUSION_SIDES]);
}
int eyeCount = 1;
if (rlIsStereoRenderEnabled()) eyeCount = 2;
for (int eye = 0; eye < eyeCount; eye++)
{
// Calculate model-view-projection matrix (MVP)
Matrix matModelViewProjection = MatrixIdentity();
if (eyeCount == 1) matModelViewProjection = MatrixMultiply(matModelView, matProjection);
else
{
// Setup current eye viewport (half screen width)
rlViewport(eye*rlGetFramebufferWidth()/2, 0, rlGetFramebufferWidth()/2, rlGetFramebufferHeight());
matModelViewProjection = MatrixMultiply(MatrixMultiply(matModelView, rlGetMatrixViewOffsetStereo(eye)), rlGetMatrixProjectionStereo(eye));
}
// Send combined model-view-projection matrix to shader
rlSetUniformMatrix(material.shader.locs[CHUNK_SHADER_LOC_MATRIX_MVP], matModelViewProjection);
// Draw mesh
rlDrawVertexArray(0, mesh.vertexCount);
}
// Unbind all bound texture maps
for (int i = 0; i < CHUNK_MAX_MATERIAL_MAPS; i++)
{
if (material.maps[i].texture.id > 0)
{
// Select current shader texture slot
rlActiveTextureSlot(i);
// Disable texture for active slot
if ((i == MATERIAL_MAP_IRRADIANCE) ||
(i == MATERIAL_MAP_PREFILTER) ||
(i == MATERIAL_MAP_CUBEMAP)) rlDisableTextureCubemap();
else rlDisableTexture();
}
}
// Disable all possible vertex array objects (or VBOs)
rlDisableVertexArray();
rlDisableVertexBuffer();
rlDisableVertexBufferElement();
// Disable shader program
rlDisableShader();
// Restore rlgl internal modelview and projection matrices
rlSetMatrixModelview(matView);
rlSetMatrixProjection(matProjection);
#endif
}
// Load shader from files and bind default locations
// NOTE: If shader string is NULL, using default vertex/fragment shaders
Shader LoadChunkShader(const char *vsFileName, const char *fsFileName)
{
Shader shader = { 0 };
char *vShaderStr = NULL;
char *fShaderStr = NULL;
if (vsFileName != NULL) vShaderStr = LoadFileText(vsFileName);
if (fsFileName != NULL) fShaderStr = LoadFileText(fsFileName);
if ((vShaderStr == NULL) && (fShaderStr == NULL)) printf("CHUNK SHADER: Shader files provided are not valid, using default shader\n");
shader = LoadChunkShaderFromMemory(vShaderStr, fShaderStr);
UnloadFileText(vShaderStr);
UnloadFileText(fShaderStr);
printf("CHUNK SHADER: Shader loaded successfully from shader files\n");
return shader;
}
// Load shader from code strings and bind default locations
Shader LoadChunkShaderFromMemory(const char *vsCode, const char *fsCode)
{
Shader shader = { 0 };
shader.id = chunkLoadShaderCode(vsCode, fsCode);
if (shader.id == rlGetShaderIdDefault()) shader.locs = rlGetShaderLocsDefault();
else if (shader.id > 0)
{
// After custom shader loading, we TRY to set default location names
// Default shader attribute locations have been binded before linking:
// vertex position location = 0
// vertex texcoord location = 1
// vertex normal location = 2
// vertex tiletexcoord location = 3
// vertex metadata1 location = 4
// vertex ambient_occlusion_sides location = 5
// NOTE: If any location is not found, loc point becomes -1
shader.locs = (int *)RL_CALLOC(CHUNK_MAX_SHADER_LOCATIONS, sizeof(int));
// All locations reset to -1 (no location)
for (int i = 0; i < CHUNK_MAX_SHADER_LOCATIONS; i++) shader.locs[i] = -1;
// Get handles to GLSL input attribute locations
shader.locs[CHUNK_SHADER_LOC_VERTEX_POSITION] = rlGetLocationAttrib(shader.id, CHUNK_DEFAULT_SHADER_ATTRIB_NAME_POSITION);
shader.locs[CHUNK_SHADER_LOC_VERTEX_TEXCOORD] = rlGetLocationAttrib(shader.id, CHUNK_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD);
shader.locs[CHUNK_SHADER_LOC_VERTEX_NORMAL] = rlGetLocationAttrib(shader.id, CHUNK_DEFAULT_SHADER_ATTRIB_NAME_NORMAL);
shader.locs[CHUNK_SHADER_LOC_VERTEX_TILETEXCOORD] = rlGetLocationAttrib(shader.id, CHUNK_DEFAULT_SHADER_ATTRIB_NAME_TILETEXCOORD);
shader.locs[CHUNK_SHADER_LOC_VERTEX_METADATA1] = rlGetLocationAttrib(shader.id, CHUNK_DEFAULT_SHADER_ATTRIB_NAME_METADATA1);
shader.locs[CHUNK_SHADER_LOC_VERTEX_AMBIENT_OCCLUSION_SIDES] = rlGetLocationAttrib(shader.id, CHUNK_DEFAULT_SHADER_ATTRIB_NAME_AMBIENT_OCCLUSION_SIDES);
// Get handles to GLSL uniform locations (vertex shader)
shader.locs[CHUNK_SHADER_LOC_MATRIX_MVP] = rlGetLocationUniform(shader.id, CHUNK_DEFAULT_SHADER_UNIFORM_NAME_MVP);
shader.locs[CHUNK_SHADER_LOC_MATRIX_VIEW] = rlGetLocationUniform(shader.id, CHUNK_DEFAULT_SHADER_UNIFORM_NAME_VIEW);
shader.locs[CHUNK_SHADER_LOC_MATRIX_PROJECTION] = rlGetLocationUniform(shader.id, CHUNK_DEFAULT_SHADER_UNIFORM_NAME_PROJECTION);
shader.locs[CHUNK_SHADER_LOC_MATRIX_MODEL] = rlGetLocationUniform(shader.id, CHUNK_DEFAULT_SHADER_UNIFORM_NAME_MODEL);
shader.locs[CHUNK_SHADER_LOC_MATRIX_NORMAL] = rlGetLocationUniform(shader.id, CHUNK_DEFAULT_SHADER_UNIFORM_NAME_NORMAL);
// Get handles to GLSL uniform locations (fragment shader)
shader.locs[CHUNK_SHADER_LOC_COLOR_DIFFUSE] = rlGetLocationUniform(shader.id, CHUNK_DEFAULT_SHADER_UNIFORM_NAME_COLOR);
shader.locs[CHUNK_SHADER_LOC_MAP_DIFFUSE] = rlGetLocationUniform(shader.id, CHUNK_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0); // SHADER_LOC_MAP_ALBEDO
shader.locs[CHUNK_SHADER_LOC_MAP_SPECULAR] = rlGetLocationUniform(shader.id, CHUNK_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE1); // SHADER_LOC_MAP_METALNESS
shader.locs[CHUNK_SHADER_LOC_MAP_NORMAL] = rlGetLocationUniform(shader.id, CHUNK_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE2);
}
return shader;
}
// #ifdef RLGL_IMPLEMENTATION
// Set vertex attribute (integer!!!!!!!!!!!!!!!!!!!!!!!!!!)
void rlSetVertexAttributeInteger(unsigned int index, int compSize, int type, int stride, int offset)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
size_t offsetNative = offset;
glVertexAttribIPointer(index, compSize, type, stride, (void *)offsetNative);
#endif
}
// Load custom shader strings and return program id
unsigned int chunkLoadShaderProgram(unsigned int vShaderId, unsigned int fShaderId)
{
unsigned int program = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
GLint success = 0;
program = glCreateProgram();
glAttachShader(program, vShaderId);
glAttachShader(program, fShaderId);
// NOTE: Default attribute shader locations must be Bound before linking
glBindAttribLocation(program, CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION, CHUNK_DEFAULT_SHADER_ATTRIB_NAME_POSITION);
glBindAttribLocation(program, CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD, CHUNK_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD);
glBindAttribLocation(program, CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL, CHUNK_DEFAULT_SHADER_ATTRIB_NAME_NORMAL);
glBindAttribLocation(program, CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TILETEXCOORD, CHUNK_DEFAULT_SHADER_ATTRIB_NAME_TILETEXCOORD);
glBindAttribLocation(program, CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_METADATA1, CHUNK_DEFAULT_SHADER_ATTRIB_NAME_METADATA1);
glBindAttribLocation(program, CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_AMBIENT_OCCLUSION_SIDES, CHUNK_DEFAULT_SHADER_ATTRIB_NAME_AMBIENT_OCCLUSION_SIDES);
// NOTE: If some attrib name is no found on the shader, it locations becomes -1
glLinkProgram(program);
// NOTE: All uniform variables are intitialised to 0 when a program links
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (success == GL_FALSE)
{
printf("CHUNK SHADER: [ID %i] Failed to link shader program\n", program);
int maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
if (maxLength > 0)
{
int length = 0;
char *log = (char *)RL_CALLOC(maxLength, sizeof(char));
glGetProgramInfoLog(program, maxLength, &length, log);
printf("SHADER: [ID %i] Link error: %s\n", program, log);
RL_FREE(log);
}
glDeleteProgram(program);
program = 0;
}
else
{
// Get the size of compiled shader program (not available on OpenGL ES 2.0)
// NOTE: If GL_LINK_STATUS is GL_FALSE, program binary length is zero
//GLint binarySize = 0;
//glGetProgramiv(id, GL_PROGRAM_BINARY_LENGTH, &binarySize);
printf("CHUNK SHADER: [ID %i] Program shader loaded successfully\n", program);
}
#endif
}
// Load shader from code strings
// NOTE: If shader string is NULL, using default vertex/fragment shaders
unsigned int chunkLoadShaderCode(const char *vsCode, const char *fsCode)
{
unsigned int id = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
unsigned int vertexShaderId = 0;
unsigned int fragmentShaderId = 0;
// Compile vertex shader (if provided)
// NOTE: If not vertex shader is provided, use default one
if (vsCode != NULL) vertexShaderId = rlCompileShader(vsCode, GL_VERTEX_SHADER);
else {
printf("vertex shader is null!\n");
exit(1);
}
// Compile fragment shader (if provided)
// NOTE: If not vertex shader is provided, use default one
if (fsCode != NULL) fragmentShaderId = rlCompileShader(fsCode, GL_FRAGMENT_SHADER);
else {
printf("fragment shader is null!\n");
exit(1);
}
if ((vertexShaderId > 0) && (fragmentShaderId > 0))
{
// One of or both shader are new, we need to compile a new shader program
id = chunkLoadShaderProgram(vertexShaderId, fragmentShaderId);
// WARNING: Shader program linkage could fail and returned id is 0
if (id > 0) glDetachShader(id, vertexShaderId);
glDeleteShader(vertexShaderId);
// WARNING: Shader program linkage could fail and returned id is 0
if (id > 0) glDetachShader(id, fragmentShaderId);
glDeleteShader(fragmentShaderId);
// In case shader program loading failed
if (id == 0)
{
printf("failed to load custom shaders!\n");
exit(1);
}
}
#endif
return id;
}
// #endif // RLGL_IMPLEMENTATION

View file

@ -1,197 +0,0 @@
/**********************************************************************************************
* This file contains modified pieces of code from the Raylib library.
* Original license follows below.
*
* LICENSE: zlib/libpng
*
* Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
**********************************************************************************************/
#include "raylib.h"
#include "rlgl.h"
#include "raymath.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(GRAPHICS_API_OPENGL_11)
#if defined(__APPLE__)
#include <OpenGL/gl.h> // OpenGL 1.1 library for OSX
#include <OpenGL/glext.h> // OpenGL extensions library
#else
// APIENTRY for OpenGL function pointer declarations is required
#if !defined(APIENTRY)
#if defined(_WIN32)
#define APIENTRY __stdcall
#else
#define APIENTRY
#endif
#endif
// WINGDIAPI definition. Some Windows OpenGL headers need it
#if !defined(WINGDIAPI) && defined(_WIN32)
#define WINGDIAPI __declspec(dllimport)
#endif
#include <GL/gl.h> // OpenGL 1.1 library
#endif
#endif
#if defined(GRAPHICS_API_OPENGL_33)
#define GLAD_MALLOC RL_MALLOC
#define GLAD_FREE RL_FREE
#define GLAD_GL_IMPLEMENTATION
#include "glad.h" // GLAD extensions loading library, includes OpenGL headers
#endif
#if defined(GRAPHICS_API_OPENGL_ES3)
#include <GLES3/gl3.h> // OpenGL ES 3.0 library
#define GL_GLEXT_PROTOTYPES
#include <GLES2/gl2ext.h> // OpenGL ES 2.0 extensions library
#elif defined(GRAPHICS_API_OPENGL_ES2)
// NOTE: OpenGL ES 2.0 can be enabled on Desktop platforms,
// in that case, functions are loaded from a custom glad for OpenGL ES 2.0
#if defined(PLATFORM_DESKTOP_GLFW) || defined(PLATFORM_DESKTOP_SDL)
#define GLAD_GLES2_IMPLEMENTATION
#include "glad_gles2.h"
#else
#define GL_GLEXT_PROTOTYPES
//#include <EGL/egl.h> // EGL library -> not required, platform layer
#include <GLES2/gl2.h> // OpenGL ES 2.0 library
#include <GLES2/gl2ext.h> // OpenGL ES 2.0 extensions library
#endif
// It seems OpenGL ES 2.0 instancing entry points are not defined on Raspberry Pi
// provided headers (despite being defined in official Khronos GLES2 headers)
#if defined(PLATFORM_DRM)
typedef void (GL_APIENTRYP PFNGLDRAWARRAYSINSTANCEDEXTPROC) (GLenum mode, GLint start, GLsizei count, GLsizei primcount);
typedef void (GL_APIENTRYP PFNGLDRAWELEMENTSINSTANCEDEXTPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei primcount);
typedef void (GL_APIENTRYP PFNGLVERTEXATTRIBDIVISOREXTPROC) (GLuint index, GLuint divisor);
#endif
#endif
#define CHUNK_MAX_MESH_VERTEX_BUFFERS 6
#define CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION 0
#define CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD 1
#define CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL 2
#define CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TILETEXCOORD 3
#define CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_METADATA1 4
#define CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_AMBIENT_OCCLUSION_SIDES 5
#define CHUNK_MAX_MATERIAL_MAPS 12
#define CHUNK_MAX_SHADER_LOCATIONS 32
#define RL_UNSIGNED_INT 0x1405
typedef enum {
CHUNK_SHADER_LOC_VERTEX_POSITION = 0, // Shader location: vertex attribute: position
CHUNK_SHADER_LOC_VERTEX_TEXCOORD, // Shader location: vertex attribute: texcoord
CHUNK_SHADER_LOC_VERTEX_NORMAL, // Shader location: vertex attribute: normal
CHUNK_SHADER_LOC_VERTEX_TILETEXCOORD, // Shader location: vertex attribute: tiletexcoord
CHUNK_SHADER_LOC_VERTEX_METADATA1, // Shader location: vertex attribute: metadata1
CHUNK_SHADER_LOC_VERTEX_AMBIENT_OCCLUSION_SIDES, // Shader location: vertex attribute: ambient_occlusion_sides
CHUNK_SHADER_LOC_MATRIX_MVP, // Shader location: matrix uniform: model-view-projection
CHUNK_SHADER_LOC_MATRIX_VIEW, // Shader location: matrix uniform: view (camera transform)
CHUNK_SHADER_LOC_MATRIX_PROJECTION, // Shader location: matrix uniform: projection
CHUNK_SHADER_LOC_MATRIX_MODEL, // Shader location: matrix uniform: model (transform)
CHUNK_SHADER_LOC_MATRIX_NORMAL, // Shader location: matrix uniform: normal
CHUNK_SHADER_LOC_VECTOR_VIEW, // Shader location: vector uniform: view
CHUNK_SHADER_LOC_COLOR_DIFFUSE, // Shader location: vector uniform: diffuse color
CHUNK_SHADER_LOC_COLOR_SPECULAR, // Shader location: vector uniform: specular color
CHUNK_SHADER_LOC_COLOR_AMBIENT, // Shader location: vector uniform: ambient color
CHUNK_SHADER_LOC_MAP_ALBEDO, // Shader location: sampler2d texture: albedo (same as: CHUNK_SHADER_LOC_MAP_DIFFUSE)
CHUNK_SHADER_LOC_MAP_METALNESS, // Shader location: sampler2d texture: metalness (same as: CHUNK_SHADER_LOC_MAP_SPECULAR)
CHUNK_SHADER_LOC_MAP_NORMAL, // Shader location: sampler2d texture: normal
CHUNK_SHADER_LOC_MAP_ROUGHNESS, // Shader location: sampler2d texture: roughness
CHUNK_SHADER_LOC_MAP_OCCLUSION, // Shader location: sampler2d texture: occlusion
CHUNK_SHADER_LOC_MAP_EMISSION, // Shader location: sampler2d texture: emission
CHUNK_SHADER_LOC_MAP_HEIGHT, // Shader location: sampler2d texture: height
CHUNK_SHADER_LOC_MAP_CUBEMAP, // Shader location: samplerCube texture: cubemap
CHUNK_SHADER_LOC_MAP_IRRADIANCE, // Shader location: samplerCube texture: irradiance
CHUNK_SHADER_LOC_MAP_PREFILTER, // Shader location: samplerCube texture: prefilter
CHUNK_SHADER_LOC_MAP_BRDF // Shader location: sampler2d texture: brdf
} chunkShaderLocationIndex;
#define CHUNK_SHADER_LOC_MAP_DIFFUSE CHUNK_SHADER_LOC_MAP_ALBEDO
#define CHUNK_SHADER_LOC_MAP_SPECULAR CHUNK_SHADER_LOC_MAP_METALNESS
#define CHUNK_DEFAULT_SHADER_ATTRIB_NAME_POSITION "vertexPosition" // Bound by default to shader location: CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION
#define CHUNK_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD "vertexTexCoord" // Bound by default to shader location: CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD
#define CHUNK_DEFAULT_SHADER_ATTRIB_NAME_NORMAL "vertexNormal" // Bound by default to shader location: CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL
#define CHUNK_DEFAULT_SHADER_ATTRIB_NAME_TILETEXCOORD "vertexTileTexCoord" // Bound by default to shader location: CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_TILETEXCOORD
#define CHUNK_DEFAULT_SHADER_ATTRIB_NAME_METADATA1 "vertexMetadata1" // Bound by default to shader location: CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_METADATA1
#define CHUNK_DEFAULT_SHADER_ATTRIB_NAME_AMBIENT_OCCLUSION_SIDES "vertexAmbientOcclusionSides" // Bound by default to shader location: CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_AMBIENT_OCCLUSION_SIDES
#define CHUNK_DEFAULT_SHADER_UNIFORM_NAME_MVP "mvp" // model-view-projection matrix
#define CHUNK_DEFAULT_SHADER_UNIFORM_NAME_VIEW "matView" // view matrix
#define CHUNK_DEFAULT_SHADER_UNIFORM_NAME_PROJECTION "matProjection" // projection matrix
#define CHUNK_DEFAULT_SHADER_UNIFORM_NAME_MODEL "matModel" // model matrix
#define CHUNK_DEFAULT_SHADER_UNIFORM_NAME_NORMAL "matNormal" // normal matrix (transpose(inverse(matModelView)))
#define CHUNK_DEFAULT_SHADER_UNIFORM_NAME_COLOR "colDiffuse" // color diffuse (base tint color, multiplied by texture color)
#define CHUNK_DEFAULT_SHADER_UNIFORM_NAME_BONE_MATRICES "boneMatrices" // bone matrices
#define CHUNK_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0 "texture0" // texture0 (texture slot active 0)
#define CHUNK_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE1 "texture1" // texture1 (texture slot active 1)
#define CHUNK_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE2 "texture2" // texture2 (texture slot active 2)
typedef struct ChunkMesh {
int vertexCount; // Number of vertices stored in arrays
int triangleCount; // Number of triangles stored (indexed or not)
// Vertex attributes data
float *vertices; // Vertex position (XYZ - 3 components per vertex) (shader-location = 0)
float *texcoords; // Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1)
float *normals; // Vertex normals (XYZ - 3 components per vertex) (shader-location = 2)
float *tiletexcoords; // Vertex tile tex coordinates (UV - 2 components per vertex) (shader-location = 3)
unsigned int *metadata1; // Vertex metadata 1 (ABCD - 4 components per vertex) (shader-location = 4)
unsigned int *ambient_occlusion_sides; // Vertex ambient occlusion sides (ABCD - 4 components per vertex) (shader-location = 5)
// OpenGL identifiers
unsigned int vaoId; // OpenGL Vertex Array Object id
unsigned int *vboId; // OpenGL Vertex Buffer Objects id (default vertex data)
} ChunkMesh;
// Model, meshes, materials and animation data
typedef struct ChunkModel {
Matrix transform; // Local transform matrix
int meshCount; // Number of meshes
int materialCount; // Number of materials
ChunkMesh *meshes; // Meshes array
Material *materials; // Materials array
int *meshMaterial; // Mesh material number
// Animation data
int boneCount; // Number of bones
BoneInfo *bones; // Bones information (skeleton)
Transform *bindPose; // Bones base transformation (pose)
} ChunkModel;
RLAPI void UploadChunkMesh(ChunkMesh *mesh, bool dynamic); // Upload mesh vertex data in GPU and provide VAO/VBO ids
RLAPI ChunkModel LoadChunkModelFromMesh(ChunkMesh mesh); // Load model from generated mesh (default material)
RLAPI void UnloadChunkModel(ChunkModel model); // Unload model (including meshes) from memory (RAM and/or VRAM)
RLAPI void UnloadChunkMesh(ChunkMesh mesh); // Unload mesh data from CPU and GPU
RLAPI void DrawChunkModel(ChunkModel model, Vector3 position, float scale, Color tint); // Draw a model (with texture if set)
RLAPI void DrawChunkModelEx(ChunkModel model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint); // Draw a model with extended parameters
RLAPI void DrawChunkMesh(ChunkMesh mesh, Material material, Matrix transform); // Draw a 3d mesh with material and transform
RLAPI Shader LoadChunkShader(const char *vsFileName, const char *fsFileName); // Load shader from files and bind default locations
RLAPI Shader LoadChunkShaderFromMemory(const char *vsCode, const char *fsCode); // Load shader from code strings and bind default locations
RLAPI unsigned int chunkLoadShaderCode(const char *vsCode, const char *fsCode); // Load shader from code strings
RLAPI unsigned int chunkLoadShaderProgram(unsigned int vShaderId, unsigned int fShaderId); // Load custom shader program
RLAPI void rlSetVertexAttributeInteger(unsigned int index, int compSize, int type, int stride, int offset);

View file

@ -1,39 +0,0 @@
pub const raylib = @cImport({
@cInclude("raylib_extension.h");
});
pub const v3 = struct {
pub inline fn new(x: f32, y: f32, z: f32) raylib.Vector3 {
return raylib.Vector3{ .x = x, .y = y, .z = z };
}
pub inline fn newShifted(x: f32, y: f32, z: f32, comptime d: comptime_int) raylib.Vector3 {
if (d % 3 == 0) {
return new(x, y, z);
} else if (d % 3 == 1) {
return new(y, z, x);
} else if (d % 3 == 2) {
return new(z, x, y);
}
}
pub inline fn add(a: raylib.Vector3, b: raylib.Vector3) raylib.Vector3 {
return raylib.Vector3Add(a, b);
}
pub inline fn sub(a: raylib.Vector3, b: raylib.Vector3) raylib.Vector3 {
return raylib.Vector3Subtract(a, b);
}
pub inline fn neg(a: raylib.Vector3) raylib.Vector3 {
return raylib.Vector3Negate(a);
}
pub inline fn scl(a: raylib.Vector3, b: f32) raylib.Vector3 {
return raylib.Vector3Scale(a, b);
}
pub inline fn nor(a: raylib.Vector3) raylib.Vector3 {
return raylib.Vector3Normalize(a);
}
pub inline fn cross(a: raylib.Vector3, b: raylib.Vector3) raylib.Vector3 {
return raylib.Vector3CrossProduct(a, b);
}
pub inline fn rotate(vec: raylib.Vector3, axis: raylib.Vector3, angle: f32) raylib.Vector3 {
return raylib.Vector3RotateByAxisAngle(vec, axis, angle);
}
};

File diff suppressed because it is too large Load diff

View file

@ -1,36 +1,32 @@
const std = @import("std");
const raylib_helper = @import("raylib_extended");
const raylib = raylib_helper.raylib;
const v3 = raylib_helper.v3;
const znoise = @import("znoise");
const glfw = @import("zglfw");
const freetype = @import("mach_freetype");
const chunks = @import("world/chunk.zig");
const camera = @import("math/camera.zig");
const TILE_TEXTURE_RESOLUTION = 16;
const benchmark_chunk_meshing = false;
const debug = true;
pub fn drawCameraPosition(camera: raylib.Camera3D, x: i32, y: i32) !void {
pub fn drawCameraPosition(cam: camera.Camera, x: i32, y: i32) !void {
var buf: [256:0]u8 = undefined;
const slice = try std.fmt.bufPrintZ(
&buf,
"position: {d:.2} {d:.2} {d:.2}",
.{ camera.position.x, camera.position.y, camera.position.z },
.{ cam.position.x, cam.position.y, cam.position.z },
);
raylib.DrawText(slice, x, y, 20, raylib.YELLOW);
}
pub fn moveCamera(camera: *raylib.Camera3D, vec: raylib.Vector3) void {
camera.position = v3.add(camera.position, vec);
camera.target = v3.add(camera.target, vec);
}
pub fn main() !void {
if (!debug) raylib.SetTraceLogLevel(raylib.LOG_ERROR);
try glfw.init();
defer glfw.terminate();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const a7r = gpa.allocator();
@ -39,15 +35,12 @@ pub fn main() !void {
if (status == .leak) std.debug.print("MEMORY LEAK DETECTED!!!!!!!!!!!!!!!!!!!!!!\n", .{}) else std.debug.print("no leaks detected.\n", .{});
}
raylib.SetConfigFlags(raylib.FLAG_WINDOW_RESIZABLE | raylib.FLAG_FULLSCREEN_MODE);
const window = try glfw.createWindow(glfw.GetMonitorWidth(), glfw.GetMonitorHeight(), "voxel test", glfw.GetCurrentMonitor());
defer glfw.destroyWindow(window);
const display = raylib.GetCurrentMonitor();
raylib.InitWindow(raylib.GetMonitorWidth(display), raylib.GetMonitorHeight(display), "voxel test");
defer raylib.CloseWindow();
raylib.DisableCursor();
raylib.SetWindowState(raylib.FLAG_VSYNC_HINT);
raylib.SetWindowState(raylib.FLAG_FULLSCREEN_MODE);
glfw.SetInputMode(window, glfw.GLFW_CURSOR, glfw.GLFW_CURSOR_DISABLED);
glfw.makeContextCurrent();
// glfw.SwapInterval(0); // turn off vsync
var camera = raylib.Camera3D{
.position = raylib.Vector3{ .x = 0, .y = 8, .z = 20 },

13
src/math/camera.zig Normal file
View file

@ -0,0 +1,13 @@
const zmath = @import("zmath");
const Camera = struct {
position: @Vector(3, f32) = .{0, 0, 0},
up: @Vector(3, f32) = .{0, 1, 0},
forward: @Vector(3, f32) = .{0, 0, 1},
fov_y: f32 = 45,
fn lookAt(self: Camera) zmath.Mat{
return zmath.lookAtLh(self.position, self.position+self.forward, self.up);
}
};

9
src/math/math_util.zig Normal file
View file

@ -0,0 +1,9 @@
const zmath = @import("zmath");
pub const Vec3 = @Vector(3, f32);
pub const Vec4 = @Vector(4, f32);
pub const Mat4 = zmath.Mat;
pub fn extendVector(a: Vec3, b: f32) Vec4 {
return .{a[0], a[1], a[2], b};
}

1
zglfw Submodule

@ -0,0 +1 @@
Subproject commit c337cb3d3f984468ea7a386335937a5d555fc024

1
zmath Submodule

@ -0,0 +1 @@
Subproject commit 5128dc8a8068aab398f844c1f5fddc8132310146