Skip to content

Commit 19b3bae

Browse files
author
ChrisDill
committed
Updating core and model examples.
- Removed unsafe from core_drop_files. - Added Utils to help with model examples. - Updated more model examples.
1 parent 1fed6ef commit 19b3bae

File tree

5 files changed

+46
-32
lines changed

5 files changed

+46
-32
lines changed

Examples/Utils.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.InteropServices;
23
using Raylib_cs;
34
using static Raylib_cs.MaterialMapType;
45

@@ -87,11 +88,24 @@ public static string[] MarshalDroppedFiles(ref int count)
8788
return s;
8889
}
8990

91+
public unsafe static Material GetMaterial(ref Model model, int materialIndex)
92+
{
93+
Material *materials = (Material*)model.materials.ToPointer();
94+
return *materials;
95+
}
96+
97+
public unsafe static Texture2D GetMaterialTexture(ref Model model, int materialIndex, MaterialMapType mapIndex)
98+
{
99+
Material *materials = (Material*)model.materials.ToPointer();
100+
MaterialMap* maps = (MaterialMap*)materials[0].maps.ToPointer();
101+
return maps[(int)mapIndex].texture;
102+
}
103+
90104
public unsafe static void SetMaterialTexture(ref Model model, int materialIndex, MaterialMapType mapIndex, ref Texture2D texture)
91105
{
92106
Material *materials = (Material*)model.materials.ToPointer();
93107
MaterialMap* maps = (MaterialMap*)materials[0].maps.ToPointer();
94-
maps[(int)MAP_ALBEDO].texture = texture;
108+
maps[(int)mapIndex].texture = texture;
95109
}
96110

97111
public unsafe static void SetMaterialShader(ref Model model, int materialIndex, ref Shader shader)

