diff --git a/Physac-cs/Physac.cs b/Physac-cs/Physac.cs index 63a3d43..d5c043e 100644 --- a/Physac-cs/Physac.cs +++ b/Physac-cs/Physac.cs @@ -7,6 +7,7 @@ */ using System; +using System.Numerics; using System.Runtime.InteropServices; using System.Security; using Raylib_cs; diff --git a/Raygui-cs/Raygui.cs b/Raygui-cs/Raygui.cs index e65efa6..5f80c58 100644 --- a/Raygui-cs/Raygui.cs +++ b/Raygui-cs/Raygui.cs @@ -6,6 +6,7 @@ * See LICENSE for details. */ +using System.Numerics; using System.Runtime.InteropServices; using System.Security; using System.Text; diff --git a/Raylib-cs/Raylib-cs.csproj b/Raylib-cs/Raylib-cs.csproj index 4411779..8fa04f4 100644 --- a/Raylib-cs/Raylib-cs.csproj +++ b/Raylib-cs/Raylib-cs.csproj @@ -34,7 +34,11 @@ <Compile Include="Raylib.cs" /> <Compile Include="Raymath.cs" /> <Compile Include="Rlgl.cs" /> - <None Include="../Logo/raylib-cs_64x64.png" Pack="true" PackagePath=""/> + <None Include="../Logo/raylib-cs_64x64.png" Pack="true" PackagePath="" /> + </ItemGroup> + + <ItemGroup> + <PackageReference Include="System.Numerics.Vectors" Version="4.5.0" /> </ItemGroup> <Target Name="GetNativeLibraryFiles"> diff --git a/Raylib-cs/Raylib.cs b/Raylib-cs/Raylib.cs index 34c0ebf..a8456d7 100644 --- a/Raylib-cs/Raylib.cs +++ b/Raylib-cs/Raylib.cs @@ -7,304 +7,12 @@ */ using System; +using System.Numerics; using System.Runtime.InteropServices; using System.Security; namespace Raylib_cs { - // Vector2 type - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - public struct Vector2 - { - public float x; - public float y; - - public Vector2(float x, float y) - { - this.x = x; - this.y = y; - } - - public Vector2(float value) - { - this.x = value; - this.y = value; - } - - public override bool Equals(object obj) - { - return (obj is Vector2) && Equals((Vector2)obj); - } - - public override int GetHashCode() - { - return x.GetHashCode() + y.GetHashCode(); - } - - public override string ToString() - { - return "Vector2(" + x + " " + y + ")"; - } - - // Common values - public static Vector2 Zero { get { return Raymath.Vector2Zero(); } } - public static Vector2 One { get { return Raymath.Vector2One(); } } - public static Vector2 UnitX { get { return new Vector2(1, 0); } } - public static Vector2 UnitY { get { return new Vector2(0, 1); } } - - // Convienient operators - public static bool operator ==(Vector2 v1, Vector2 v2) - { - return (v1.x == v2.x && v1.y == v2.y); - } - - public static bool operator !=(Vector2 v1, Vector2 v2) - { - return !(v1 == v2); - } - - public static bool operator >(Vector2 v1, Vector2 v2) - { - return v1.x > v2.x && v1.y > v2.y; - } - - public static bool operator <(Vector2 v1, Vector2 v2) - { - return v1.x < v2.x && v1.y < v2.y; - } - - public static Vector2 operator +(Vector2 v1, Vector2 v2) - { - return Raymath.Vector2Add(v1, v2); - } - - public static Vector2 operator -(Vector2 v1, Vector2 v2) - { - return Raymath.Vector2Subtract(v1, v2); - } - - public static Vector2 operator *(Vector2 v1, Vector2 v2) - { - return Raymath.Vector2MultiplyV(v1, v2); - } - - public static Vector2 operator *(Vector2 v, float scale) - { - return Raymath.Vector2Scale(v, scale); - } - - public static Vector2 operator *(float scale, Vector2 v) - { - return Raymath.Vector2Scale(v, scale); - } - - public static Vector2 operator /(Vector2 v1, Vector2 v2) - { - return Raymath.Vector2DivideV(v1, v2); - } - - public static Vector2 operator /(Vector2 v1, float div) - { - return Raymath.Vector2Divide(v1, div); - } - - public static Vector2 operator -(Vector2 v1) - { - return Raymath.Vector2Negate(v1); - } - } - - // Vector3 type - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - public struct Vector3 - { - public float x; - public float y; - public float z; - - public Vector3(float x, float y, float z) - { - this.x = x; - this.y = y; - this.z = z; - } - - public Vector3(float value) - { - this.x = value; - this.y = value; - this.z = value; - } - - // Extensions - public override bool Equals(object obj) - { - return (obj is Vector3) && Equals((Vector3)obj); - } - - public override int GetHashCode() - { - return x.GetHashCode() + y.GetHashCode() + z.GetHashCode(); - } - - public override string ToString() - { - return "Vector3(" + x + " " + y + " " + z + ")"; - } - - // Common values - public static Vector3 Zero { get { return Raymath.Vector3Zero(); } } - public static Vector3 One { get { return Raymath.Vector3One(); } } - public static Vector3 UnitX { get { return new Vector3(1, 0, 0); } } - public static Vector3 UnitY { get { return new Vector3(0, 1, 0); } } - public static Vector3 UnitZ { get { return new Vector3(0, 0, 1); } } - - // Convienient operators - public static bool operator ==(Vector3 v1, Vector3 v2) - { - return (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z); - } - - public static bool operator !=(Vector3 v1, Vector3 v2) - { - return !(v1 == v2); - } - - public static bool operator >(Vector3 v1, Vector3 v2) - { - return v1.x > v2.x && v1.y > v2.y && v1.z > v2.z; - } - - public static bool operator <(Vector3 v1, Vector3 v2) - { - return v1.x < v2.x && v1.y < v2.y && v1.z < v2.z; - } - - public static Vector3 operator +(Vector3 v1, Vector3 v2) - { - return Raymath.Vector3Add(v1, v2); - } - - public static Vector3 operator -(Vector3 v1, Vector3 v2) - { - return Raymath.Vector3Subtract(v1, v2); - } - - public static Vector3 operator *(Vector3 v1, Vector3 v2) - { - return Raymath.Vector3MultiplyV(v1, v2); - } - - public static Vector3 operator *(Vector3 v, float scale) - { - return Raymath.Vector3Scale(v, scale); - } - - public static Vector3 operator *(float scale, Vector3 v) - { - return Raymath.Vector3Scale(v, scale); - } - - public static Vector3 operator /(Vector3 v1, Vector3 v2) - { - return Raymath.Vector3DivideV(v1, v2); - } - - public static Vector3 operator /(Vector3 v1, float div) - { - return Raymath.Vector3Divide(v1, div); - } - - public static Vector3 operator -(Vector3 v1) - { - return Raymath.Vector3Negate(v1); - } - } - - // Vector4 type - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - public struct Vector4 - { - public float x; - public float y; - public float z; - public float w; - - public Vector4(float x, float y, float z, float w) - { - this.x = x; - this.y = y; - this.z = z; - this.w = w; - } - - public Vector4(float value) - { - x = value; - y = value; - z = value; - w = value; - } - - public override bool Equals(object obj) - { - return (obj is Vector4) && Equals((Vector4)obj); - } - - public override int GetHashCode() - { - return x.GetHashCode() + y.GetHashCode() + z.GetHashCode() + w.GetHashCode(); - } - - public override string ToString() - { - return "Vector4(" + x + " " + y + " " + z + " " + w + ")"; - } - - // convienient operators - public static bool operator ==(Vector4 v1, Vector4 v2) - { - return (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z && v1.w == v2.w); - } - - public static bool operator !=(Vector4 v1, Vector4 v2) - { - return !(v1 == v2); - } - - public static bool operator >(Vector4 v1, Vector4 v2) - { - return v1.x > v2.x && v1.y > v2.y && v1.z > v2.z && v1.w > v2.w; - } - - public static bool operator <(Vector4 v1, Vector4 v2) - { - return v1.x < v2.x && v1.y < v2.y && v1.z < v2.z && v1.w < v2.w; - } - } - - // Matrix type (OpenGL style 4x4 - right handed, column major) - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - public struct Matrix - { - public float m0; - public float m4; - public float m8; - public float m12; - public float m1; - public float m5; - public float m9; - public float m13; - public float m2; - public float m6; - public float m10; - public float m14; - public float m3; - public float m7; - public float m11; - public float m15; - } - // Color type, RGBA (32bit) [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct Color @@ -598,7 +306,7 @@ namespace Raylib_cs [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct Model { - public Matrix transform; // Local transform matrix + public Matrix4x4 transform; // Local transform matrix public int meshCount; // Number of meshes // meshes refers to a Mesh * @@ -1381,11 +1089,11 @@ namespace Raylib_cs // Returns camera transform matrix (view matrix) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix GetCameraMatrix(Camera3D camera); + public static extern Matrix4x4 GetCameraMatrix(Camera3D camera); // Returns camera 2d transform matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix GetCameraMatrix2D(Camera2D camera); + public static extern Matrix4x4 GetCameraMatrix2D(Camera2D camera); // Returns the screen space position for a 3d world space position [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -2856,7 +2564,7 @@ namespace Raylib_cs // Set shader uniform value (matrix 4x4) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); + public static extern void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix4x4 mat); // Set shader uniform value for texture [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -2864,19 +2572,19 @@ namespace Raylib_cs // Set a custom projection matrix (replaces internal projection matrix) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void SetMatrixProjection(Matrix proj); + public static extern void SetMatrixProjection(Matrix4x4 proj); // Set a custom modelview matrix (replaces internal modelview matrix) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void SetMatrixModelview(Matrix view); + public static extern void SetMatrixModelview(Matrix4x4 view); // Get internal modelview matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix GetMatrixModelview(); + public static extern Matrix4x4 GetMatrixModelview(); // Get internal projection matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix GetMatrixProjection(); + public static extern Matrix4x4 GetMatrixProjection(); // Texture maps generation (PBR) diff --git a/Raylib-cs/Raymath.cs b/Raylib-cs/Raymath.cs index 32da55f..02a91a3 100644 --- a/Raylib-cs/Raymath.cs +++ b/Raylib-cs/Raymath.cs @@ -6,10 +6,9 @@ * See LICENSE for details. */ -using System; +using System.Numerics; using System.Runtime.InteropServices; using System.Security; -using Quaternion = Raylib_cs.Vector4; namespace Raylib_cs { @@ -174,7 +173,7 @@ namespace Raylib_cs // Transforms a Vector3 by a given Matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Vector3 Vector3Transform(Vector3 v, Matrix mat); + public static extern Vector3 Vector3Transform(Vector3 v, Matrix4x4 mat); // Transform a vector by quaternion rotation [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -207,90 +206,90 @@ namespace Raylib_cs // Compute matrix determinant [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern float MatrixDeterminant(Matrix mat); + public static extern float MatrixDeterminant(Matrix4x4 mat); // Returns the trace of the matrix (sum of the values along the diagonal) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern float MatrixTrace(Matrix mat); + public static extern float MatrixTrace(Matrix4x4 mat); // Transposes provided matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixTranspose(Matrix mat); + public static extern Matrix4x4 MatrixTranspose(Matrix4x4 mat); // Invert provided matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixInvert(Matrix mat); + public static extern Matrix4x4 MatrixInvert(Matrix4x4 mat); // Normalize provided matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixNormalize(Matrix mat); + public static extern Matrix4x4 MatrixNormalize(Matrix4x4 mat); // Returns identity matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixIdentity(); + public static extern Matrix4x4 MatrixIdentity(); // Add two matrices [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixAdd(Matrix left, Matrix right); + public static extern Matrix4x4 MatrixAdd(Matrix4x4 left, Matrix4x4 right); // Subtract two matrices (left - right) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixSubtract(Matrix left, Matrix right); + public static extern Matrix4x4 MatrixSubtract(Matrix4x4 left, Matrix4x4 right); // Returns translation matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixTranslate(float x, float y, float z); + public static extern Matrix4x4 MatrixTranslate(float x, float y, float z); // Create rotation matrix from axis and angle // NOTE: Angle should be provided in radians [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixRotate(Vector3 axis, float angle); + public static extern Matrix4x4 MatrixRotate(Vector3 axis, float angle); // Returns xyz-rotation matrix (angles in radians) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixRotateXYZ(Vector3 ang); + public static extern Matrix4x4 MatrixRotateXYZ(Vector3 ang); // Returns x-rotation matrix (angle in radians) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixRotateX(float angle); + public static extern Matrix4x4 MatrixRotateX(float angle); // Returns y-rotation matrix (angle in radians) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixRotateY(float angle); + public static extern Matrix4x4 MatrixRotateY(float angle); // Returns z-rotation matrix (angle in radians) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixRotateZ(float angle); + public static extern Matrix4x4 MatrixRotateZ(float angle); // Returns scaling matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixScale(float x, float y, float z); + public static extern Matrix4x4 MatrixScale(float x, float y, float z); // Returns two matrix multiplication // NOTE: When multiplying matrices... the order matters! [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixMultiply(Matrix left, Matrix right); + public static extern Matrix4x4 MatrixMultiply(Matrix4x4 left, Matrix4x4 right); // Returns perspective projection matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixFrustum(double left, double right, double bottom, double top, double near, double far); + public static extern Matrix4x4 MatrixFrustum(double left, double right, double bottom, double top, double near, double far); // Returns perspective projection matrix // NOTE: Angle should be provided in radians [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixPerspective(double fovy, double aspect, double near, double far); + public static extern Matrix4x4 MatrixPerspective(double fovy, double aspect, double near, double far); // Returns orthographic projection matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixOrtho(double left, double right, double bottom, double top, double near, double far); + public static extern Matrix4x4 MatrixOrtho(double left, double right, double bottom, double top, double near, double far); // Returns camera look-at matrix (view matrix) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up); + public static extern Matrix4x4 MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up); // Returns float array of matrix data [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern float16 MatrixToFloatV(Matrix mat); + public static extern float16 MatrixToFloatV(Matrix4x4 mat); // Returns identity quaternion [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -330,11 +329,11 @@ namespace Raylib_cs // Returns a quaternion for a given rotation matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Quaternion QuaternionFromMatrix(Matrix mat); + public static extern Quaternion QuaternionFromMatrix(Matrix4x4 mat); // Returns a matrix for a given quaternion [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Matrix QuaternionToMatrix(Quaternion q); + public static extern Matrix4x4 QuaternionToMatrix(Quaternion q); // Returns rotation quaternion for an angle and axis // NOTE: angle must be provided in radians @@ -356,6 +355,6 @@ namespace Raylib_cs // Transform a quaternion given a transformation matrix [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Quaternion QuaternionTransform(Quaternion q, Matrix mat); + public static extern Quaternion QuaternionTransform(Quaternion q, Matrix4x4 mat); } } diff --git a/Raylib-cs/Rlgl.cs b/Raylib-cs/Rlgl.cs index 30d2a0f..f6858b1 100644 --- a/Raylib-cs/Rlgl.cs +++ b/Raylib-cs/Rlgl.cs @@ -7,6 +7,7 @@ */ using System; +using System.Numerics; using System.Runtime.InteropServices; using System.Security; @@ -282,7 +283,7 @@ namespace Raylib_cs // Get world coordinates from screen coordinates [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Vector3 rlUnproject(Vector3 source, Matrix proj, Matrix view); + public static extern Vector3 rlUnproject(Vector3 source, Matrix4x4 proj, Matrix4x4 view); // Textures data management // Load texture in GPU @@ -354,7 +355,7 @@ namespace Raylib_cs // Draw a 3d mesh with material and transform [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void rlDrawMesh(Mesh mesh, Material material, Matrix transform); + public static extern void rlDrawMesh(Mesh mesh, Material material, Matrix4x4 transform); // Unload mesh data from CPU and GPU [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] diff --git a/Raylib-cs/Rlights.cs b/Raylib-cs/Rlights.cs new file mode 100644 index 0000000..39b144d --- /dev/null +++ b/Raylib-cs/Rlights.cs @@ -0,0 +1,92 @@ + +namespace Raylib_cs +{ + //---------------------------------------------------------------------------------- + // Types and Structures Definition + //---------------------------------------------------------------------------------- + public struct Light + { + public bool enabled; + public LightType type; + public Vector3 position; + public Vector3 target; + public Color color; + public int enabledLoc; + public int typeLoc; + public int posLoc; + public int targetLoc; + public int colorLoc; + } + + // Light type + public enum LightType + { + LIGHT_DIRECTIONAL, + LIGHT_POINT + } + + public static class Rlights + { + //---------------------------------------------------------------------------------- + // Global Variables Definition + //---------------------------------------------------------------------------------- + public const int MAX_LIGHTS = 4; + public static int lightsCount = 0; // Current amount of created lights + public const float LIGHT_DISTANCE = 3.5f; + public const float LIGHT_HEIGHT = 1.0f; + + //---------------------------------------------------------------------------------- + // Module Functions Definition + //---------------------------------------------------------------------------------- + + public static Light CreateLight(LightType type, Vector3 pos, Vector3 targ, Color color, Shader shader) + { + Light light = new Light(); + + if (lightsCount < MAX_LIGHTS) + { + light.enabled = true; + light.type = type; + light.position = pos; + light.target = targ; + light.color = color; + + string enabledName = $"lights[{lightsCount}].enabled\0"; + string typeName = $"lights[{lightsCount}].type\0"; + string posName = $"lights[{lightsCount}].position\0"; + string targetName = $"lights[{lightsCount}].target\0"; + string colorName = $"lights[{lightsCount}].color\0"; + + light.enabledLoc = Raylib.GetShaderLocation(shader, enabledName); + light.typeLoc = Raylib.GetShaderLocation(shader, typeName); + light.posLoc = Raylib.GetShaderLocation(shader, posName); + light.targetLoc = Raylib.GetShaderLocation(shader, targetName); + light.colorLoc = Raylib.GetShaderLocation(shader, colorName); + + UpdateLightValues(shader, light); + lightsCount++; + } + return light; + } + + public static void UpdateLightValues(Shader shader, Light light) + { + // Send to shader light enabled state and type + /*Raylib.SetShaderValue(shader, light.enabledLoc, new int[] { light.enabled ? 1 : 0 }, ShaderUniformDataType.UNIFORM_INT); + Raylib.SetShaderValue(shader, light.typeLoc, new int[] { (int)light.type }, ShaderUniformDataType.UNIFORM_INT); + + // Send to shader light position values + float[] position = { light.position.x, light.position.y, light.position.z }; + Raylib.SetShaderValue(shader, light.posLoc, ref position, ShaderUniformDataType.UNIFORM_VEC3); + + // Send to shader light target position values + float[] target = { light.target.x, light.target.y, light.target.z }; + Raylib.SetShaderValue(shader, light.targetLoc, ref target, ShaderUniformDataType.UNIFORM_VEC3); + + // Send to shader light color values + float[] color = { light.color.r / 255, light.color.g / 255, + light.color.b / 255, light.color.a / 255 }; + Raylib.SetShaderValue(shader, light.colorLoc, ref color, ShaderUniformDataType.UNIFORM_VEC4);*/ + } + } +}