fix build and basic_window example

some restructuring due to the recent changes to usingnamespace.

also cleaned up some deprecated stuff from raylib 3.7.

- appended the contents of raylib-wa.zig into raylib-zig.zig
- using raylib functions requires `const rl = @import("raylib");`
    (and accesing the identifiers inside rl, like `rl.InitWindow`)

only the basic_window example was updated, and it looks like it crashes
on keyboard inputs.

many thanks to @nektro :)
This commit is contained in:
Francisco Demartino 2022-01-08 04:20:11 -03:00
parent 3f19a5742a
commit 5e275e93df
14 changed files with 237 additions and 166 deletions

134
examples/old/core/2d_camera.zig Executable file
View file

@ -0,0 +1,134 @@
//
// 2d_camera
// Zig version:
// Author: Nikolas Wipper
// Date: 2020-02-16
//
usingnamespace @import("raylib");
usingnamespace @import("raylib-math");
const MAX_BUILDINGS = 100;
pub fn main() anyerror!void
{
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - 2d camera");
var player = Rectangle { .x = 400, .y = 280, .width = 40, .height = 40 };
var buildings: [MAX_BUILDINGS]Rectangle = undefined;
var buildColors: [MAX_BUILDINGS]Color = undefined;
var spacing: i32 = 0;
for (buildings) |_, i|
{
buildings[i].width = @intToFloat(f32, GetRandomValue(50, 200));
buildings[i].height = @intToFloat(f32, GetRandomValue(100, 800));
buildings[i].y = screenHeight - 130 - buildings[i].height;
buildings[i].x = @intToFloat(f32, -6000 + spacing);
spacing += @floatToInt(i32, buildings[i].width);
buildColors[i] = Color { .r = @intCast(u8, GetRandomValue(200, 240)), .g = @intCast(u8, GetRandomValue(200, 240)),
.b = @intCast(u8, GetRandomValue(200, 250)), .a = 255 };
}
var camera = Camera2D {
.target = Vector2 { .x = player.x + 20, .y = player.y + 20 },
.offset = Vector2 { .x = screenWidth/2, .y = screenHeight/2 },
.rotation = 0,
.zoom = 1,
};
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
//----------------------------------------------------------------------------------
// Player movement
if (IsKeyDown(KeyboardKey.KEY_RIGHT)) { player.x += 2; }
else if (IsKeyDown(KeyboardKey.KEY_LEFT)) { player.x -= 2; }
// Camera target follows player
camera.target = Vector2 { .x = player.x + 20, .y = player.y + 20 };
// Camera rotation controls
if (IsKeyDown(KeyboardKey.KEY_A)) { camera.rotation -= 1; }
else if (IsKeyDown(KeyboardKey.KEY_S)) { camera.rotation += 1; }
// Limit camera rotation to 80 degrees (-40 to 40)
camera.rotation = Clamp(camera.rotation, -40, 40);
// Camera zoom controls
camera.zoom += GetMouseWheelMove() * 0.05;
camera.zoom = Clamp(camera.zoom, 0.1, 3.0);
// Camera reset (zoom and rotation)
if (IsKeyPressed(KeyboardKey.KEY_R))
{
camera.zoom = 1.0;
camera.rotation = 0.0;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
camera.Begin();
DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY);
for (buildings) |building, i|
{
//DrawRectangleRec(building, buildColors[i]);
DrawRectangle(@floatToInt(c_int, building.x), @floatToInt(c_int, building.y),
@floatToInt(c_int, building.width), @floatToInt(c_int, building.height), buildColors[i]);
}
DrawRectangle(@floatToInt(c_int, player.x), @floatToInt(c_int, player.y),
@floatToInt(c_int, player.width), @floatToInt(c_int, player.height), DARKGRAY);
//DrawRectangleRec(player, RED);
DrawLine(@floatToInt(c_int, camera.target.x), -screenHeight*10, @floatToInt(c_int, camera.target.x), screenHeight*10, GREEN);
DrawLine(-screenWidth*10, @floatToInt(c_int, camera.target.y), screenWidth*10, @floatToInt(c_int, camera.target.y), GREEN);
camera.End();
DrawText("SCREEN AREA", 640, 10, 20, RED);
DrawRectangle(0, 0, screenWidth, 5, RED);
DrawRectangle(0, 5, 5, screenHeight - 10, RED);
DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, RED);
DrawRectangle(0, screenHeight - 5, screenWidth, 5, RED);
DrawRectangle( 10, 10, 250, 113, Fade(SKYBLUE, 0.5));
DrawRectangleLines( 10, 10, 250, 113, BLUE);
DrawText("Free 2d camera controls:", 20, 20, 10, BLACK);
DrawText("- Right/Left to move Offset", 40, 40, 10, DARKGRAY);
DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY);
DrawText("- A / S to Rotate", 40, 80, 10, DARKGRAY);
DrawText("- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}

View file

@ -0,0 +1,55 @@
//
// input_keys
// Zig version:
// Author: Nikolas Wipper
// Date: 2020-02-16
//
usingnamespace @import("raylib");
pub fn main() anyerror!void
{
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - keyboard input");
var ballPosition = Vector2 { .x = screenWidth/2, .y = screenHeight/2 };
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 (IsKeyDown(KeyboardKey.KEY_RIGHT)) { ballPosition.x += 2.0; }
if (IsKeyDown(KeyboardKey.KEY_LEFT)) { ballPosition.x -= 2.0; }
if (IsKeyDown(KeyboardKey.KEY_UP)) { ballPosition.y -= 2.0; }
if (IsKeyDown(KeyboardKey.KEY_DOWN)) { ballPosition.y += 2.0; }
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("move the ball with arrow keys", 10, 10, 20, DARKGRAY);
DrawCircle(@floatToInt(c_int, ballPosition.x), @floatToInt(c_int, ballPosition.y), 50, MAROON);
//DrawCircleV(ballPosition, 50, MAROON);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}

View file

@ -0,0 +1,59 @@
//
// input_mouse
// Zig version:
// Author: Nikolas Wipper
// Date: 2020-02-16
//
usingnamespace @import("raylib");
pub fn main() anyerror!void
{
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - mouse input");
var ballPosition = Vector2 { .x = -100.0, .y = -100.0 };
var ballColor = DARKBLUE;
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
//----------------------------------------------------------------------------------
ballPosition = GetMousePosition();
ballPosition.x = @intToFloat(f32, GetMouseX());
ballPosition.y = @intToFloat(f32, GetMouseY());
if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON)) { ballColor = MAROON; }
else if (IsMouseButtonPressed(MouseButton.MOUSE_MIDDLE_BUTTON)) { ballColor = LIME; }
else if (IsMouseButtonPressed(MouseButton.MOUSE_RIGHT_BUTTON)) { ballColor = DARKBLUE; }
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawCircle(@floatToInt(c_int, ballPosition.x), @floatToInt(c_int, ballPosition.y), 50, ballColor);
//DrawCircleV(ballPosition, 40, ballColor);
DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}