Examples/core/core_drop_files.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ namespace Examples
1818
{
1919
public class core_drop_files
2020
{
21-
22-
23-
public unsafe static int Main()
21+
public static int Main()
2422
{
2523
// Initialization
2624
//--------------------------------------------------------------------------------------
@@ -44,7 +42,6 @@ public unsafe static int Main()
4442
{
4543
droppedFiles = Utils.MarshalDroppedFiles(ref count);
4644
}
47-
4845
//----------------------------------------------------------------------------------
4946

5047
// Draw

Examples/models/models_animation.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using static Raylib_cs.CameraType;
2525
using static Raylib_cs.CameraMode;
2626
using static Raylib_cs.KeyboardKey;
27+
using static Raylib_cs.MaterialMapType;
2728

2829
namespace Examples
2930
{
@@ -46,15 +47,18 @@ public unsafe static int Main()
4647
camera.fovy = 45.0f; // Camera field-of-view Y
4748
camera.type = CAMERA_PERSPECTIVE; // Camera mode type
4849

49-
Model model = LoadModel("resources/guy/guy.iqm"); // Load the animated model mesh and basic data
50-
Texture2D texture = LoadTexture("resources/guy/guytex.png"); // Load model texture and set material
51-
// SetMaterialTexture(model.materials[0], MAP_ALBEDO, texture); // Set model material map texture
50+
Model model = LoadModel("resources/guy/guy.iqm"); // Load the animated model mesh and basic data
51+
Texture2D texture = LoadTexture("resources/guy/guytex.png"); // Load model texture and set material
52+
Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); // Set model material map texture
5253

5354
Vector3 position = new Vector3(0.0f, 0.0f, 0.0f); // Set model position
5455

5556
// Load animation data
5657
int animsCount = 0;
57-
IntPtr anims = LoadModelAnimations("resources/guy/guyanim.iqm", ref animsCount);
58+
IntPtr animsPtr = LoadModelAnimations("resources/guy/guyanim.iqm", ref animsCount);
59+
60+
ModelAnimation *anims = (ModelAnimation*)animsPtr.ToPointer();
61+
5862
int animFrameCounter = 0;
5963

6064
SetCameraMode(camera, CAMERA_FREE); // Set free camera mode
@@ -73,8 +77,8 @@ public unsafe static int Main()
7377
if (IsKeyDown(KEY_SPACE))
7478
{
7579
animFrameCounter++;
76-
// UpdateModelAnimation(model, anims[0], animFrameCounter);
77-
// if (animFrameCounter >= anims[0].frameCount) animFrameCounter = 0;
80+
UpdateModelAnimation(model, anims[0], animFrameCounter);
81+
if (animFrameCounter >= anims[0].frameCount) animFrameCounter = 0;
7882
}
7983
//----------------------------------------------------------------------------------
8084

@@ -90,7 +94,8 @@ public unsafe static int Main()
9094

9195
for (int i = 0; i < model.boneCount; i++)
9296
{
93-
// DrawCube(anims[0].framePoses[animFrameCounter][i].translation, 0.2f, 0.2f, 0.2f, RED);
97+
Transform **framePoses = (Transform**)anims[0].framePoses.ToPointer();
98+
DrawCube(framePoses[animFrameCounter][i].translation, 0.2f, 0.2f, 0.2f, RED);
9499
}
95100

96101
DrawGrid(10, 1.0f); // Draw a grid
@@ -109,7 +114,7 @@ public unsafe static int Main()
109114
UnloadTexture(texture); // Unload texture
110115

111116
// Unload model animations data
112-
// for (int i = 0; i < animsCount; i++) UnloadModelAnimation(anims[i]);
117+
for (int i = 0; i < animsCount; i++) UnloadModelAnimation(anims[i]);
113118

114119
UnloadModel(model); // Unload model
115120

Examples/models/models_mesh_picking.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,18 @@ public unsafe static int Main()
3838
camera.position = new Vector3(20.0f, 20.0f, 20.0f); // Camera3D position
3939
camera.target = new Vector3(0.0f, 8.0f, 0.0f); // Camera3D looking at point
4040
camera.up = new Vector3(0.0f, 1.6f, 0.0f); // Camera3D up vector (rotation towards target)
41-
camera.fovy = 45.0f; // Camera3D field-of-view Y
42-
camera.type = CAMERA_PERSPECTIVE; // Camera3D mode type
41+
camera.fovy = 45.0f; // Camera3D field-of-view Y
42+
camera.type = CAMERA_PERSPECTIVE; // Camera3D mode type
4343

44-
Ray ray; // Picking ray
44+
Ray ray = new Ray(); // Picking ray
4545

4646
Model tower = LoadModel("resources/models/turret.obj"); // Load OBJ model
4747
Texture2D texture = LoadTexture("resources/models/turret_diffuse.png"); // Load model texture
48-
49-
// Set map diffuse texture
50-
Utils.SetMaterialTexture(ref tower, 0, MAP_ALBEDO, ref texture);
48+
Utils.SetMaterialTexture(ref tower, 0, MAP_ALBEDO, ref texture); // Set map diffuse texture
5149

5250
Vector3 towerPos = new Vector3(0.0f, 0.0f, 0.0f); // Set model position
53-
5451
Mesh* meshes = (Mesh*)tower.meshes.ToPointer();
55-
BoundingBox towerBBox = MeshBoundingBox(meshes[0]); // Get mesh bounding box
56-
52+
BoundingBox towerBBox = MeshBoundingBox(meshes[0]); // Get mesh bounding box
5753
bool hitMeshBBox = false;
5854
bool hitTriangle = false;
5955

@@ -112,7 +108,7 @@ public unsafe static int Main()
112108
}
113109
else hitTriangle = false;
114110

115-
RayHitInfo meshHitInfo;
111+
RayHitInfo meshHitInfo = new RayHitInfo();
116112

117113
// Check ray collision against bounding box first, before trying the full ray-mesh test
118114
if (CheckCollisionRayBox(ray, towerBBox))
@@ -121,7 +117,7 @@ public unsafe static int Main()
121117

122118
// Check ray collision against model
123119
// NOTE: It considers model.transform matrix!
124-
meshHitInfo = GetCollisionRayModel(ray, ref tower);
120+
meshHitInfo = GetCollisionRayModel(ray, tower);
125121

126122
if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance))
127123
{
@@ -133,6 +129,7 @@ public unsafe static int Main()
133129
}
134130
hitMeshBBox = false;
135131
//----------------------------------------------------------------------------------
132+
136133
// Draw
137134
//----------------------------------------------------------------------------------
138135
BeginDrawing();

