mirror of
https://github.com/raylib-cs/raylib-cs
synced 2025-06-30 19:03:42 -04:00
Update/merge examples back into main repo (#192)
This commit is contained in:
126
Examples/Models/AnimationDemo.cs
Normal file
126
Examples/Models/AnimationDemo.cs
Normal file
@ -0,0 +1,126 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Load 3d model with animations and play them
|
||||
*
|
||||
* 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 contributed by Culacant (@culacant) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Copyright (c) 2019 Culacant (@culacant) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************
|
||||
*
|
||||
* To export a model from blender, make sure it is not posed, the vertices need to be in the
|
||||
* same position as they would be in edit mode.
|
||||
* and that the scale of your models is set to 0. Scaling can be done from the export menu.
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class AnimationDemo
|
||||
{
|
||||
public unsafe static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
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, 0.0f, 0.0f);
|
||||
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
|
||||
camera.FovY = 45.0f;
|
||||
camera.Projection = CameraProjection.CAMERA_PERSPECTIVE;
|
||||
|
||||
Model model = LoadModel("resources/models/iqm/guy.iqm");
|
||||
Texture2D texture = LoadTexture("resources/models/iqm/guytex.png");
|
||||
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.MATERIAL_MAP_ALBEDO, ref texture);
|
||||
|
||||
Vector3 position = new(0.0f, 0.0f, 0.0f);
|
||||
// Load animation data
|
||||
uint animsCount = 0;
|
||||
var anims = LoadModelAnimations("resources/models/iqm/guyanim.iqm", ref animsCount);
|
||||
int animFrameCounter = 0;
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(ref camera, CameraMode.CAMERA_FREE);
|
||||
|
||||
// Play animation when spacebar is held down
|
||||
if (IsKeyDown(KeyboardKey.KEY_SPACE))
|
||||
{
|
||||
animFrameCounter++;
|
||||
UpdateModelAnimation(model, anims[0], animFrameCounter);
|
||||
if (animFrameCounter >= anims[0].FrameCount)
|
||||
{
|
||||
animFrameCounter = 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
|
||||
);
|
||||
|
||||
for (int i = 0; i < model.BoneCount; i++)
|
||||
{
|
||||
var framePoses = anims[0].FramePoses;
|
||||
DrawCube(framePoses[animFrameCounter][i].Translation, 0.2f, 0.2f, 0.2f, Color.RED);
|
||||
}
|
||||
|
||||
DrawGrid(10, 1.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawText("PRESS SPACE to PLAY MODEL ANIMATION", 10, 10, 20, Color.MAROON);
|
||||
DrawText("(c) Guy IQM 3D model by @culacant", screenWidth - 200, screenHeight - 20, 10, Color.GRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(texture);
|
||||
|
||||
for (int i = 0; i < animsCount; i++)
|
||||
{
|
||||
UnloadModelAnimation(anims[i]);
|
||||
}
|
||||
|
||||
UnloadModel(model);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
134
Examples/Models/BillboardDemo.cs
Normal file
134
Examples/Models/BillboardDemo.cs
Normal file
@ -0,0 +1,134 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Drawing billboards
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class BillboardDemo
|
||||
{
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
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.CAMERA_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);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(ref camera, CameraMode.CAMERA_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();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(bill);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
144
Examples/Models/BoxCollisions.cs
Normal file
144
Examples/Models/BoxCollisions.cs
Normal file
@ -0,0 +1,144 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Detect basic 3d collisions (box vs sphere vs box)
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class BoxCollisions
|
||||
{
|
||||
public static int Main()
|
||||
{
|
||||
// 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.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.CAMERA_PERSPECTIVE;
|
||||
|
||||
Vector3 playerPosition = new(0.0f, 1.0f, 2.0f);
|
||||
Vector3 playerSize = new(1.0f, 2.0f, 1.0f);
|
||||
Color playerColor = Color.GREEN;
|
||||
|
||||
Vector3 enemyBoxPos = new(-4.0f, 1.0f, 0.0f);
|
||||
Vector3 enemyBoxSize = new(2.0f, 2.0f, 2.0f);
|
||||
|
||||
Vector3 enemySpherePos = new(4.0f, 0.0f, 0.0f);
|
||||
float enemySphereSize = 1.5f;
|
||||
|
||||
bool collision = false;
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Move player
|
||||
if (IsKeyDown(KeyboardKey.KEY_RIGHT))
|
||||
{
|
||||
playerPosition.X += 0.2f;
|
||||
}
|
||||
else if (IsKeyDown(KeyboardKey.KEY_LEFT))
|
||||
{
|
||||
playerPosition.X -= 0.2f;
|
||||
}
|
||||
else if (IsKeyDown(KeyboardKey.KEY_DOWN))
|
||||
{
|
||||
playerPosition.Z += 0.2f;
|
||||
}
|
||||
else if (IsKeyDown(KeyboardKey.KEY_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();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
104
Examples/Models/CubicmapDemo.cs
Normal file
104
Examples/Models/CubicmapDemo.cs
Normal file
@ -0,0 +1,104 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Cubicmap loading and drawing
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class CubicmapDemo
|
||||
{
|
||||
public static int Main()
|
||||
{
|
||||
// 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.CAMERA_PERSPECTIVE;
|
||||
|
||||
Image image = LoadImage("resources/cubicmap.png");
|
||||
Texture2D cubicmap = LoadTextureFromImage(image);
|
||||
|
||||
Mesh mesh = GenMeshCubicmap(image, new Vector3(1.0f, 1.0f, 1.0f));
|
||||
Model model = LoadModelFromMesh(mesh);
|
||||
|
||||
// NOTE: By default each cube is mapped to one part of texture atlas
|
||||
Texture2D texture = LoadTexture("resources/cubicmap_atlas.png");
|
||||
|
||||
// Set map diffuse texture
|
||||
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.MATERIAL_MAP_ALBEDO, ref texture);
|
||||
|
||||
Vector3 mapPosition = new(-16.0f, 0.0f, -8.0f);
|
||||
UnloadImage(image);
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(ref camera, CameraMode.CAMERA_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();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(cubicmap);
|
||||
UnloadTexture(texture);
|
||||
UnloadModel(model);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
155
Examples/Models/FirstPersonMaze.cs
Normal file
155
Examples/Models/FirstPersonMaze.cs
Normal file
@ -0,0 +1,155 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
* Copyright (c) 2019 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class FirstPersonMaze
|
||||
{
|
||||
public unsafe static int Main()
|
||||
{
|
||||
// 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.CAMERA_PERSPECTIVE;
|
||||
|
||||
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);
|
||||
|
||||
// NOTE: By default each cube is mapped to one part of texture atlas
|
||||
Texture2D texture = LoadTexture("resources/cubicmap_atlas.png");
|
||||
|
||||
// Set map diffuse texture
|
||||
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.MATERIAL_MAP_ALBEDO, ref texture);
|
||||
|
||||
// Get map image data to be used for collision detection
|
||||
Color* mapPixels = LoadImageColors(imMap);
|
||||
UnloadImage(imMap);
|
||||
|
||||
Vector3 mapPosition = new(-16.0f, 0.0f, -8.0f);
|
||||
Vector3 playerPosition = camera.Position;
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
Vector3 oldCamPos = camera.Position;
|
||||
|
||||
UpdateCamera(ref camera, CameraMode.CAMERA_FIRST_PERSON);
|
||||
|
||||
// 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)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
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)
|
||||
{
|
||||
// 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();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadImageColors(mapPixels);
|
||||
|
||||
UnloadTexture(cubicmap);
|
||||
UnloadTexture(texture);
|
||||
UnloadModel(model);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
85
Examples/Models/GeometricShapes.cs
Normal file
85
Examples/Models/GeometricShapes.cs
Normal file
@ -0,0 +1,85 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Draw some basic geometric shapes (cube, sphere, cylinder...)
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class GeometricShapes
|
||||
{
|
||||
public static int Main()
|
||||
{
|
||||
// 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.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.CAMERA_PERSPECTIVE;
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// 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();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
92
Examples/Models/HeightmapDemo.cs
Normal file
92
Examples/Models/HeightmapDemo.cs
Normal file
@ -0,0 +1,92 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Heightmap loading and drawing
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class HeightmapDemo
|
||||
{
|
||||
public static int Main()
|
||||
{
|
||||
// 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.CAMERA_PERSPECTIVE;
|
||||
|
||||
Image image = LoadImage("resources/heightmap.png");
|
||||
Texture2D texture = LoadTextureFromImage(image);
|
||||
|
||||
Mesh mesh = GenMeshHeightmap(image, new Vector3(16, 8, 16));
|
||||
Model model = LoadModelFromMesh(mesh);
|
||||
|
||||
// Set map diffuse texture
|
||||
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.MATERIAL_MAP_ALBEDO, ref texture);
|
||||
|
||||
Vector3 mapPosition = new(-8.0f, 0.0f, -8.0f);
|
||||
|
||||
UnloadImage(image);
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(ref camera, CameraMode.CAMERA_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();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(texture);
|
||||
UnloadModel(model);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
140
Examples/Models/MeshGeneration.cs
Normal file
140
Examples/Models/MeshGeneration.cs
Normal file
@ -0,0 +1,140 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib example - procedural 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)
|
||||
*
|
||||
* Copyright (c) 2017 Ramon Santamaria (Ray San)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class MeshGeneration
|
||||
{
|
||||
public static int Main()
|
||||
{
|
||||
// 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);
|
||||
UnloadImage(isChecked);
|
||||
|
||||
Model[] models = new Model[7];
|
||||
|
||||
models[0] = LoadModelFromMesh(GenMeshPlane(2, 2, 5, 5));
|
||||
models[1] = LoadModelFromMesh(GenMeshCube(2.0f, 1.0f, 2.0f));
|
||||
models[2] = LoadModelFromMesh(GenMeshSphere(2, 32, 32));
|
||||
models[3] = LoadModelFromMesh(GenMeshHemiSphere(2, 16, 16));
|
||||
models[4] = LoadModelFromMesh(GenMeshCylinder(1, 2, 16));
|
||||
models[5] = LoadModelFromMesh(GenMeshTorus(0.25f, 4.0f, 16, 32));
|
||||
models[6] = LoadModelFromMesh(GenMeshKnot(1.0f, 2.0f, 16, 128));
|
||||
|
||||
// Set isChecked texture as default diffuse component for all models material
|
||||
for (int i = 0; i < models.Length; i++)
|
||||
{
|
||||
// Set map diffuse texture
|
||||
Raylib.SetMaterialTexture(ref models[i], 0, MaterialMapIndex.MATERIAL_MAP_ALBEDO, ref texture);
|
||||
}
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera3D 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);
|
||||
camera.FovY = 45.0f;
|
||||
camera.Projection = CameraProjection.CAMERA_PERSPECTIVE;
|
||||
|
||||
// Model drawing position
|
||||
Vector3 position = new(0.0f, 0.0f, 0.0f);
|
||||
|
||||
int currentModel = 0;
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(ref camera, CameraMode.CAMERA_ORBITAL);
|
||||
|
||||
if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON))
|
||||
{
|
||||
// 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;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
for (int i = 0; i < models.Length; i++)
|
||||
{
|
||||
UnloadModel(models[i]);
|
||||
}
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
248
Examples/Models/MeshPicking.cs
Normal file
248
Examples/Models/MeshPicking.cs
Normal file
@ -0,0 +1,248 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Mesh picking in 3d mode, ground plane, triangle, mesh
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
||||
* Example contributed by Joel Davis (@joeld42)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
using static Raylib_cs.Raymath;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class MeshPicking
|
||||
{
|
||||
public unsafe static int Main()
|
||||
{
|
||||
// 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.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);
|
||||
camera.FovY = 45.0f;
|
||||
camera.Projection = CameraProjection.CAMERA_PERSPECTIVE;
|
||||
|
||||
// Picking ray
|
||||
Ray ray = new();
|
||||
|
||||
Model tower = LoadModel("resources/models/obj/turret.obj");
|
||||
Texture2D texture = LoadTexture("resources/models/obj/turret_diffuse.png");
|
||||
Raylib.SetMaterialTexture(ref tower, 0, MaterialMapIndex.MATERIAL_MAP_ALBEDO, ref texture);
|
||||
|
||||
Vector3 towerPos = new(0.0f, 0.0f, 0.0f);
|
||||
BoundingBox 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);
|
||||
|
||||
// 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);
|
||||
|
||||
Vector3 bary = new(0.0f, 0.0f, 0.0f);
|
||||
|
||||
// Test sphere
|
||||
Vector3 sp = new(-30.0f, 5.0f, 5.0f);
|
||||
float sr = 4.0f;
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Main game loop
|
||||
//--------------------------------------------------------------------------------------
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
//----------------------------------------------------------------------------------
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (IsCursorHidden())
|
||||
{
|
||||
UpdateCamera(ref camera, CameraMode.CAMERA_FIRST_PERSON);
|
||||
}
|
||||
|
||||
// Toggle camera controls
|
||||
if (IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_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 = GetMouseRay(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();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadModel(tower);
|
||||
UnloadTexture(texture);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
316
Examples/Models/ModelCubeTexture.cs
Normal file
316
Examples/Models/ModelCubeTexture.cs
Normal file
@ -0,0 +1,316 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Draw textured cube
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class ModelCubeTexture
|
||||
{
|
||||
public static int Main()
|
||||
{
|
||||
// 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.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.CAMERA_PERSPECTIVE;
|
||||
|
||||
// Load texture to be applied to the cubes sides
|
||||
Texture2D texture = LoadTexture("resources/cubicmap_atlas.png");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
// Draw cube with an applied texture
|
||||
DrawCubeTexture(texture, new Vector3(-2.0f, 2.0f, 0.0f), 2.0f, 4.0f, 2.0f, Color.WHITE);
|
||||
|
||||
// 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
|
||||
);
|
||||
|
||||
DrawGrid(10, 1.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(texture);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Draw cube textured
|
||||
// NOTE: Cube position is the center position
|
||||
static void DrawCubeTexture(
|
||||
Texture2D texture,
|
||||
Vector3 position,
|
||||
float width,
|
||||
float height,
|
||||
float length,
|
||||
Color color
|
||||
)
|
||||
{
|
||||
float x = position.X;
|
||||
float y = position.Y;
|
||||
float z = position.Z;
|
||||
|
||||
// Set desired texture to be enabled while drawing following vertex data
|
||||
Rlgl.SetTexture(texture.Id);
|
||||
|
||||
// Vertex data transformation can be defined with the commented lines,
|
||||
// but in this example we calculate the transformed vertex data directly when calling Rlgl.Vertex3f()
|
||||
// Rlgl.PushMatrix();
|
||||
// NOTE: Transformation is applied in inverse order (scale -> rotate -> translate)
|
||||
// Rlgl.Translatef(2.0f, 0.0f, 0.0f);
|
||||
// Rlgl.Rotatef(45, 0, 1, 0);
|
||||
// Rlgl.Scalef(2.0f, 2.0f, 2.0f);
|
||||
|
||||
Rlgl.Begin(DrawMode.QUADS);
|
||||
Rlgl.Color4ub(color.R, color.G, color.B, color.A);
|
||||
|
||||
// Front Face
|
||||
// Normal Pointing Towards Viewer
|
||||
Rlgl.Normal3f(0.0f, 0.0f, 1.0f);
|
||||
Rlgl.TexCoord2f(0.0f, 0.0f);
|
||||
// Bottom Left Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x - width / 2, y - height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f(1.0f, 0.0f);
|
||||
// Bottom Right Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x + width / 2, y - height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f(1.0f, 1.0f);
|
||||
// Top Right Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x + width / 2, y + height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f(0.0f, 1.0f);
|
||||
// Top Left Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x - width / 2, y + height / 2, z + length / 2);
|
||||
|
||||
// Back Face
|
||||
// Normal Pointing Away From Viewer
|
||||
Rlgl.Normal3f(0.0f, 0.0f, -1.0f);
|
||||
Rlgl.TexCoord2f(1.0f, 0.0f);
|
||||
// Bottom Right Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x - width / 2, y - height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(1.0f, 1.0f);
|
||||
// Top Right Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x - width / 2, y + height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(0.0f, 1.0f);
|
||||
// Top Left Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x + width / 2, y + height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(0.0f, 0.0f);
|
||||
// Bottom Left Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x + width / 2, y - height / 2, z - length / 2);
|
||||
|
||||
// Top Face
|
||||
// Normal Pointing Up
|
||||
Rlgl.Normal3f(0.0f, 1.0f, 0.0f);
|
||||
Rlgl.TexCoord2f(0.0f, 1.0f);
|
||||
// Top Left Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x - width / 2, y + height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(0.0f, 0.0f);
|
||||
// Bottom Left Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x - width / 2, y + height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f(1.0f, 0.0f);
|
||||
// Bottom Right Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x + width / 2, y + height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f(1.0f, 1.0f);
|
||||
// Top Right Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x + width / 2, y + height / 2, z - length / 2);
|
||||
|
||||
// Bottom Face
|
||||
// Normal Pointing Down
|
||||
Rlgl.Normal3f(0.0f, -1.0f, 0.0f);
|
||||
Rlgl.TexCoord2f(1.0f, 1.0f);
|
||||
// Top Right Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x - width / 2, y - height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(0.0f, 1.0f);
|
||||
// Top Left Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x + width / 2, y - height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(0.0f, 0.0f);
|
||||
// Bottom Left Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x + width / 2, y - height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f(1.0f, 0.0f);
|
||||
// Bottom Right Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x - width / 2, y - height / 2, z + length / 2);
|
||||
|
||||
// Right face
|
||||
// Normal Pointing Right
|
||||
Rlgl.Normal3f(1.0f, 0.0f, 0.0f);
|
||||
Rlgl.TexCoord2f(1.0f, 0.0f);
|
||||
// Bottom Right Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x + width / 2, y - height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(1.0f, 1.0f);
|
||||
// Top Right Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x + width / 2, y + height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(0.0f, 1.0f);
|
||||
// Top Left Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x + width / 2, y + height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f(0.0f, 0.0f);
|
||||
// Bottom Left Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x + width / 2, y - height / 2, z + length / 2);
|
||||
|
||||
// Left Face
|
||||
// Normal Pointing Left
|
||||
Rlgl.Normal3f(-1.0f, 0.0f, 0.0f);
|
||||
Rlgl.TexCoord2f(0.0f, 0.0f);
|
||||
// Bottom Left Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x - width / 2, y - height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(1.0f, 0.0f);
|
||||
// Bottom Right Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x - width / 2, y - height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f(1.0f, 1.0f);
|
||||
// Top Right Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x - width / 2, y + height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f(0.0f, 1.0f);
|
||||
// Top Left Of The Texture and Quad
|
||||
Rlgl.Vertex3f(x - width / 2, y + height / 2, z - length / 2);
|
||||
Rlgl.End();
|
||||
//rlPopMatrix();
|
||||
|
||||
Rlgl.SetTexture(0);
|
||||
}
|
||||
|
||||
// Draw cube with texture piece applied to all faces
|
||||
static void DrawCubeTextureRec(
|
||||
Texture2D texture,
|
||||
Rectangle source,
|
||||
Vector3 position,
|
||||
float width,
|
||||
float height,
|
||||
float length,
|
||||
Color color
|
||||
)
|
||||
{
|
||||
float x = position.X;
|
||||
float y = position.Y;
|
||||
float z = position.Z;
|
||||
float texWidth = (float)texture.Width;
|
||||
float texHeight = (float)texture.Height;
|
||||
|
||||
// Set desired texture to be enabled while drawing following vertex data
|
||||
Rlgl.SetTexture(texture.Id);
|
||||
|
||||
// We calculate the normalized texture coordinates for the desired texture-source-rectangle
|
||||
// It means converting from (tex.Width, tex.Height) coordinates to [0.0f, 1.0f] equivalent
|
||||
Rlgl.Begin(DrawMode.QUADS);
|
||||
Rlgl.Color4ub(color.R, color.G, color.B, color.A);
|
||||
|
||||
// Front face
|
||||
Rlgl.Normal3f(0.0f, 0.0f, 1.0f);
|
||||
Rlgl.TexCoord2f(source.X / texWidth, (source.Y + source.Height) / texHeight);
|
||||
Rlgl.Vertex3f(x - width / 2, y - height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f((source.X + source.Width) / texWidth, (source.Y + source.Height) / texHeight);
|
||||
Rlgl.Vertex3f(x + width / 2, y - height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f((source.X + source.Width) / texWidth, source.Y / texHeight);
|
||||
Rlgl.Vertex3f(x + width / 2, y + height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f(source.X / texWidth, source.Y / texHeight);
|
||||
Rlgl.Vertex3f(x - width / 2, y + height / 2, z + length / 2);
|
||||
|
||||
// Back face
|
||||
Rlgl.Normal3f(0.0f, 0.0f, -1.0f);
|
||||
Rlgl.TexCoord2f((source.X + source.Width) / texWidth, (source.Y + source.Height) / texHeight);
|
||||
Rlgl.Vertex3f(x - width / 2, y - height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f((source.X + source.Width) / texWidth, source.Y / texHeight);
|
||||
Rlgl.Vertex3f(x - width / 2, y + height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(source.X / texWidth, source.Y / texHeight);
|
||||
Rlgl.Vertex3f(x + width / 2, y + height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(source.X / texWidth, (source.Y + source.Height) / texHeight);
|
||||
Rlgl.Vertex3f(x + width / 2, y - height / 2, z - length / 2);
|
||||
|
||||
// Top face
|
||||
Rlgl.Normal3f(0.0f, 1.0f, 0.0f);
|
||||
Rlgl.TexCoord2f(source.X / texWidth, source.Y / texHeight);
|
||||
Rlgl.Vertex3f(x - width / 2, y + height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(source.X / texWidth, (source.Y + source.Height) / texHeight);
|
||||
Rlgl.Vertex3f(x - width / 2, y + height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f((source.X + source.Width) / texWidth, (source.Y + source.Height) / texHeight);
|
||||
Rlgl.Vertex3f(x + width / 2, y + height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f((source.X + source.Width) / texWidth, source.Y / texHeight);
|
||||
Rlgl.Vertex3f(x + width / 2, y + height / 2, z - length / 2);
|
||||
|
||||
// Bottom face
|
||||
Rlgl.Normal3f(0.0f, -1.0f, 0.0f);
|
||||
Rlgl.TexCoord2f((source.X + source.Width) / texWidth, source.Y / texHeight);
|
||||
Rlgl.Vertex3f(x - width / 2, y - height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(source.X / texWidth, source.Y / texHeight);
|
||||
Rlgl.Vertex3f(x + width / 2, y - height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(source.X / texWidth, (source.Y + source.Height) / texHeight);
|
||||
Rlgl.Vertex3f(x + width / 2, y - height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f((source.X + source.Width) / texWidth, (source.Y + source.Height) / texHeight);
|
||||
Rlgl.Vertex3f(x - width / 2, y - height / 2, z + length / 2);
|
||||
|
||||
// Right face
|
||||
Rlgl.Normal3f(1.0f, 0.0f, 0.0f);
|
||||
Rlgl.TexCoord2f((source.X + source.Width) / texWidth, (source.Y + source.Height) / texHeight);
|
||||
Rlgl.Vertex3f(x + width / 2, y - height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f((source.X + source.Width) / texWidth, source.Y / texHeight);
|
||||
Rlgl.Vertex3f(x + width / 2, y + height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f(source.X / texWidth, source.Y / texHeight);
|
||||
Rlgl.Vertex3f(x + width / 2, y + height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f(source.X / texWidth, (source.Y + source.Height) / texHeight);
|
||||
Rlgl.Vertex3f(x + width / 2, y - height / 2, z + length / 2);
|
||||
|
||||
// Left face
|
||||
Rlgl.Normal3f(-1.0f, 0.0f, 0.0f);
|
||||
Rlgl.TexCoord2f(source.X / texWidth, (source.Y + source.Height) / texHeight);
|
||||
Rlgl.Vertex3f(x - width / 2, y - height / 2, z - length / 2);
|
||||
Rlgl.TexCoord2f((source.X + source.Width) / texWidth, (source.Y + source.Height) / texHeight);
|
||||
Rlgl.Vertex3f(x - width / 2, y - height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f((source.X + source.Width) / texWidth, source.Y / texHeight);
|
||||
Rlgl.Vertex3f(x - width / 2, y + height / 2, z + length / 2);
|
||||
Rlgl.TexCoord2f(source.X / texWidth, source.Y / texHeight);
|
||||
Rlgl.Vertex3f(x - width / 2, y + height / 2, z - length / 2);
|
||||
|
||||
Rlgl.End();
|
||||
|
||||
Rlgl.SetTexture(0);
|
||||
}
|
||||
}
|
||||
|
157
Examples/Models/ModelLoading.cs
Normal file
157
Examples/Models/ModelLoading.cs
Normal file
@ -0,0 +1,157 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Models loading
|
||||
*
|
||||
* raylib supports multiple models file formats:
|
||||
*
|
||||
* - 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.
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class ModelLoading
|
||||
{
|
||||
public unsafe static int Main()
|
||||
{
|
||||
// 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.CAMERA_PERSPECTIVE;
|
||||
|
||||
Model model = LoadModel("resources/models/obj/castle.obj");
|
||||
Texture2D texture = LoadTexture("resources/models/obj/castle_diffuse.png");
|
||||
|
||||
// Set map diffuse texture
|
||||
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.MATERIAL_MAP_ALBEDO, ref texture);
|
||||
|
||||
Vector3 position = new(0.0f, 0.0f, 0.0f);
|
||||
BoundingBox bounds = GetMeshBoundingBox(model.Meshes[0]);
|
||||
|
||||
// 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;
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(ref camera, CameraMode.CAMERA_FREE);
|
||||
|
||||
if (IsFileDropped())
|
||||
{
|
||||
string[] files = Raylib.GetDroppedFiles();
|
||||
|
||||
if (files.Length == 1)
|
||||
{
|
||||
if (IsFileExtension(files[0], ".obj") ||
|
||||
IsFileExtension(files[0], ".gltf") ||
|
||||
IsFileExtension(files[0], ".iqm")
|
||||
)
|
||||
{
|
||||
UnloadModel(model);
|
||||
model = LoadModel(files[0]);
|
||||
|
||||
// Set current map diffuse texture
|
||||
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.MATERIAL_MAP_ALBEDO, ref texture);
|
||||
|
||||
bounds = GetMeshBoundingBox(model.Meshes[0]);
|
||||
|
||||
// TODO: Move camera position from target enough distance to visualize model properly
|
||||
}
|
||||
else if (IsFileExtension(files[0], ".png"))
|
||||
{
|
||||
// Unload model texture and load new one
|
||||
UnloadTexture(texture);
|
||||
texture = LoadTexture(files[0]);
|
||||
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.MATERIAL_MAP_ALBEDO, ref texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Select model on mouse click
|
||||
if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON))
|
||||
{
|
||||
// Check collision between ray and box
|
||||
if (GetRayCollisionBox(GetMouseRay(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();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(texture);
|
||||
UnloadModel(model);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
113
Examples/Models/OrthographicProjection.cs
Normal file
113
Examples/Models/OrthographicProjection.cs
Normal file
@ -0,0 +1,113 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Show the difference between perspective and orthographic projection
|
||||
*
|
||||
* This program is heavily based on the geometric objects example
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
* Copyright (c) 2018 Max Danielsson ref Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class OrthographicProjection
|
||||
{
|
||||
public const float FOVY_PERSPECTIVE = 45.0f;
|
||||
public const float WIDTH_ORTHOGRAPHIC = 10.0f;
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// 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.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.CAMERA_PERSPECTIVE;
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (IsKeyPressed(KeyboardKey.KEY_SPACE))
|
||||
{
|
||||
if (camera.Projection == CameraProjection.CAMERA_PERSPECTIVE)
|
||||
{
|
||||
camera.FovY = WIDTH_ORTHOGRAPHIC;
|
||||
camera.Projection = CameraProjection.CAMERA_ORTHOGRAPHIC;
|
||||
}
|
||||
else
|
||||
{
|
||||
camera.FovY = FOVY_PERSPECTIVE;
|
||||
camera.Projection = CameraProjection.CAMERA_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.CAMERA_ORTHOGRAPHIC)
|
||||
{
|
||||
DrawText("ORTHOGRAPHIC", 10, 40, 20, Color.BLACK);
|
||||
}
|
||||
else if (camera.Projection == CameraProjection.CAMERA_PERSPECTIVE)
|
||||
{
|
||||
DrawText("PERSPECTIVE", 10, 40, 20, Color.BLACK);
|
||||
}
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
138
Examples/Models/SkyboxDemo.cs
Normal file
138
Examples/Models/SkyboxDemo.cs
Normal file
@ -0,0 +1,138 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Skybox loading and drawing
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
* Copyright (c) 2017 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class SkyboxDemo
|
||||
{
|
||||
public static int Main()
|
||||
{
|
||||
// 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.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);
|
||||
camera.FovY = 45.0f;
|
||||
camera.Projection = CameraProjection.CAMERA_PERSPECTIVE;
|
||||
|
||||
// Load skybox model
|
||||
Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
|
||||
Model skybox = LoadModelFromMesh(cube);
|
||||
|
||||
// Load skybox shader and set required locations
|
||||
// NOTE: Some locations are automatically set at shader loading
|
||||
Shader shader = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/skybox.fs");
|
||||
Raylib.SetMaterialShader(ref skybox, 0, ref shader);
|
||||
Raylib.SetShaderValue(
|
||||
shader,
|
||||
GetShaderLocation(shader, "environmentMap"),
|
||||
(int)MaterialMapIndex.MATERIAL_MAP_CUBEMAP,
|
||||
ShaderUniformDataType.SHADER_UNIFORM_INT
|
||||
);
|
||||
Raylib.SetShaderValue(
|
||||
shader,
|
||||
GetShaderLocation(shader, "vflipped"),
|
||||
1,
|
||||
ShaderUniformDataType.SHADER_UNIFORM_INT
|
||||
);
|
||||
|
||||
// Load cubemap shader and setup required shader locations
|
||||
Shader shdrCubemap = LoadShader(
|
||||
"resources/shaders/glsl330/cubemap.vs",
|
||||
"resources/shaders/glsl330/cubemap.fs"
|
||||
);
|
||||
Raylib.SetShaderValue(
|
||||
shdrCubemap,
|
||||
GetShaderLocation(shdrCubemap, "equirectangularMap"),
|
||||
0,
|
||||
ShaderUniformDataType.SHADER_UNIFORM_INT
|
||||
);
|
||||
|
||||
// Load HDR panorama (sphere) texture
|
||||
string panoFileName = "resources/dresden_square_2k.hdr";
|
||||
Texture2D panorama = LoadTexture(panoFileName);
|
||||
|
||||
// Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture
|
||||
// NOTE: New texture is generated rendering to texture, shader computes the sphre->cube coordinates mapping
|
||||
// Texture2D cubemap = PBR.GenTextureCubemap(shdrCubemap, panorama, 1024, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
|
||||
// Raylib.SetMaterialTexture(ref skybox, 0, MaterialMapIndex.MATERIAL_MAP_CUBEMAP, ref cubemap);
|
||||
// Texture not required anymore, cubemap already generated
|
||||
UnloadTexture(panorama);
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(ref camera, CameraMode.CAMERA_FIRST_PERSON);
|
||||
|
||||
// Load new cubemap texture on drag & drop
|
||||
if (IsFileDropped())
|
||||
{
|
||||
string[] files = Raylib.GetDroppedFiles();
|
||||
|
||||
if (files.Length == 1)
|
||||
{
|
||||
if (IsFileExtension(files[0], ".png;.jpg;.hdr;.bmp;.tga"))
|
||||
{
|
||||
// Unload cubemap texture and load new one
|
||||
UnloadTexture(Raylib.GetMaterialTexture(ref skybox, 0, MaterialMapIndex.MATERIAL_MAP_CUBEMAP));
|
||||
panorama = LoadTexture(files[0]);
|
||||
panoFileName = files[0];
|
||||
|
||||
// Generate cubemap from panorama texture
|
||||
// cubemap = PBR.GenTextureCubemap(shdrCubemap, panorama, 1024, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
|
||||
// Raylib.SetMaterialTexture(ref skybox, 0, MaterialMapIndex.MATERIAL_MAP_CUBEMAP, ref cubemap);
|
||||
UnloadTexture(panorama);
|
||||
}
|
||||
}
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
DrawModel(skybox, new Vector3(0, 0, 0), 1.0f, Color.WHITE);
|
||||
DrawGrid(10, 1.0f);
|
||||
EndMode3D();
|
||||
|
||||
DrawFPS(10, 10);
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadModel(skybox);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
196
Examples/Models/SolarSystem.cs
Normal file
196
Examples/Models/SolarSystem.cs
Normal file
@ -0,0 +1,196 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - rlgl module usage with push/pop matrix transformations
|
||||
*
|
||||
* This example uses [rlgl] module funtionality (pseudo-OpenGL 1.1 style coding)
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
* Copyright (c) 2018 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class SolarSystem
|
||||
{
|
||||
public static int Main()
|
||||
{
|
||||
// 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.CAMERA_PERSPECTIVE;
|
||||
|
||||
// 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;
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(ref camera, CameraMode.CAMERA_FREE);
|
||||
|
||||
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);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
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);
|
||||
// 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);
|
||||
|
||||
// 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 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();
|
||||
|
||||
// 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();
|
||||
|
||||
DrawText("EARTH ORBITING AROUND THE SUN!", 400, 10, 20, Color.MAROON);
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
int rings = 16;
|
||||
int 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 (int j = 0; j < slices; j++)
|
||||
{
|
||||
Rlgl.Vertex3f(
|
||||
MathF.Cos(DEG2RAD * (270 + (180 / (rings + 1)) * i)) * MathF.Sin(DEG2RAD * (j * 360 / slices)),
|
||||
MathF.Sin(DEG2RAD * (270 + (180 / (rings + 1)) * i)),
|
||||
MathF.Cos(DEG2RAD * (270 + (180 / (rings + 1)) * i)) * MathF.Cos(DEG2RAD * (j * 360 / slices))
|
||||
);
|
||||
Rlgl.Vertex3f(
|
||||
MathF.Cos(DEG2RAD * (270 + (180 / (rings + 1)) * (i + 1))) * MathF.Sin(DEG2RAD * ((j + 1) * 360 / slices)),
|
||||
MathF.Sin(DEG2RAD * (270 + (180 / (rings + 1)) * (i + 1))),
|
||||
MathF.Cos(DEG2RAD * (270 + (180 / (rings + 1)) * (i + 1))) * MathF.Cos(DEG2RAD * ((j + 1) * 360 / slices))
|
||||
);
|
||||
Rlgl.Vertex3f(
|
||||
MathF.Cos(DEG2RAD * (270 + (180 / (rings + 1)) * (i + 1))) * MathF.Sin(DEG2RAD * (j * 360 / slices)),
|
||||
MathF.Sin(DEG2RAD * (270 + (180 / (rings + 1)) * (i + 1))),
|
||||
MathF.Cos(DEG2RAD * (270 + (180 / (rings + 1)) * (i + 1))) * MathF.Cos(DEG2RAD * (j * 360 / slices))
|
||||
);
|
||||
|
||||
Rlgl.Vertex3f(
|
||||
MathF.Cos(DEG2RAD * (270 + (180 / (rings + 1)) * i)) * MathF.Sin(DEG2RAD * (j * 360 / slices)),
|
||||
MathF.Sin(DEG2RAD * (270 + (180 / (rings + 1)) * i)),
|
||||
MathF.Cos(DEG2RAD * (270 + (180 / (rings + 1)) * i)) * MathF.Cos(DEG2RAD * (j * 360 / slices))
|
||||
);
|
||||
Rlgl.Vertex3f(
|
||||
MathF.Cos(DEG2RAD * (270 + (180 / (rings + 1)) * (i))) * MathF.Sin(DEG2RAD * ((j + 1) * 360 / slices)),
|
||||
MathF.Sin(DEG2RAD * (270 + (180 / (rings + 1)) * (i))),
|
||||
MathF.Cos(DEG2RAD * (270 + (180 / (rings + 1)) * (i))) * MathF.Cos(DEG2RAD * ((j + 1) * 360 / slices))
|
||||
);
|
||||
Rlgl.Vertex3f(
|
||||
MathF.Cos(DEG2RAD * (270 + (180 / (rings + 1)) * (i + 1))) * MathF.Sin(DEG2RAD * ((j + 1) * 360 / slices)),
|
||||
MathF.Sin(DEG2RAD * (270 + (180 / (rings + 1)) * (i + 1))),
|
||||
MathF.Cos(DEG2RAD * (270 + (180 / (rings + 1)) * (i + 1))) * MathF.Cos(DEG2RAD * ((j + 1) * 360 / slices))
|
||||
);
|
||||
}
|
||||
}
|
||||
Rlgl.End();
|
||||
}
|
||||
}
|
||||
|
117
Examples/Models/WavingCubes.cs
Normal file
117
Examples/Models/WavingCubes.cs
Normal file
@ -0,0 +1,117 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* 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 contributed by Codecat (@codecat) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Copyright (c) 2019 Codecat (@codecat) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class WavingCubes
|
||||
{
|
||||
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.CAMERA_PERSPECTIVE;
|
||||
|
||||
// Specify the amount of blocks in each direction
|
||||
const int numBlocks = 15;
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// 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();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
164
Examples/Models/YawPitchRoll.cs
Normal file
164
Examples/Models/YawPitchRoll.cs
Normal file
@ -0,0 +1,164 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Plane rotations (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 contributed by Berni (@Berni8k) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Copyright (c) 2017-2021 Berni (@Berni8k) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
using static Raylib_cs.Raymath;
|
||||
|
||||
namespace Examples.Models;
|
||||
|
||||
public class YawPitchRoll
|
||||
{
|
||||
public unsafe 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.CAMERA_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.MATERIAL_MAP_DIFFUSE].Texture = texture;
|
||||
|
||||
float pitch = 0.0f;
|
||||
float roll = 0.0f;
|
||||
float yaw = 0.0f;
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Plane roll (x-axis) controls
|
||||
if (IsKeyDown(KeyboardKey.KEY_DOWN))
|
||||
{
|
||||
pitch += 0.6f;
|
||||
}
|
||||
else if (IsKeyDown(KeyboardKey.KEY_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.KEY_S))
|
||||
{
|
||||
yaw += 1.0f;
|
||||
}
|
||||
else if (IsKeyDown(KeyboardKey.KEY_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.KEY_LEFT))
|
||||
{
|
||||
roll += 1.0f;
|
||||
}
|
||||
else if (IsKeyDown(KeyboardKey.KEY_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();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadModel(model);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user