View file

@ -0,0 +1,53 @@
//
// input_mouse_wheel
// Zig version:
// Author: Nikolas Wipper
// Date: 2020-02-16
//
usingnamespace @import("raylib");
pub fn main() anyerror!void
{
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
var boxPositionY: f32 = screenHeight / 2 - 40;
var scrollSpeed: f32 = 4; // Scrolling speed in pixels
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
//----------------------------------------------------------------------------------
boxPositionY -= (GetMouseWheelMove() * scrollSpeed);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(WHITE);
DrawRectangle(screenWidth/2 - 40, @floatToInt(c_int, boxPositionY), 80, 80, MAROON);
DrawText("Use mouse wheel to move the cube up and down!", 10, 10, 20, GRAY);
DrawText(FormatText("Box position Y: %03i", @floatToInt(c_int, boxPositionY)), 10, 40, 20, LIGHTGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}

View file

@ -0,0 +1,84 @@
//
// input_multitouch
// Zig version:
// Author: Nikolas Wipper
// Date: 2020-02-16
//
usingnamespace @import("raylib");
pub fn main() anyerror!void
{
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
var ballPosition = Vector2 { .x = -100.0, .y = -100.0 };
var ballColor = BEIGE;
var touchCounter: f32 = 0;
var touchPosition = Vector2 { .x = 0.0, .y = 0.0 };
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
//----------------------------------------------------------------------------------
ballPosition = GetMousePosition();
ballColor = BEIGE;
if (IsMouseButtonDown(MouseButton.MOUSE_LEFT_BUTTON)) { ballColor = MAROON; }
if (IsMouseButtonDown(MouseButton.MOUSE_MIDDLE_BUTTON)) { ballColor = LIME; }
if (IsMouseButtonDown(MouseButton.MOUSE_RIGHT_BUTTON)) { ballColor = DARKBLUE; }
if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON)) { touchCounter = 10; }
if (IsMouseButtonPressed(MouseButton.MOUSE_MIDDLE_BUTTON)) { touchCounter = 10; }
if (IsMouseButtonPressed(MouseButton.MOUSE_RIGHT_BUTTON)) { touchCounter = 10; }
if (touchCounter > 0) { touchCounter -= 1; }
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
const nums = [_]i32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (nums) |i|
{
touchPosition = GetTouchPosition(i); // Get the touch point
if ((touchPosition.x >= 0) and (touchPosition.y >= 0)) // Make sure point is not (-1,-1) as this means there is no touch for it
{
// Draw circle and touch index number
DrawCircle(@floatToInt(c_int, touchPosition.x), @floatToInt(c_int, touchPosition.y), 34, ORANGE);
//DrawCircleV(touchPosition, 34, ORANGE);
DrawText(FormatText("%d", i), @floatToInt(c_int, touchPosition.x) - 10, @floatToInt(c_int, touchPosition.y) - 70, 40, BLACK);
}
}
// Draw the normal mouse location
DrawCircle(@floatToInt(c_int, ballPosition.x), @floatToInt(c_int, ballPosition.y), 30 + (touchCounter*3), ballColor);
//DrawCircleV(ballPosition, 30 + (touchCounter*3), ballColor);
DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);
DrawText("touch the screen at multiple locations to get multiple balls", 10, 30, 20, DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}