Examples/models/models_skybox.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using static Raylib_cs.Raylib;
1414
using static Raylib_cs.Color;
1515
using static Raylib_cs.CameraMode;
16+
using static Raylib_cs.CameraType;
1617
using static Raylib_cs.MaterialMapType;
1718
using static Raylib_cs.ShaderUniformDataType;
1819

@@ -30,29 +31,29 @@ public unsafe static int Main()
3031
InitWindow(screenWidth, screenHeight, "raylib [models] example - skybox loading and drawing");
3132

3233
// Define the camera to look into our 3d world
33-
Camera3D camera = new Camera3D(new Vector3(1.0f, 1.0f, 1.0f), new Vector3(4.0f, 1.0f, 4.0f), new Vector3(0.0f, 1.0f, 0.0f), 45.0f, 0);
34+
Camera3D camera = new Camera3D(new Vector3(1.0f, 1.0f, 1.0f), new Vector3(4.0f, 1.0f, 4.0f), new Vector3(0.0f, 1.0f, 0.0f), 45.0f, CAMERA_PERSPECTIVE);
3435

3536
// Load skybox model
3637
Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
3738
Model skybox = LoadModelFromMesh(cube);
3839

3940
// Load skybox shader and set required locations
4041
// NOTE: Some locations are automatically set at shader loading
41-
Material * materials = (Material *)skybox.materials.ToPointer();
42-
materials[0].shader = LoadShader("resources/shaders/skybox.vs", "resources/shaders/skybox.fs");
43-
SetShaderValue(materials[0].shader, GetShaderLocation(materials[0].shader, "environmentMap"), new int[] { (int)MAP_CUBEMAP }, UNIFORM_INT);
42+
Shader shader = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/skybox.fs");
43+
Utils.SetMaterialShader(ref skybox, 0, ref shader);
44+
Utils.SetShaderValue(shader, GetShaderLocation(shader, "environmentMap"), new int[] { (int)MAP_CUBEMAP }, UNIFORM_INT);
4445

4546
// Load cubemap shader and setup required shader locations
46-
Shader shdrCubemap = LoadShader("resources/shaders/cubemap.vs", "resources/shaders/cubemap.fs");
47-
SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), new int[] { 0 }, UNIFORM_INT);
47+
Shader shdrCubemap = LoadShader("resources/shaders/glsl330/cubemap.vs", "resources/shaders/glsl330/cubemap.fs");
48+
Utils.SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), new int[] { 0 }, UNIFORM_INT);
4849

4950
// Load HDR panorama (sphere) texture
5051
Texture2D texHDR = LoadTexture("resources/dresden_square.hdr");
5152

5253
// Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture
5354
// NOTE: New texture is generated rendering to texture, shader computes the sphre->cube coordinates mapping
54-
MaterialMap* maps = (MaterialMap*)materials[0].maps.ToPointer();
55-
maps[(int)MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, texHDR, 512);
55+
Texture2D cubemap = GenTextureCubemap(shdrCubemap, texHDR, 512);
56+
Utils.SetMaterialTexture(ref skybox, 0, MAP_CUBEMAP, ref cubemap);
5657

5758
UnloadTexture(texHDR); // Texture not required anymore, cubemap already generated
5859
UnloadShader(shdrCubemap); // Unload cubemap generation shader, not required anymore

0 commit comments

Comments
 (0)