From bc8589499277758cdbed90bae80e720cba3d9146 Mon Sep 17 00:00:00 2001 From: Not-Nik Date: Sun, 25 Jul 2021 02:19:01 +0200 Subject: [PATCH] Blatant copy rcorre's 'Fix SetShaderValue and port rlights.' --- ReadMe.md | 2 +- build.zig | 5 + examples/ReadMe.md | 17 +- examples/shaders/rlights.zig | 126 ++++++++++++++ examples/shaders/shaders_basic_lighting.zig | 182 ++++++++++++++++++++ lib/generate_functions.py | 3 + lib/raylib-wa.zig | 28 +-- 7 files changed, 342 insertions(+), 21 deletions(-) create mode 100644 examples/shaders/rlights.zig create mode 100644 examples/shaders/shaders_basic_lighting.zig diff --git a/ReadMe.md b/ReadMe.md index 2ecd324..bdef2a9 100755 --- a/ReadMe.md +++ b/ReadMe.md @@ -6,7 +6,7 @@ Manually tweaked, auto generated [raylib](https://github.com/raysan5/raylib) bin Bindings tested on raylib version 3.7 and Zig 0.9.0-dev -Thanks to jessrud, mbcrocci, Gertkeno and sacredbirdman for their contributions to this binding. +Thanks to jessrud, mbcrocci, rcorre, Gertkeno and sacredbirdman for their contributions to this binding. The binding currently only supports a subset of raylib. For more information read [here](https://github.com/Not-Nik/raylib-zig#technical-restrictions). diff --git a/build.zig b/build.zig index aa7a60a..cac5a50 100755 --- a/build.zig +++ b/build.zig @@ -55,6 +55,11 @@ pub fn build(b: *Builder) void { .path = "examples/models/models_loading.zig", .desc = "Loads a model and renders it", }, + .{ + .name = "shaders_basic_lighting", + .path = "examples/shaders/shaders_basic_lighting.zig", + .desc = "Loads a model and renders it", + }, }; const examples_step = b.step("examples", "Builds all the examples"); diff --git a/examples/ReadMe.md b/examples/ReadMe.md index 9196ef0..37cfa1a 100755 --- a/examples/ReadMe.md +++ b/examples/ReadMe.md @@ -1,6 +1,6 @@ # Examples -Making raylib bindings in zig is pretty straight forward since zig has a built-in c parser, so I am now on a quest to port all the examples. +Making raylib bindings in zig is pretty straight forward since zig has a built-in c parser, so I am now on a quest to port all the examples. ### category: core @@ -9,13 +9,9 @@ Examples using raylib core platform functionality like window creation, inputs, | ## | example | developer | |----|----------|:----------:| | 01 | [core_basic_window](core/basic_window.zig) | ray -| | | 02 | [core_input_keys](core/input_keys.zig) | ray -| | | 04 | [core_input_mouse_wheel](core/input_mouse_wheel.zig) | ray -| | | 06 | [core_input_multitouch](core/input_multitouch.zig) | [Berni](https://github.com/Berni8k) -| | | 08 | [core_2d_camera](core/2d_camera.zig) | ray ### category: models @@ -24,4 +20,13 @@ Examples using raylib models functionality, including models loading/generation | ## | example | developer | |----|----------|:----------:| -| 74 | [models_loading](models/models_loading.zig) ([Won't work](https://github.com/G3bE/raylib-zig#Technical-restrictions)) | ray +| 74 | [models_loading](models/models_loading.zig) | ray + +### category: shaders + +Examples using raylib shaders functionality, including shaders loading, parameters configuration and drawing using them (model shaders and postprocessing shaders). This +functionality is directly provided by raylib rlgl module. + +| ## | example | developer | +|----|----------|:----------:| +| 74 | [shaders_basic_lighting](shaders/shaders_basic_lighting.zig) | [Chris Camacho](https://github.com/codifies) diff --git a/examples/shaders/rlights.zig b/examples/shaders/rlights.zig new file mode 100644 index 0000000..9a54903 --- /dev/null +++ b/examples/shaders/rlights.zig @@ -0,0 +1,126 @@ +// This is a zig port of rlights.h by Ryan Roden-Corrent (rcorre). +// The original notice follows: +// +// ********************************************************************************************* +// +// raylib.lights - Some useful functions to deal with lights data +// +// CONFIGURATION: +// +// #define RLIGHTS_IMPLEMENTATION +// Generates the implementation of the library into the included file. +// If not defined, the library is in header only mode and can be included +// in other headers or source files without problems. But only ONE file should +// hold the implementation. +// +// LICENSE: zlib/libpng +// +// Copyright (c) 2017-2020 Victor Fisac (@victorfisac) and 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. +// +// *********************************************************************************************/ + +const std = @import("std"); +usingnamespace @import("raylib"); + +const max_lights = 4; + +pub const Light = struct { + type: LightType, + position: Vector3, + target: Vector3, + color: Color, + enabled: bool, + + // Shader locations + enabledLoc: i32, + typeLoc: i32, + posLoc: i32, + targetLoc: i32, + colorLoc: i32, +}; + +// Light type +pub const LightType = enum { directional, point }; + +var lightsCount: u32 = 0; + +fn getShaderLoc(shader: Shader, name: []const u8) !i32 { + // TODO: Below code doesn't look good to me, + // it assumes a specific shader naming and structure + // Probably this implementation could be improved + // (note from original C implementation, I don't have a better idea) + var buf: [32]u8 = undefined; + const key = try std.fmt.bufPrintZ(buf[0..], "lights[{}].{s}", .{ lightsCount, name }); + return GetShaderLocation(shader, key); +} + +// Create a light and get shader locations +pub fn CreateLight(typ: LightType, position: Vector3, target: Vector3, color: Color, shader: Shader) !Light { + if (lightsCount >= max_lights) { + return error.TooManyLights; + } + + const light = Light{ + .enabled = true, + .type = typ, + .position = position, + .target = target, + .color = color, + + .enabledLoc = try getShaderLoc(shader, "enabled"), + .typeLoc = try getShaderLoc(shader, "type"), + .posLoc = try getShaderLoc(shader, "position"), + .targetLoc = try getShaderLoc(shader, "target"), + .colorLoc = try getShaderLoc(shader, "color"), + }; + UpdateLightValues(shader, light); + + lightsCount += 1; + + return light; +} + +// Send light properties to shader +// NOTE: Light shader locations should be available +pub fn UpdateLightValues(shader: Shader, light: Light) void { + // Send to shader light enabled state and type + SetShaderValue(shader, light.enabledLoc, &light.enabled, @enumToInt(ShaderUniformDataType.UNIFORM_INT)); + SetShaderValue(shader, light.typeLoc, &light.type, @enumToInt(ShaderUniformDataType.UNIFORM_INT)); + + // Send to shader light position values + const position = [3]f32{ light.position.x, light.position.y, light.position.z }; + SetShaderValue(shader, light.posLoc, &position, @enumToInt(ShaderUniformDataType.UNIFORM_VEC3)); + + // Send to shader light target position values + const target = [3]f32{ light.target.x, light.target.y, light.target.z }; + SetShaderValue(shader, light.targetLoc, &target, @enumToInt(ShaderUniformDataType.UNIFORM_VEC3)); + + // Send to shader light color values + const color = [4]f32{ + @intToFloat(f32, light.color.r) / 255.0, + @intToFloat(f32, light.color.g) / 255.0, + @intToFloat(f32, light.color.b) / 255.0, + @intToFloat(f32, light.color.a) / 255.0, + }; + SetShaderValue(shader, light.colorLoc, &color, @enumToInt(ShaderUniformDataType.UNIFORM_VEC4)); +} diff --git a/examples/shaders/shaders_basic_lighting.zig b/examples/shaders/shaders_basic_lighting.zig new file mode 100644 index 0000000..5ce551b --- /dev/null +++ b/examples/shaders/shaders_basic_lighting.zig @@ -0,0 +1,182 @@ +// +// shaders_basic_lighting +// Zig version: +// Author: Ryan Roden-Corrent +// Date: 2021-07-24 +// +// NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders +// support, +// OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 +// version. +// +// NOTE: Shaders used in this example are #version 330 (OpenGL 3.3). +usingnamespace @import("raylib"); +usingnamespace @import("rlights.zig"); +usingnamespace @import("raylib-math"); + +const resourceDir = "raylib/examples/shaders/resources/"; + +pub fn main() !void { + // Initialization + //-------------------------------------------------------------------------------------- + const screenWidth = 800; + const screenHeight = 450; + + //SetConfigFlags(.FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x + // (if available) + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting"); + + // Define the camera to look into our 3d world + const camera = Camera{ + .position = .{ .x = 2.0, .y = 2.0, .z = 6.0 }, // Camera position + .target = .{ .x = 0.0, .y = 0.5, .z = 0.0 }, // Camera looking at point + .up = .{ .x = 0.0, .y = 1.0, .z = 0.0 }, // Camera up vector (rotation towards target) + .fovy = 45.0, // Camera field-of-view Y + .type = CameraType.CAMERA_PERSPECTIVE, // Camera mode type + }; + + // Load models + var modelA = LoadModelFromMesh(GenMeshTorus(0.4, 1.0, 16, 32)); + var modelB = LoadModelFromMesh(GenMeshCube(1.0, 1.0, 1.0)); + var modelC = LoadModelFromMesh(GenMeshSphere(0.5, 32, 32)); + + // Load models texture + const texture = LoadTexture(resourceDir ++ "texel_checker.png"); + + // Assign texture to default model material + modelA.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; + modelB.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; + modelC.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; + + var shader = LoadShader( + resourceDir ++ "/shaders/glsl330/base_lighting.vs", + resourceDir ++ "/shaders/glsl330/lighting.fs", + ); + + // Get some shader loactions + shader.locs[@enumToInt(ShaderLocationIndex.LOC_MATRIX_MODEL)] = GetShaderLocation(shader, "matModel"); + shader.locs[@enumToInt(ShaderLocationIndex.LOC_VECTOR_VIEW)] = GetShaderLocation(shader, "viewPos"); + + // ambient light level + const ambientLoc = GetShaderLocation(shader, "ambient"); + const ambientVals = [4]f32{ 0.2, 0.2, 0.2, 1.0 }; + SetShaderValue(shader, ambientLoc, &ambientVals, @enumToInt(ShaderUniformDataType.UNIFORM_VEC4)); + + var angle: f32 = 6.282; + + // All models use the same shader + modelA.materials[0].shader = shader; + modelB.materials[0].shader = shader; + modelC.materials[0].shader = shader; + + // Using 4 point lights, white, red, green and blue + var lights = [_]Light{ + try CreateLight(LightType.point, .{ .x = 4, .y = 2, .z = 4 }, Vector3Zero(), WHITE, shader), + try CreateLight(LightType.point, .{ .x = 4, .y = 2, .z = 4 }, Vector3Zero(), RED, shader), + try CreateLight(LightType.point, .{ .x = 0, .y = 4, .z = 2 }, Vector3Zero(), GREEN, shader), + try CreateLight(LightType.point, .{ .x = 0, .y = 4, .z = 2 }, Vector3Zero(), BLUE, shader), + }; + + SetCameraMode(camera, CameraMode.CAMERA_ORBITAL); // Set an orbital camera mode + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + if (IsKeyPressed(KeyboardKey.KEY_W)) { + lights[0].enabled = !lights[0].enabled; + } + if (IsKeyPressed(KeyboardKey.KEY_R)) { + lights[1].enabled = !lights[1].enabled; + } + if (IsKeyPressed(KeyboardKey.KEY_G)) { + lights[2].enabled = !lights[2].enabled; + } + if (IsKeyPressed(KeyboardKey.KEY_B)) { + lights[3].enabled = !lights[3].enabled; + } + + //UpdateCamera(&camera); // Update camera + + // Make the lights do differing orbits + angle -= 0.02; + lights[0].position.x = @cos(angle) * 4.0; + lights[0].position.z = @sin(angle) * 4.0; + lights[1].position.x = @cos(-angle * 0.6) * 4.0; + lights[1].position.z = @sin(-angle * 0.6) * 4.0; + lights[2].position.y = @cos(angle * 0.2) * 4.0; + lights[2].position.z = @sin(angle * 0.2) * 4.0; + lights[3].position.y = @cos(-angle * 0.35) * 4.0; + lights[3].position.z = @sin(-angle * 0.35) * 4.0; + + UpdateLightValues(shader, lights[0]); + UpdateLightValues(shader, lights[1]); + UpdateLightValues(shader, lights[2]); + UpdateLightValues(shader, lights[3]); + + // Rotate the torus + modelA.transform = + MatrixMultiply(modelA.transform, MatrixRotateX(-0.025)); + modelA.transform = + MatrixMultiply(modelA.transform, MatrixRotateZ(0.012)); + + // Update the light shader with the camera view position + const cameraPos = [3]f32{ camera.position.x, camera.position.y, camera.position.z }; + SetShaderValue(shader, shader.locs[@enumToInt(ShaderLocationIndex.LOC_VECTOR_VIEW)], &cameraPos, @enumToInt(ShaderUniformDataType.UNIFORM_VEC3)); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + BeginMode3D(camera); + + // Draw the three models + DrawModel(modelA, Vector3Zero(), 1.0, WHITE); + DrawModel(modelB, .{ .x = -1.6, .y = 0, .z = 0 }, 1.0, WHITE); + DrawModel(modelC, .{ .x = 1.6, .y = 0, .z = 0 }, 1.0, WHITE); + + // Draw markers to show where the lights are + if (lights[0].enabled) { + DrawSphereEx(lights[0].position, 0.2, 8, 8, WHITE); + } + if (lights[1].enabled) { + DrawSphereEx(lights[1].position, 0.2, 8, 8, RED); + } + if (lights[2].enabled) { + DrawSphereEx(lights[2].position, 0.2, 8, 8, GREEN); + } + if (lights[3].enabled) { + DrawSphereEx(lights[3].position, 0.2, 8, 8, BLUE); + } + + DrawGrid(10, 1.0); + + EndMode3D(); + + DrawFPS(10, 10); + + DrawText("Use keys RGBW to toggle lights", 10, 30, 20, DARKGRAY); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadModel(modelA); // Unload the modelA + UnloadModel(modelB); // Unload the modelB + UnloadModel(modelC); // Unload the modelC + + UnloadTexture(texture); // Unload the texture + UnloadShader(shader); // Unload shader + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- +} diff --git a/lib/generate_functions.py b/lib/generate_functions.py index 77295d2..994e3fc 100644 --- a/lib/generate_functions.py +++ b/lib/generate_functions.py @@ -36,6 +36,9 @@ def fix_pointer(name: str, t: str): pre += "[*c]" if len(pre) != 0: t = pre + "const " + t + + if t == "[*c]const void": + t = "*const c_void" return name, t diff --git a/lib/raylib-wa.zig b/lib/raylib-wa.zig index 4c78aab..6bd0b45 100644 --- a/lib/raylib-wa.zig +++ b/lib/raylib-wa.zig @@ -23,7 +23,7 @@ pub extern fn SetWindowPosition(x: c_int, y: c_int) void; pub extern fn SetWindowMonitor(monitor: c_int) void; pub extern fn SetWindowMinSize(width: c_int, height: c_int) void; pub extern fn SetWindowSize(width: c_int, height: c_int) void; -pub extern fn GetWindowHandle() [*c]const void; +pub extern fn GetWindowHandle() *const c_void; pub extern fn GetScreenWidth() c_int; pub extern fn GetScreenHeight() c_int; pub extern fn GetMonitorCount() c_int; @@ -68,8 +68,8 @@ pub extern fn LoadShader(vsFileName: [*c]const u8, fsFileName: [*c]const u8) Sha pub extern fn LoadShaderFromMemory(vsCode: [*c]const u8, fsCode: [*c]const u8) Shader; pub extern fn GetShaderLocation(shader: Shader, uniformName: [*c]const u8) c_int; pub extern fn GetShaderLocationAttrib(shader: Shader, attribName: [*c]const u8) c_int; -pub extern fn SetShaderValue(shader: Shader, locIndex: c_int, value: [*c]const void, uniformType: c_int) void; -pub extern fn SetShaderValueV(shader: Shader, locIndex: c_int, value: [*c]const void, uniformType: c_int, count: c_int) void; +pub extern fn SetShaderValue(shader: Shader, locIndex: c_int, value: *const c_void, uniformType: c_int) void; +pub extern fn SetShaderValueV(shader: Shader, locIndex: c_int, value: *const c_void, uniformType: c_int, count: c_int) void; pub extern fn SetShaderValueMatrix(shader: Shader, locIndex: c_int, mat: Matrix) void; pub extern fn SetShaderValueTexture(shader: Shader, locIndex: c_int, texture: Texture2D) void; pub extern fn UnloadShader(shader: Shader) void; @@ -89,9 +89,9 @@ pub extern fn TakeScreenshot(fileName: [*c]const u8) void; pub extern fn SetConfigFlags(flags: c_uint) void; pub extern fn TraceLog(logLevel: c_int, text: [*c]const u8, ...) void; pub extern fn SetTraceLogLevel(logLevel: c_int) void; -pub extern fn MemAlloc(size: c_int) [*c]const void; -pub extern fn MemRealloc(ptr: [*c]const void, size: c_int) [*c]const void; -pub extern fn MemFree(ptr: [*c]const void) void; +pub extern fn MemAlloc(size: c_int) *const c_void; +pub extern fn MemRealloc(ptr: *const c_void, size: c_int) *const c_void; +pub extern fn MemFree(ptr: *const c_void) void; pub extern fn SetTraceLogCallback(callback: TraceLogCallback) void; pub extern fn SetLoadFileDataCallback(callback: LoadFileDataCallback) void; pub extern fn SetSaveFileDataCallback(callback: SaveFileDataCallback) void; @@ -99,7 +99,7 @@ pub extern fn SetLoadFileTextCallback(callback: LoadFileTextCallback) void; pub extern fn SetSaveFileTextCallback(callback: SaveFileTextCallback) void; pub extern fn LoadFileData(fileName: [*c]const u8, bytesRead: [*c]const c_uint) [*c]const u8; pub extern fn UnloadFileData(data: [*c]const u8) void; -pub extern fn SaveFileData(fileName: [*c]const u8, data: [*c]const void, bytesToWrite: c_uint) bool; +pub extern fn SaveFileData(fileName: [*c]const u8, data: *const c_void, bytesToWrite: c_uint) bool; pub extern fn LoadFileText(fileName: [*c]const u8) [*c]const u8; pub extern fn UnloadFileText(text: [*c]const u8) void; pub extern fn SaveFileText(fileName: [*c]const u8, text: [*c]const u8) bool; @@ -282,8 +282,8 @@ pub extern fn LoadTextureCubemap(image: Image, layout: c_int) TextureCubemap; pub extern fn LoadRenderTexture(width: c_int, height: c_int) RenderTexture2D; pub extern fn UnloadTexture(texture: Texture2D) void; pub extern fn UnloadRenderTexture(target: RenderTexture2D) void; -pub extern fn UpdateTexture(texture: Texture2D, pixels: [*c]const void) void; -pub extern fn UpdateTextureRec(texture: Texture2D, rec: Rectangle, pixels: [*c]const void) void; +pub extern fn UpdateTexture(texture: Texture2D, pixels: *const c_void) void; +pub extern fn UpdateTextureRec(texture: Texture2D, rec: Rectangle, pixels: *const c_void) void; pub extern fn GetTextureData(texture: Texture2D) Image; pub extern fn GetScreenData() Image; pub extern fn GenTextureMipmaps(texture: [*c]const Texture2D) void; @@ -307,8 +307,8 @@ pub extern fn ColorFromHSV(hue: f32, saturation: f32, value: f32) Color; pub extern fn ColorAlpha(color: Color, alpha: f32) Color; pub extern fn ColorAlphaBlend(dst: Color, src: Color, tint: Color) Color; pub extern fn GetColor(hexValue: c_int) Color; -pub extern fn GetPixelColor(srcPtr: [*c]const void, format: c_int) Color; -pub extern fn SetPixelColor(dstPtr: [*c]const void, color: Color, format: c_int) void; +pub extern fn GetPixelColor(srcPtr: *const c_void, format: c_int) Color; +pub extern fn SetPixelColor(dstPtr: *const c_void, color: Color, format: c_int) void; pub extern fn GetPixelDataSize(width: c_int, height: c_int, format: c_int) c_int; pub extern fn GetFontDefault() Font; pub extern fn LoadFont(fileName: [*c]const u8) Font; @@ -371,7 +371,7 @@ pub extern fn LoadModelFromMesh(mesh: Mesh) Model; pub extern fn UnloadModel(model: Model) void; pub extern fn UnloadModelKeepMeshes(model: Model) void; pub extern fn UploadMesh(mesh: [*c]const Mesh, dynamic: bool) void; -pub extern fn UpdateMeshBuffer(mesh: Mesh, index: c_int, data: [*c]const void, dataSize: c_int, offset: c_int) void; +pub extern fn UpdateMeshBuffer(mesh: Mesh, index: c_int, data: *const c_void, dataSize: c_int, offset: c_int) void; pub extern fn DrawMesh(mesh: Mesh, material: Material, transform: Matrix) void; pub extern fn DrawMeshInstanced(mesh: Mesh, material: Material, transforms: [*c]const Matrix, instances: c_int) void; pub extern fn UnloadMesh(mesh: Mesh) void; @@ -424,7 +424,7 @@ pub extern fn LoadWave(fileName: [*c]const u8) Wave; pub extern fn LoadWaveFromMemory(fileType: [*c]const u8, fileData: [*c]const u8, dataSize: c_int) Wave; pub extern fn LoadSound(fileName: [*c]const u8) Sound; pub extern fn LoadSoundFromWave(wave: Wave) Sound; -pub extern fn UpdateSound(sound: Sound, data: [*c]const void, samplesCount: c_int) void; +pub extern fn UpdateSound(sound: Sound, data: *const c_void, samplesCount: c_int) void; pub extern fn UnloadWave(wave: Wave) void; pub extern fn UnloadSound(sound: Sound) void; pub extern fn ExportWave(wave: Wave, fileName: [*c]const u8) bool; @@ -458,7 +458,7 @@ pub extern fn SetMusicPitch(music: Music, pitch: f32) void; pub extern fn GetMusicTimeLength(music: Music) f32; pub extern fn GetMusicTimePlayed(music: Music) f32; pub extern fn InitAudioStream(sampleRate: c_uint, sampleSize: c_uint, channels: c_uint) AudioStream; -pub extern fn UpdateAudioStream(stream: AudioStream, data: [*c]const void, samplesCount: c_int) void; +pub extern fn UpdateAudioStream(stream: AudioStream, data: *const c_void, samplesCount: c_int) void; pub extern fn CloseAudioStream(stream: AudioStream) void; pub extern fn IsAudioStreamProcessed(stream: AudioStream) bool; pub extern fn PlayAudioStream(stream: AudioStream) void;