View file

@ -0,0 +1,129 @@
//
// models_loading
// Zig version:
// Author: Nikolas Wipper
// Date: 2020-02-15
//
usingnamespace @import("raylib");
const resourceDir = "raylib/examples/models/resources/";
pub fn main() anyerror!void {
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - models loading");
// Define the camera to look into our 3d world
var camera = Camera{
.position = Vector3{ .x = 50.0, .y = 50.0, .z = 50.0 }, // Camera position
.target = Vector3{ .x = 0.0, .y = 10.0, .z = 0.0 }, // Camera looking at point
.up = Vector3{ .x = 0.0, .y = 1.0, .z = 0.0 }, // Camera up vector (rotation towards target)
.fovy = 45.0, // Camera field-of-view Y
.projection = CameraProjection.CAMERA_PERSPECTIVE, // Camera mode type
};
var model = LoadModel(resourceDir ++ "models/castle.obj"); // Load model
var texture = LoadTexture(resourceDir ++ "models/castle_diffuse.png"); // Load model texture
model.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; // Set map diffuse texture
var position = Vector3{ .x = 0.0, .y = 0.0, .z = 0.0 }; // Set model position
var bounds = MeshBoundingBox(model.meshes[0]); // Set model bounds
// NOTE: bounds are calculated from the original size of the model,
// if model is scaled on drawing, bounds must be also scaled
camera.SetMode(CameraMode.CAMERA_FREE); // Set a free camera mode
var selected = false; // Selected object flag
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
//----------------------------------------------------------------------------------
camera.Update();
// Load new models/textures on drag&drop
if (IsFileDropped()) {
var count: c_int = 0;
var droppedFiles = GetDroppedFiles(&count);
if (count == 1) // Only support one file dropped
{
if (IsFileExtension(droppedFiles[0], ".obj") or
IsFileExtension(droppedFiles[0], ".gltf") or
IsFileExtension(droppedFiles[0], ".iqm")) // Model file formats supported
{
UnloadModel(model); // Unload previous model
model = LoadModel(droppedFiles[0]); // Load new model
model.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; // Set current map diffuse texture
bounds = MeshBoundingBox(model.meshes[0]);
// TODO: Move camera position from target enough distance to visualize model properly
} else if (IsFileExtension(droppedFiles[0], ".png")) // Texture file formats supported
{
// Unload current model texture and load new one
UnloadTexture(texture);
texture = LoadTexture(droppedFiles[0]);
model.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture;
}
}
ClearDroppedFiles(); // Clear internal buffers
}
// Select model on mouse click
if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON)) {
// Check collision between ray and box
if (CheckCollisionRayBox(GetMouseRay(GetMousePosition(), camera), bounds)) {
selected = !selected;
} else {
selected = false;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
camera.Begin();
DrawModel(model, position, 1.0, WHITE); // Draw 3d model with texture
DrawGrid(20, 10.0); // Draw a grid
if (selected) DrawBoundingBox(bounds, GREEN); // Draw selection box
camera.End();
DrawText("Drag & drop model to load mesh/texture.", 10, GetScreenHeight() - 20, 10, DARKGRAY);
if (selected) DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, GREEN);
DrawText("(c) Castle 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture); // Unload texture
UnloadModel(model); // Unload model
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}

View file

@ -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.SHADER_UNIFORM_INT));
SetShaderValue(shader, light.typeLoc, &light.type, @enumToInt(ShaderUniformDataType.SHADER_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.SHADER_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.SHADER_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.SHADER_UNIFORM_VEC4));
}

View file

@ -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
.projection = CameraProjection.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.SHADER_LOC_MATRIX_MODEL)] = GetShaderLocation(shader, "matModel");
shader.locs[@enumToInt(ShaderLocationIndex.SHADER_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.SHADER_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.SHADER_LOC_VECTOR_VIEW)], &cameraPos, @enumToInt(ShaderUniformDataType.SHADER_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
//--------------------------------------------------------------------------------------
}