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/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/raylib-wa.zig b/lib/raylib-wa.zig index 8dff39b..f27abbd 100644 --- a/lib/raylib-wa.zig +++ b/lib/raylib-wa.zig @@ -366,7 +366,7 @@ pub extern fn GetShapesTexture() Texture2D; pub extern fn GetShapesTextureRec() Rectangle; pub extern fn SetShapesTexture(texture: Texture2D, source: Rectangle) void; pub extern fn GetShaderLocation(shader: Shader, uniformName: [*c]const u8) c_int; -pub extern fn SetShaderValue(shader: Shader, uniformLoc: c_int, value: [*c]const void, uniformType: c_int) void; +pub extern fn SetShaderValue(shader: Shader, uniformLoc: c_int, value: *const c_void, uniformType: c_int) void; pub extern fn SetShaderValueV(shader: Shader, uniformLoc: c_int, value: [*c]const void, uniformType: c_int, count: c_int) void; pub extern fn SetShaderValueMatrix(shader: Shader, uniformLoc: c_int, mat: Matrix) void; pub extern fn SetShaderValueTexture(shader: Shader, uniformLoc: c_int, texture: Texture2D) void; @@ -441,5 +441,3 @@ pub extern fn StopAudioStream(stream: AudioStream) void; pub extern fn SetAudioStreamVolume(stream: AudioStream, volume: f32) void; pub extern fn SetAudioStreamPitch(stream: AudioStream, pitch: f32) void; pub extern fn SetAudioStreamBufferSizeDefault(size: c_int) void; - -