This commit is contained in:
catangent 2025-06-16 23:32:09 +01:00
parent 1cc01e7cf9
commit 6b25507808
14 changed files with 390 additions and 231 deletions

6
.gitmodules vendored
View file

@ -1,6 +1,6 @@
[submodule "raylib"]
path = raylib
url = https://github.com/raysan5/raylib
[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

View file

@ -4,13 +4,10 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const rl = raylib: {
const rl = b.dependency("raylib", .{
.target = target,
.optimize = optimize,
});
break :raylib rl;
};
const rl_ext = b.dependency("raylib_extended", .{
.target = target,
.optimize = optimize,
});
const znoise = b.dependency("znoise", .{
.target = target,
.optimize = optimize,
@ -23,9 +20,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
exe.linkLibrary(rl.artifact("raylib"));
exe.root_module.addImport("raylib_extended", rl_ext.module("raylib_extended"));
exe.root_module.addImport("znoise", znoise.module("root"));
exe.root_module.addIncludePath(b.path("src/c"));
exe.linkLibrary(znoise.artifact("FastNoiseLite"));
b.installArtifact(exe);
@ -45,10 +41,9 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
exe_unit_tests.linkLibrary(rl.artifact("raylib"));
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.addIncludePath(b.path("src/c"));
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);

View file

@ -6,7 +6,7 @@
.dependencies = .{
.znoise = .{ .path = "znoise" },
.raylib = .{ .path = "raylib" },
.raylib_extended = .{ .path = "raylib-extended" },
},
.paths = .{

1
raylib

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

29
raylib-extended/build.zig Normal file
View file

@ -0,0 +1,29 @@
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

@ -0,0 +1,19 @@
.{
.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",
},
}

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

View file

@ -1,136 +1,4 @@
/**********************************************************************************************
* 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>
#define CHUNK_MAX_MESH_VERTEX_BUFFERS 5
#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_MAX_MATERIAL_MAPS 12
#define CHUNK_MAX_SHADER_LOCATIONS 32
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_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_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)
float *metadata1; // Vertex metadata 1 (ABCD - 4 components per vertex) (shader-location = 4)
// 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
#include "raylib_extension.h"
// Upload vertex data into a VAO (if supported) and VBO
void UploadChunkMesh(ChunkMesh *mesh, bool dynamic) {
@ -149,6 +17,7 @@ void UploadChunkMesh(ChunkMesh *mesh, bool dynamic) {
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();
@ -177,10 +46,17 @@ void UploadChunkMesh(ChunkMesh *mesh, bool dynamic) {
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(float), dynamic);
rlSetVertexAttribute(CHUNK_DEFAULT_SHADER_ATTRIB_LOCATION_METADATA1, 4, RL_FLOAT, 0, 0, 0);
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)");
@ -249,6 +125,7 @@ void UnloadChunkMesh(ChunkMesh mesh)
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)
@ -432,8 +309,13 @@ void DrawChunkMesh(ChunkMesh mesh, Material material, Matrix transform)
// 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_FLOAT, 0, 0, 0);
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;
@ -516,18 +398,19 @@ Shader LoadChunkShaderFromMemory(const char *vsCode, const char *fsCode)
{
Shader shader = { 0 };
shader.id = rlLoadShaderCode(vsCode, fsCode);
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 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
@ -542,6 +425,7 @@ Shader LoadChunkShaderFromMemory(const char *vsCode, const char *fsCode)
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);
@ -560,7 +444,19 @@ Shader LoadChunkShaderFromMemory(const char *vsCode, const char *fsCode)
return shader;
}
#ifdef RLGL_IMPLEMENTATION
// #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)
@ -580,6 +476,7 @@ unsigned int chunkLoadShaderProgram(unsigned int vShaderId, unsigned int fShader
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
@ -591,7 +488,7 @@ unsigned int chunkLoadShaderProgram(unsigned int vShaderId, unsigned int fShader
if (success == GL_FALSE)
{
printf("SHADER: [ID %i] Failed to link shader program\n", program);
printf("CHUNK SHADER: [ID %i] Failed to link shader program\n", program);
int maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
@ -616,7 +513,7 @@ unsigned int chunkLoadShaderProgram(unsigned int vShaderId, unsigned int fShader
//GLint binarySize = 0;
//glGetProgramiv(id, GL_PROGRAM_BINARY_LENGTH, &binarySize);
printf(RL_LOG_INFO, "SHADER: [ID %i] Program shader loaded successfully\n", program);
printf("CHUNK SHADER: [ID %i] Program shader loaded successfully\n", program);
}
#endif
}
@ -672,4 +569,4 @@ unsigned int chunkLoadShaderCode(const char *vsCode, const char *fsCode)
return id;
}
#endif // RLGL_IMPLEMENTATION
// #endif // RLGL_IMPLEMENTATION

View file

@ -0,0 +1,197 @@
/**********************************************************************************************
* 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

@ -6,11 +6,12 @@ uniform vec2 textureTiling;
in vec2 fragTexCoord;
in vec3 fragNormal;
in vec2 fragTileTexCoord;
flat in int ambientOcclusionSide1;
flat in int ambientOcclusionSide2;
flat in int ambientOcclusionCorner1;
flat in int ambientOcclusionCorner2;
flat in int ambientOcclusionCorner3;
flat in uvec4 ambientOcclusionSides;
flat in int topLeftObscured;
flat in int topRightObscured;
flat in int bottomLeftObscured;
flat in int bottomRightObscured;
flat in int quadHeight;
flat in int quadWidth;
@ -32,12 +33,23 @@ void main()
ivec2 floorFragTileTexCoord = ivec2(fragTileTexCoord);
if(fragTileTexCoord.x < 1 && (((ambientOcclusionSide1 >> floorFragTileTexCoord.y) & 1) == 1)) outColor *= 0.5 + fakeArcsin(fragTileTexCoord.x);
//if(fragTileTexCoord.x < 1 && (int(ambientOcclusionSides.x >> floorFragTileTexCoord.x) & 1) == 1) {
//if(fragTileTexCoord.x < 1 && (ambientOcclusionSides.x & uint(0x20000000)) > uint(0)) {
//if(fragTileTexCoord.x < 1 && (ambientOcclusionSides.x & uint(0x40000000)) > uint(0)) {
//if(fragTileTexCoord.x < 1 && (ambientOcclusionSides.x & uint(0x10000000)) > uint(0)) {
//if ((fragTileTexCoord.x < 1) && (ambientOcclusionSides.x > uint(0))) {
// if(fragTileTexCoord.x < 0.125 && fragTileTexCoord.x < fract(fragTileTexCoord.y) && fragTileTexCoord.x + fract(fragTileTexCoord.y) < 1.0 && ((ambientOcclusionSide1 >> floorFragTileTexCoord.y) & 1) == 1) outColor *= 0.5;
//if((fragTileTexCoord.x < 0.25 || fragTileTexCoord.x > quadWidth-0.25) && (((ambientOcclusionSide1 >> floorFragTileTexCoord.y) & 1) == 1)) outColor *= 0.5;
//if((fragTileTexCoord.y < 0.25 || fragTileTexCoord.y > quadHeight-0.25) && (((ambientOcclusionSide2 >> floorFragTileTexCoord.y) & 1) == 1)) outColor *= 0.5;
if(fragTileTexCoord.x < 1 && ((ambientOcclusionSides.x >> floorFragTileTexCoord.x) & uint(1)) == uint(1)) {
outColor *= 0.5 + fakeArcsin(fragTileTexCoord.x);
}
outColor.a = 1;
outColor.r = ambientOcclusionSides.x > uint(0) ? 1.0 : 0.0;
//outColor.g = ((ambientOcclusionSides.x & uint(0x01000000)) > uint(0)) ? 1.0 : 0.0;
uint bit = uint(fragTileTexCoord * 32);
outColor.g = (((ambientOcclusionSides.x >> bit) & uint(1)) == uint(1)) ?
((bit % uint(2) == uint(0)) ? 1.0 : 0.8):
((bit % uint(2) == uint(0)) ? 0.0 : 0.2);
}

View file

@ -4,17 +4,18 @@ in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
in vec2 vertexTileTexCoord;
in vec4 vertexMetadata1;
in uvec4 vertexMetadata1;
in uvec4 vertexAmbientOcclusionSides;
out vec2 fragTexCoord;
out vec3 fragNormal;
out vec2 fragTileTexCoord;
flat out int ambientOcclusionSide1;
flat out int ambientOcclusionSide2;
flat out int ambientOcclusionCorner1;
flat out int ambientOcclusionCorner2;
flat out int ambientOcclusionCorner3;
flat out uvec4 ambientOcclusionSides;
flat out int topLeftObscured;
flat out int topRightObscured;
flat out int bottomLeftObscured;
flat out int bottomRightObscured;
flat out int quadHeight;
flat out int quadWidth;
@ -24,16 +25,15 @@ void main() {
fragTexCoord = vertexTexCoord;
fragTileTexCoord = vertexTileTexCoord;
fragNormal = vertexNormal;
ambientOcclusionSides = vertexAmbientOcclusionSides;
gl_Position = mvp*vec4(vertexPosition, 1.0);
// metadata 1 parsing
ambientOcclusionSide1 = floatBitsToInt(vertexMetadata1.x);
ambientOcclusionSide2 = floatBitsToInt(vertexMetadata1.y);
int metadata1Z = floatBitsToInt(vertexMetadata1.z);
int metadata1W = floatBitsToInt(vertexMetadata1.w);
ambientOcclusionCorner1 = (metadata1Z & 0x1) >> 0; // Take 0th bit.
ambientOcclusionCorner2 = (metadata1Z & 0x2) >> 1; // Take 1st bit.
ambientOcclusionCorner3 = (metadata1Z & 0x4) >> 2; // Take 2nd bit.
quadHeight = (metadata1Z & 0x1f8) >> 3; // Take 3rd-8th bits.
quadWidth = (metadata1Z & 0x7e00) >> 9; // Take 9th-14th bits.
int metadata1W = int(vertexMetadata1.w);
topLeftObscured = (metadata1W & 0x1); // Take 0th bit.
topRightObscured = (metadata1W & 0x2) >> 1; // Take 1st bits.
bottomLeftObscured = (metadata1W & 0x4) >> 2; // Take 2nd bits.
bottomRightObscured = (metadata1W & 0x8) >> 3; // Take 3rd bits.
quadHeight = (metadata1W & 0x3f0) >> 4; // Take 4rd-9th bits.
quadWidth = (metadata1W & 0xfc00) >> 10; // Take 10th-16th bits.
}

View file

@ -1,6 +1,6 @@
const std = @import("std");
const raylib_helper = @import("lib_helpers/raylib_helper.zig");
const raylib_helper = @import("raylib_extended");
const raylib = raylib_helper.raylib;
const v3 = raylib_helper.v3;
const znoise = @import("znoise");
@ -66,15 +66,16 @@ pub fn main() !void {
const shader = raylib.LoadChunkShader("resources/shaders/tiling.vs", "resources/shaders/tiling.fs");
defer raylib.UnloadShader(shader);
//std.debug.print("shader id: {}\n", .{shader.id});
//std.debug.print("shader attrib position loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexPosition")});
//std.debug.print("shader attrib texcoord loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexTexCoord")});
//std.debug.print("shader attrib normal loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexNormal")});
//std.debug.print("shader attrib tiletexcoord loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexTileTexCoord")});
//std.debug.print("shader attrib metadata1 loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexMetadata1")});
//for (0..32) |i|{
// std.debug.print("shader loc {}: {}\n", .{i, shader.locs[i]});
//}
std.debug.print("shader id: {}\n", .{shader.id});
std.debug.print("shader attrib position loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexPosition")});
std.debug.print("shader attrib texcoord loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexTexCoord")});
std.debug.print("shader attrib normal loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexNormal")});
std.debug.print("shader attrib tiletexcoord loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexTileTexCoord")});
std.debug.print("shader attrib metadata1 loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexMetadata1")});
std.debug.print("shader attrib ambientOcclusionSides loc: {}\n", .{raylib.GetShaderLocationAttrib(shader, "vertexAmbientOcclusionSides")});
// for (0..32) |i|{
// std.debug.print("shader loc {}: {}\n", .{i, shader.locs[i]});
// }
raylib.SetShaderValue(shader, raylib.GetShaderLocation(shader, "textureTiling"), &.{ @as(f32, @floatFromInt(tile_columns)), @as(f32, @floatFromInt(tile_rows)) }, raylib.SHADER_UNIFORM_VEC2);
var chunk = try chunks.Chunk.init(a7r);

View file

@ -1,11 +1,13 @@
const std = @import("std");
const raylib_helper = @import("../lib_helpers/raylib_helper.zig");
const raylib_helper = @import("raylib_extended");
const raylib = raylib_helper.raylib;
const v3 = raylib_helper.v3;
const A7r = std.mem.Allocator;
const comptimePrint = std.fmt.comptimePrint;
const VERTICES_BLOCK_SIZE = 2 * 3 * 3;
const TEXCOORDS_BLOCK_SIZE = 2 * 2 * 3;
const X_DIRECTION = 0;
const Y_DIRECTION = 1;
const Z_DIRECTION = 2;
@ -32,15 +34,16 @@ const RawQuad = struct {
// Quad shader metadata. Has to be 128 bytes in size.
const Metadata1 = packed struct {
ambient_occlusion_1: u32,
ambient_occlusion_2: u32,
ambient_occlusion_corner1: bool,
ambient_occlusion_corner2: bool,
ambient_occlusion_corner3: bool,
top_left_obscured: bool,
top_right_obscured: bool,
bottom_right_obscured: bool,
bottom_left_obscured: bool,
quad_height: u6,
quad_width: u6,
unused: u17 = 0,
unused: u16 = 0,
unused_2: u32 = 0,
unused_3: u32 = 0,
unused_4: u32 = 0,
};
comptime {
@ -93,7 +96,7 @@ pub const Chunk = struct {
}
// Create a raw quad with specified parameters and surface, accounting for dimension and sign. Surface is the block ID.
fn pack_raw_quad(
fn packRawQuad(
x: f32,
y_start: usize, y_end: usize,
z_start: usize, z_end: usize,
@ -113,6 +116,18 @@ pub const Chunk = struct {
const yright: f32 = if (sign == 1) ymax else ymin;
const zleft: f32 = if (sign == 1) zmin else zmax;
const zright: f32 = if (sign == 1) zmax else zmin;
_ = y_minus_obscuring_pattern;
_ = y_plus_obscuring_pattern;
_ = z_plus_obscuring_pattern;
if(dimention == 0 and z_minus_obscuring_pattern == 0b10000){
std.debug.print("z_minus_obscuring_pattern {b}\n", .{z_minus_obscuring_pattern});
std.debug.print("dimension {}\n", .{dimention});
std.debug.print("x {}\n", .{x});
std.debug.print("y {}-{}\n", .{y_start, y_end});
std.debug.print("z {}-{}\n", .{z_start, z_end});
}
var raw_quad: RawQuad = undefined;
switch (dimention) {
X_DIRECTION => {
@ -126,10 +141,10 @@ pub const Chunk = struct {
.width = zmax - zmin,
.height = ymax - ymin,
.top_obscuring_pattern = z_plus_obscuring_pattern,
.left_obscuring_pattern = z_minus_obscuring_pattern,
.right_obscuring_pattern = y_plus_obscuring_pattern,
.bottom_obscuring_pattern = y_minus_obscuring_pattern,
.top_obscuring_pattern = 0, //z_plus_obscuring_pattern,
.left_obscuring_pattern = if(z_minus_obscuring_pattern == 0b10000) 0x80 else 0,
.right_obscuring_pattern = 0, //y_plus_obscuring_pattern,
.bottom_obscuring_pattern = 0, //y_minus_obscuring_pattern,
.top_left_obscured = false,
.top_right_obscured = false,
.bottom_right_obscured = false,
@ -257,7 +272,7 @@ pub const Chunk = struct {
}
}
// std.debug.print("{}\n", .{z_minus_obscuring_pattern});
const raw_quad = pack_raw_quad(@floatFromInt(raw_x), y_start, y_end, z_start, z_end, sign, dimention, surface, y_minus_obscuring_pattern, y_plus_obscuring_pattern, z_minus_obscuring_pattern, z_plus_obscuring_pattern);
const raw_quad = packRawQuad(@floatFromInt(raw_x), y_start, y_end, z_start, z_end, sign, dimention, surface, y_minus_obscuring_pattern, y_plus_obscuring_pattern, z_minus_obscuring_pattern, z_plus_obscuring_pattern);
try raw_quads.append(raw_quad);
};
}
@ -273,7 +288,8 @@ pub const Chunk = struct {
const texcoords: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 2)));
const tiletexcoords: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 2)));
const normals: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 3)));
const metadata1_packed: [*]f32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 4)));
const metadata1_packed: [*]u32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 4)));
const ambient_occlusion_sides: [*]u32 = @ptrCast(@alignCast(raylib.MemAlloc(arr_size * 4)));
for (raw_quads.items, 0..) |raw_quad, i| {
if (raw_quad.tile <= 0) continue; // air tile, no texture
@ -310,13 +326,12 @@ pub const Chunk = struct {
}
// Store metadata into OpenGL buffers.
for (0..3) |j| {
for (0..6) |j| {
const metadata1 = Metadata1{
.ambient_occlusion_1 = raw_quad.left_obscuring_pattern,
.ambient_occlusion_2 = raw_quad.right_obscuring_pattern,
.ambient_occlusion_corner1 = raw_quad.top_left_obscured,
.ambient_occlusion_corner2 = raw_quad.bottom_left_obscured,
.ambient_occlusion_corner3 = raw_quad.top_right_obscured,
.top_left_obscured = raw_quad.top_left_obscured,
.top_right_obscured = raw_quad.top_right_obscured,
.bottom_left_obscured = raw_quad.bottom_left_obscured,
.bottom_right_obscured = raw_quad.bottom_right_obscured,
.quad_height = @intFromFloat(raw_quad.height),
.quad_width = @intFromFloat(raw_quad.width),
};
@ -325,20 +340,13 @@ pub const Chunk = struct {
metadata1_packed[24 * i + 4 * j + k] = @bitCast(@as(f32, metadata1_baked[k]));
}
}
for (3..6) |j| {
const metadata1 = Metadata1{
.ambient_occlusion_1 = raw_quad.right_obscuring_pattern,
.ambient_occlusion_2 = raw_quad.bottom_obscuring_pattern,
.ambient_occlusion_corner1 = raw_quad.bottom_right_obscured,
.ambient_occlusion_corner2 = raw_quad.top_right_obscured,
.ambient_occlusion_corner3 = raw_quad.bottom_left_obscured,
.quad_height = @intFromFloat(raw_quad.height),
.quad_width = @intFromFloat(raw_quad.width),
};
const metadata1_baked: [4]f32 = @bitCast(metadata1);
for (0..4) |k| {
metadata1_packed[24 * i + 4 * j + k] = @bitCast(@as(f32, metadata1_baked[k]));
}
// Store ambient occlusion sides into OpenGL buffers.
for (0..6) |j| {
ambient_occlusion_sides[24 * i + 4 * j + 0] = raw_quad.left_obscuring_pattern;
ambient_occlusion_sides[24 * i + 4 * j + 1] = raw_quad.right_obscuring_pattern;
ambient_occlusion_sides[24 * i + 4 * j + 2] = raw_quad.top_obscuring_pattern;
ambient_occlusion_sides[24 * i + 4 * j + 3] = raw_quad.bottom_obscuring_pattern;
}
}
@ -352,6 +360,7 @@ pub const Chunk = struct {
.tiletexcoords = tiletexcoords,
.normals = normals,
.metadata1 = metadata1_packed,
.ambient_occlusion_sides = ambient_occlusion_sides,
.vaoId = 0,
.vboId = null,