Watch
2
0
Fork
You've already forked raylib-cs
0

chore: rebase to main with translation ports

This commit is contained in:
tiger tiger tiger 2026-07-17 07:49:07 +02:00
commit 8024c6ac40
134 changed files with 15456 additions and 10650 deletions

View file

@ -1,11 +1,15 @@
/*******************************************************************************************
*
* raylib [models] example - Drawing billboards
* raylib [models] example - billboard rendering
*
* This example has been created using raylib 1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example complexity rating: [] 3/4
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
* Example originally created with raylib 1.3, last time updated with raylib 3.5
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -14,121 +18,131 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class BillboardDemo
public partial class BillboardDemo : IExample
{
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Billboard Demo";
public string Title => "raylib [models] example - billboard rendering";
private Camera3D camera;
private Texture2D bill;
private Vector3 billPositionStatic;
private Vector3 billPositionRotating;
private Rectangle source;
private Vector3 billUp;
private Vector2 size;
private Vector2 origin;
private float distanceStatic;
private float distanceRotating;
private float rotation;
public void Init()
{
// Define the camera to look into our 3d world
camera = new();
camera.Position = new Vector3(5.0f, 4.0f, 5.0f); // Camera position
camera.Target = new Vector3(0.0f, 2.0f, 0.0f); // Camera looking at point
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
camera.FovY = 45.0f; // Camera field-of-view Y
camera.Projection = CameraProjection.Perspective; // Camera projection type
bill = LoadTexture("resources/billboard.png"); // Our billboard texture
billPositionStatic = new(0.0f, 2.0f, 0.0f); // Position of static billboard
billPositionRotating = new(1.0f, 2.0f, 1.0f); // Position of rotating billboard
// Entire billboard texture, source is used to take a segment from a larger texture
source = new(0.0f, 0.0f, (float)bill.Width, (float)bill.Height);
// NOTE: Billboard locked on axis-Y
billUp = new(0.0f, 1.0f, 0.0f);
// Set the height of the rotating billboard to 1.0 with the aspect ratio fixed
size = new(source.Width / source.Height, 1.0f);
// Rotate around origin
// Here we choose to rotate around the image center
origin = size * 0.5f;
// Distance is needed for the correct billboard draw order
// Larger distance (further away from the camera) should be drawn prior to smaller distance
distanceStatic = 0.0f;
distanceRotating = 0.0f;
rotation = 0.0f;
}
public void Update()
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Orbital);
rotation += 0.4f;
distanceStatic = Vector3.Distance(camera.Position, billPositionStatic);
distanceRotating = Vector3.Distance(camera.Position, billPositionRotating);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawGrid(10, 1.0f); // Draw a grid
// Draw order matters!
if (distanceStatic > distanceRotating)
{
DrawBillboard(camera, bill, billPositionStatic, 2.0f, Color.White);
DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, size, origin, rotation, Color.White);
}
else
{
DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, size, origin, rotation, Color.White);
DrawBillboard(camera, bill, billPositionStatic, 2.0f, Color.White);
}
EndMode3D();
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadTexture(bill); // Unload texture
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - billboard rendering");
InitWindow(screenWidth, screenHeight, "raylib [models] example - drawing billboards");
// Define the camera to look into our 3d world
Camera3D camera = new();
camera.Position = new Vector3(5.0f, 4.0f, 5.0f);
camera.Target = new Vector3(0.0f, 2.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
camera.FovY = 45.0f;
camera.Projection = CameraProjection.Perspective;
// Our texture billboard
Texture2D bill = LoadTexture("resources/billboard.png");
// Position of billboard billboard
Vector3 billPositionStatic = new(0.0f, 2.0f, 0.0f);
Vector3 billPositionRotating = new(1.0f, 2.0f, 1.0f);
// Entire billboard texture, source is used to take a segment from a larger texture.
Rectangle source = new(0.0f, 0.0f, (float)bill.Width, (float)bill.Height);
// NOTE: Billboard locked on axis-Y
Vector3 billUp = new(0.0f, 1.0f, 0.0f);
// Rotate around origin
// Here we choose to rotate around the image center
// NOTE: (-1, 1) is the range where origin.X, origin.Y is inside the texture
Vector2 rotateOrigin = Vector2.Zero;
// Distance is needed for the correct billboard draw order
// Larger distance (further away from the camera) should be drawn prior to smaller distance.
float distanceStatic = 0.0f;
float distanceRotating = 0.0f;
float rotation = 0.0f;
SetTargetFPS(60);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new BillboardDemo();
game.Init();
// Main game loop
while (!WindowShouldClose())
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Orbital);
rotation += 0.4f;
distanceStatic = Vector3.Distance(camera.Position, billPositionStatic);
distanceRotating = Vector3.Distance(camera.Position, billPositionRotating);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawGrid(10, 1.0f);
// Draw order matters!
if (distanceStatic > distanceRotating)
{
DrawBillboard(camera, bill, billPositionStatic, 2.0f, Color.White);
DrawBillboardPro(
camera,
bill,
source,
billPositionRotating,
billUp,
new Vector2(1.0f, 1.0f),
rotateOrigin,
rotation,
Color.White
);
}
else
{
DrawBillboardPro(
camera,
bill,
source,
billPositionRotating,
billUp,
new Vector2(1.0f, 1.0f),
rotateOrigin,
rotation,
Color.White
);
DrawBillboard(camera, bill, billPositionStatic, 2.0f, Color.White);
}
EndMode3D();
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(bill);
CloseWindow();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
}

View file

@ -1,11 +1,15 @@
/*******************************************************************************************
*
* raylib [models] example - Detect basic 3d collisions (box vs sphere vs box)
* raylib [models] example - box collisions
*
* This example has been created using raylib 1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example complexity rating: [] 1/4
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
* Example originally created with raylib 1.3, last time updated with raylib 3.5
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -14,129 +18,165 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class BoxCollisions
public partial class BoxCollisions : IExample
{
public static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Box Collisions";
public string Title => "raylib [models] example - box collisions";
private Camera3D camera;
private Vector3 playerPosition;
private Vector3 playerSize;
private Color playerColor;
private Vector3 enemyBoxPos;
private Vector3 enemyBoxSize;
private Vector3 enemySpherePos;
private float enemySphereSize;
private bool collision;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - box collisions");
// Define the camera to look into our 3d world
Camera3D camera = new();
camera = new();
camera.Position = new Vector3(0.0f, 10.0f, 10.0f);
camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
camera.FovY = 45.0f;
camera.Projection = CameraProjection.Perspective;
Vector3 playerPosition = new(0.0f, 1.0f, 2.0f);
Vector3 playerSize = new(1.0f, 2.0f, 1.0f);
Color playerColor = Color.Green;
playerPosition = new(0.0f, 1.0f, 2.0f);
playerSize = new(1.0f, 2.0f, 1.0f);
playerColor = Color.Green;
Vector3 enemyBoxPos = new(-4.0f, 1.0f, 0.0f);
Vector3 enemyBoxSize = new(2.0f, 2.0f, 2.0f);
enemyBoxPos = new(-4.0f, 1.0f, 0.0f);
enemyBoxSize = new(2.0f, 2.0f, 2.0f);
Vector3 enemySpherePos = new(4.0f, 0.0f, 0.0f);
float enemySphereSize = 1.5f;
enemySpherePos = new(4.0f, 0.0f, 0.0f);
enemySphereSize = 1.5f;
bool collision = false;
collision = false;
}
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
public void Update()
{
// Update
//----------------------------------------------------------------------------------
// Move player
if (IsKeyDown(KeyboardKey.Right))
{
playerPosition.X += 0.2f;
}
else if (IsKeyDown(KeyboardKey.Left))
{
playerPosition.X -= 0.2f;
}
else if (IsKeyDown(KeyboardKey.Down))
{
playerPosition.Z += 0.2f;
}
else if (IsKeyDown(KeyboardKey.Up))
{
playerPosition.Z -= 0.2f;
}
collision = false;
// Check collisions player vs enemy-box
BoundingBox box1 = new(
playerPosition - (playerSize / 2),
playerPosition + (playerSize / 2)
);
BoundingBox box2 = new(
enemyBoxPos - (enemyBoxSize / 2),
enemyBoxPos + (enemyBoxSize / 2)
);
if (CheckCollisionBoxes(box1, box2))
{
collision = true;
}
// Check collisions player vs enemy-sphere
if (CheckCollisionBoxSphere(box1, enemySpherePos, enemySphereSize))
{
collision = true;
}
if (collision)
{
playerColor = Color.Red;
}
else
{
playerColor = Color.Green;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
// Draw enemy-box
DrawCube(enemyBoxPos, enemyBoxSize.X, enemyBoxSize.Y, enemyBoxSize.Z, Color.Gray);
DrawCubeWires(enemyBoxPos, enemyBoxSize.X, enemyBoxSize.Y, enemyBoxSize.Z, Color.DarkGray);
// Draw enemy-sphere
DrawSphere(enemySpherePos, enemySphereSize, Color.Gray);
DrawSphereWires(enemySpherePos, enemySphereSize, 16, 16, Color.DarkGray);
// Draw player
DrawCubeV(playerPosition, playerSize, playerColor);
DrawGrid(10, 1.0f); // Draw a grid
EndMode3D();
DrawText("Move player with arrow keys to collide", 220, 40, 20, Color.Gray);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - box collisions");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new BoxCollisions();
game.Init();
// Main game loop
while (!WindowShouldClose())
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Move player
if (IsKeyDown(KeyboardKey.Right))
{
playerPosition.X += 0.2f;
}
else if (IsKeyDown(KeyboardKey.Left))
{
playerPosition.X -= 0.2f;
}
else if (IsKeyDown(KeyboardKey.Down))
{
playerPosition.Z += 0.2f;
}
else if (IsKeyDown(KeyboardKey.Up))
{
playerPosition.Z -= 0.2f;
}
collision = false;
// Check collisions player vs enemy-box
BoundingBox box1 = new(
playerPosition - (playerSize / 2),
playerPosition + (playerSize / 2)
);
BoundingBox box2 = new(
enemyBoxPos - (enemyBoxSize / 2),
enemyBoxPos + (enemyBoxSize / 2)
);
if (CheckCollisionBoxes(box1, box2))
{
collision = true;
}
// Check collisions player vs enemy-sphere
if (CheckCollisionBoxSphere(box1, enemySpherePos, enemySphereSize))
{
collision = true;
}
if (collision)
{
playerColor = Color.Red;
}
else
{
playerColor = Color.Green;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
// Draw enemy-box
DrawCube(enemyBoxPos, enemyBoxSize.X, enemyBoxSize.Y, enemyBoxSize.Z, Color.Gray);
DrawCubeWires(enemyBoxPos, enemyBoxSize.X, enemyBoxSize.Y, enemyBoxSize.Z, Color.DarkGray);
// Draw enemy-sphere
DrawSphere(enemySpherePos, enemySphereSize, Color.Gray);
DrawSphereWires(enemySpherePos, enemySphereSize, 16, 16, Color.DarkGray);
// Draw player
DrawCubeV(playerPosition, playerSize, playerColor);
DrawGrid(10, 1.0f);
EndMode3D();
DrawText("Move player with cursors to collide", 220, 40, 20, Color.Gray);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;

View file

@ -1,11 +1,15 @@
/*******************************************************************************************
*
* raylib [models] example - Cubicmap loading and drawing
* raylib [models] example - cubicmap rendering
*
* This example has been created using raylib 1.8 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example complexity rating: [] 2/4
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
* Example originally created with raylib 1.8, last time updated with raylib 3.5
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -14,91 +18,128 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class CubicmapDemo
public partial class CubicmapDemo : IExample
{
public static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Cubicmap Demo";
public string Title => "raylib [models] example - cubicmap rendering";
private Camera3D camera;
private Texture2D cubicmap;
private Texture2D texture;
private Model model;
private Vector3 mapPosition;
private bool pause;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - cubesmap loading and drawing");
// Define the camera to look into our 3d world
Camera3D camera = new();
camera.Position = new Vector3(16.0f, 14.0f, 16.0f);
camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
camera.FovY = 45.0f;
camera.Projection = CameraProjection.Perspective;
camera = new();
camera.Position = new Vector3(16.0f, 14.0f, 16.0f); // Camera position
camera.Target = new Vector3(0.0f, 0.0f, 0.0f); // Camera looking at point
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
camera.FovY = 45.0f; // Camera field-of-view Y
camera.Projection = CameraProjection.Perspective; // Camera projection type
Image image = LoadImage("resources/cubicmap.png");
Texture2D cubicmap = LoadTextureFromImage(image);
var image = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
cubicmap = LoadTextureFromImage(image); // Convert image to texture to display (VRAM)
Mesh mesh = GenMeshCubicmap(image, new Vector3(1.0f, 1.0f, 1.0f));
Model model = LoadModelFromMesh(mesh);
var mesh = GenMeshCubicmap(image, new Vector3(1.0f, 1.0f, 1.0f));
model = LoadModelFromMesh(mesh);
// NOTE: By default each cube is mapped to one part of texture atlas
Texture2D texture = LoadTexture("resources/cubicmap_atlas.png");
texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture
// Set map diffuse texture
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Albedo, ref texture);
Vector3 mapPosition = new(-16.0f, 0.0f, -8.0f);
UnloadImage(image);
mapPosition = new(-16.0f, 0.0f, -8.0f); // Set model position
SetTargetFPS(60);
UnloadImage(image); // Unload cubesmap image from RAM, already uploaded to VRAM
pause = false; // Pause camera orbital rotation (and zoom)
}
public void Update()
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KeyboardKey.P))
{
pause = !pause;
}
if (!pause)
{
UpdateCamera(ref camera, CameraMode.Orbital);
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModel(model, mapPosition, 1.0f, Color.White);
EndMode3D();
Vector2 position = new(screenWidth - cubicmap.Width * 4 - 20, 20);
DrawTextureEx(cubicmap, position, 0.0f, 4.0f, Color.White);
DrawRectangleLines(
screenWidth - cubicmap.Width * 4 - 20,
20,
cubicmap.Width * 4,
cubicmap.Height * 4,
Color.Green
);
DrawText("cubicmap image used to", 658, 90, 10, Color.Gray);
DrawText("generate map 3d model", 658, 104, 10, Color.Gray);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadTexture(cubicmap); // Unload cubicmap texture
UnloadTexture(texture); // Unload map texture
UnloadModel(model); // Unload map model
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - cubicmap rendering");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new CubicmapDemo();
game.Init();
// Main game loop
while (!WindowShouldClose())
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Orbital);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModel(model, mapPosition, 1.0f, Color.White);
EndMode3D();
Vector2 position = new(screenWidth - cubicmap.Width * 4 - 20, 20);
DrawTextureEx(cubicmap, position, 0.0f, 4.0f, Color.White);
DrawRectangleLines(
screenWidth - cubicmap.Width * 4 - 20,
20,
cubicmap.Width * 4,
cubicmap.Height * 4,
Color.Green
);
DrawText("cubicmap image used to", 658, 90, 10, Color.Gray);
DrawText("generate map 3d model", 658, 104, 10, Color.Gray);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(cubicmap);
UnloadTexture(texture);
UnloadModel(model);
CloseWindow();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
}

View file

@ -3,19 +3,28 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class DynamicMesh
public partial class DynamicMesh : IExample
{
public unsafe static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
private const int triangleRows = 48;
private const int vertexRows = triangleRows + 1;
public string Name => "Models / Dynamic Mesh";
public string Title => "raylib [models] example - dynamic mesh";
private Camera3D camera;
private Mesh dynamicMesh;
private Texture2D texture;
private Color[] pixels;
private Material material;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - dynamic mesh");
// Define the camera to look into our 3d world
Camera3D camera = new();
camera = new();
camera.Position = Vector3.One * 1.5f;
camera.Target = camera.Position + new Vector3(1f, -0.25f, 1f);
camera.Up = Vector3.UnitY;
@ -23,18 +32,14 @@ public class DynamicMesh
camera.Projection = CameraProjection.Perspective;
// Generate a dynamic mesh using utils to allocate/access mesh attribute data
const int triangleRows = 48;
const int vertexRows = triangleRows + 1;
Mesh dynamicMesh = new(vertexRows * vertexRows, triangleRows * triangleRows * 2);
dynamicMesh = new(vertexRows * vertexRows, triangleRows * triangleRows * 2);
dynamicMesh.AllocVertices();
dynamicMesh.AllocTexCoords();
dynamicMesh.AllocIndices();
Span<Vector3> vertices = dynamicMesh.VerticesAs<Vector3>();
Span<Vector2> texcoords = dynamicMesh.TexCoordsAs<Vector2>();
Span<ushort> indices = dynamicMesh.IndicesAs<ushort>();
var indices = dynamicMesh.IndicesAs<ushort>();
for (int z = 0, i = 0; z < triangleRows; z++)
{
for (int x = 0; x < triangleRows; x++, i += 6)
for (var x = 0; x < triangleRows; x++, i += 6)
{
indices[i + 0] = (ushort)(x + (z * vertexRows));
indices[i + 1] = (ushort)(indices[i] + vertexRows);
@ -47,73 +52,96 @@ public class DynamicMesh
UploadMesh(ref dynamicMesh, true);
// Allocate the texture
Image image = GenImageColor(triangleRows, triangleRows, Color.Blank);
Texture2D texture = LoadTextureFromImage(image);
Color[] pixels = new Color[texture.Width * texture.Height];
var image = GenImageColor(triangleRows, triangleRows, Color.Blank);
texture = LoadTextureFromImage(image);
pixels = new Color[texture.Width * texture.Height];
UnloadImage(image);
// Load the material
Material material = LoadMaterialDefault();
material = LoadMaterialDefault();
SetMaterialTexture(ref material, MaterialMapIndex.Diffuse, texture);
}
public void Update()
{
// Update
//----------------------------------------------------------------------------------
var time = (float)GetTime();
Random random = new(42);
var vertices = dynamicMesh.VerticesAs<Vector3>();
var texcoords = dynamicMesh.TexCoordsAs<Vector2>();
for (int z = 0, i = 0; z < vertexRows; z++)
{
for (var x = 0; x < vertexRows; x++, i++)
{
var noiseX = SmoothNoise(time + random.Next(10000));
var noiseZ = SmoothNoise(time + random.Next(10000));
vertices[i].X = x + noiseX - .5f;
vertices[i].Y = (noiseX + noiseZ) / 2;
vertices[i].Z = z + noiseZ - .5f;
texcoords[i].X = (x - noiseZ) / triangleRows;
texcoords[i].Y = (z - noiseX) / triangleRows;
}
}
UpdateMeshBuffer<Vector3>(dynamicMesh, Mesh.VboIdIndexVertices, vertices, 0);
UpdateMeshBuffer<Vector2>(dynamicMesh, Mesh.VboIdIndexTexCoords, texcoords, 0);
for (int y = 0, i = 0; y < texture.Height; y++)
{
for (var x = 0; x < texture.Width; x++, i++)
{
pixels[i] = new(32, 178, 170, 255);
pixels[i] = ColorBrightness(pixels[i], (SmoothNoise(time + random.Next(10000)) / 8) - (1 / 16f));
pixels[i] = ColorAlpha(pixels[i], (triangleRows - new Vector2(x, y).Length()) / triangleRows);
}
}
UpdateTexture(texture, pixels);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawMesh(dynamicMesh, material, Matrix4x4.Identity);
EndMode3D();
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadMaterial(material);
// Raylib.UnloadTexture(texture); <- No need to unload the texture. UnloadMaterial(Material) already unloaded it for us
UnloadMesh(dynamicMesh);
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - dynamic mesh");
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
var game = new DynamicMesh();
game.Init();
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
float time = (float)GetTime();
Random random = new(42);
for (int z = 0, i = 0; z < vertexRows; z++)
{
for (int x = 0; x < vertexRows; x++, i++)
{
float noiseX = SmoothNoise(time + random.Next(10000));
float noiseZ = SmoothNoise(time + random.Next(10000));
vertices[i].X = x + noiseX - .5f;
vertices[i].Y = (noiseX + noiseZ) / 2;
vertices[i].Z = z + noiseZ - .5f;
texcoords[i].X = (x - noiseZ) / triangleRows;
texcoords[i].Y = (z - noiseX) / triangleRows;
}
}
UpdateMeshBuffer<Vector3>(dynamicMesh, Mesh.VboIdIndexVertices, vertices, 0);
UpdateMeshBuffer<Vector2>(dynamicMesh, Mesh.VboIdIndexTexCoords, texcoords, 0);
for (int y = 0, i = 0; y < texture.Height; y++)
{
for (int x = 0; x < texture.Width; x++, i++)
{
pixels[i] = new(32, 178, 170, 255);
pixels[i] = ColorBrightness(pixels[i], (SmoothNoise(time + random.Next(10000)) / 8) - (1 / 16f));
pixels[i] = ColorAlpha(pixels[i], (triangleRows - new Vector2(x, y).Length()) / triangleRows);
}
}
UpdateTexture(texture, pixels);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawMesh(dynamicMesh, material, Matrix4x4.Identity);
EndMode3D();
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadMaterial(material);
// Raylib.UnloadTexture(texture); <- No need to unload the texture. UnloadMaterial(Material) already unloaded it for us
UnloadMesh(dynamicMesh);
CloseWindow();
//--------------------------------------------------------------------------------------

View file

@ -2,10 +2,14 @@
*
* raylib [models] example - first person maze
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example complexity rating: [] 2/4
*
* Copyright (c) 2019 Ramon Santamaria (@raysan5)
* Example originally created with raylib 2.5, last time updated with raylib 3.5
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2019-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -14,140 +18,163 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class FirstPersonMaze
public unsafe partial class FirstPersonMaze : IExample
{
public unsafe static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / First Person Maze";
public string Title => "raylib [models] example - first person maze";
public bool CursorDisabled => true;
private Camera3D camera;
private Texture2D cubicmap;
private Texture2D texture;
private Model model;
private Color* mapPixels;
private Vector3 mapPosition;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - first person maze");
// Define the camera to look into our 3d world
Camera3D camera = new();
camera.Position = new Vector3(0.2f, 0.4f, 0.2f);
camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
camera.FovY = 45.0f;
camera.Projection = CameraProjection.Perspective;
camera = new();
camera.Position = new Vector3(0.2f, 0.4f, 0.2f); // Camera position
camera.Target = new Vector3(0.185f, 0.4f, 0.0f); // Camera looking at point
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
camera.FovY = 45.0f; // Camera field-of-view Y
camera.Projection = CameraProjection.Perspective; // Camera projection type
Image imMap = LoadImage("resources/cubicmap.png");
Texture2D cubicmap = LoadTextureFromImage(imMap);
Mesh mesh = GenMeshCubicmap(imMap, new Vector3(1.0f, 1.0f, 1.0f));
Model model = LoadModelFromMesh(mesh);
var imMap = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
cubicmap = LoadTextureFromImage(imMap); // Convert image to texture to display (VRAM)
var mesh = GenMeshCubicmap(imMap, new Vector3(1.0f, 1.0f, 1.0f));
model = LoadModelFromMesh(mesh);
// NOTE: By default each cube is mapped to one part of texture atlas
Texture2D texture = LoadTexture("resources/cubicmap_atlas.png");
texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture
// Set map diffuse texture
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Albedo, ref texture);
// Get map image data to be used for collision detection
Color* mapPixels = LoadImageColors(imMap);
UnloadImage(imMap);
mapPixels = LoadImageColors(imMap);
UnloadImage(imMap); // Unload image from RAM
Vector3 mapPosition = new(-16.0f, 0.0f, -8.0f);
Vector3 playerPosition = camera.Position;
mapPosition = new(-16.0f, 0.0f, -8.0f); // Set model position
}
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
public void Update()
{
// Update
//----------------------------------------------------------------------------------
var oldCamPos = camera.Position; // Store old camera position
// Main game loop
while (!WindowShouldClose())
UpdateCamera(ref camera, CameraMode.FirstPerson);
// Check player collision (we simplify to 2D collision detection)
Vector2 playerPos = new(camera.Position.X, camera.Position.Z);
var playerRadius = 0.1f; // Collision radius (player is modelled as a cilinder for collision)
var playerCellX = (int)(playerPos.X - mapPosition.X + 0.5f);
var playerCellY = (int)(playerPos.Y - mapPosition.Z + 0.5f);
// Out-of-limits security check
if (playerCellX < 0)
{
// Update
//----------------------------------------------------------------------------------
Vector3 oldCamPos = camera.Position;
playerCellX = 0;
}
else if (playerCellX >= cubicmap.Width)
{
playerCellX = cubicmap.Width - 1;
}
UpdateCamera(ref camera, CameraMode.FirstPerson);
if (playerCellY < 0)
{
playerCellY = 0;
}
else if (playerCellY >= cubicmap.Height)
{
playerCellY = cubicmap.Height - 1;
}
// Check player collision (we simplify to 2D collision detection)
Vector2 playerPos = new(camera.Position.X, camera.Position.Z);
// Collision radius (player is modelled as a cilinder for collision)
float playerRadius = 0.1f;
int playerCellX = (int)(playerPos.X - mapPosition.X + 0.5f);
int playerCellY = (int)(playerPos.Y - mapPosition.Z + 0.5f);
// Out-of-limits security check
if (playerCellX < 0)
// Check map collisions using image data and player position against surrounding cells only
for (var y = playerCellY - 1; y <= playerCellY + 1; y++)
{
// Avoid map accessing out of bounds
if ((y >= 0) && (y < cubicmap.Height))
{
playerCellX = 0;
}
else if (playerCellX >= cubicmap.Width)
{
playerCellX = cubicmap.Width - 1;
}
if (playerCellY < 0)
{
playerCellY = 0;
}
else if (playerCellY >= cubicmap.Height)
{
playerCellY = cubicmap.Height - 1;
}
// Check map collisions using image data and player position
// TODO: Improvement: Just check player surrounding cells for collision
for (int y = 0; y < cubicmap.Height; y++)
{
for (int x = 0; x < cubicmap.Width; x++)
for (var x = playerCellX - 1; x <= playerCellX + 1; x++)
{
Color* mapPixelsData = mapPixels;
// Collision: Color.white pixel, only check R channel
Rectangle rec = new(
mapPosition.X - 0.5f + x * 1.0f,
mapPosition.Z - 0.5f + y * 1.0f,
1.0f,
1.0f
);
bool collision = CheckCollisionCircleRec(playerPos, playerRadius, rec);
if ((mapPixelsData[y * cubicmap.Width + x].R == 255) && collision)
// NOTE: Collision: Only checking R channel for white pixel
if (((x >= 0) && (x < cubicmap.Width)) &&
(mapPixels[y * cubicmap.Width + x].R == 255) &&
(CheckCollisionCircleRec(playerPos, playerRadius,
new Rectangle(mapPosition.X - 0.5f + x * 1.0f, mapPosition.Z - 0.5f + y * 1.0f, 1.0f, 1.0f))))
{
// Collision detected, reset camera position
camera.Position = oldCamPos;
}
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
// Draw maze map
BeginMode3D(camera);
DrawModel(model, mapPosition, 1.0f, Color.White);
EndMode3D();
DrawTextureEx(cubicmap, new Vector2(GetScreenWidth() - cubicmap.Width * 4 - 20, 20), 0.0f, 4.0f, Color.White);
DrawRectangleLines(GetScreenWidth() - cubicmap.Width * 4 - 20, 20, cubicmap.Width * 4, cubicmap.Height * 4, Color.Green);
// Draw player position radar
DrawRectangle(GetScreenWidth() - cubicmap.Width * 4 - 20 + playerCellX * 4, 20 + playerCellY * 4, 4, 4, Color.Red);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModel(model, mapPosition, 1.0f, Color.White); // Draw maze map
EndMode3D();
DrawTextureEx(cubicmap, new Vector2(GetScreenWidth() - cubicmap.Width * 4 - 20, 20), 0.0f, 4.0f, Color.White);
DrawRectangleLines(GetScreenWidth() - cubicmap.Width * 4 - 20, 20, cubicmap.Width * 4, cubicmap.Height * 4, Color.Green);
// Draw player position radar
DrawRectangle(GetScreenWidth() - cubicmap.Width * 4 - 20 + playerCellX * 4, 20 + playerCellY * 4, 4, 4, Color.Red);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadImageColors(mapPixels); // Unload color array
UnloadTexture(cubicmap); // Unload cubicmap texture
UnloadTexture(texture); // Unload map texture
UnloadModel(model); // Unload map model
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - first person maze");
DisableCursor(); // Limit cursor to relative movement inside the window
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new FirstPersonMaze();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadImageColors(mapPixels);
UnloadTexture(cubicmap);
UnloadTexture(texture);
UnloadModel(model);
CloseWindow();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;

View file

@ -1,11 +1,15 @@
/*******************************************************************************************
*
* raylib [models] example - Draw some basic geometric shapes (cube, sphere, cylinder...)
* raylib [models] example - geometric shapes
*
* This example has been created using raylib 1.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example complexity rating: [] 1/4
*
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
* Example originally created with raylib 1.0, last time updated with raylib 3.5
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -14,70 +18,91 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class GeometricShapes
public partial class GeometricShapes : IExample
{
public static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Geometric Shapes";
public string Title => "raylib [models] example - geometric shapes";
private Camera3D camera;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - geometric shapes");
// Define the camera to look into our 3d world
Camera3D camera = new();
camera = new();
camera.Position = new Vector3(0.0f, 10.0f, 10.0f);
camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
camera.FovY = 45.0f;
camera.Projection = CameraProjection.Perspective;
}
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
public void Update()
{
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawCube(new Vector3(-4.0f, 0.0f, 2.0f), 2.0f, 5.0f, 2.0f, Color.Red);
DrawCubeWires(new Vector3(-4.0f, 0.0f, 2.0f), 2.0f, 5.0f, 2.0f, Color.Gold);
DrawCubeWires(new Vector3(-4.0f, 0.0f, -2.0f), 3.0f, 6.0f, 2.0f, Color.Maroon);
DrawSphere(new Vector3(-1.0f, 0.0f, -2.0f), 1.0f, Color.Green);
DrawSphereWires(new Vector3(1.0f, 0.0f, 2.0f), 2.0f, 16, 16, Color.Lime);
DrawCylinder(new Vector3(4.0f, 0.0f, -2.0f), 1.0f, 2.0f, 3.0f, 4, Color.SkyBlue);
DrawCylinderWires(new Vector3(4.0f, 0.0f, -2.0f), 1.0f, 2.0f, 3.0f, 4, Color.DarkBlue);
DrawCylinderWires(new Vector3(4.5f, -1.0f, 2.0f), 1.0f, 1.0f, 2.0f, 6, Color.Brown);
DrawCylinder(new Vector3(1.0f, 0.0f, -4.0f), 0.0f, 1.5f, 3.0f, 8, Color.Gold);
DrawCylinderWires(new Vector3(1.0f, 0.0f, -4.0f), 0.0f, 1.5f, 3.0f, 8, Color.Pink);
DrawCapsule(new Vector3(-3.0f, 1.5f, -4.0f), new Vector3(-4.0f, -1.0f, -4.0f), 1.2f, 8, 8, Color.Violet);
DrawCapsuleWires(new Vector3(-3.0f, 1.5f, -4.0f), new Vector3(-4.0f, -1.0f, -4.0f), 1.2f, 8, 8, Color.Purple);
DrawGrid(10, 1.0f); // Draw a grid
EndMode3D();
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - geometric shapes");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new GeometricShapes();
game.Init();
// Main game loop
while (!WindowShouldClose())
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawCube(new Vector3(-4.0f, 0.0f, 2.0f), 2.0f, 5.0f, 2.0f, Color.Red);
DrawCubeWires(new Vector3(-4.0f, 0.0f, 2.0f), 2.0f, 5.0f, 2.0f, Color.Gold);
DrawCubeWires(new Vector3(-4.0f, 0.0f, -2.0f), 3.0f, 6.0f, 2.0f, Color.Maroon);
DrawSphere(new Vector3(-1.0f, 0.0f, -2.0f), 1.0f, Color.Green);
DrawSphereWires(new Vector3(1.0f, 0.0f, 2.0f), 2.0f, 16, 16, Color.Lime);
DrawCylinder(new Vector3(4.0f, 0.0f, -2.0f), 1.0f, 2.0f, 3.0f, 4, Color.SkyBlue);
DrawCylinderWires(new Vector3(4.0f, 0.0f, -2.0f), 1.0f, 2.0f, 3.0f, 4, Color.DarkBlue);
DrawCylinderWires(new Vector3(4.5f, -1.0f, 2.0f), 1.0f, 1.0f, 2.0f, 6, Color.Brown);
DrawCylinder(new Vector3(1.0f, 0.0f, -4.0f), 0.0f, 1.5f, 3.0f, 8, Color.Gold);
DrawCylinderWires(new Vector3(1.0f, 0.0f, -4.0f), 0.0f, 1.5f, 3.0f, 8, Color.Pink);
DrawGrid(10, 1.0f);
EndMode3D();
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;

View file

@ -1,11 +1,15 @@
/*******************************************************************************************
*
* raylib [models] example - Heightmap loading and drawing
* raylib [models] example - heightmap rendering
*
* This example has been created using raylib 1.8 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example complexity rating: [] 1/4
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
* Example originally created with raylib 1.8, last time updated with raylib 3.5
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -14,77 +18,102 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class HeightmapDemo
public partial class HeightmapDemo : IExample
{
public static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Heightmap Demo";
public string Title => "raylib [models] example - heightmap rendering";
private Camera3D camera;
private Texture2D texture;
private Model model;
private Vector3 mapPosition;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - heightmap loading and drawing");
// Define our custom camera to look into our 3d world
Camera3D camera = new();
camera.Position = new Vector3(18.0f, 16.0f, 18.0f);
camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
camera.FovY = 45.0f;
camera.Projection = CameraProjection.Perspective;
camera = new();
camera.Position = new Vector3(18.0f, 21.0f, 18.0f); // Camera position
camera.Target = new Vector3(0.0f, 0.0f, 0.0f); // Camera looking at point
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
camera.FovY = 45.0f; // Camera field-of-view Y
camera.Projection = CameraProjection.Perspective; // Camera projection type
Image image = LoadImage("resources/heightmap.png");
Texture2D texture = LoadTextureFromImage(image);
var image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM)
texture = LoadTextureFromImage(image); // Convert image to texture (VRAM)
Mesh mesh = GenMeshHeightmap(image, new Vector3(16, 8, 16));
Model model = LoadModelFromMesh(mesh);
var mesh = GenMeshHeightmap(image, new Vector3(16, 8, 16)); // Generate heightmap mesh (RAM and VRAM)
model = LoadModelFromMesh(mesh); // Load model from generated mesh
// Set map diffuse texture
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Albedo, ref texture);
Vector3 mapPosition = new(-8.0f, 0.0f, -8.0f);
mapPosition = new(-8.0f, 0.0f, -8.0f); // Define model position
UnloadImage(image);
UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM
}
SetTargetFPS(60);
public void Update()
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Orbital);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModel(model, mapPosition, 1.0f, Color.Red);
DrawGrid(20, 1.0f);
EndMode3D();
DrawTexture(texture, screenWidth - texture.Width - 20, 20, Color.White);
DrawRectangleLines(screenWidth - texture.Width - 20, 20, texture.Width, texture.Height, Color.Green);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadTexture(texture); // Unload texture
UnloadModel(model); // Unload model
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - heightmap rendering");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new HeightmapDemo();
game.Init();
// Main game loop
while (!WindowShouldClose())
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Orbital);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModel(model, mapPosition, 1.0f, Color.Red);
DrawGrid(20, 1.0f);
EndMode3D();
DrawTexture(texture, screenWidth - texture.Width - 20, 20, Color.White);
DrawRectangleLines(screenWidth - texture.Width - 20, 20, texture.Width, texture.Height, Color.Green);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture);
UnloadModel(model);
CloseWindow();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;

View file

@ -25,93 +25,122 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class LoadingGltf
public partial class LoadingGltf : IExample
{
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Loading GLTF";
public string Title => "raylib [models] example - loading gltf";
private Camera3D camera;
private Model model;
private Vector3 position;
private unsafe ModelAnimation* anims;
private int animCount;
private int animIndex;
private float animCurrentFrame;
public unsafe void Init()
{
// Define the camera to look into our 3d world
camera = new();
camera.Position = new Vector3(6.0f, 6.0f, 6.0f); // Camera position
camera.Target = new Vector3(0.0f, 2.0f, 0.0f); // Camera looking at point
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
camera.FovY = 45.0f; // Camera field-of-view Y
camera.Projection = CameraProjection.Perspective; // Camera projection type
// Load model
model = LoadModel("resources/models/gltf/robot.glb");
position = new(0.0f, 0.0f, 0.0f); // Set model world position
// Load model animations
anims = LoadModelAnimations("resources/models/gltf/robot.glb", ref animCount);
// Animation playing variables
animIndex = 0; // Current animation playing
animCurrentFrame = 0.0f; // Current animation frame
}
public unsafe void Update()
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Orbital);
// Select current animation
if (IsKeyPressed(KeyboardKey.Right))
{
animIndex = (animIndex + 1) % animCount;
}
else if (IsKeyPressed(KeyboardKey.Left))
{
animIndex = (animIndex + animCount - 1) % animCount;
}
// Update model animation
animCurrentFrame = (animCurrentFrame + 1) % anims[animIndex].KeyFrameCount;
UpdateModelAnimation(model, anims[animIndex], (float)animCurrentFrame);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModel(
model,
position,
1f,
Color.White
);
DrawGrid(10, 1.0f);
EndMode3D();
DrawText($"Current animation: {anims[animIndex].NameToString()}", 10, 40, 20, Color.Maroon);
DrawText("Use the LEFT/RIGHT keys to switch animation", 10, 10, 20, Color.Gray);
EndDrawing();
//----------------------------------------------------------------------------------
}
public unsafe void Unload()
{
UnloadModelAnimations(anims, animCount); // Unload model animations data
UnloadModel(model); // Unload model
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - loading gltf");
// Define the camera to look into our 3d world
Camera3D camera = new();
camera.Position = new Vector3(6.0f, 6.0f, 6.0f);
camera.Target = new Vector3(0.0f, 2.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
camera.FovY = 45.0f;
camera.Projection = CameraProjection.Perspective;
Model model = LoadModel("resources/models/gltf/robot.glb");
Vector3 position = new(0.0f, 0.0f, 0.0f);
// Load animation data
var anims = LoadModelAnimations("resources/models/gltf/robot.glb");
// Animation playing variables
int animIndex = 0;
float animCurrentFrame = 0.0f;
SetTargetFPS(60);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new LoadingGltf();
game.Init();
// Main game loop
while (!WindowShouldClose())
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Orbital);
if (IsKeyPressed(KeyboardKey.Right))
{
animIndex = (animIndex + 1) % anims.Length;
}
else if (IsKeyPressed(KeyboardKey.Left))
{
animIndex = (animIndex + anims.Length - 1) % anims.Length;
}
// Update model animation
animCurrentFrame = (animCurrentFrame + 1) % anims[animIndex].KeyFrameCount;
UpdateModelAnimation(model, anims[animIndex], (float)animCurrentFrame);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModel(
model,
position,
1f,
Color.White
);
DrawGrid(10, 1.0f);
EndMode3D();
DrawText($"Current animation: {anims[animIndex].NameToString()}", 10, 40, 20, Color.Maroon);
DrawText("Use the LEFT/RIGHT keys to switch animation", 10, 10, 20, Color.Gray);
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadModelAnimations(anims);
UnloadModel(model);
CloseWindow();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
}

View file

@ -25,93 +25,121 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class LoadingIqm
public partial class LoadingIqm : IExample
{
public unsafe static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Loading IQM";
public string Title => "raylib [models] example - loading iqm";
private Camera3D camera;
private Model model;
private Texture2D texture;
private Vector3 position;
private unsafe ModelAnimation* anims;
private int animCount;
private int animIndex;
private float animCurrentFrame;
public unsafe void Init()
{
// Define the camera to look into our 3d world
camera = new();
camera.Position = new Vector3(10.0f, 10.0f, 10.0f); // Camera position
camera.Target = new Vector3(0.0f, 4.0f, 0.0f); // Camera looking at point
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
camera.FovY = 45.0f; // Camera field-of-view Y
camera.Projection = CameraProjection.Perspective; // Camera mode type
model = LoadModel("resources/models/iqm/guy.iqm"); // Load the animated model mesh and basic data
texture = LoadTexture("resources/models/iqm/guytex.png"); // Load model texture and set material
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Diffuse, ref texture); // Set model material map texture
position = new(0.0f, 0.0f, 0.0f); // Set model position
// Load animation data
anims = LoadModelAnimations("resources/models/iqm/guyanim.iqm", ref animCount);
// Animation playing variables
animIndex = 0; // Current animation playing
animCurrentFrame = 0.0f; // Current animation frame (supporting interpolated frames)
}
public unsafe void Update()
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Orbital);
// Play animation when spacebar is held down
animCurrentFrame += 1.0f;
UpdateModelAnimation(model, anims[0], animCurrentFrame);
if (animCurrentFrame >= anims[0].KeyFrameCount)
{
animCurrentFrame = 0;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModelEx(
model,
position,
new Vector3(1.0f, 0.0f, 0.0f),
-90.0f,
new Vector3(1.0f, 1.0f, 1.0f),
Color.White
);
DrawGrid(10, 1.0f);
EndMode3D();
DrawText($"Current animation: {anims[animIndex].NameToString()}", 10, 10, 20, Color.Maroon);
DrawText("(c) Guy IQM 3D model by @culacant", screenWidth - 200, screenHeight - 20, 10, Color.Gray);
EndDrawing();
//----------------------------------------------------------------------------------
}
public unsafe void Unload()
{
UnloadTexture(texture); // Unload texture
UnloadModelAnimations(anims, animCount); // Unload model animations data
UnloadModel(model); // Unload model
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - loading iqm");
InitWindow(screenWidth, screenHeight, "raylib [models] example - model animation");
// Define the camera to look into our 3d world
Camera3D camera = new();
camera.Position = new Vector3(10.0f, 10.0f, 10.0f);
camera.Target = new Vector3(0.0f, 4.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
camera.FovY = 45.0f;
camera.Projection = CameraProjection.Perspective;
Model model = LoadModel("resources/models/iqm/guy.iqm");
Texture2D texture = LoadTexture("resources/models/iqm/guytex.png");
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Diffuse, ref texture);
Vector3 position = new(0.0f, 0.0f, 0.0f);
// Load animation data
var anims = LoadModelAnimations("resources/models/iqm/guyanim.iqm");
// Animation playing variables
int animIndex = 0;
float animCurrentFrame = 0.0f;
SetTargetFPS(60);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new LoadingIqm();
game.Init();
// Main game loop
while (!WindowShouldClose())
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Orbital);
// Play animation when spacebar is held down
animCurrentFrame += 1.0f;
UpdateModelAnimation(model, anims[0], animCurrentFrame);
if (animCurrentFrame >= anims[0].KeyFrameCount)
{
animCurrentFrame = 0;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModelEx(
model,
position,
new Vector3(1.0f, 0.0f, 0.0f),
-90.0f,
new Vector3(1.0f, 1.0f, 1.0f),
Color.White
);
DrawGrid(10, 1.0f);
EndMode3D();
DrawText($"Current animation: {anims[animIndex].NameToString()}", 10, 10, 20, Color.Maroon);
DrawText("(c) Guy IQM 3D model by @culacant", screenWidth - 200, screenHeight - 20, 10, Color.Gray);
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture);
UnloadModelAnimations(anims);
UnloadModel(model);
CloseWindow();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
}

View file

@ -3,19 +3,24 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class MeshDemo
public partial class MeshDemo : IExample
{
public unsafe static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Mesh Demo";
public string Title => "raylib [models] example - mesh demo";
private Camera3D camera;
private Model model;
private Texture2D texture;
private float rotationAngle;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh demo");
// Define the camera to look into our 3d world
Camera3D camera = new();
camera = new();
camera.Position = Vector3.One * 1.5f;
camera.Target = Vector3.Zero;
camera.Up = Vector3.UnitY;
@ -28,10 +33,10 @@ public class MeshDemo
tetrahedron.AllocTexCoords();
tetrahedron.AllocColors();
tetrahedron.AllocIndices();
Span<Vector3> vertices = tetrahedron.VerticesAs<Vector3>();
Span<Vector2> texcoords = tetrahedron.TexCoordsAs<Vector2>();
Span<Color> colors = tetrahedron.ColorsAs<Color>();
Span<ushort> indices = tetrahedron.IndicesAs<ushort>();
var vertices = tetrahedron.VerticesAs<Vector3>();
var texcoords = tetrahedron.TexCoordsAs<Vector2>();
var colors = tetrahedron.ColorsAs<Color>();
var indices = tetrahedron.IndicesAs<ushort>();
// Coordinates for a regular tetrahedron
vertices[0] = new(MathF.Sqrt(8f / 9f), 0f, -1f / 3f);
@ -65,49 +70,69 @@ public class MeshDemo
indices[10] = 3;
indices[11] = 2;
float rotationAngle = 0f;
rotationAngle = 0f;
Raylib.UploadMesh(ref tetrahedron, false);
Model model = Raylib.LoadModelFromMesh(tetrahedron);
model = Raylib.LoadModelFromMesh(tetrahedron);
Image image = Raylib.GenImagePerlinNoise(16, 16, 0, 0, 1000f);
var image = Raylib.GenImagePerlinNoise(16, 16, 0, 0, 1000f);
Raylib.ImageBlurGaussian(ref image, 2);
Raylib.ImageColorBrightness(ref image, 100);
Raylib.ImageDither(ref image, 4, 4, 4, 4);
Texture2D texture = Raylib.LoadTextureFromImage(image);
texture = Raylib.LoadTextureFromImage(image);
Raylib.UnloadImage(image);
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Diffuse, ref texture);
}
public void Update()
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Free);
rotationAngle = Raymath.Wrap(rotationAngle += 1f, 0f, 360f);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
Raylib.DrawModelEx(model, Vector3.Zero, Vector3.UnitX, rotationAngle, Vector3.One, Color.White);
EndMode3D();
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadTexture(texture);
UnloadModel(model);
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh demo");
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
var game = new MeshDemo();
game.Init();
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Free);
rotationAngle = Raymath.Wrap(rotationAngle += 1f, 0f, 360f);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
Raylib.DrawModelEx(model, Vector3.Zero, Vector3.UnitX, rotationAngle, Vector3.One, Color.White);
EndMode3D();
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture);
UnloadModel(model);
CloseWindow();
//--------------------------------------------------------------------------------------

View file

@ -1,11 +1,15 @@
/*******************************************************************************************
*
* raylib example - procedural mesh generation
* raylib [models] example - mesh generation
*
* This example has been created using raylib 1.8 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example complexity rating: [] 2/4
*
* Copyright (c) 2017 Ramon Santamaria (Ray San)
* Example originally created with raylib 1.8, last time updated with raylib 4.0
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2017-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -14,25 +18,31 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class MeshGeneration
public partial class MeshGeneration : IExample
{
public static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Mesh Generation";
public string Title => "raylib [models] example - mesh generation";
private Texture2D texture;
private Model[] models;
private Camera3D camera;
private Vector3 position;
private int currentModel;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation");
// We generate a isChecked image for texturing
Image isChecked = GenImageChecked(2, 2, 1, 1, Color.Red, Color.Green);
Texture2D texture = LoadTextureFromImage(isChecked);
// We generate a checked image for texturing
var isChecked = GenImageChecked(2, 2, 1, 1, Color.Red, Color.Green);
texture = LoadTextureFromImage(isChecked);
UnloadImage(isChecked);
Model[] models = new Model[9];
models = new Model[9];
models[0] = LoadModelFromMesh(GenMeshPlane(2, 2, 5, 5));
models[0] = LoadModelFromMesh(GenMeshPlane(2, 2, 4, 3));
models[1] = LoadModelFromMesh(GenMeshCube(2.0f, 1.0f, 2.0f));
models[2] = LoadModelFromMesh(GenMeshSphere(2, 32, 32));
models[3] = LoadModelFromMesh(GenMeshHemiSphere(2, 16, 16));
@ -42,15 +52,17 @@ public class MeshGeneration
models[7] = LoadModelFromMesh(GenMeshPoly(5, 2.0f));
models[8] = LoadModelFromMesh(GenMeshCustom());
// Set isChecked texture as default diffuse component for all models material
for (int i = 0; i < models.Length; i++)
// NOTE: Generated meshes could be exported using ExportMesh()
// Set checked texture as default diffuse component for all models material
for (var i = 0; i < models.Length; i++)
{
// Set map diffuse texture
Raylib.SetMaterialTexture(ref models[i], 0, MaterialMapIndex.Albedo, ref texture);
}
// Define the camera to look into our 3d world
Camera3D camera = new();
camera = new();
camera.Position = new Vector3(5.0f, 5.0f, 5.0f);
camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
@ -58,92 +70,102 @@ public class MeshGeneration
camera.Projection = CameraProjection.Perspective;
// Model drawing position
Vector3 position = new(0.0f, 0.0f, 0.0f);
position = new(0.0f, 0.0f, 0.0f);
int currentModel = 0;
currentModel = 0;
}
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
public void Update()
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Orbital);
// Main game loop
while (!WindowShouldClose())
if (IsMouseButtonPressed(MouseButton.Left))
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Orbital);
if (IsMouseButtonPressed(MouseButton.Left))
{
// Cycle between the textures
currentModel = (currentModel + 1) % models.Length;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModel(models[currentModel], position, 1.0f, Color.White);
DrawGrid(10, 1.0f);
EndMode3D();
DrawRectangle(30, 400, 310, 30, ColorAlpha(Color.SkyBlue, 0.5f));
DrawRectangleLines(30, 400, 310, 30, ColorAlpha(Color.DarkBlue, 0.5f));
DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL MODELS", 40, 410, 10, Color.Blue);
switch (currentModel)
{
case 0:
DrawText("PLANE", 680, 10, 20, Color.DarkBlue);
break;
case 1:
DrawText("CUBE", 680, 10, 20, Color.DarkBlue);
break;
case 2:
DrawText("SPHERE", 680, 10, 20, Color.DarkBlue);
break;
case 3:
DrawText("HEMISPHERE", 640, 10, 20, Color.DarkBlue);
break;
case 4:
DrawText("CYLINDER", 680, 10, 20, Color.DarkBlue);
break;
case 5:
DrawText("TORUS", 680, 10, 20, Color.DarkBlue);
break;
case 6:
DrawText("KNOT", 680, 10, 20, Color.DarkBlue);
break;
case 7:
DrawText("POLY", 680, 10, 20, Color.DarkBlue);
break;
case 8:
DrawText("Custom (triagnle)", 580, 10, 20, Color.DarkBlue);
break;
default:
break;
}
EndDrawing();
//----------------------------------------------------------------------------------
currentModel = (currentModel + 1) % models.Length; // Cycle between the textures
}
// De-Initialization
//--------------------------------------------------------------------------------------
for (int i = 0; i < models.Length; i++)
if (IsKeyPressed(KeyboardKey.Right))
{
currentModel++;
if (currentModel >= models.Length)
{
currentModel = 0;
}
}
else if (IsKeyPressed(KeyboardKey.Left))
{
currentModel--;
if (currentModel < 0)
{
currentModel = models.Length - 1;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModel(models[currentModel], position, 1.0f, Color.White);
DrawGrid(10, 1.0f);
EndMode3D();
DrawRectangle(30, 400, 310, 30, Fade(Color.SkyBlue, 0.5f));
DrawRectangleLines(30, 400, 310, 30, Fade(Color.DarkBlue, 0.5f));
DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL MODELS", 40, 410, 10, Color.Blue);
switch (currentModel)
{
case 0:
DrawText("PLANE", 680, 10, 20, Color.DarkBlue);
break;
case 1:
DrawText("CUBE", 680, 10, 20, Color.DarkBlue);
break;
case 2:
DrawText("SPHERE", 680, 10, 20, Color.DarkBlue);
break;
case 3:
DrawText("HEMISPHERE", 640, 10, 20, Color.DarkBlue);
break;
case 4:
DrawText("CYLINDER", 680, 10, 20, Color.DarkBlue);
break;
case 5:
DrawText("TORUS", 680, 10, 20, Color.DarkBlue);
break;
case 6:
DrawText("KNOT", 680, 10, 20, Color.DarkBlue);
break;
case 7:
DrawText("POLY", 680, 10, 20, Color.DarkBlue);
break;
case 8:
DrawText("Custom (triangle)", 580, 10, 20, Color.DarkBlue);
break;
default:
break;
}
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadTexture(texture); // Unload texture
// Unload models data (GPU VRAM)
for (var i = 0; i < models.Length; i++)
{
UnloadModel(models[i]);
}
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
// Generate a simple triangle mesh from code
@ -153,9 +175,9 @@ public class MeshGeneration
mesh.AllocVertices();
mesh.AllocTexCoords();
mesh.AllocNormals();
Span<Vector3> vertices = mesh.VerticesAs<Vector3>();
Span<Vector2> texcoords = mesh.TexCoordsAs<Vector2>();
Span<Vector3> normals = mesh.NormalsAs<Vector3>();
var vertices = mesh.VerticesAs<Vector3>();
var texcoords = mesh.TexCoordsAs<Vector2>();
var normals = mesh.NormalsAs<Vector3>();
// Vertex at (0, 0, 0)
vertices[0] = new(0, 0, 0);
@ -177,4 +199,32 @@ public class MeshGeneration
return mesh;
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new MeshGeneration();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
}

View file

@ -1,12 +1,17 @@
/*******************************************************************************************
*
* raylib [models] example - Mesh picking in 3d mode, ground plane, triangle, mesh
* raylib [models] example - mesh picking
*
* This example has been created using raylib 1.7 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example complexity rating: [] 3/4
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
* Example contributed by Joel Davis (@joeld42)
* Example originally created with raylib 1.7, last time updated with raylib 4.0
*
* Example contributed by Joel Davis (@joeld42) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2017-2025 Joel Davis (@joeld42) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -16,19 +21,38 @@ using static Raylib_cs.Raymath;
namespace Examples.Models;
public class MeshPicking
public partial class MeshPicking : IExample
{
public unsafe static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Mesh Picking";
public string Title => "raylib [models] example - mesh picking";
public bool CursorDisabled => true;
private Camera3D camera;
private Ray ray;
private Model tower;
private Texture2D texture;
private Vector3 towerPos;
private BoundingBox towerBBox;
private Vector3 g0;
private Vector3 g1;
private Vector3 g2;
private Vector3 g3;
private Vector3 ta;
private Vector3 tb;
private Vector3 tc;
private Vector3 bary;
private Vector3 sp;
private float sr;
public unsafe void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh picking");
// Define the camera to look into our 3d world
Camera3D camera;
camera = new();
camera.Position = new Vector3(20.0f, 20.0f, 20.0f);
camera.Target = new Vector3(0.0f, 8.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.6f, 0.0f);
@ -36,210 +60,232 @@ public class MeshPicking
camera.Projection = CameraProjection.Perspective;
// Picking ray
Ray ray = new();
ray = new();
Model tower = LoadModel("resources/models/obj/turret.obj");
Texture2D texture = LoadTexture("resources/models/obj/turret_diffuse.png");
tower = LoadModel("resources/models/obj/turret.obj");
texture = LoadTexture("resources/models/obj/turret_diffuse.png");
Raylib.SetMaterialTexture(ref tower, 0, MaterialMapIndex.Albedo, ref texture);
Vector3 towerPos = new(0.0f, 0.0f, 0.0f);
BoundingBox towerBBox = GetMeshBoundingBox(tower.Meshes[0]);
towerPos = new(0.0f, 0.0f, 0.0f);
towerBBox = GetMeshBoundingBox(tower.Meshes[0]);
// Ground quad
Vector3 g0 = new(-50.0f, 0.0f, -50.0f);
Vector3 g1 = new(-50.0f, 0.0f, 50.0f);
Vector3 g2 = new(50.0f, 0.0f, 50.0f);
Vector3 g3 = new(50.0f, 0.0f, -50.0f);
g0 = new(-50.0f, 0.0f, -50.0f);
g1 = new(-50.0f, 0.0f, 50.0f);
g2 = new(50.0f, 0.0f, 50.0f);
g3 = new(50.0f, 0.0f, -50.0f);
// Test triangle
Vector3 ta = new(-25.0f, 0.5f, 0.0f);
Vector3 tb = new(-4.0f, 2.5f, 1.0f);
Vector3 tc = new(-8.0f, 6.5f, 0.0f);
ta = new(-25.0f, 0.5f, 0.0f);
tb = new(-4.0f, 2.5f, 1.0f);
tc = new(-8.0f, 6.5f, 0.0f);
Vector3 bary = new(0.0f, 0.0f, 0.0f);
bary = new(0.0f, 0.0f, 0.0f);
// Test sphere
Vector3 sp = new(-30.0f, 5.0f, 5.0f);
float sr = 4.0f;
sp = new(-30.0f, 5.0f, 5.0f);
sr = 4.0f;
}
public unsafe void Update()
{
//----------------------------------------------------------------------------------
// Update
//----------------------------------------------------------------------------------
if (IsCursorHidden())
{
UpdateCamera(ref camera, CameraMode.FirstPerson);
}
// Toggle camera controls
if (IsMouseButtonPressed(MouseButton.Right))
{
if (IsCursorHidden())
{
EnableCursor();
}
else
{
DisableCursor();
}
}
// Display information about closest hit
RayCollision collision = new();
var hitObjectName = "None";
collision.Distance = float.MaxValue;
collision.Hit = false;
var cursorColor = Color.White;
// Get ray and test against objects
ray = GetScreenToWorldRay(GetMousePosition(), camera);
// Check ray collision against ground quad
var groundHitInfo = GetRayCollisionQuad(ray, g0, g1, g2, g3);
if (groundHitInfo.Hit && (groundHitInfo.Distance < collision.Distance))
{
collision = groundHitInfo;
cursorColor = Color.Green;
hitObjectName = "Ground";
}
// Check ray collision against test triangle
var triHitInfo = GetRayCollisionTriangle(ray, ta, tb, tc);
if (triHitInfo.Hit && (triHitInfo.Distance < collision.Distance))
{
collision = triHitInfo;
cursorColor = Color.Purple;
hitObjectName = "Triangle";
bary = Vector3Barycenter(collision.Point, ta, tb, tc);
}
// Check ray collision against test sphere
var sphereHitInfo = GetRayCollisionSphere(ray, sp, sr);
if ((sphereHitInfo.Hit) && (sphereHitInfo.Distance < collision.Distance))
{
collision = sphereHitInfo;
cursorColor = Color.Orange;
hitObjectName = "Sphere";
}
// Check ray collision against bounding box first, before trying the full ray-mesh test
var boxHitInfo = GetRayCollisionBox(ray, towerBBox);
if (boxHitInfo.Hit && boxHitInfo.Distance < collision.Distance)
{
collision = boxHitInfo;
cursorColor = Color.Orange;
hitObjectName = "Box";
// Check ray collision against model meshes
RayCollision meshHitInfo = new();
for (var m = 0; m < tower.MeshCount; m++)
{
// NOTE: We consider the model.Transform for the collision check but
// it can be checked against any transform matrix, used when checking against same
// model drawn multiple times with multiple transforms
meshHitInfo = GetRayCollisionMesh(ray, tower.Meshes[m], tower.Transform);
if (meshHitInfo.Hit)
{
// Save the closest hit mesh
if ((!collision.Hit) || (collision.Distance > meshHitInfo.Distance))
{
collision = meshHitInfo;
}
break;
}
}
if (meshHitInfo.Hit)
{
collision = meshHitInfo;
cursorColor = Color.Orange;
hitObjectName = "Mesh";
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
// Draw the tower
// WARNING: If scale is different than 1.0f,
// not considered by GetRayCollisionModel()
DrawModel(tower, towerPos, 1.0f, Color.White);
// Draw the test triangle
DrawLine3D(ta, tb, Color.Purple);
DrawLine3D(tb, tc, Color.Purple);
DrawLine3D(tc, ta, Color.Purple);
// Draw the test sphere
DrawSphereWires(sp, sr, 8, 8, Color.Purple);
// Draw the mesh bbox if we hit it
if (boxHitInfo.Hit)
{
DrawBoundingBox(towerBBox, Color.Lime);
}
// If we hit something, draw the cursor at the hit point
if (collision.Hit)
{
DrawCube(collision.Point, 0.3f, 0.3f, 0.3f, cursorColor);
DrawCubeWires(collision.Point, 0.3f, 0.3f, 0.3f, Color.Red);
var normalEnd = collision.Point + collision.Normal;
DrawLine3D(collision.Point, normalEnd, Color.Red);
}
DrawRay(ray, Color.Maroon);
DrawGrid(10, 10.0f);
EndMode3D();
// Draw some debug GUI text
DrawText($"Hit Object: {hitObjectName}", 10, 50, 10, Color.Black);
if (collision.Hit)
{
var ypos = 70;
DrawText($"Distance: {collision.Distance}", 10, ypos, 10, Color.Black);
DrawText($"Hit Pos: {collision.Point}", 10, ypos + 15, 10, Color.Black);
DrawText($"Hit Norm: {collision.Normal}", 10, ypos + 30, 10, Color.Black);
if (triHitInfo.Hit && hitObjectName == "Triangle")
{
DrawText($"Barycenter: {bary}", 10, ypos + 45, 10, Color.Black);
}
}
DrawText("Right click mouse to toggle camera controls", 10, 430, 10, Color.Gray);
DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, Color.Gray);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadModel(tower);
UnloadTexture(texture);
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh picking");
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
var game = new MeshPicking();
game.Init();
//----------------------------------------------------------------------------------
// Main game loop
//--------------------------------------------------------------------------------------
while (!WindowShouldClose())
{
//----------------------------------------------------------------------------------
// Update
//----------------------------------------------------------------------------------
if (IsCursorHidden())
{
UpdateCamera(ref camera, CameraMode.FirstPerson);
}
// Toggle camera controls
if (IsMouseButtonPressed(MouseButton.Right))
{
if (IsCursorHidden())
{
EnableCursor();
}
else
{
DisableCursor();
}
}
// Display information about closest hit
RayCollision collision = new();
string hitObjectName = "None";
collision.Distance = float.MaxValue;
collision.Hit = false;
Color cursorColor = Color.White;
// Get ray and test against objects
ray = GetScreenToWorldRay(GetMousePosition(), camera);
// Check ray collision aginst ground quad
RayCollision groundHitInfo = GetRayCollisionQuad(ray, g0, g1, g2, g3);
if (groundHitInfo.Hit && (groundHitInfo.Distance < collision.Distance))
{
collision = groundHitInfo;
cursorColor = Color.Green;
hitObjectName = "Ground";
}
// Check ray collision against test triangle
RayCollision triHitInfo = GetRayCollisionTriangle(ray, ta, tb, tc);
if (triHitInfo.Hit && (triHitInfo.Distance < collision.Distance))
{
collision = triHitInfo;
cursorColor = Color.Purple;
hitObjectName = "Triangle";
bary = Vector3Barycenter(collision.Point, ta, tb, tc);
}
// Check ray collision against test sphere
RayCollision sphereHitInfo = GetRayCollisionSphere(ray, sp, sr);
if ((sphereHitInfo.Hit) && (sphereHitInfo.Distance < collision.Distance))
{
collision = sphereHitInfo;
cursorColor = Color.Orange;
hitObjectName = "Sphere";
}
// Check ray collision against bounding box first, before trying the full ray-mesh test
RayCollision boxHitInfo = GetRayCollisionBox(ray, towerBBox);
if (boxHitInfo.Hit && boxHitInfo.Distance < collision.Distance)
{
collision = boxHitInfo;
cursorColor = Color.Orange;
hitObjectName = "Box";
// Check ray collision against model meshes
RayCollision meshHitInfo = new();
for (int m = 0; m < tower.MeshCount; m++)
{
// NOTE: We consider the model.Transform for the collision check but
// it can be checked against any transform matrix, used when checking against same
// model drawn multiple times with multiple transforms
meshHitInfo = GetRayCollisionMesh(ray, tower.Meshes[m], tower.Transform);
if (meshHitInfo.Hit)
{
// Save the closest hit mesh
if ((!collision.Hit) || (collision.Distance > meshHitInfo.Distance))
{
collision = meshHitInfo;
}
break;
}
}
if (meshHitInfo.Hit)
{
collision = meshHitInfo;
cursorColor = Color.Orange;
hitObjectName = "Mesh";
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
// Draw the tower
DrawModel(tower, towerPos, 1.0f, Color.White);
// Draw the test triangle
DrawLine3D(ta, tb, Color.Purple);
DrawLine3D(tb, tc, Color.Purple);
DrawLine3D(tc, ta, Color.Purple);
// Draw the test sphere
DrawSphereWires(sp, sr, 8, 8, Color.Purple);
// Draw the mesh bbox if we hit it
if (boxHitInfo.Hit)
{
DrawBoundingBox(towerBBox, Color.Lime);
}
// If we hit something, draw the cursor at the hit point
if (collision.Hit)
{
DrawCube(collision.Point, 0.3f, 0.3f, 0.3f, cursorColor);
DrawCubeWires(collision.Point, 0.3f, 0.3f, 0.3f, Color.Red);
Vector3 normalEnd = collision.Point + collision.Normal;
DrawLine3D(collision.Point, normalEnd, Color.Red);
}
DrawRay(ray, Color.Maroon);
DrawGrid(10, 10.0f);
EndMode3D();
// Draw some debug GUI text
DrawText($"Hit Object: {hitObjectName}", 10, 50, 10, Color.Black);
if (collision.Hit)
{
int ypos = 70;
DrawText($"Distance: {collision.Distance}", 10, ypos, 10, Color.Black);
DrawText($"Hit Pos: {collision.Point}", 10, ypos + 15, 10, Color.Black);
DrawText($"Hit Norm: {collision.Normal}", 10, ypos + 30, 10, Color.Black);
if (triHitInfo.Hit)
{
DrawText($"Barycenter: {bary}", 10, ypos + 45, 10, Color.Black);
}
}
DrawText("Right click mouse to toggle camera controls", 10, 430, 10, Color.Gray);
DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, Color.Gray);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadModel(tower);
UnloadTexture(texture);
CloseWindow();
//--------------------------------------------------------------------------------------

View file

@ -1,13 +1,15 @@
/*******************************************************************************************
*
* raylib [models] example - Draw textured cube
* raylib [models] example - textured cube
*
* Example complexity rating: [] 2/4
*
* Example originally created with raylib 4.5, last time updated with raylib 4.5
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2022-2023 Ramon Santamaria (@raysan5)
* Copyright (c) 2022-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -16,19 +18,22 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class ModelCubeTexture
public partial class ModelCubeTexture : IExample
{
public static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Model Cube Texture";
public string Title => "raylib [models] example - textured cube";
private Camera3D camera;
private Texture2D texture;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - draw cube texture");
// Define the camera to look into our 3d world
Camera3D camera;
camera = new();
camera.Position = new Vector3(0.0f, 10.0f, 10.0f);
camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
@ -36,60 +41,48 @@ public class ModelCubeTexture
camera.Projection = CameraProjection.Perspective;
// Load texture to be applied to the cubes sides
Texture2D texture = LoadTexture("resources/cubicmap_atlas.png");
texture = LoadTexture("resources/cubicmap_atlas.png");
}
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
public void Update()
{
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
BeginMode3D(camera);
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
// Draw cube with an applied texture
DrawCubeTexture(texture, new Vector3(-2.0f, 2.0f, 0.0f), 2.0f, 4.0f, 2.0f, Color.White);
BeginMode3D(camera);
// Draw cube with an applied texture, but only a defined rectangle piece of the texture
DrawCubeTextureRec(
texture,
new Rectangle(0, texture.Height / 2, texture.Width / 2, texture.Height / 2),
new Vector3(2.0f, 1.0f, 0.0f),
2.0f,
2.0f,
2.0f,
Color.White
);
// Draw cube with an applied texture
DrawCubeTexture(texture, new Vector3(-2.0f, 2.0f, 0.0f), 2.0f, 4.0f, 2.0f, Color.White);
DrawGrid(10, 1.0f);
// Draw cube with an applied texture, but only a defined rectangle piece of the texture
DrawCubeTextureRec(
texture,
new Rectangle(0, texture.Height / 2, texture.Width / 2, texture.Height / 2),
new Vector3(2.0f, 1.0f, 0.0f),
2.0f,
2.0f,
2.0f,
Color.White
);
EndMode3D();
DrawGrid(10, 1.0f);
EndDrawing();
//----------------------------------------------------------------------------------
}
EndMode3D();
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
public void Unload()
{
UnloadTexture(texture);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
// Draw cube textured
// NOTE: Cube position is the center position
static void DrawCubeTexture(
private static void DrawCubeTexture(
Texture2D texture,
Vector3 position,
float width,
@ -98,9 +91,9 @@ public class ModelCubeTexture
Color color
)
{
float x = position.X;
float y = position.Y;
float z = position.Z;
var x = position.X;
var y = position.Y;
var z = position.Z;
// Set desired texture to be enabled while drawing following vertex data
Rlgl.SetTexture(texture.Id);
@ -218,7 +211,7 @@ public class ModelCubeTexture
}
// Draw cube with texture piece applied to all faces
static void DrawCubeTextureRec(
private static void DrawCubeTextureRec(
Texture2D texture,
Rectangle source,
Vector3 position,
@ -228,11 +221,11 @@ public class ModelCubeTexture
Color color
)
{
float x = position.X;
float y = position.Y;
float z = position.Z;
float texWidth = (float)texture.Width;
float texHeight = (float)texture.Height;
var x = position.X;
var y = position.Y;
var z = position.Z;
var texWidth = (float)texture.Width;
var texHeight = (float)texture.Height;
// Set desired texture to be enabled while drawing following vertex data
Rlgl.SetTexture(texture.Id);
@ -312,5 +305,32 @@ public class ModelCubeTexture
Rlgl.SetTexture(0);
}
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - textured cube");
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
var game = new ModelCubeTexture();
game.Init();
// Main game loop
while (!WindowShouldClose())
{
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View file

@ -1,20 +1,28 @@
/*******************************************************************************************
*
* raylib [models] example - Models loading
* raylib [models] example - loading
*
* raylib supports multiple models file formats:
* Example complexity rating: [] 1/4
*
* - OBJ > Text file, must include vertex position-texcoords-normals information,
* if files references some .mtl materials file, it will be loaded (or try to)
* - GLTF > Modern text/binary file format, includes lot of information and it could
* also reference external files, raylib will try loading mesh and materials data
* - IQM > Binary file format including mesh vertex data but also animation data,
* raylib can load .iqm animations.
* NOTE: raylib supports multiple models file formats:
*
* This example has been created using raylib 2.6 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* - OBJ > Text file format. Must include vertex position-texcoords-normals information,
* if .obj references some .mtl materials file, it will be tried to be loaded
* - GLTF/GLB > Text/binary file formats. Includes lot of information and it could
* also reference external files, mesh and materials data will be tried to be loaded
* - IQM > Binary file format. Includes mesh vertex data but also animation data,
* meshes and animation data can be loaded
* - VOX > Binary file format. MagikaVoxel mesh format:
* https://github.com/ephtracy/voxel-model/blob/master/MagicaVoxel-file-format-vox.txt
* - M3D > Binary file format. Model 3D format:
* https://bztsrc.gitlab.io/model3d
*
* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
* Example originally created with raylib 2.0, last time updated with raylib 4.2
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -23,135 +31,171 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class ModelLoading
public partial class ModelLoading : IExample
{
public unsafe static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Model Loading";
public string Title => "raylib [models] example - loading";
private Camera3D camera;
private Model model;
private Texture2D texture;
private Vector3 position;
private BoundingBox bounds;
private bool selected;
public unsafe void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - models loading");
// Define the camera to look into our 3d world
Camera3D camera = new();
camera.Position = new Vector3(50.0f, 50.0f, 50.0f);
camera.Target = new Vector3(0.0f, 10.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
camera.FovY = 45.0f;
camera.Projection = CameraProjection.Perspective;
camera = new();
camera.Position = new Vector3(50.0f, 50.0f, 50.0f); // Camera position
camera.Target = new Vector3(0.0f, 12.0f, 0.0f); // Camera looking at point
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
camera.FovY = 45.0f; // Camera field-of-view Y
camera.Projection = CameraProjection.Perspective; // Camera mode type
Model model = LoadModel("resources/models/obj/castle.obj");
Texture2D texture = LoadTexture("resources/models/obj/castle_diffuse.png");
model = LoadModel("resources/models/obj/castle.obj"); // Load model
texture = LoadTexture("resources/models/obj/castle_diffuse.png"); // Load model texture
// Set map diffuse texture
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Albedo, ref texture);
Vector3 position = new(0.0f, 0.0f, 0.0f);
BoundingBox bounds = GetMeshBoundingBox(model.Meshes[0]);
position = new(0.0f, 0.0f, 0.0f); // Set model position
bounds = GetMeshBoundingBox(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
bool selected = false;
selected = false;
}
public unsafe void Update()
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Orbital);
#if BROWSER
// NOTE: Drag & drop file loading (IsFileDropped) is not available in the browser
// host, so it is skipped here. The default model stays loaded.
#else
// Load new models/textures on drag&drop
if (IsFileDropped())
{
var droppedFiles = Raylib.GetDroppedFiles();
if (droppedFiles.Length == 1) // Only support one file dropped
{
if (IsFileExtension(droppedFiles[0], ".obj") ||
IsFileExtension(droppedFiles[0], ".gltf") ||
IsFileExtension(droppedFiles[0], ".glb") ||
IsFileExtension(droppedFiles[0], ".vox") ||
IsFileExtension(droppedFiles[0], ".iqm") ||
IsFileExtension(droppedFiles[0], ".m3d") // Model file formats supported
)
{
UnloadModel(model); // Unload previous model
model = LoadModel(droppedFiles[0]); // Load new model
// Set current map diffuse texture
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Albedo, ref texture);
bounds = GetMeshBoundingBox(model.Meshes[0]);
// Move camera position from target enough distance to visualize model properly
camera.Position.X = bounds.Max.X + 10.0f;
camera.Position.Y = bounds.Max.Y + 10.0f;
camera.Position.Z = bounds.Max.Z + 10.0f;
}
else if (IsFileExtension(droppedFiles[0], ".png")) // Texture file formats supported
{
// Unload current model texture and load new one
UnloadTexture(texture);
texture = LoadTexture(droppedFiles[0]);
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Albedo, ref texture);
}
}
}
#endif
// Select model on mouse click
if (IsMouseButtonPressed(MouseButton.Left))
{
// Check collision between ray and box
if (GetRayCollisionBox(GetScreenToWorldRay(GetMousePosition(), camera), bounds).Hit)
{
selected = !selected;
}
else
{
selected = false;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModel(model, position, 1.0f, Color.White); // Draw 3d model with texture
DrawGrid(20, 10.0f); // Draw a grid
if (selected)
{
DrawBoundingBox(bounds, Color.Green); // Draw selection box
}
EndMode3D();
DrawText("Drag & drop model to load mesh/texture.", 10, GetScreenHeight() - 20, 10, Color.DarkGray);
if (selected)
{
DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, Color.Green);
}
DrawText("(c) Castle 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, Color.Gray);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadTexture(texture);
UnloadModel(model);
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - loading");
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
var game = new ModelLoading();
game.Init();
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Orbital);
if (IsFileDropped())
{
string[] droppedFiles = Raylib.GetDroppedFiles();
if (droppedFiles.Length == 1)
{
if (IsFileExtension(droppedFiles[0], ".obj") ||
IsFileExtension(droppedFiles[0], ".gltf") ||
IsFileExtension(droppedFiles[0], ".glb") ||
IsFileExtension(droppedFiles[0], ".vox") ||
IsFileExtension(droppedFiles[0], ".iqm") ||
IsFileExtension(droppedFiles[0], ".m3d")
)
{
UnloadModel(model);
model = LoadModel(droppedFiles[0]);
// Set current map diffuse texture
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Albedo, ref texture);
bounds = GetMeshBoundingBox(model.Meshes[0]);
// TODO: Move camera position from target enough distance to visualize model properly
}
else if (IsFileExtension(droppedFiles[0], ".png"))
{
// Unload model texture and load new one
UnloadTexture(texture);
texture = LoadTexture(droppedFiles[0]);
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Albedo, ref texture);
}
}
}
// Select model on mouse click
if (IsMouseButtonPressed(MouseButton.Left))
{
// Check collision between ray and box
if (GetRayCollisionBox(GetScreenToWorldRay(GetMousePosition(), camera), bounds).Hit)
{
selected = !selected;
}
else
{
selected = false;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawModel(model, position, 1.0f, Color.White);
DrawGrid(20, 10.0f);
if (selected)
{
DrawBoundingBox(bounds, Color.Green);
}
EndMode3D();
DrawText("Drag & drop model to load mesh/texture.", 10, GetScreenHeight() - 20, 10, Color.DarkGray);
if (selected)
{
DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, Color.Green);
}
DrawText("(c) Castle 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, Color.Gray);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture);
UnloadModel(model);
CloseWindow();
//--------------------------------------------------------------------------------------

View file

@ -1,13 +1,17 @@
/*******************************************************************************************
*
* raylib [models] example - Show the difference between perspective and orthographic projection
* raylib [models] example - orthographic projection
*
* This program is heavily based on the geometric objects example
* Example complexity rating: [] 1/4
*
* This example has been created using raylib 1.9.7 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example originally created with raylib 2.0, last time updated with raylib 3.7
*
* Copyright (c) 2018 Max Danielsson ref Ramon Santamaria (@raysan5)
* Example contributed by Max Danielsson (@autious) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2018-2025 Max Danielsson (@autious) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -16,96 +20,119 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class OrthographicProjection
public partial class OrthographicProjection : IExample
{
public const float FOVY_PERSPECTIVE = 45.0f;
public const float WIDTH_ORTHOGRAPHIC = 10.0f;
public static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Orthographic Projection";
public string Title => "raylib [models] example - orthographic projection";
private Camera3D camera;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - geometric shapes");
// Define the camera to look into our 3d world
Camera3D camera = new();
camera = new();
camera.Position = new Vector3(0.0f, 10.0f, 10.0f);
camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
camera.FovY = FOVY_PERSPECTIVE;
camera.Projection = CameraProjection.Perspective;
}
public void Update()
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KeyboardKey.Space))
{
if (camera.Projection == CameraProjection.Perspective)
{
camera.FovY = WIDTH_ORTHOGRAPHIC;
camera.Projection = CameraProjection.Orthographic;
}
else
{
camera.FovY = FOVY_PERSPECTIVE;
camera.Projection = CameraProjection.Perspective;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawCube(new Vector3(-4.0f, 0.0f, 2.0f), 2.0f, 5.0f, 2.0f, Color.Red);
DrawCubeWires(new Vector3(-4.0f, 0.0f, 2.0f), 2.0f, 5.0f, 2.0f, Color.Gold);
DrawCubeWires(new Vector3(-4.0f, 0.0f, -2.0f), 3.0f, 6.0f, 2.0f, Color.Maroon);
DrawSphere(new Vector3(-1.0f, 0.0f, -2.0f), 1.0f, Color.Green);
DrawSphereWires(new Vector3(1.0f, 0.0f, 2.0f), 2.0f, 16, 16, Color.Lime);
DrawCylinder(new Vector3(4.0f, 0.0f, -2.0f), 1.0f, 2.0f, 3.0f, 4, Color.SkyBlue);
DrawCylinderWires(new Vector3(4.0f, 0.0f, -2.0f), 1.0f, 2.0f, 3.0f, 4, Color.DarkBlue);
DrawCylinderWires(new Vector3(4.5f, -1.0f, 2.0f), 1.0f, 1.0f, 2.0f, 6, Color.Brown);
DrawCylinder(new Vector3(1.0f, 0.0f, -4.0f), 0.0f, 1.5f, 3.0f, 8, Color.Gold);
DrawCylinderWires(new Vector3(1.0f, 0.0f, -4.0f), 0.0f, 1.5f, 3.0f, 8, Color.Pink);
DrawGrid(10, 1.0f); // Draw a grid
EndMode3D();
DrawText("Press Spacebar to switch camera type", 10, GetScreenHeight() - 30, 20, Color.DarkGray);
if (camera.Projection == CameraProjection.Orthographic)
{
DrawText("ORTHOGRAPHIC", 10, 40, 20, Color.Black);
}
else if (camera.Projection == CameraProjection.Perspective)
{
DrawText("PERSPECTIVE", 10, 40, 20, Color.Black);
}
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - orthographic projection");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new OrthographicProjection();
game.Init();
// Main game loop
while (!WindowShouldClose())
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KeyboardKey.Space))
{
if (camera.Projection == CameraProjection.Perspective)
{
camera.FovY = WIDTH_ORTHOGRAPHIC;
camera.Projection = CameraProjection.Orthographic;
}
else
{
camera.FovY = FOVY_PERSPECTIVE;
camera.Projection = CameraProjection.Perspective;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawCube(new Vector3(-4.0f, 0.0f, 2.0f), 2.0f, 5.0f, 2.0f, Color.Red);
DrawCubeWires(new Vector3(-4.0f, 0.0f, 2.0f), 2.0f, 5.0f, 2.0f, Color.Gold);
DrawCubeWires(new Vector3(-4.0f, 0.0f, -2.0f), 3.0f, 6.0f, 2.0f, Color.Maroon);
DrawSphere(new Vector3(-1.0f, 0.0f, -2.0f), 1.0f, Color.Green);
DrawSphereWires(new Vector3(1.0f, 0.0f, 2.0f), 2.0f, 16, 16, Color.Lime);
DrawCylinder(new Vector3(4.0f, 0.0f, -2.0f), 1.0f, 2.0f, 3.0f, 4, Color.SkyBlue);
DrawCylinderWires(new Vector3(4.0f, 0.0f, -2.0f), 1.0f, 2.0f, 3.0f, 4, Color.DarkBlue);
DrawCylinderWires(new Vector3(4.5f, -1.0f, 2.0f), 1.0f, 1.0f, 2.0f, 6, Color.Brown);
DrawCylinder(new Vector3(1.0f, 0.0f, -4.0f), 0.0f, 1.5f, 3.0f, 8, Color.Gold);
DrawCylinderWires(new Vector3(1.0f, 0.0f, -4.0f), 0.0f, 1.5f, 3.0f, 8, Color.Pink);
DrawGrid(10, 1.0f);
EndMode3D();
DrawText("Press Spacebar to switch camera type", 10, GetScreenHeight() - 30, 20, Color.DarkGray);
if (camera.Projection == CameraProjection.Orthographic)
{
DrawText("ORTHOGRAPHIC", 10, 40, 20, Color.Black);
}
else if (camera.Projection == CameraProjection.Perspective)
{
DrawText("PERSPECTIVE", 10, 40, 20, Color.Black);
}
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;

View file

@ -1,32 +1,49 @@
/*******************************************************************************************
*
* raylib [models] example - Skybox loading and drawing
* raylib [models] example - skybox rendering
*
* This example has been created using raylib 1.8 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example complexity rating: [] 2/4
*
* Copyright (c) 2017 Ramon Santamaria (@raysan5)
* Example originally created with raylib 1.8, last time updated with raylib 4.0
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2017-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System;
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Models;
public class SkyboxDemo
public partial class SkyboxDemo : IExample
{
public static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
// GLSL version used for shaders (330 desktop, 100 web/GLES)
public const int GlslVersion = 330;
public string Name => "Models / Skybox Demo";
public string Title => "raylib [models] example - skybox rendering";
public bool CursorDisabled => true;
private Camera3D camera;
private Model skybox;
private bool useHdr;
private Shader shdrCubemap;
private string skyboxFileName;
private Texture2D panorama;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - skybox loading and drawing");
// Define the camera to look into our 3d world
Camera3D camera = new();
camera = new();
camera.Position = new Vector3(1.0f, 1.0f, 1.0f);
camera.Target = new Vector3(4.0f, 1.0f, 4.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
@ -34,14 +51,19 @@ public class SkyboxDemo
camera.Projection = CameraProjection.Perspective;
// Load skybox model
Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
Model skybox = LoadModelFromMesh(cube);
var cube = GenMeshCube(1.0f, 1.0f, 1.0f);
skybox = LoadModelFromMesh(cube);
bool useHdr = false;
// Set this to true to use an HDR Texture
// NOTE: raylib must be built with HDR Support for this to work: SUPPORT_FILEFORMAT_HDR
useHdr = false;
// Load skybox shader and set required locations
// NOTE: Some locations are automatically set at shader loading
Shader shdrSkybox = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/skybox.fs");
var shdrSkybox = LoadShader(
$"resources/shaders/glsl{GlslVersion}/skybox.vs",
$"resources/shaders/glsl{GlslVersion}/skybox.fs"
);
Raylib.SetShaderValue(
shdrSkybox,
@ -67,9 +89,9 @@ public class SkyboxDemo
Raylib.SetMaterialShader(ref skybox, 0, ref shdrSkybox);
// Load cubemap shader and setup required shader locations
Shader shdrCubemap = LoadShader(
"resources/shaders/glsl330/cubemap.vs",
"resources/shaders/glsl330/cubemap.fs"
shdrCubemap = LoadShader(
$"resources/shaders/glsl{GlslVersion}/cubemap.vs",
$"resources/shaders/glsl{GlslVersion}/cubemap.fs"
);
Raylib.SetShaderValue(
shdrCubemap,
@ -79,14 +101,12 @@ public class SkyboxDemo
);
// Load skybox
string skyboxFileName = "resources/dresden_square_2k.hdr";
Texture2D panorama;
skyboxFileName = "resources/dresden_square_2k.hdr";
if (useHdr)
{
panorama = LoadTexture(skyboxFileName);
Texture2D cubemap = GenTextureCubemap(
var cubemap = GenTextureCubemap(
shdrCubemap,
panorama,
1024,
@ -97,108 +117,130 @@ public class SkyboxDemo
}
else
{
Image img = LoadImage("resources/skybox.png");
Texture2D cubemap = LoadTextureCubemap(img, CubemapLayout.AutoDetect);
// TODO: WARNING: On PLATFORM_WEB it requires a big amount of memory to process input image
// and generate the required cubemap image to be passed to rlLoadTextureCubemap()
var img = LoadImage("resources/skybox.png");
var cubemap = LoadTextureCubemap(img, CubemapLayout.AutoDetect);
SetMaterialTexture(ref skybox, 0, MaterialMapIndex.Cubemap, ref cubemap);
UnloadImage(img);
}
}
DisableCursor();
public void Update()
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.FirstPerson);
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
// Load new cubemap texture on drag & drop
if (IsFileDropped())
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.FirstPerson);
var files = Raylib.GetDroppedFiles();
// Load new cubemap texture on drag & drop
if (IsFileDropped())
if (files.Length == 1)
{
string[] files = Raylib.GetDroppedFiles();
if (files.Length == 1)
if (IsFileExtension(files[0], ".png;.jpg;.hdr;.bmp;.tga"))
{
if (IsFileExtension(files[0], ".png;.jpg;.hdr;.bmp;.tga"))
// Unload cubemap texture and load new one
UnloadTexture(Raylib.GetMaterialTexture(ref skybox, 0, MaterialMapIndex.Cubemap));
if (useHdr)
{
// Unload cubemap texture and load new one
UnloadTexture(Raylib.GetMaterialTexture(ref skybox, 0, MaterialMapIndex.Cubemap));
if (useHdr)
{
panorama = LoadTexture(files[0]);
Texture2D cubemap = GenTextureCubemap(
shdrCubemap,
panorama,
1024,
PixelFormat.UncompressedR8G8B8A8
);
SetMaterialTexture(ref skybox, 0, MaterialMapIndex.Cubemap, ref cubemap);
UnloadTexture(panorama);
}
else
{
Image img = LoadImage(files[0]);
Texture2D cubemap = LoadTextureCubemap(img, CubemapLayout.AutoDetect);
SetMaterialTexture(ref skybox, 0, MaterialMapIndex.Cubemap, ref cubemap);
UnloadImage(img);
}
skyboxFileName = files[0];
panorama = LoadTexture(files[0]);
var cubemap = GenTextureCubemap(
shdrCubemap,
panorama,
1024,
PixelFormat.UncompressedR8G8B8A8
);
SetMaterialTexture(ref skybox, 0, MaterialMapIndex.Cubemap, ref cubemap);
UnloadTexture(panorama);
}
else
{
var img = LoadImage(files[0]);
var cubemap = LoadTextureCubemap(img, CubemapLayout.AutoDetect);
SetMaterialTexture(ref skybox, 0, MaterialMapIndex.Cubemap, ref cubemap);
UnloadImage(img);
}
skyboxFileName = files[0];
}
}
//----------------------------------------------------------------------------------
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
BeginMode3D(camera);
// We are inside the cube, we need to disable backface culling!
Rlgl.DisableBackfaceCulling();
Rlgl.DisableDepthMask();
DrawModel(skybox, Vector3.Zero, 1.0f, Color.White);
Rlgl.EnableBackfaceCulling();
Rlgl.EnableDepthMask();
// We are inside the cube, we need to disable backface culling!
Rlgl.DisableBackfaceCulling();
Rlgl.DisableDepthMask();
DrawModel(skybox, Vector3.Zero, 1.0f, Color.White);
Rlgl.EnableBackfaceCulling();
Rlgl.EnableDepthMask();
DrawGrid(10, 1.0f);
DrawGrid(10, 1.0f);
EndMode3D();
EndMode3D();
if (useHdr)
{
DrawText(
$"Panorama image from hdrihaven.com: {skyboxFileName}",
10,
GetScreenHeight() - 20,
10,
Color.Black
);
}
else
{
DrawText($": {skyboxFileName}", 10, GetScreenHeight() - 20, 10, Color.Black);
}
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
if (useHdr)
{
DrawText(
$"Panorama image from hdrihaven.com: {skyboxFileName}",
10,
GetScreenHeight() - 20,
10,
Color.Black
);
}
else
{
DrawText($": {skyboxFileName}", 10, GetScreenHeight() - 20, 10, Color.Black);
}
// De-Initialization
//--------------------------------------------------------------------------------------
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadShader(Raylib.GetMaterial(ref skybox, 0).Shader);
UnloadTexture(Raylib.GetMaterialTexture(ref skybox, 0, MaterialMapIndex.Cubemap));
UnloadModel(skybox);
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - skybox rendering");
DisableCursor(); // Limit cursor to relative movement inside the window
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
var game = new SkyboxDemo();
game.Init();
// Main game loop
while (!WindowShouldClose())
{
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow();
//--------------------------------------------------------------------------------------
@ -215,10 +257,10 @@ public class SkyboxDemo
// STEP 1: Setup framebuffer
//------------------------------------------------------------------------------------------
uint rbo = Rlgl.LoadTextureDepth(size, size, true);
var rbo = Rlgl.LoadTextureDepth(size, size, true);
cubemap.Id = Rlgl.LoadTextureCubemap(null, size, format, 1);
uint fbo = Rlgl.LoadFramebuffer();
var fbo = Rlgl.LoadFramebuffer();
Rlgl.FramebufferAttach(
fbo,
rbo,
@ -235,7 +277,7 @@ public class SkyboxDemo
);
// Check if framebuffer is complete with attachments (valid)
if (Rlgl.FramebufferComplete(fbo))
if (Rlgl.FramebufferComplete(fbo) != 0)
{
Console.WriteLine($"FBO: [ID {fbo}] Framebuffer object created successfully");
}
@ -247,7 +289,7 @@ public class SkyboxDemo
Rlgl.EnableShader(shader.Id);
// Define projection matrix and send it to shader
Matrix4x4 matFboProjection = Raymath.MatrixPerspective(
var matFboProjection = Raymath.MatrixPerspective(
90.0f * DEG2RAD,
1.0f,
Rlgl.CULL_DISTANCE_NEAR,
@ -256,14 +298,14 @@ public class SkyboxDemo
Rlgl.SetUniformMatrix(shader.Locs[(int)ShaderLocationIndex.MatrixProjection], matFboProjection);
// Define view matrix for every side of the cubemap
Matrix4x4[] fboViews = new[]
var fboViews = new[]
{
Raymath.MatrixLookAt(Vector3.Zero, new Vector3(-1.0f, 0.0f, 0.0f), new Vector3( 0.0f, -1.0f, 0.0f)),
Raymath.MatrixLookAt(Vector3.Zero, new Vector3( 1.0f, 0.0f, 0.0f), new Vector3( 0.0f, -1.0f, 0.0f)),
Raymath.MatrixLookAt(Vector3.Zero, new Vector3(-1.0f, 0.0f, 0.0f), new Vector3( 0.0f, -1.0f, 0.0f)),
Raymath.MatrixLookAt(Vector3.Zero, new Vector3( 0.0f, 1.0f, 0.0f), new Vector3( 0.0f, 0.0f, 1.0f)),
Raymath.MatrixLookAt(Vector3.Zero, new Vector3( 0.0f, -1.0f, 0.0f), new Vector3( 0.0f, 0.0f, -1.0f)),
Raymath.MatrixLookAt(Vector3.Zero, new Vector3( 0.0f, 0.0f, -1.0f), new Vector3( 0.0f, -1.0f, 0.0f)),
Raymath.MatrixLookAt(Vector3.Zero, new Vector3( 0.0f, 0.0f, 1.0f), new Vector3( 0.0f, -1.0f, 0.0f)),
Raymath.MatrixLookAt(Vector3.Zero, new Vector3( 0.0f, 0.0f, -1.0f), new Vector3( 0.0f, -1.0f, 0.0f)),
};
// Set viewport to current fbo dimensions
@ -273,7 +315,7 @@ public class SkyboxDemo
Rlgl.ActiveTextureSlot(0);
Rlgl.EnableTexture(panorama.Id);
for (int i = 0; i < 6; i++)
for (var i = 0; i < 6; i++)
{
// Set the view matrix for the current cube face
Rlgl.SetUniformMatrix(shader.Locs[(int)ShaderLocationIndex.MatrixView], fboViews[i]);

View file

@ -1,13 +1,17 @@
/*******************************************************************************************
*
* raylib [models] example - rlgl module usage with push/pop matrix transformations
* raylib [models] example - rlgl solar system
*
* This example uses [rlgl] module funtionality (pseudo-OpenGL 1.1 style coding)
* Example complexity rating: [] 4/4
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* NOTE: This example uses [rlgl] module functionality (pseudo-OpenGL 1.1 style coding)
*
* Copyright (c) 2018 Ramon Santamaria (@raysan5)
* Example originally created with raylib 2.5, last time updated with raylib 4.0
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2018-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -17,145 +21,137 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class SolarSystem
public partial class SolarSystem : IExample
{
public static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
private const float sunRadius = 4.0f;
private const float earthRadius = 0.6f;
private const float earthOrbitRadius = 8.0f;
private const float moonRadius = 0.16f;
private const float moonOrbitRadius = 1.5f;
public string Name => "Models / Solar System";
public string Title => "raylib [models] example - rlgl solar system";
private Camera3D camera;
private float rotationSpeed;
private float earthRotation;
private float earthOrbitRotation;
private float moonRotation;
private float moonOrbitRotation;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
const float sunRadius = 4.0f;
const float earthRadius = 0.6f;
const float earthOrbitRadius = 8.0f;
const float moonRadius = 0.16f;
const float moonOrbitRadius = 1.5f;
InitWindow(screenWidth, screenHeight, "raylib [models] example - rlgl module usage with push/pop matrix transformations");
// Define the camera to look into our 3d world
Camera3D camera = new();
camera.Position = new Vector3(16.0f, 16.0f, 16.0f);
camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
camera.FovY = 45.0f;
camera.Projection = CameraProjection.Perspective;
camera = new();
camera.Position = new Vector3(16.0f, 16.0f, 16.0f); // Camera position
camera.Target = new Vector3(0.0f, 0.0f, 0.0f); // Camera looking at point
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
camera.FovY = 45.0f; // Camera field-of-view Y
camera.Projection = CameraProjection.Perspective; // Camera projection type
// General system rotation speed
float rotationSpeed = 0.2f;
// Rotation of earth around itself (days) in degrees
float earthRotation = 0.0f;
// Rotation of earth around the Sun (years) in degrees
float earthOrbitRotation = 0.0f;
// Rotation of moon around itself
float moonRotation = 0.0f;
// Rotation of moon around earth in degrees
float moonOrbitRotation = 0.0f;
rotationSpeed = 0.2f; // General system rotation speed
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
earthRotation = 0.0f; // Rotation of earth around itself (days) in degrees
earthOrbitRotation = 0.0f; // Rotation of earth around the Sun (years) in degrees
moonRotation = 0.0f; // Rotation of moon around itself
moonOrbitRotation = 0.0f; // Rotation of moon around earth in degrees
}
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Free);
public void Update()
{
// Update
//----------------------------------------------------------------------------------
earthRotation += (5.0f * rotationSpeed);
earthOrbitRotation += (365 / 360.0f * (5.0f * rotationSpeed) * rotationSpeed);
moonRotation += (2.0f * rotationSpeed);
moonOrbitRotation += (8.0f * rotationSpeed);
//----------------------------------------------------------------------------------
earthRotation += (5.0f * rotationSpeed);
earthOrbitRotation += (365 / 360.0f * (5.0f * rotationSpeed) * rotationSpeed);
moonRotation += (2.0f * rotationSpeed);
moonOrbitRotation += (8.0f * rotationSpeed);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
BeginMode3D(camera);
Rlgl.PushMatrix();
// Scale Sun
Rlgl.Scalef(sunRadius, sunRadius, sunRadius);
// Draw the Sun
DrawSphereBasic(Color.Gold);
Rlgl.PopMatrix();
Rlgl.PushMatrix();
// Scale Sun
Rlgl.Scalef(sunRadius, sunRadius, sunRadius);
// Draw the Sun
DrawSphereBasic(Color.Gold);
Rlgl.PopMatrix();
Rlgl.PushMatrix();
// Rotation for Earth orbit around Sun
Rlgl.Rotatef(earthOrbitRotation, 0.0f, 1.0f, 0.0f);
// Translation for Earth orbit
Rlgl.Translatef(earthOrbitRadius, 0.0f, 0.0f);
Rlgl.PushMatrix();
// Rotation for Earth orbit around Sun
Rlgl.Rotatef(earthOrbitRotation, 0.0f, 1.0f, 0.0f);
// Translation for Earth orbit
Rlgl.Translatef(earthOrbitRadius, 0.0f, 0.0f);
// Rotation for Earth orbit around Sun inverted
Rlgl.Rotatef(-earthOrbitRotation, 0.0f, 1.0f, 0.0f);
Rlgl.PushMatrix();
// Rotation for Earth itself
Rlgl.Rotatef(earthRotation, 0.25f, 1.0f, 0.0f);
// Scale Earth
Rlgl.Scalef(earthRadius, earthRadius, earthRadius);
Rlgl.PushMatrix();
// Rotation for Earth itself
Rlgl.Rotatef(earthRotation, 0.25f, 1.0f, 0.0f);
// Scale Earth
Rlgl.Scalef(earthRadius, earthRadius, earthRadius);
// Draw the Earth
DrawSphereBasic(Color.Blue);
Rlgl.PopMatrix();
// Draw the Earth
DrawSphereBasic(Color.Blue);
Rlgl.PopMatrix();
// Rotation for Moon orbit around Earth
Rlgl.Rotatef(moonOrbitRotation, 0.0f, 1.0f, 0.0f);
// Translation for Moon orbit
Rlgl.Translatef(moonOrbitRadius, 0.0f, 0.0f);
// Rotation for Moon itself
Rlgl.Rotatef(moonRotation, 0.0f, 1.0f, 0.0f);
// Scale Moon
Rlgl.Scalef(moonRadius, moonRadius, moonRadius);
// Rotation for Moon orbit around Earth
Rlgl.Rotatef(moonOrbitRotation, 0.0f, 1.0f, 0.0f);
// Translation for Moon orbit
Rlgl.Translatef(moonOrbitRadius, 0.0f, 0.0f);
// Rotation for Moon orbit around Earth inverted
Rlgl.Rotatef(-moonOrbitRotation, 0.0f, 1.0f, 0.0f);
// Rotation for Moon itself
Rlgl.Rotatef(moonRotation, 0.0f, 1.0f, 0.0f);
// Scale Moon
Rlgl.Scalef(moonRadius, moonRadius, moonRadius);
// Draw the Moon
DrawSphereBasic(Color.LightGray);
Rlgl.PopMatrix();
// Draw the Moon
DrawSphereBasic(Color.LightGray);
Rlgl.PopMatrix();
// Some reference elements (not affected by previous matrix transformations)
DrawCircle3D(
new Vector3(0.0f, 0.0f, 0.0f),
earthOrbitRadius,
new Vector3(1, 0, 0),
90.0f,
Fade(Color.Red, 0.5f)
);
DrawGrid(20, 1.0f);
// Some reference elements (not affected by previous matrix transformations)
DrawCircle3D(
new Vector3(0.0f, 0.0f, 0.0f),
earthOrbitRadius,
new Vector3(1, 0, 0),
90.0f,
ColorAlpha(Color.Red, 0.5f)
);
DrawGrid(20, 1.0f);
EndMode3D();
EndMode3D();
DrawText("EARTH ORBITING AROUND THE SUN!", 400, 10, 20, Color.Maroon);
DrawFPS(10, 10);
DrawText("EARTH ORBITING AROUND THE SUN!", 400, 10, 20, Color.Maroon);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
public void Unload()
{
}
// Draw sphere without any matrix transformation
// NOTE: Sphere is drawn in world position ( 0, 0, 0 ) with radius 1.0f
static void DrawSphereBasic(Color color)
private static void DrawSphereBasic(Color color)
{
int rings = 16;
int slices = 16;
var rings = 16;
var slices = 16;
Rlgl.Begin(DrawMode.Triangles);
Rlgl.Color4ub(color.R, color.G, color.B, color.A);
for (int i = 0; i < (rings + 2); i++)
for (var i = 0; i < (rings + 2); i++)
{
for (int j = 0; j < slices; j++)
for (var j = 0; j < slices; j++)
{
Rlgl.Vertex3f(
MathF.Cos(DEG2RAD * (270 + (180 / (rings + 1)) * i)) * MathF.Sin(DEG2RAD * (j * 360 / slices)),
@ -192,5 +188,32 @@ public class SolarSystem
}
Rlgl.End();
}
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [models] example - rlgl solar system");
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
var game = new SolarSystem();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
}

View file

@ -1,13 +1,17 @@
/*******************************************************************************************
*
* raylib [models] example - Waving cubes
* raylib [models] example - waving cubes
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example complexity rating: [] 3/4
*
* Example originally created with raylib 2.5, last time updated with raylib 3.7
*
* Example contributed by Codecat (@codecat) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2019 Codecat (@codecat) and Ramon Santamaria (@raysan5)
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2019-2025 Codecat (@codecat) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -17,101 +21,125 @@ using static Raylib_cs.Raylib;
namespace Examples.Models;
public class WavingCubes
public partial class WavingCubes : IExample
{
private const int screenWidth = 800;
private const int screenHeight = 450;
// Specify the amount of blocks in each direction
private const int numBlocks = 15;
public string Name => "Models / Waving Cubes";
public string Title => "raylib [models] example - waving cubes";
private Camera3D camera;
public void Init()
{
// Initialize the camera
camera = new();
camera.Position = new Vector3(30.0f, 20.0f, 30.0f); // Camera position
camera.Target = new Vector3(0.0f, 0.0f, 0.0f); // Camera looking at point
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
camera.FovY = 70.0f; // Camera field-of-view Y
camera.Projection = CameraProjection.Perspective; // Camera projection type
}
public void Update()
{
// Update
//----------------------------------------------------------------------------------
var time = GetTime();
// Calculate time scale for cube position and size
var scale = (2.0f + (float)Math.Sin(time)) * 0.7f;
// Move camera around the scene
var cameraTime = time * 0.3;
camera.Position.X = (float)Math.Cos(cameraTime) * 40.0f;
camera.Position.Z = (float)Math.Sin(cameraTime) * 40.0f;
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawGrid(10, 5.0f);
for (var x = 0; x < numBlocks; x++)
{
for (var y = 0; y < numBlocks; y++)
{
for (var z = 0; z < numBlocks; z++)
{
// Scale of the blocks depends on x/y/z positions
var blockScale = (x + y + z) / 30.0f;
// Scatter makes the waving effect by adding blockScale over time
var scatter = (float)Math.Sin(blockScale * 20.0f + (float)(time * 4.0f));
// Calculate the cube position
Vector3 cubePos = new(
(float)(x - numBlocks / 2) * (scale * 3.0f) + scatter,
(float)(y - numBlocks / 2) * (scale * 2.0f) + scatter,
(float)(z - numBlocks / 2) * (scale * 3.0f) + scatter
);
// Pick a color with a hue depending on cube position for the rainbow color effect
// NOTE: This function is quite costly to be done per cube and frame,
// pre-catching the results into a separate array could improve performance
var cubeColor = ColorFromHSV((float)(((x + y + z) * 18) % 360), 0.75f, 0.9f);
// Calculate cube size
var cubeSize = (2.4f - scale) * blockScale;
// And finally, draw the cube!
DrawCube(cubePos, cubeSize, cubeSize, cubeSize, cubeColor);
}
}
}
EndMode3D();
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - waving cubes");
// Initialize the camera
Camera3D camera = new();
camera.Position = new Vector3(30.0f, 20.0f, 30.0f);
camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
camera.FovY = 70.0f;
camera.Projection = CameraProjection.Perspective;
// Specify the amount of blocks in each direction
const int numBlocks = 15;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
var game = new WavingCubes();
game.Init();
// Main game loop
while (!WindowShouldClose())
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
double time = GetTime();
// Calculate time scale for cube position and size
float scale = (2.0f + (float)Math.Sin(time)) * 0.7f;
// Move camera around the scene
double cameraTime = time * 0.3;
camera.Position.X = (float)Math.Cos(cameraTime) * 40.0f;
camera.Position.Z = (float)Math.Sin(cameraTime) * 40.0f;
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
BeginMode3D(camera);
DrawGrid(10, 5.0f);
for (int x = 0; x < numBlocks; x++)
{
for (int y = 0; y < numBlocks; y++)
{
for (int z = 0; z < numBlocks; z++)
{
// Scale of the blocks depends on x/y/z positions
float blockScale = (x + y + z) / 30.0f;
// Scatter makes the waving effect by adding blockScale over time
float scatter = (float)Math.Sin(blockScale * 20.0f + (float)(time * 4.0f));
// Calculate the cube position
Vector3 cubePos = new(
(float)(x - numBlocks / 2) * (scale * 3.0f) + scatter,
(float)(y - numBlocks / 2) * (scale * 2.0f) + scatter,
(float)(z - numBlocks / 2) * (scale * 3.0f) + scatter
);
// Pick a color with a hue depending on cube position for the rainbow color effect
Color cubeColor = ColorFromHSV((float)(((x + y + z) * 18) % 360), 0.75f, 0.9f);
// Calculate cube size
float cubeSize = (2.4f - scale) * blockScale;
// And finally, draw the cube!
DrawCube(cubePos, cubeSize, cubeSize, cubeSize, cubeColor);
}
}
}
EndMode3D();
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
}

View file

@ -1,13 +1,17 @@
/*******************************************************************************************
*
* raylib [models] example - Plane rotations (yaw, pitch, roll)
* raylib [models] example - yaw pitch roll
*
* This example has been created using raylib 1.8 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example complexity rating: [] 2/4
*
* Example originally created with raylib 1.8, last time updated with raylib 4.0
*
* Example contributed by Berni (@Berni8k) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2017-2021 Berni (@Berni8k) and Ramon Santamaria (@raysan5)
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2017-2025 Berni (@Berni8k) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
@ -17,146 +21,178 @@ using static Raylib_cs.Raymath;
namespace Examples.Models;
public class YawPitchRoll
public partial class YawPitchRoll : IExample
{
public unsafe static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Models / Yaw Pitch Roll";
public string Title => "raylib [models] example - yaw pitch roll";
private Camera3D camera;
private Model model;
private Texture2D texture;
private float pitch;
private float roll;
private float yaw;
public unsafe void Init()
{
camera = new();
camera.Position = new Vector3(0.0f, 50.0f, -120.0f);// Camera position perspective
camera.Target = new Vector3(0.0f, 0.0f, 0.0f); // Camera looking at point
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
camera.FovY = 30.0f; // Camera field-of-view Y
camera.Projection = CameraProjection.Perspective; // Camera type
model = LoadModel("resources/models/obj/plane.obj"); // Load model
texture = LoadTexture("resources/models/obj/plane_diffuse.png"); // Load model texture
SetTextureWrap(texture, TextureWrap.Repeat); // Force Repeat to avoid issue on Web version
model.Materials[0].Maps[(int)MaterialMapIndex.Diffuse].Texture = texture; // Set map diffuse texture
pitch = 0.0f;
roll = 0.0f;
yaw = 0.0f;
}
public void Update()
{
// Update
//----------------------------------------------------------------------------------
// Plane pitch (x-axis) controls
if (IsKeyDown(KeyboardKey.Down))
{
pitch += 0.6f;
}
else if (IsKeyDown(KeyboardKey.Up))
{
pitch -= 0.6f;
}
else
{
if (pitch > 0.3f)
{
pitch -= 0.3f;
}
else if (pitch < -0.3f)
{
pitch += 0.3f;
}
}
// Plane yaw (y-axis) controls
if (IsKeyDown(KeyboardKey.S))
{
yaw += 1.0f;
}
else if (IsKeyDown(KeyboardKey.A))
{
yaw -= 1.0f;
}
else
{
if (yaw > 0.0f)
{
yaw -= 0.5f;
}
else if (yaw < 0.0f)
{
yaw += 0.5f;
}
}
// Plane roll (z-axis) controls
if (IsKeyDown(KeyboardKey.Left))
{
roll += 1.0f;
}
else if (IsKeyDown(KeyboardKey.Right))
{
roll -= 1.0f;
}
else
{
if (roll > 0.0f)
{
roll -= 0.5f;
}
else if (roll < 0.0f)
{
roll += 0.5f;
}
}
// Tranformation matrix for rotations
model.Transform = MatrixRotateXYZ(new Vector3(DEG2RAD * pitch, DEG2RAD * yaw, DEG2RAD * roll));
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
// Draw 3D model (recomended to draw 3D always before 2D)
BeginMode3D(camera);
// Draw 3d model with texture
DrawModel(model, new Vector3(0.0f, -8.0f, 0.0f), 1.0f, Color.White);
DrawGrid(10, 10.0f);
EndMode3D();
// Draw controls info
DrawRectangle(30, 370, 260, 70, Fade(Color.Green, 0.5f));
DrawRectangleLines(30, 370, 260, 70, Fade(Color.DarkGreen, 0.5f));
DrawText("Pitch controlled with: KEY_UP / KEY_DOWN", 40, 380, 10, Color.DarkGray);
DrawText("Roll controlled with: KEY_LEFT / KEY_RIGHT", 40, 400, 10, Color.DarkGray);
DrawText("Yaw controlled with: KEY_A / KEY_S", 40, 420, 10, Color.DarkGray);
DrawText(
"(c) WWI Plane Model created by GiaHanLam",
screenWidth - 240,
screenHeight - 20,
10,
Color.DarkGray
);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadModel(model);
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - plane rotations (yaw, pitch, roll)");
Camera3D camera = new();
camera.Position = new Vector3(0.0f, 50.0f, -120.0f);
camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
camera.FovY = 30.0f;
camera.Projection = CameraProjection.Perspective;
// Model loading
Model model = LoadModel("resources/models/obj/plane.obj");
Texture2D texture = LoadTexture("resources/models/obj/plane_diffuse.png");
model.Materials[0].Maps[(int)MaterialMapIndex.Diffuse].Texture = texture;
float pitch = 0.0f;
float roll = 0.0f;
float yaw = 0.0f;
InitWindow(screenWidth, screenHeight, "raylib [models] example - yaw pitch roll");
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
while (!WindowShouldClose())
var game = new YawPitchRoll();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Plane roll (x-axis) controls
if (IsKeyDown(KeyboardKey.Down))
{
pitch += 0.6f;
}
else if (IsKeyDown(KeyboardKey.Up))
{
pitch -= 0.6f;
}
else
{
if (pitch > 0.3f)
{
pitch -= 0.3f;
}
else if (pitch < -0.3f)
{
pitch += 0.3f;
}
}
// Plane yaw (y-axis) controls
if (IsKeyDown(KeyboardKey.S))
{
yaw += 1.0f;
}
else if (IsKeyDown(KeyboardKey.A))
{
yaw -= 1.0f;
}
else
{
if (yaw > 0.0f)
{
yaw -= 0.5f;
}
else if (yaw < 0.0f)
{
yaw += 0.5f;
}
}
// Plane pitch (z-axis) controls
if (IsKeyDown(KeyboardKey.Left))
{
roll += 1.0f;
}
else if (IsKeyDown(KeyboardKey.Right))
{
roll -= 1.0f;
}
else
{
if (roll > 0.0f)
{
roll -= 0.5f;
}
else if (roll < 0.0f)
{
roll += 0.5f;
}
}
// Tranformation matrix for rotations
model.Transform = MatrixRotateXYZ(new Vector3(DEG2RAD * pitch, DEG2RAD * yaw, DEG2RAD * roll));
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
// Draw 3D model (recomended to draw 3D always before 2D)
BeginMode3D(camera);
// Draw 3d model with texture
DrawModel(model, new Vector3(0.0f, -8.0f, 0.0f), 1.0f, Color.White);
DrawGrid(10, 10.0f);
EndMode3D();
// Draw controls info
DrawRectangle(30, 370, 260, 70, Fade(Color.Green, 0.5f));
DrawRectangleLines(30, 370, 260, 70, Fade(Color.DarkGreen, 0.5f));
DrawText("Pitch controlled with: KEY_UP / KEY_DOWN", 40, 380, 10, Color.DarkGray);
DrawText("Roll controlled with: KEY_LEFT / KEY_RIGHT", 40, 400, 10, Color.DarkGray);
DrawText("Yaw controlled with: KEY_A / KEY_S", 40, 420, 10, Color.DarkGray);
DrawText(
"(c) WWI Plane Model created by GiaHanLam",
screenWidth - 240,
screenHeight - 20,
10,
Color.DarkGray
);
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadModel(model);
CloseWindow();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;