From 2fa7e00f169c87585a46b178e6282ed63889189c Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 5 Nov 2015 12:28:45 +0100 Subject: Variables initialization --- src/models.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index 4943aa5e..f7e39863 100644 --- a/src/models.c +++ b/src/models.c @@ -557,11 +557,10 @@ void DrawGizmo(Vector3 position) // Load a 3d model (from file) Model LoadModel(const char *fileName) { - VertexData vData; - Model model; + VertexData vData = { 0 }; - // TODO: Initialize default data for model in case loading fails, maybe a cube? + // NOTE: Initialize default data for model in case loading fails, maybe a cube? if (strcmp(GetExtension(fileName),"obj") == 0) vData = LoadOBJ(fileName); else TraceLog(WARNING, "[%s] Model extension not recognized, it can't be loaded", fileName); @@ -1589,7 +1588,7 @@ static float GetHeightValue(Color pixel) // Load OBJ mesh data static VertexData LoadOBJ(const char *fileName) { - VertexData vData; + VertexData vData = { 0 }; char dataType; char comments[200]; -- cgit v1.2.3 From 4db2da91850fcc55ec08df253e533e236eb91451 Mon Sep 17 00:00:00 2001 From: victorfisac Date: Mon, 21 Dec 2015 16:42:13 +0100 Subject: Added new matrix location points and extra functions - New model and view transformation matrix added, useful for shaders. Modelview matrix not deleted to keep opengl 1.1 pipeline compatibility. - New extra function added DrawModelWiresEx() to set a rotation and scale transformations to a wire model drawing. - Other writing and little audio.c bug fixed. --- src/audio.c | 2 +- src/libraylib.a | Bin 0 -> 394980 bytes src/models.c | 185 +++++++++++++++++++++++++++++--------------------------- src/raylib.h | 5 +- src/rlgl.c | 10 +++ src/rlgl.h | 4 +- 6 files changed, 114 insertions(+), 92 deletions(-) create mode 100644 src/libraylib.a (limited to 'src/models.c') diff --git a/src/audio.c b/src/audio.c index 8ef71116..6313c9dc 100644 --- a/src/audio.c +++ b/src/audio.c @@ -92,7 +92,7 @@ typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; //---------------------------------------------------------------------------------- // Global Variables Definition //---------------------------------------------------------------------------------- -bool musicEnabled = false; +static bool musicEnabled = false; static Music currentMusic; // Current music loaded // NOTE: Only one music file playing at a time diff --git a/src/libraylib.a b/src/libraylib.a new file mode 100644 index 00000000..1da3aae4 Binary files /dev/null and b/src/libraylib.a differ diff --git a/src/models.c b/src/models.c index f7e39863..090c4d86 100644 --- a/src/models.c +++ b/src/models.c @@ -64,7 +64,7 @@ static VertexData LoadOBJ(const char *fileName); // Draw cube // NOTE: Cube position is the center position -void DrawCube(Vector3 position, float width, float height, float lenght, Color color) +void DrawCube(Vector3 position, float width, float height, float length, Color color) { float x = 0.0f; float y = 0.0f; @@ -81,58 +81,58 @@ void DrawCube(Vector3 position, float width, float height, float lenght, Color c rlColor4ub(color.r, color.g, color.b, color.a); // Front Face ----------------------------------------------------- - rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Left - rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Right - rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left + rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left + rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right + rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left - rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Right - rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left - rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Right + rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right + rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left + rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right // Back Face ------------------------------------------------------ - rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Bottom Left - rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left - rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Right + rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left + rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left + rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right - rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right - rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Right - rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left + rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right + rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right + rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left // Top Face ------------------------------------------------------- - rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left - rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Bottom Left - rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Bottom Right + rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left + rlVertex3f(x-width/2, y+height/2, z+length/2); // Bottom Left + rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right - rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right - rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left - rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Bottom Right + rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right + rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left + rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right // Bottom Face ---------------------------------------------------- - rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Top Left - rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Right - rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Left + rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left + rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right + rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left - rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Top Right - rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Right - rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Top Left + rlVertex3f(x+width/2, y-height/2, z-length/2); // Top Right + rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right + rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left // Right face ----------------------------------------------------- - rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Right - rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right - rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Left + rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right + rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right + rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Left - rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Left - rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Right - rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Left + rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Left + rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right + rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Left // Left Face ------------------------------------------------------ - rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Bottom Right - rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left - rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Right + rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Right + rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left + rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Right - rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Left - rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left - rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Bottom Right + rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left + rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left + rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Right rlEnd(); rlPopMatrix(); } @@ -144,7 +144,7 @@ void DrawCubeV(Vector3 position, Vector3 size, Color color) } // Draw cube wires -void DrawCubeWires(Vector3 position, float width, float height, float lenght, Color color) +void DrawCubeWires(Vector3 position, float width, float height, float length, Color color) { float x = 0.0f; float y = 0.0f; @@ -160,62 +160,62 @@ void DrawCubeWires(Vector3 position, float width, float height, float lenght, Co // Front Face ----------------------------------------------------- // Bottom Line - rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Left - rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Right + rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left + rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right // Left Line - rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Right - rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Right + rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right + rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right // Top Line - rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Right - rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left + rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right + rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left // Right Line - rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left - rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Left + rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left + rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left // Back Face ------------------------------------------------------ // Bottom Line - rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Bottom Left - rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Right + rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left + rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right // Left Line - rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Right - rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right + rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right + rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right // Top Line - rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right - rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left + rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right + rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left // Right Line - rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left - rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Bottom Left + rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left + rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left // Top Face ------------------------------------------------------- // Left Line - rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left Front - rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left Back + rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left Front + rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left Back // Right Line - rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Right Front - rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right Back + rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right Front + rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right Back // Bottom Face --------------------------------------------------- // Left Line - rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Top Left Front - rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Top Left Back + rlVertex3f(x-width/2, y-height/2, z+length/2); // Top Left Front + rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left Back // Right Line - rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Top Right Front - rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Top Right Back + rlVertex3f(x+width/2, y-height/2, z+length/2); // Top Right Front + rlVertex3f(x+width/2, y-height/2, z-length/2); // Top Right Back rlEnd(); rlPopMatrix(); } // Draw cube // NOTE: Cube position is the center position -void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float height, float lenght, Color color) +void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float height, float length, Color color) { float x = position.x; float y = position.y; @@ -233,40 +233,40 @@ void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float hei rlColor4ub(color.r, color.g, color.b, color.a); // Front Face rlNormal3f(0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer - rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Left Of The Texture and Quad - rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Right Of The Texture and Quad - rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Right Of The Texture and Quad - rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left Of The Texture and Quad + rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left Of The Texture and Quad + rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right Of The Texture and Quad + rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right Of The Texture and Quad + rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left Of The Texture and Quad // Back Face rlNormal3f( 0.0f, 0.0f,-1.0f); // Normal Pointing Away From Viewer - rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Bottom Right Of The Texture and Quad - rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Right Of The Texture and Quad - rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Left Of The Texture and Quad - rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Left Of The Texture and Quad + rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Right Of The Texture and Quad + rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Right Of The Texture and Quad + rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Left Of The Texture and Quad + rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Left Of The Texture and Quad // Top Face rlNormal3f( 0.0f, 1.0f, 0.0f); // Normal Pointing Up - rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left Of The Texture and Quad - rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Bottom Left Of The Texture and Quad - rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Bottom Right Of The Texture and Quad - rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right Of The Texture and Quad + rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left Of The Texture and Quad + rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x-width/2, y+height/2, z+length/2); // Bottom Left Of The Texture and Quad + rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right Of The Texture and Quad + rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right Of The Texture and Quad // Bottom Face rlNormal3f( 0.0f,-1.0f, 0.0f); // Normal Pointing Down - rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Top Right Of The Texture and Quad - rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Top Left Of The Texture and Quad - rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Left Of The Texture and Quad - rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Right Of The Texture and Quad + rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Right Of The Texture and Quad + rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x+width/2, y-height/2, z-length/2); // Top Left Of The Texture and Quad + rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Left Of The Texture and Quad + rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Right Of The Texture and Quad // Right face rlNormal3f( 1.0f, 0.0f, 0.0f); // Normal Pointing Right - rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Right Of The Texture and Quad - rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right Of The Texture and Quad - rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Left Of The Texture and Quad - rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Left Of The Texture and Quad + rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right Of The Texture and Quad + rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right Of The Texture and Quad + rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Left Of The Texture and Quad + rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Left Of The Texture and Quad // Left Face rlNormal3f(-1.0f, 0.0f, 0.0f); // Normal Pointing Left - rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Bottom Left Of The Texture and Quad - rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Right Of The Texture and Quad - rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Right Of The Texture and Quad - rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left Of The Texture and Quad + rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left Of The Texture and Quad + rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Right Of The Texture and Quad + rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Right Of The Texture and Quad + rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left Of The Texture and Quad rlEnd(); //rlPopMatrix(); @@ -534,12 +534,12 @@ void DrawGrid(int slices, float spacing) void DrawGizmo(Vector3 position) { // NOTE: RGB = XYZ - float lenght = 1.0f; + float length = 1.0f; rlPushMatrix(); rlTranslatef(position.x, position.y, position.z); //rlRotatef(rotation, 0, 1, 0); - rlScalef(lenght, lenght, lenght); + rlScalef(length, length, length); rlBegin(RL_LINES); rlColor3f(1.0f, 0.0f, 0.0f); rlVertex3f(0.0f, 0.0f, 0.0f); @@ -1164,6 +1164,13 @@ void DrawModelWires(Model model, Vector3 position, float scale, Color color) rlglDrawModel(model, position, 0.0f, rotationAxis, vScale, color, true); } +// Draw a model wires (with texture if set) with extended parameters +void DrawModelWiresEx(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color tint) +{ + // NOTE: Rotation must be provided in degrees, it's converted to radians inside rlglDrawModel() + rlglDrawModel(model, position, rotationAngle, rotationAxis, scale, tint, true); +} + // Draw a billboard void DrawBillboard(Camera camera, Texture2D texture, Vector3 center, float size, Color tint) { diff --git a/src/raylib.h b/src/raylib.h index daf8133c..cf401cca 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -329,7 +329,9 @@ typedef struct Shader { // Uniforms int projectionLoc; // Projection matrix uniform location point (vertex shader) - int modelviewLoc; // ModeView matrix uniform location point (vertex shader) + int modelviewLoc; // ModelView matrix uniform location point (vertex shader) + int modelLoc; // Model transformation matrix uniform location point (vertex shader) + int viewLoc; // View transformation matrix uniform location point (vertex shader) int tintColorLoc; // Color uniform location point (fragment shader) int mapDiffuseLoc; // Diffuse map texture uniform location point (fragment shader) @@ -666,6 +668,7 @@ void SetModelTexture(Model *model, Texture2D texture); void DrawModel(Model model, Vector3 position, float scale, Color tint); // Draw a model (with texture if set) void DrawModelEx(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color tint); // Draw a model with extended parameters void DrawModelWires(Model model, Vector3 position, float scale, Color color); // Draw a model wires (with texture if set) +void DrawModelWiresEx(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color tint); // Draw a model wires (with texture if set) with extended parameters void DrawBillboard(Camera camera, Texture2D texture, Vector3 center, float size, Color tint); // Draw a billboard texture void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vector3 center, float size, Color tint); // Draw a billboard texture defined by sourceRec diff --git a/src/rlgl.c b/src/rlgl.c index 3862ac74..f9108342 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1498,6 +1498,8 @@ void rlglDrawModel(Model model, Vector3 position, float rotationAngle, Vector3 r glUseProgram(model.shader.id); // Apply transformation provided in model.transform matrix + // TODO: review if at this point the modelview matrix just contains view matrix values + Matrix viewworld = modelview; // Store view matrix before applying model transformations Matrix modelviewworld = MatrixMultiply(model.transform, modelview); // World-space transformation // Apply transformations provided in function @@ -1513,6 +1515,8 @@ void rlglDrawModel(Model model, Vector3 position, float rotationAngle, Vector3 r // NOTE: Drawing in OpenGL 3.3+, transform is passed to shader glUniformMatrix4fv(model.shader.projectionLoc, 1, false, GetMatrixVector(projection)); + glUniformMatrix4fv(model.shader.modelLoc, 1, false, GetMatrixVector(transform)); + glUniformMatrix4fv(model.shader.viewLoc, 1, false, GetMatrixVector(viewworld)); glUniformMatrix4fv(model.shader.modelviewLoc, 1, false, GetMatrixVector(modelviewworld)); // Apply color tinting to model @@ -2242,6 +2246,8 @@ Shader LoadShader(char *vsFileName, char *fsFileName) // Get handles to GLSL uniform locations (vertex shader) shader.modelviewLoc = glGetUniformLocation(shader.id, "modelviewMatrix"); + shader.modelLoc = glGetUniformLocation(shader.id, "modelMatrix"); + shader.viewLoc = glGetUniformLocation(shader.id, "viewMatrix"); shader.projectionLoc = glGetUniformLocation(shader.id, "projectionMatrix"); // Get handles to GLSL uniform locations (fragment shader) @@ -2781,6 +2787,8 @@ static Shader LoadDefaultShader(void) // Get handles to GLSL uniform locations (vertex shader) shader.modelviewLoc = glGetUniformLocation(shader.id, "modelviewMatrix"); + shader.modelLoc = glGetUniformLocation(shader.id, "modelMatrix"); + shader.viewLoc = glGetUniformLocation(shader.id, "viewMatrix"); shader.projectionLoc = glGetUniformLocation(shader.id, "projectionMatrix"); // Get handles to GLSL uniform locations (fragment shader) @@ -2861,6 +2869,8 @@ static Shader LoadSimpleShader(void) // Get handles to GLSL uniform locations (vertex shader) shader.modelviewLoc = glGetUniformLocation(shader.id, "modelviewMatrix"); + shader.modelLoc = glGetUniformLocation(shader.id, "modelMatrix"); + shader.viewLoc = glGetUniformLocation(shader.id, "viewMatrix"); shader.projectionLoc = glGetUniformLocation(shader.id, "projectionMatrix"); // Get handles to GLSL uniform locations (fragment shader) diff --git a/src/rlgl.h b/src/rlgl.h index 0960fa83..a7df043e 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -160,7 +160,9 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; // Uniforms int projectionLoc; // Projection matrix uniform location point (vertex shader) - int modelviewLoc; // ModeView matrix uniform location point (vertex shader) + int modelviewLoc; // ModelView matrix uniform location point (vertex shader) + int modelLoc; // Model transformation matrix uniform location point (vertex shader) + int viewLoc; // View transformation matrix uniform location point (vertex shader) int tintColorLoc; // Color uniform location point (fragment shader) int mapDiffuseLoc; // Diffuse map texture uniform location point (fragment shader) -- cgit v1.2.3 From 5659249dfa9525ca47732d808d86d9ca7aec0bf5 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 30 Dec 2015 13:35:03 +0100 Subject: Some tweaks and details review --- src/core.c | 6 ++---- src/libraylib.a | Bin 432294 -> 0 bytes src/models.c | 6 +++++- 3 files changed, 7 insertions(+), 5 deletions(-) delete mode 100644 src/libraylib.a (limited to 'src/models.c') diff --git a/src/core.c b/src/core.c index 3fc8b47d..acfc63d5 100644 --- a/src/core.c +++ b/src/core.c @@ -350,8 +350,6 @@ void InitWindow(int width, int height, struct android_app *state) //state->userData = &engine; app->onAppCmd = AndroidCommandCallback; - - //InitGesturesSystem(app); // NOTE: Must be called by user InitAssetManager(app->activity->assetManager); @@ -1568,7 +1566,7 @@ static void InitTimer(void) previousTime = GetTime(); // Get time as double } -// Get current time measure since InitTimer() +// Get current time measure (in seconds) since InitTimer() static double GetTime(void) { #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) @@ -1576,7 +1574,7 @@ static double GetTime(void) #elif defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); - uint64_t time = ts.tv_sec*1000000000LLU + (uint64_t)ts.tv_nsec; + uint64_t time = (uint64_t)ts.tv_sec*1000000000LLU + (uint64_t)ts.tv_nsec; return (double)(time - baseTime)*1e-9; #endif diff --git a/src/libraylib.a b/src/libraylib.a deleted file mode 100644 index 5ba9eda7..00000000 Binary files a/src/libraylib.a and /dev/null differ diff --git a/src/models.c b/src/models.c index 090c4d86..f78be41d 100644 --- a/src/models.c +++ b/src/models.c @@ -577,7 +577,10 @@ Model LoadModel(const char *fileName) model = rlglLoadModel(vData); // Upload vertex data to GPU // Now that vertex data is uploaded to GPU, we can free arrays - // NOTE: We don't need CPU vertex data on OpenGL 3.3 or ES2 + // NOTE 1: We don't need CPU vertex data on OpenGL 3.3 or ES2... for static meshes... + // NOTE 2: ...but we could keep CPU vertex data in case we need to update the mesh + + /* if (rlGetVersion() != OPENGL_11) { free(vData.vertices); @@ -585,6 +588,7 @@ Model LoadModel(const char *fileName) free(vData.normals); free(vData.colors); } + */ } return model; -- cgit v1.2.3 From 1793f2c3b87f1ed487216e23f3074085753ee346 Mon Sep 17 00:00:00 2001 From: victorfisac Date: Thu, 7 Jan 2016 16:18:24 +0100 Subject: Added collision check between ray and box - Added CheckCollisionRayBox() function. - Updated and improved core 3d picking example (currently working as expected). --- examples/core_3d_picking.c | 14 +++++++++++--- src/models.c | 20 ++++++++++++++++++++ src/raylib.h | 1 + 3 files changed, 32 insertions(+), 3 deletions(-) (limited to 'src/models.c') diff --git a/examples/core_3d_picking.c b/examples/core_3d_picking.c index 2fc05e81..0d6f4ac7 100644 --- a/examples/core_3d_picking.c +++ b/examples/core_3d_picking.c @@ -24,9 +24,12 @@ int main() Camera camera = {{ 0.0, 10.0, 10.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }}; Vector3 cubePosition = { 0.0, 1.0, 0.0 }; + Vector3 cubeSize = { 2.0, 2.0, 2.0 }; Ray ray; // Picking line ray + bool collision = false; + SetCameraMode(CAMERA_FREE); // Set a free camera mode SetCameraPosition(camera.position); // Set internal camera position to match our camera position @@ -45,7 +48,10 @@ int main() // NOTE: This function is NOT WORKING properly! ray = GetMouseRay(GetMousePosition(), camera); - // TODO: Check collision between ray and box + // Check collision between ray and box + collision = CheckCollisionRayBox(ray, + (Vector3){cubePosition.x - cubeSize.x / 2,cubePosition.y - cubeSize.y / 2,cubePosition.z - cubeSize.z / 2}, + (Vector3){cubePosition.x + cubeSize.x / 2,cubePosition.y + cubeSize.y / 2,cubePosition.z + cubeSize.z / 2}); } //---------------------------------------------------------------------------------- @@ -57,8 +63,8 @@ int main() Begin3dMode(camera); - DrawCube(cubePosition, 2, 2, 2, GRAY); - DrawCubeWires(cubePosition, 2, 2, 2, DARKGRAY); + DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, GRAY); + DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, DARKGRAY); DrawGrid(10.0, 1.0); @@ -67,6 +73,8 @@ int main() End3dMode(); DrawText("Try selecting the box with mouse!", 240, 10, 20, GRAY); + + if(collision) DrawText("BOX SELECTED", (screenWidth - MeasureText("BOX SELECTED", 30)) / 2, screenHeight * 0.1f, 30, GREEN); DrawFPS(10, 10); diff --git a/src/models.c b/src/models.c index f78be41d..dd170e0b 100644 --- a/src/models.c +++ b/src/models.c @@ -1336,6 +1336,26 @@ bool CheckCollisionBoxSphere(Vector3 minBBox, Vector3 maxBBox, Vector3 centerSph return collision; } +// Detect collision between ray and box +bool CheckCollisionRayBox(Ray ray, Vector3 minBBox, Vector3 maxBBox) +{ + bool collision = false; + + float t[8]; + t[0] = (minBBox.x - ray.position.x) / ray.direction.x; + t[1] = (maxBBox.x - ray.position.x) / ray.direction.x; + t[2] = (minBBox.y - ray.position.y) / ray.direction.y; + t[3] = (maxBBox.y - ray.position.y) / ray.direction.y; + t[4] = (minBBox.z - ray.position.z) / ray.direction.z; + t[5] = (maxBBox.z - ray.position.z) / ray.direction.z; + t[6] = fmax(fmax(fmin(t[0], t[1]), fmin(t[2], t[3])), fmin(t[4], t[5])); + t[7] = fmin(fmin(fmax(t[0], t[1]), fmax(t[2], t[3])), fmax(t[4], t[5])); + + collision = !(t[7] < 0 || t[6] > t[7]); + + return collision; +} + // TODO: Useful function to check collision area? //BoundingBox GetCollisionArea(BoundingBox box1, BoundingBox box2) diff --git a/src/raylib.h b/src/raylib.h index 72211b59..b6900a97 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -756,6 +756,7 @@ void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vec bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB); // Detect collision between two spheres bool CheckCollisionBoxes(Vector3 minBBox1, Vector3 maxBBox1, Vector3 minBBox2, Vector3 maxBBox2); // Detect collision between two boxes bool CheckCollisionBoxSphere(Vector3 minBBox, Vector3 maxBBox, Vector3 centerSphere, float radiusSphere); // Detect collision between box and sphere +bool CheckCollisionRayBox(Ray ray, Vector3 minBBox, Vector3 maxBBox); // Detect collision between ray and box Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *playerPosition, float radius); // Detect collision of player radius with cubicmap // NOTE: Return the normal vector of the impacted surface -- cgit v1.2.3 From 3b4d8442e049f969c737722cb72f1c8ad66621a7 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 13 Jan 2016 19:30:35 +0100 Subject: Corrected some float values --- src/core.c | 16 ++++++++-------- src/models.c | 62 ++++++++++++++++++++++++++++++------------------------------ src/rlgl.c | 36 +++++++++++++++++------------------ 3 files changed, 57 insertions(+), 57 deletions(-) (limited to 'src/models.c') diff --git a/src/core.c b/src/core.c index 8b61aaf7..6314941b 100644 --- a/src/core.c +++ b/src/core.c @@ -309,8 +309,8 @@ void InitWindow(int width, int height, const char *title) emscripten_set_touchcancel_callback("#canvas", NULL, 1, EmscriptenInputCallback); #endif - mousePosition.x = screenWidth/2; - mousePosition.y = screenHeight/2; + mousePosition.x = (float)screenWidth/2.0f; + mousePosition.y = (float)screenHeight/2.0f; // raylib logo appearing animation (if enabled) if (showLogo) @@ -577,7 +577,7 @@ void Begin3dMode(Camera camera) // Setup perspective projection float aspect = (float)screenWidth/(float)screenHeight; - double top = 0.1f*tan(45.0f*PI / 360.0f); + double top = 0.1f*tan(45.0f*PI/360.0f); double right = top*aspect; // NOTE: zNear and zFar values are important when computing depth buffer values @@ -608,7 +608,7 @@ void End3dMode(void) // Set target FPS for the game void SetTargetFPS(int fps) { - targetTime = 1 / (double)fps; + targetTime = 1.0/(double)fps; TraceLog(INFO, "Target time per frame: %02.03f milliseconds", (float)targetTime*1000); } @@ -625,7 +625,7 @@ float GetFrameTime(void) // As we are operate quite a lot with frameTime, // it could be no stable, so we round it before passing it around // NOTE: There are still problems with high framerates (>500fps) - double roundedFrameTime = round(frameTime*10000)/10000; + double roundedFrameTime = round(frameTime*10000)/10000.0; return (float)roundedFrameTime; // Time in seconds to run a frame } @@ -806,8 +806,8 @@ Ray GetMouseRay(Vector2 mousePosition, Camera camera) // Calculate normalized device coordinates // NOTE: y value is negative - float x = (2.0f * mousePosition.x) / GetScreenWidth() - 1.0f; - float y = 1.0f - (2.0f * mousePosition.y) / GetScreenHeight(); + float x = (2.0f*mousePosition.x)/(float)GetScreenWidth() - 1.0f; + float y = 1.0f - (2.0f*mousePosition.y)/(float)GetScreenHeight(); float z = 1.0f; // Store values in a vector @@ -880,7 +880,7 @@ Vector2 WorldToScreen(Vector3 position, Camera camera) Vector3 ndcPos = { worldPos.x / worldPos.w, -worldPos.y / worldPos.w, worldPos.z / worldPos.z }; // Calculate 2d screen position vector - Vector2 screenPosition = { (ndcPos.x + 1.0f) / 2.0f * GetScreenWidth(), (ndcPos.y + 1.0f) / 2.0f * GetScreenHeight() }; + Vector2 screenPosition = { (ndcPos.x + 1.0f)/2.0f*(float)GetScreenWidth(), (ndcPos.y + 1.0f)/2.0f*(float)GetScreenHeight() }; return screenPosition; } diff --git a/src/models.c b/src/models.c index dd170e0b..8b49731d 100644 --- a/src/models.c +++ b/src/models.c @@ -741,8 +741,8 @@ Model LoadCubicmap(Image cubicmap) // Map cube size will be 1.0 float mapCubeSide = 1.0f; - int mapWidth = cubicmap.width * (int)mapCubeSide; - int mapHeight = cubicmap.height * (int)mapCubeSide; + int mapWidth = cubicmap.width*(int)mapCubeSide; + int mapHeight = cubicmap.height*(int)mapCubeSide; // NOTE: Max possible number of triangles numCubes * (12 triangles by cube) int maxTriangles = cubicmap.width*cubicmap.height*12; @@ -753,11 +753,11 @@ Model LoadCubicmap(Image cubicmap) float w = mapCubeSide; float h = mapCubeSide; - float h2 = mapCubeSide * 1.5; // TODO: Review walls height... + float h2 = mapCubeSide*1.5f; // TODO: Review walls height... - Vector3 *mapVertices = (Vector3 *)malloc(maxTriangles * 3 * sizeof(Vector3)); - Vector2 *mapTexcoords = (Vector2 *)malloc(maxTriangles * 3 * sizeof(Vector2)); - Vector3 *mapNormals = (Vector3 *)malloc(maxTriangles * 3 * sizeof(Vector3)); + Vector3 *mapVertices = (Vector3 *)malloc(maxTriangles*3*sizeof(Vector3)); + Vector2 *mapTexcoords = (Vector2 *)malloc(maxTriangles*3*sizeof(Vector2)); + Vector3 *mapNormals = (Vector3 *)malloc(maxTriangles*3*sizeof(Vector3)); // Define the 6 normals of the cube, we will combine them accordingly later... Vector3 n1 = { 1.0f, 0.0f, 0.0f }; @@ -775,12 +775,12 @@ Model LoadCubicmap(Image cubicmap) float height; } RectangleF; - RectangleF rightTexUV = { 0, 0, 0.5, 0.5 }; - RectangleF leftTexUV = { 0.5, 0, 0.5, 0.5 }; - RectangleF frontTexUV = { 0, 0, 0.5, 0.5 }; - RectangleF backTexUV = { 0.5, 0, 0.5, 0.5 }; - RectangleF topTexUV = { 0, 0.5, 0.5, 0.5 }; - RectangleF bottomTexUV = { 0.5, 0.5, 0.5, 0.5 }; + RectangleF rightTexUV = { 0.0f, 0.0f, 0.5f, 0.5f }; + RectangleF leftTexUV = { 0.5f, 0.0f, 0.5f, 0.5f }; + RectangleF frontTexUV = { 0.0f, 0.0f, 0.5f, 0.5f }; + RectangleF backTexUV = { 0.5f, 0.0f, 0.5f, 0.5f }; + RectangleF topTexUV = { 0.0f, 0.5f, 0.5f, 0.5f }; + RectangleF bottomTexUV = { 0.5f, 0.5f, 0.5f, 0.5f }; for (int z = 0; z < mapHeight; z += mapCubeSide) { @@ -1147,7 +1147,7 @@ void SetModelTexture(Model *model, Texture2D texture) void DrawModel(Model model, Vector3 position, float scale, Color tint) { Vector3 vScale = { scale, scale, scale }; - Vector3 rotationAxis = { 0, 0, 0 }; + Vector3 rotationAxis = { 0.0f, 0.0f, 0.0f }; DrawModelEx(model, position, 0.0f, rotationAxis, vScale, tint); } @@ -1163,7 +1163,7 @@ void DrawModelEx(Model model, Vector3 position, float rotationAngle, Vector3 rot void DrawModelWires(Model model, Vector3 position, float scale, Color color) { Vector3 vScale = { scale, scale, scale }; - Vector3 rotationAxis = { 0, 0, 0 }; + Vector3 rotationAxis = { 0.0f, 0.0f, 0.0f }; rlglDrawModel(model, position, 0.0f, rotationAxis, vScale, color, true); } @@ -1188,7 +1188,7 @@ void DrawBillboard(Camera camera, Texture2D texture, Vector3 center, float size, //Vector3 up = { viewMatrix.m1, viewMatrix.m5, viewMatrix.m9 }; // NOTE: Billboard locked to axis-Y - Vector3 up = { 0, 1, 0 }; + Vector3 up = { 0.0f, 1.0f, 0.0f }; /* a-------b | | @@ -1366,7 +1366,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p Color *cubicmapPixels = GetImageData(cubicmap); // Detect the cell where the player is located - Vector3 impactDirection = { 0, 0, 0 }; + Vector3 impactDirection = { 0.0f, 0.0f, 0.0f }; int locationCellX = 0; int locationCellY = 0; @@ -1389,7 +1389,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p { playerPosition->x = locationCellX + mapPosition.x - (CUBIC_MAP_HALF_BLOCK_SIZE - radius); playerPosition->z = locationCellY + mapPosition.z - (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1405,7 +1405,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p { playerPosition->x = locationCellX + mapPosition.x - (CUBIC_MAP_HALF_BLOCK_SIZE - radius); playerPosition->z = locationCellY + mapPosition.z + (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1421,7 +1421,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p { playerPosition->x = locationCellX + mapPosition.x + (CUBIC_MAP_HALF_BLOCK_SIZE - radius); playerPosition->z = locationCellY + mapPosition.z - (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1437,7 +1437,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p { playerPosition->x = locationCellX + mapPosition.x + (CUBIC_MAP_HALF_BLOCK_SIZE - radius); playerPosition->z = locationCellY + mapPosition.z + (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1452,7 +1452,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if ((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius) { playerPosition->x = locationCellX + mapPosition.x - (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 1, 0, 0}; + impactDirection = (Vector3){ 1.0f, 0.0f, 0.0f }; } } } @@ -1464,7 +1464,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if ((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius) { playerPosition->x = locationCellX + mapPosition.x + (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 1, 0, 0}; + impactDirection = (Vector3){ 1.0f, 0.0f, 0.0f }; } } } @@ -1476,7 +1476,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius) { playerPosition->z = locationCellY + mapPosition.z - (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 0, 0, 1}; + impactDirection = (Vector3){ 0.0f, 0.0f, 1.0f }; } } } @@ -1488,7 +1488,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius) { playerPosition->z = locationCellY + mapPosition.z + (CUBIC_MAP_HALF_BLOCK_SIZE - radius); - impactDirection = (Vector3) { 0, 0, 1}; + impactDirection = (Vector3){ 0.0f, 0.0f, 1.0f }; } } } @@ -1512,7 +1512,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius / 3) && ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius / 3)) { - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1535,7 +1535,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius / 3) && ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius / 3)) { - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1558,7 +1558,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius / 3) && ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius / 3)) { - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1581,7 +1581,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius / 3) && ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius / 3)) { - impactDirection = (Vector3) { 1, 0, 1}; + impactDirection = (Vector3){ 1.0f, 0.0f, 1.0f }; } } } @@ -1591,13 +1591,13 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p // Floor collision if (playerPosition->y <= radius) { - playerPosition->y = radius + 0.01; + playerPosition->y = radius + 0.01f; impactDirection = (Vector3) { impactDirection.x, 1, impactDirection.z}; } // Roof collision - else if (playerPosition->y >= 1.5 - radius) + else if (playerPosition->y >= (1.5f - radius)) { - playerPosition->y = (1.5 - radius) - 0.01; + playerPosition->y = (1.5f - radius) - 0.01f; impactDirection = (Vector3) { impactDirection.x, 1, impactDirection.z}; } diff --git a/src/rlgl.c b/src/rlgl.c index e3c763be..7afa374e 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -821,10 +821,10 @@ void rlDeleteBuffers(unsigned int id) void rlClearColor(byte r, byte g, byte b, byte a) { // Color values clamp to 0.0f(0) and 1.0f(255) - float cr = (float)r / 255; - float cg = (float)g / 255; - float cb = (float)b / 255; - float ca = (float)a / 255; + float cr = (float)r/255; + float cg = (float)g/255; + float cb = (float)b/255; + float ca = (float)a/255; glClearColor(cr, cg, cb, ca); } @@ -1104,12 +1104,12 @@ void rlglInitPostpro(void) quadData.vertexCount = 6; - float w = screenWidth; - float h = screenHeight; + float w = (float)screenWidth; + float h = (float)screenHeight; - float quadPositions[6*3] = { w, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, h, 0.0, 0, h, 0.0, w, h, 0.0, w, 0.0, 0.0 }; - float quadTexcoords[6*2] = { 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0 }; - float quadNormals[6*3] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0 }; + float quadPositions[6*3] = { w, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, h, 0.0f, 0.0f, h, 0.0f, w, h, 0.0f, w, 0.0f, 0.0f }; + float quadTexcoords[6*2] = { 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f }; + float quadNormals[6*3] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }; unsigned char quadColors[6*4] = { 255 }; quadData.vertices = quadPositions; @@ -1667,7 +1667,7 @@ void rlglInitGraphics(int offsetX, int offsetY, int width, int height) // NOTE: Using global variables: screenWidth, screenHeight Vector3 rlglUnproject(Vector3 source, Matrix proj, Matrix view) { - Vector3 result = { 0, 0, 0 }; // Object coordinates + Vector3 result = { 0.0f, 0.0f, 0.0f }; // Object coordinates //GLint viewport[4]; //glGetIntegerv(GL_VIEWPORT, viewport); // Not available on OpenGL ES 2.0 @@ -1698,11 +1698,11 @@ Vector3 rlglUnproject(Vector3 source, Matrix proj, Matrix view) quat.x = ((source.x - (float)x)/(float)width)*2.0f - 1.0f; quat.y = ((source.y - (float)y)/(float)height)*2.0f - 1.0f; quat.z = source.z*2.0f - 1.0f; - quat.w = 1.0; + quat.w = 1.0f; QuaternionTransform(&quat, modelviewprojection); - if (quat.w != 0.0) + if (quat.w != 0.0f) { quat.x /= quat.w; quat.y /= quat.w; @@ -2171,7 +2171,7 @@ void *rlglReadTexturePixels(Texture2D texture) // Render texture to fbo glBindFramebuffer(GL_FRAMEBUFFER, fbo.id); - glClearColor(0.0, 0.0, 0.0, 0.0); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepthf(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0, 0, width, height); @@ -2189,7 +2189,7 @@ void *rlglReadTexturePixels(Texture2D texture) quad.transform = MatrixIdentity(); quad.shader = simpleShader; - DrawModel(quad, (Vector3){ 0, 0, 0 }, 1.0f, WHITE); + DrawModel(quad, (Vector3){ 0.0f, 0.0f, 0.0f }, 1.0f, WHITE); pixels = (unsigned char *)malloc(texture.width*texture.height*3*sizeof(unsigned char)); @@ -3239,19 +3239,19 @@ static pixel *GenNextMipmap(pixel *srcData, int srcWidth, int srcHeight) int x2, y2; pixel prow, pcol; - int width = srcWidth / 2; - int height = srcHeight / 2; + int width = srcWidth/2; + int height = srcHeight/2; pixel *mipmap = (pixel *)malloc(width*height*sizeof(pixel)); // Scaling algorithm works perfectly (box-filter) for (int y = 0; y < height; y++) { - y2 = 2 * y; + y2 = 2*y; for (int x = 0; x < width; x++) { - x2 = 2 * x; + x2 = 2*x; prow.r = (srcData[y2*srcWidth + x2].r + srcData[y2*srcWidth + x2 + 1].r)/2; prow.g = (srcData[y2*srcWidth + x2].g + srcData[y2*srcWidth + x2 + 1].g)/2; -- cgit v1.2.3 From fd05d3e3531964a38ea84df920264a1ed14bb777 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 18 Jan 2016 13:36:18 +0100 Subject: Rename VertexData struct to Mesh Reviewed vertex type variables --- src/models.c | 248 +++++++++++++++++++++++++++++------------------------------ src/raylib.h | 34 +++++--- src/rlgl.c | 16 ++-- src/rlgl.h | 29 ++++--- 4 files changed, 171 insertions(+), 156 deletions(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index 8b49731d..d4b21258 100644 --- a/src/models.c +++ b/src/models.c @@ -56,7 +56,7 @@ extern unsigned int whiteTexture; // Module specific Functions Declaration //---------------------------------------------------------------------------------- static float GetHeightValue(Color pixel); -static VertexData LoadOBJ(const char *fileName); +static Mesh LoadOBJ(const char *fileName); //---------------------------------------------------------------------------------- // Module Functions Definition @@ -558,23 +558,23 @@ void DrawGizmo(Vector3 position) Model LoadModel(const char *fileName) { Model model; - VertexData vData = { 0 }; + Mesh mesh = { 0 }; // NOTE: Initialize default data for model in case loading fails, maybe a cube? - if (strcmp(GetExtension(fileName),"obj") == 0) vData = LoadOBJ(fileName); + if (strcmp(GetExtension(fileName),"obj") == 0) mesh = LoadOBJ(fileName); else TraceLog(WARNING, "[%s] Model extension not recognized, it can't be loaded", fileName); - // NOTE: At this point we have all vertex, texcoord, normal data for the model in vData struct + // NOTE: At this point we have all vertex, texcoord, normal data for the model in mesh struct - if (vData.vertexCount == 0) + if (mesh.vertexCount == 0) { TraceLog(WARNING, "Model could not be loaded"); } else { // NOTE: model properties (transform, texture, shader) are initialized inside rlglLoadModel() - model = rlglLoadModel(vData); // Upload vertex data to GPU + model = rlglLoadModel(mesh); // Upload vertex data to GPU // Now that vertex data is uploaded to GPU, we can free arrays // NOTE 1: We don't need CPU vertex data on OpenGL 3.3 or ES2... for static meshes... @@ -583,10 +583,10 @@ Model LoadModel(const char *fileName) /* if (rlGetVersion() != OPENGL_11) { - free(vData.vertices); - free(vData.texcoords); - free(vData.normals); - free(vData.colors); + free(mesh.vertices); + free(mesh.texcoords); + free(mesh.normals); + free(mesh.colors); } */ } @@ -595,7 +595,7 @@ Model LoadModel(const char *fileName) } // Load a 3d model (from vertex data) -Model LoadModelEx(VertexData data) +Model LoadModelEx(Mesh data) { Model model; @@ -610,7 +610,7 @@ Model LoadModelEx(VertexData data) // Load a heightmap image as a 3d model Model LoadHeightmap(Image heightmap, float maxHeight) { - VertexData vData; + Mesh mesh; int mapX = heightmap.width; int mapZ = heightmap.height; @@ -621,12 +621,12 @@ Model LoadHeightmap(Image heightmap, float maxHeight) // TODO: Consider resolution when generating model data? int numTriangles = (mapX-1)*(mapZ-1)*2; // One quad every four pixels - vData.vertexCount = numTriangles*3; + mesh.vertexCount = numTriangles*3; - vData.vertices = (float *)malloc(vData.vertexCount*3*sizeof(float)); - vData.normals = (float *)malloc(vData.vertexCount*3*sizeof(float)); - vData.texcoords = (float *)malloc(vData.vertexCount*2*sizeof(float)); - vData.colors = (unsigned char *)malloc(vData.vertexCount*4*sizeof(unsigned char)); // Not used... + mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float)); + mesh.colors = (unsigned char *)malloc(mesh.vertexCount*4*sizeof(unsigned char)); // Not used... int vCounter = 0; // Used to count vertices float by float int tcCounter = 0; // Used to count texcoords float by float @@ -644,51 +644,51 @@ Model LoadHeightmap(Image heightmap, float maxHeight) //---------------------------------------------------------- // one triangle - 3 vertex - vData.vertices[vCounter] = x; - vData.vertices[vCounter + 1] = GetHeightValue(heightmapPixels[x + z*mapX])*scaleFactor; - vData.vertices[vCounter + 2] = z; + mesh.vertices[vCounter] = x; + mesh.vertices[vCounter + 1] = GetHeightValue(heightmapPixels[x + z*mapX])*scaleFactor; + mesh.vertices[vCounter + 2] = z; - vData.vertices[vCounter + 3] = x; - vData.vertices[vCounter + 4] = GetHeightValue(heightmapPixels[x + (z+1)*mapX])*scaleFactor; - vData.vertices[vCounter + 5] = z+1; + mesh.vertices[vCounter + 3] = x; + mesh.vertices[vCounter + 4] = GetHeightValue(heightmapPixels[x + (z+1)*mapX])*scaleFactor; + mesh.vertices[vCounter + 5] = z+1; - vData.vertices[vCounter + 6] = x+1; - vData.vertices[vCounter + 7] = GetHeightValue(heightmapPixels[(x+1) + z*mapX])*scaleFactor; - vData.vertices[vCounter + 8] = z; + mesh.vertices[vCounter + 6] = x+1; + mesh.vertices[vCounter + 7] = GetHeightValue(heightmapPixels[(x+1) + z*mapX])*scaleFactor; + mesh.vertices[vCounter + 8] = z; // another triangle - 3 vertex - vData.vertices[vCounter + 9] = vData.vertices[vCounter + 6]; - vData.vertices[vCounter + 10] = vData.vertices[vCounter + 7]; - vData.vertices[vCounter + 11] = vData.vertices[vCounter + 8]; + mesh.vertices[vCounter + 9] = mesh.vertices[vCounter + 6]; + mesh.vertices[vCounter + 10] = mesh.vertices[vCounter + 7]; + mesh.vertices[vCounter + 11] = mesh.vertices[vCounter + 8]; - vData.vertices[vCounter + 12] = vData.vertices[vCounter + 3]; - vData.vertices[vCounter + 13] = vData.vertices[vCounter + 4]; - vData.vertices[vCounter + 14] = vData.vertices[vCounter + 5]; + mesh.vertices[vCounter + 12] = mesh.vertices[vCounter + 3]; + mesh.vertices[vCounter + 13] = mesh.vertices[vCounter + 4]; + mesh.vertices[vCounter + 14] = mesh.vertices[vCounter + 5]; - vData.vertices[vCounter + 15] = x+1; - vData.vertices[vCounter + 16] = GetHeightValue(heightmapPixels[(x+1) + (z+1)*mapX])*scaleFactor; - vData.vertices[vCounter + 17] = z+1; + mesh.vertices[vCounter + 15] = x+1; + mesh.vertices[vCounter + 16] = GetHeightValue(heightmapPixels[(x+1) + (z+1)*mapX])*scaleFactor; + mesh.vertices[vCounter + 17] = z+1; vCounter += 18; // 6 vertex, 18 floats // Fill texcoords array with data //-------------------------------------------------------------- - vData.texcoords[tcCounter] = (float)x / (mapX-1); - vData.texcoords[tcCounter + 1] = (float)z / (mapZ-1); + mesh.texcoords[tcCounter] = (float)x / (mapX-1); + mesh.texcoords[tcCounter + 1] = (float)z / (mapZ-1); - vData.texcoords[tcCounter + 2] = (float)x / (mapX-1); - vData.texcoords[tcCounter + 3] = (float)(z+1) / (mapZ-1); + mesh.texcoords[tcCounter + 2] = (float)x / (mapX-1); + mesh.texcoords[tcCounter + 3] = (float)(z+1) / (mapZ-1); - vData.texcoords[tcCounter + 4] = (float)(x+1) / (mapX-1); - vData.texcoords[tcCounter + 5] = (float)z / (mapZ-1); + mesh.texcoords[tcCounter + 4] = (float)(x+1) / (mapX-1); + mesh.texcoords[tcCounter + 5] = (float)z / (mapZ-1); - vData.texcoords[tcCounter + 6] = vData.texcoords[tcCounter + 4]; - vData.texcoords[tcCounter + 7] = vData.texcoords[tcCounter + 5]; + mesh.texcoords[tcCounter + 6] = mesh.texcoords[tcCounter + 4]; + mesh.texcoords[tcCounter + 7] = mesh.texcoords[tcCounter + 5]; - vData.texcoords[tcCounter + 8] = vData.texcoords[tcCounter + 2]; - vData.texcoords[tcCounter + 9] = vData.texcoords[tcCounter + 3]; + mesh.texcoords[tcCounter + 8] = mesh.texcoords[tcCounter + 2]; + mesh.texcoords[tcCounter + 9] = mesh.texcoords[tcCounter + 3]; - vData.texcoords[tcCounter + 10] = (float)(x+1) / (mapX-1); - vData.texcoords[tcCounter + 11] = (float)(z+1) / (mapZ-1); + mesh.texcoords[tcCounter + 10] = (float)(x+1) / (mapX-1); + mesh.texcoords[tcCounter + 11] = (float)(z+1) / (mapZ-1); tcCounter += 12; // 6 texcoords, 12 floats // Fill normals array with data @@ -696,9 +696,9 @@ Model LoadHeightmap(Image heightmap, float maxHeight) // NOTE: Current Model implementation doe not use normals! for (int i = 0; i < 18; i += 3) { - vData.normals[nCounter + i] = 0.0f; - vData.normals[nCounter + i + 1] = 1.0f; - vData.normals[nCounter + i + 2] = 0.0f; + mesh.normals[nCounter + i] = 0.0f; + mesh.normals[nCounter + i + 1] = 1.0f; + mesh.normals[nCounter + i + 2] = 0.0f; } // TODO: Calculate normals in an efficient way @@ -713,20 +713,20 @@ Model LoadHeightmap(Image heightmap, float maxHeight) // Fill color data // NOTE: Not used any more... just one plain color defined at DrawModel() - for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255; + for (int i = 0; i < (4*mesh.vertexCount); i++) mesh.colors[i] = 255; - // NOTE: At this point we have all vertex, texcoord, normal data for the model in vData struct + // NOTE: At this point we have all vertex, texcoord, normal data for the model in mesh struct - Model model = rlglLoadModel(vData); + Model model = rlglLoadModel(mesh); // Now that vertex data is uploaded to GPU, we can free arrays // NOTE: We don't need CPU vertex data on OpenGL 3.3 or ES2 if (rlGetVersion() != OPENGL_11) { - free(vData.vertices); - free(vData.texcoords); - free(vData.normals); - free(vData.colors); + free(mesh.vertices); + free(mesh.texcoords); + free(mesh.normals); + free(mesh.colors); } return model; @@ -735,7 +735,7 @@ Model LoadHeightmap(Image heightmap, float maxHeight) // Load a map image as a 3d model (cubes based) Model LoadCubicmap(Image cubicmap) { - VertexData vData; + Mesh mesh; Color *cubicmapPixels = GetImageData(cubicmap); @@ -1041,25 +1041,25 @@ Model LoadCubicmap(Image cubicmap) } // Move data from mapVertices temp arays to vertices float array - vData.vertexCount = vCounter; + mesh.vertexCount = vCounter; - vData.vertices = (float *)malloc(vData.vertexCount*3*sizeof(float)); - vData.normals = (float *)malloc(vData.vertexCount*3*sizeof(float)); - vData.texcoords = (float *)malloc(vData.vertexCount*2*sizeof(float)); - vData.colors = (unsigned char *)malloc(vData.vertexCount*4*sizeof(unsigned char)); // Not used... + mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float)); + mesh.colors = (unsigned char *)malloc(mesh.vertexCount*4*sizeof(unsigned char)); // Not used... // Fill color data // NOTE: Not used any more... just one plain color defined at DrawModel() - for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255; + for (int i = 0; i < (4*mesh.vertexCount); i++) mesh.colors[i] = 255; int fCounter = 0; // Move vertices data for (int i = 0; i < vCounter; i++) { - vData.vertices[fCounter] = mapVertices[i].x; - vData.vertices[fCounter + 1] = mapVertices[i].y; - vData.vertices[fCounter + 2] = mapVertices[i].z; + mesh.vertices[fCounter] = mapVertices[i].x; + mesh.vertices[fCounter + 1] = mapVertices[i].y; + mesh.vertices[fCounter + 2] = mapVertices[i].z; fCounter += 3; } @@ -1068,9 +1068,9 @@ Model LoadCubicmap(Image cubicmap) // Move normals data for (int i = 0; i < nCounter; i++) { - vData.normals[fCounter] = mapNormals[i].x; - vData.normals[fCounter + 1] = mapNormals[i].y; - vData.normals[fCounter + 2] = mapNormals[i].z; + mesh.normals[fCounter] = mapNormals[i].x; + mesh.normals[fCounter + 1] = mapNormals[i].y; + mesh.normals[fCounter + 2] = mapNormals[i].z; fCounter += 3; } @@ -1079,8 +1079,8 @@ Model LoadCubicmap(Image cubicmap) // Move texcoords data for (int i = 0; i < tcCounter; i++) { - vData.texcoords[fCounter] = mapTexcoords[i].x; - vData.texcoords[fCounter + 1] = mapTexcoords[i].y; + mesh.texcoords[fCounter] = mapTexcoords[i].x; + mesh.texcoords[fCounter + 1] = mapTexcoords[i].y; fCounter += 2; } @@ -1090,18 +1090,18 @@ Model LoadCubicmap(Image cubicmap) free(cubicmapPixels); - // NOTE: At this point we have all vertex, texcoord, normal data for the model in vData struct + // NOTE: At this point we have all vertex, texcoord, normal data for the model in mesh struct - Model model = rlglLoadModel(vData); + Model model = rlglLoadModel(mesh); // Now that vertex data is uploaded to GPU, we can free arrays // NOTE: We don't need CPU vertex data on OpenGL 3.3 or ES2 if (rlGetVersion() != OPENGL_11) { - free(vData.vertices); - free(vData.texcoords); - free(vData.normals); - free(vData.colors); + free(mesh.vertices); + free(mesh.texcoords); + free(mesh.normals); + free(mesh.colors); } return model; @@ -1617,9 +1617,9 @@ static float GetHeightValue(Color pixel) } // Load OBJ mesh data -static VertexData LoadOBJ(const char *fileName) +static Mesh LoadOBJ(const char *fileName) { - VertexData vData = { 0 }; + Mesh mesh = { 0 }; char dataType; char comments[200]; @@ -1636,7 +1636,7 @@ static VertexData LoadOBJ(const char *fileName) if (objFile == NULL) { TraceLog(WARNING, "[%s] OBJ file could not be opened", fileName); - return vData; + return mesh; } // First reading pass: Get numVertex, numNormals, numTexCoords, numTriangles @@ -1747,15 +1747,15 @@ static VertexData LoadOBJ(const char *fileName) } // At this point all vertex data (v, vt, vn) has been gathered on midVertices, midTexCoords, midNormals - // Now we can organize that data into our VertexData struct + // Now we can organize that data into our Mesh struct - vData.vertexCount = numTriangles*3; + mesh.vertexCount = numTriangles*3; // Additional arrays to store vertex data as floats - vData.vertices = (float *)malloc(vData.vertexCount*3*sizeof(float)); - vData.texcoords = (float *)malloc(vData.vertexCount*2*sizeof(float)); - vData.normals = (float *)malloc(vData.vertexCount*3*sizeof(float)); - vData.colors = (unsigned char *)malloc(vData.vertexCount*4*sizeof(unsigned char)); + mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float)); + mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.colors = (unsigned char *)malloc(mesh.vertexCount*4*sizeof(unsigned char)); int vCounter = 0; // Used to count vertices float by float int tcCounter = 0; // Used to count texcoords float by float @@ -1783,32 +1783,32 @@ static VertexData LoadOBJ(const char *fileName) else if (numNormals == 0) fscanf(objFile, "%i/%i %i/%i %i/%i", &vNum[0], &vtNum[0], &vNum[1], &vtNum[1], &vNum[2], &vtNum[2]); else fscanf(objFile, "%i/%i/%i %i/%i/%i %i/%i/%i", &vNum[0], &vtNum[0], &vnNum[0], &vNum[1], &vtNum[1], &vnNum[1], &vNum[2], &vtNum[2], &vnNum[2]); - vData.vertices[vCounter] = midVertices[vNum[0]-1].x; - vData.vertices[vCounter + 1] = midVertices[vNum[0]-1].y; - vData.vertices[vCounter + 2] = midVertices[vNum[0]-1].z; + mesh.vertices[vCounter] = midVertices[vNum[0]-1].x; + mesh.vertices[vCounter + 1] = midVertices[vNum[0]-1].y; + mesh.vertices[vCounter + 2] = midVertices[vNum[0]-1].z; vCounter += 3; - vData.vertices[vCounter] = midVertices[vNum[1]-1].x; - vData.vertices[vCounter + 1] = midVertices[vNum[1]-1].y; - vData.vertices[vCounter + 2] = midVertices[vNum[1]-1].z; + mesh.vertices[vCounter] = midVertices[vNum[1]-1].x; + mesh.vertices[vCounter + 1] = midVertices[vNum[1]-1].y; + mesh.vertices[vCounter + 2] = midVertices[vNum[1]-1].z; vCounter += 3; - vData.vertices[vCounter] = midVertices[vNum[2]-1].x; - vData.vertices[vCounter + 1] = midVertices[vNum[2]-1].y; - vData.vertices[vCounter + 2] = midVertices[vNum[2]-1].z; + mesh.vertices[vCounter] = midVertices[vNum[2]-1].x; + mesh.vertices[vCounter + 1] = midVertices[vNum[2]-1].y; + mesh.vertices[vCounter + 2] = midVertices[vNum[2]-1].z; vCounter += 3; if (numNormals > 0) { - vData.normals[nCounter] = midNormals[vnNum[0]-1].x; - vData.normals[nCounter + 1] = midNormals[vnNum[0]-1].y; - vData.normals[nCounter + 2] = midNormals[vnNum[0]-1].z; + mesh.normals[nCounter] = midNormals[vnNum[0]-1].x; + mesh.normals[nCounter + 1] = midNormals[vnNum[0]-1].y; + mesh.normals[nCounter + 2] = midNormals[vnNum[0]-1].z; nCounter += 3; - vData.normals[nCounter] = midNormals[vnNum[1]-1].x; - vData.normals[nCounter + 1] = midNormals[vnNum[1]-1].y; - vData.normals[nCounter + 2] = midNormals[vnNum[1]-1].z; + mesh.normals[nCounter] = midNormals[vnNum[1]-1].x; + mesh.normals[nCounter + 1] = midNormals[vnNum[1]-1].y; + mesh.normals[nCounter + 2] = midNormals[vnNum[1]-1].z; nCounter += 3; - vData.normals[nCounter] = midNormals[vnNum[2]-1].x; - vData.normals[nCounter + 1] = midNormals[vnNum[2]-1].y; - vData.normals[nCounter + 2] = midNormals[vnNum[2]-1].z; + mesh.normals[nCounter] = midNormals[vnNum[2]-1].x; + mesh.normals[nCounter + 1] = midNormals[vnNum[2]-1].y; + mesh.normals[nCounter + 2] = midNormals[vnNum[2]-1].z; nCounter += 3; } else @@ -1817,17 +1817,17 @@ static VertexData LoadOBJ(const char *fileName) Vector3 norm = VectorCrossProduct(VectorSubtract(midVertices[vNum[1]-1], midVertices[vNum[0]-1]), VectorSubtract(midVertices[vNum[2]-1], midVertices[vNum[0]-1])); VectorNormalize(&norm); - vData.normals[nCounter] = norm.x; - vData.normals[nCounter + 1] = norm.y; - vData.normals[nCounter + 2] = norm.z; + mesh.normals[nCounter] = norm.x; + mesh.normals[nCounter + 1] = norm.y; + mesh.normals[nCounter + 2] = norm.z; nCounter += 3; - vData.normals[nCounter] = norm.x; - vData.normals[nCounter + 1] = norm.y; - vData.normals[nCounter + 2] = norm.z; + mesh.normals[nCounter] = norm.x; + mesh.normals[nCounter + 1] = norm.y; + mesh.normals[nCounter + 2] = norm.z; nCounter += 3; - vData.normals[nCounter] = norm.x; - vData.normals[nCounter + 1] = norm.y; - vData.normals[nCounter + 2] = norm.z; + mesh.normals[nCounter] = norm.x; + mesh.normals[nCounter + 1] = norm.y; + mesh.normals[nCounter + 2] = norm.z; nCounter += 3; } @@ -1835,14 +1835,14 @@ static VertexData LoadOBJ(const char *fileName) { // NOTE: If using negative texture coordinates with a texture filter of GL_CLAMP_TO_EDGE doesn't work! // NOTE: Texture coordinates are Y flipped upside-down - vData.texcoords[tcCounter] = midTexCoords[vtNum[0]-1].x; - vData.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[0]-1].y; + mesh.texcoords[tcCounter] = midTexCoords[vtNum[0]-1].x; + mesh.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[0]-1].y; tcCounter += 2; - vData.texcoords[tcCounter] = midTexCoords[vtNum[1]-1].x; - vData.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[1]-1].y; + mesh.texcoords[tcCounter] = midTexCoords[vtNum[1]-1].x; + mesh.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[1]-1].y; tcCounter += 2; - vData.texcoords[tcCounter] = midTexCoords[vtNum[2]-1].x; - vData.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[2]-1].y; + mesh.texcoords[tcCounter] = midTexCoords[vtNum[2]-1].x; + mesh.texcoords[tcCounter + 1] = 1.0f - midTexCoords[vtNum[2]-1].y; tcCounter += 2; } } break; @@ -1853,19 +1853,19 @@ static VertexData LoadOBJ(const char *fileName) fclose(objFile); // Security check, just in case no normals or no texcoords defined in OBJ - if (numTexCoords == 0) for (int i = 0; i < (2*vData.vertexCount); i++) vData.texcoords[i] = 0.0f; + if (numTexCoords == 0) for (int i = 0; i < (2*mesh.vertexCount); i++) mesh.texcoords[i] = 0.0f; // NOTE: We set all vertex colors to white // NOTE: Not used any more... just one plain color defined at DrawModel() - for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255; + for (int i = 0; i < (4*mesh.vertexCount); i++) mesh.colors[i] = 255; // Now we can free temp mid* arrays free(midVertices); free(midNormals); free(midTexCoords); - // NOTE: At this point we have all vertex, texcoord, normal data for the model in vData struct + // NOTE: At this point we have all vertex, texcoord, normal data for the model in mesh struct TraceLog(INFO, "[%s] Model loaded successfully in RAM (CPU)", fileName); - return vData; + return mesh; } diff --git a/src/raylib.h b/src/raylib.h index 1a99f007..16311df8 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -308,17 +308,27 @@ typedef struct Camera { Vector3 up; } Camera; +// Bounding box type +typedef struct BoundingBox { + Vector3 min; + Vector3 max; +} BoundingBox; + // Vertex data definning a mesh -// NOTE: If using OpenGL 1.1, data loaded in CPU; if OpenGL 3.3+ data loaded in GPU (vaoId) -typedef struct VertexData { - int vertexCount; - float *vertices; // 3 components per vertex - float *texcoords; // 2 components per vertex - float *normals; // 3 components per vertex - unsigned char *colors; // 4 components per vertex - unsigned int vaoId; - unsigned int vboId[4]; -} VertexData; +typedef struct Mesh { + int vertexCount; // num vertices + float *vertices; // vertex position (XYZ - 3 components per vertex) + float *texcoords; // vertex texture coordinates (UV - 2 components per vertex) + float *texcoords2; // vertex second texture coordinates (useful for lightmaps) + float *normals; // vertex normals (XYZ - 3 components per vertex) + float *tangents; // vertex tangents (XYZ - 3 components per vertex) + unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) + + BoundingBox bounds; // mesh limits defined by min and max points + + unsigned int vaoId; // OpenGL Vertex Array Object id + unsigned int vboId[6]; // OpenGL Vertex Buffer Objects id (6 types of vertex data) +} Mesh; // Shader type (generic shader) typedef struct Shader { @@ -349,7 +359,7 @@ typedef struct Shader { // 3d Model type typedef struct Model { - VertexData mesh; + Mesh mesh; Matrix transform; Texture2D texture; // Only for OpenGL 1.1, on newer versions this should be in the shader Shader shader; @@ -742,7 +752,7 @@ void DrawGizmo(Vector3 position); // Model 3d Loading and Drawing Functions (Module: models) //------------------------------------------------------------------------------------ Model LoadModel(const char *fileName); // Load a 3d model (.OBJ) -Model LoadModelEx(VertexData data); // Load a 3d model (from vertex data) +Model LoadModelEx(Mesh data); // Load a 3d model (from vertex data) //Model LoadModelFromRES(const char *rresName, int resId); // TODO: Load a 3d model from rRES file (raylib Resource) Model LoadHeightmap(Image heightmap, float maxHeight); // Load a heightmap image as a 3d model Model LoadCubicmap(Image cubicmap); // Load a map image as a 3d model (cubes based) diff --git a/src/rlgl.c b/src/rlgl.c index 7afa374e..ca08e1a2 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1100,9 +1100,9 @@ void rlglInitPostpro(void) if (postproFbo.id > 0) { // Create a simple quad model to render fbo texture - VertexData quadData; + Mesh quad; - quadData.vertexCount = 6; + quad.vertexCount = 6; float w = (float)screenWidth; float h = (float)screenHeight; @@ -1112,12 +1112,12 @@ void rlglInitPostpro(void) float quadNormals[6*3] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }; unsigned char quadColors[6*4] = { 255 }; - quadData.vertices = quadPositions; - quadData.texcoords = quadTexcoords; - quadData.normals = quadNormals; - quadData.colors = quadColors; + quad.vertices = quadPositions; + quad.texcoords = quadTexcoords; + quad.normals = quadNormals; + quad.colors = quadColors; - postproQuad = rlglLoadModel(quadData); + postproQuad = rlglLoadModel(quad); // NOTE: postproFbo.colorTextureId must be assigned to postproQuad model shader } @@ -1982,7 +1982,7 @@ void rlglGenerateMipmaps(Texture2D texture) } // Load vertex data into a VAO (if supported) and VBO -Model rlglLoadModel(VertexData mesh) +Model rlglLoadModel(Mesh mesh) { Model model; diff --git a/src/rlgl.h b/src/rlgl.h index d33844ce..066e0339 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -131,17 +131,22 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; COMPRESSED_ASTC_8x8_RGBA // 2 bpp } TextureFormat; - // VertexData type + // Mesh with vertex data type // NOTE: If using OpenGL 1.1, data loaded in CPU; if OpenGL 3.3+ data loaded in GPU (vaoId) - typedef struct VertexData { - int vertexCount; - float *vertices; // 3 components per vertex - float *texcoords; // 2 components per vertex - float *normals; // 3 components per vertex - unsigned char *colors; - unsigned int vaoId; - unsigned int vboId[4]; - } VertexData; + typedef struct Mesh { + int vertexCount; // num vertices + float *vertices; // vertex position (XYZ - 3 components per vertex) + float *texcoords; // vertex texture coordinates (UV - 2 components per vertex) + float *texcoords2; // vertex second texture coordinates (useful for lightmaps) + float *normals; // vertex normals (XYZ - 3 components per vertex) + float *tangents; // vertex tangents (XYZ - 3 components per vertex) + unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) + + BoundingBox bounds; // mesh limits defined by min and max points + + unsigned int vaoId; // OpenGL Vertex Array Object id + unsigned int vboId[6]; // OpenGL Vertex Buffer Objects id (6 types of vertex data) + } Mesh; // Shader type typedef struct Shader { @@ -179,7 +184,7 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; // 3d Model type typedef struct Model { - VertexData mesh; + Mesh mesh; Matrix transform; Texture2D texture; Shader shader; @@ -254,7 +259,7 @@ void rlglGenerateMipmaps(Texture2D texture); // Gene void rlglInitPostpro(void); // Initialize postprocessing system void rlglDrawPostpro(void); // Draw with postprocessing shader -Model rlglLoadModel(VertexData mesh); // Upload vertex data into GPU and provided VAO/VBO ids +Model rlglLoadModel(Mesh mesh); // Upload vertex data into GPU and provided VAO/VBO ids void rlglDrawModel(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color color, bool wires); Vector3 rlglUnproject(Vector3 source, Matrix proj, Matrix view); // Get world coordinates from screen coordinates -- cgit v1.2.3 From 29c618a35e19c1c00be94bf423ad6af7ecf1d3f8 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 19 Jan 2016 20:27:41 +0100 Subject: Added some functions (incomplete) --- src/models.c | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index d4b21258..06044820 100644 --- a/src/models.c +++ b/src/models.c @@ -1336,18 +1336,28 @@ bool CheckCollisionBoxSphere(Vector3 minBBox, Vector3 maxBBox, Vector3 centerSph return collision; } -// Detect collision between ray and box +// Detect collision between ray and sphere +bool CheckCollisionRaySphere(Ray ray, Vector3 spherePosition, float sphereRadius) +{ + bool collision = false; + + // TODO: implement collision... + + return collision; +} + +// Detect collision between ray and bounding box bool CheckCollisionRayBox(Ray ray, Vector3 minBBox, Vector3 maxBBox) { bool collision = false; float t[8]; - t[0] = (minBBox.x - ray.position.x) / ray.direction.x; - t[1] = (maxBBox.x - ray.position.x) / ray.direction.x; - t[2] = (minBBox.y - ray.position.y) / ray.direction.y; - t[3] = (maxBBox.y - ray.position.y) / ray.direction.y; - t[4] = (minBBox.z - ray.position.z) / ray.direction.z; - t[5] = (maxBBox.z - ray.position.z) / ray.direction.z; + t[0] = (minBBox.x - ray.position.x)/ray.direction.x; + t[1] = (maxBBox.x - ray.position.x)/ray.direction.x; + t[2] = (minBBox.y - ray.position.y)/ray.direction.y; + t[3] = (maxBBox.y - ray.position.y)/ray.direction.y; + t[4] = (minBBox.z - ray.position.z)/ray.direction.z; + t[5] = (maxBBox.z - ray.position.z)/ray.direction.z; t[6] = fmax(fmax(fmin(t[0], t[1]), fmin(t[2], t[3])), fmin(t[4], t[5])); t[7] = fmin(fmin(fmax(t[0], t[1]), fmax(t[2], t[3])), fmax(t[4], t[5])); @@ -1359,6 +1369,32 @@ bool CheckCollisionRayBox(Ray ray, Vector3 minBBox, Vector3 maxBBox) // TODO: Useful function to check collision area? //BoundingBox GetCollisionArea(BoundingBox box1, BoundingBox box2) +// Calculate mesh bounding box limits +BoundingBox CalculateBoundingBox(Mesh mesh) +{ + // Get min and max vertex to construct bounds (AABB) + Vector3 minVertex = mesh.vertices[0]; + Vector3 maxVertex = mesh.vertices[0]; + + for (int i = 1; i < mesh.vertexCount; i++) + { + // TODO: Compare min and max with previous vertex + //minVertex = Vector3.Min(minVertex, mesh.vertices[i]); + //maxVertex = Vector3.Max(maxVertex, mesh.vertices[i]); + } + + // NOTE: For OBB, transform mesh by model transform matrix + //minVertex = VectorTransform(meshMin, mesh.transform); + //maxVertex = VectorTransform(meshMax, mesh.transform); + + // Create the bounding box + BoundingBox box; + box.min = minVertex; + box.max = maxVertex; + + return box; +} + // Detect and resolve cubicmap collisions // NOTE: player position (or camera) is modified inside this function Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *playerPosition, float radius) -- cgit v1.2.3 From efa1c96d19095c801a01dbf9b0214a82b7ae8c11 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 20 Jan 2016 18:20:05 +0100 Subject: Adapted raymath as single header library Added support for single header implementation and also inline functions support --- src/core.c | 45 ++++++- src/models.c | 6 +- src/raylib.h | 4 +- src/raymath.h | 414 ++++++++++++++++++++++++++++++++-------------------------- src/rlgl.c | 2 + src/rlgl.h | 9 +- 6 files changed, 282 insertions(+), 198 deletions(-) (limited to 'src/models.c') diff --git a/src/core.c b/src/core.c index 7b7d65fc..f55dba50 100644 --- a/src/core.c +++ b/src/core.c @@ -38,9 +38,12 @@ #include "raylib.h" // raylib main header #include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 3.3+ or ES2 -#include "raymath.h" // Required for data type Matrix and Matrix functions #include "utils.h" // TraceLog() function // NOTE: Includes Android fopen map, InitAssetManager() + +#define RAYMATH_IMPLEMENTATION // Use raymath as a header-only library (includes implementation) +#define RAYMATH_EXTERN_INLINE // Compile raymath functions as static inline (remember, it's a compiler hint) +#include "raymath.h" // Required for Vector3 and Matrix functions #include // Standard input / output lib #include // Declares malloc() and free() for memory management, rand(), atexit() @@ -643,6 +646,46 @@ float *ColorToFloat(Color color) return buffer; } +// Converts Vector3 to float array +float *VectorToFloat(Vector3 vec) +{ + static float buffer[3]; + + buffer[0] = vec.x; + buffer[1] = vec.y; + buffer[2] = vec.z; + + return buffer; +} + +// Converts Matrix to float array +// NOTE: Returned vector is a transposed version of the Matrix struct, +// it should be this way because, despite raymath use OpenGL column-major convention, +// Matrix struct memory alignment and variables naming are not coherent +float *MatrixToFloat(Matrix mat) +{ + static float buffer[16]; + + buffer[0] = mat.m0; + buffer[1] = mat.m4; + buffer[2] = mat.m8; + buffer[3] = mat.m12; + buffer[4] = mat.m1; + buffer[5] = mat.m5; + buffer[6] = mat.m9; + buffer[7] = mat.m13; + buffer[8] = mat.m2; + buffer[9] = mat.m6; + buffer[10] = mat.m10; + buffer[11] = mat.m14; + buffer[12] = mat.m3; + buffer[13] = mat.m7; + buffer[14] = mat.m11; + buffer[15] = mat.m15; + + return buffer; +} + // Returns a Color struct from hexadecimal value Color GetColor(int hexValue) { diff --git a/src/models.c b/src/models.c index 06044820..3d228a30 100644 --- a/src/models.c +++ b/src/models.c @@ -34,8 +34,8 @@ #include // Required for strcmp() #include // Used for sin, cos, tan -#include "raymath.h" // Required for data type Matrix and Matrix functions #include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 3.3+ or ES2 +#include "raymath.h" // Required for data type Matrix and Matrix functions //---------------------------------------------------------------------------------- // Defines and Macros @@ -1373,8 +1373,8 @@ bool CheckCollisionRayBox(Ray ray, Vector3 minBBox, Vector3 maxBBox) BoundingBox CalculateBoundingBox(Mesh mesh) { // Get min and max vertex to construct bounds (AABB) - Vector3 minVertex = mesh.vertices[0]; - Vector3 maxVertex = mesh.vertices[0]; + Vector3 minVertex = (Vector3){ mesh.vertices[0], mesh.vertices[1], mesh.vertices[2] }; + Vector3 maxVertex = (Vector3){ mesh.vertices[0], mesh.vertices[1], mesh.vertices[2] }; for (int i = 1; i < mesh.vertexCount; i++) { diff --git a/src/raylib.h b/src/raylib.h index 16311df8..0a768fe4 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -547,8 +547,8 @@ float GetFrameTime(void); // Returns time in s Color GetColor(int hexValue); // Returns a Color struct from hexadecimal value int GetHexValue(Color color); // Returns hexadecimal value for a Color float *ColorToFloat(Color color); // Converts Color to float array and normalizes -float *VectorToFloat(Vector3 vec); // Converts Vector3 to float array (defined in raymath module) -float *MatrixToVector(Matrix mat); // Converts Matrix to float array (defined in raymath module) +float *VectorToFloat(Vector3 vec); // Converts Vector3 to float array +float *MatrixToFloat(Matrix mat); // Converts Matrix to float array int GetRandomValue(int min, int max); // Returns a random value between min and max (both included) Color Fade(Color color, float alpha); // Color fade-in or fade-out, alpha goes from 0.0f to 1.0f diff --git a/src/raymath.h b/src/raymath.h index f5912795..8ad32528 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -1,9 +1,23 @@ /********************************************************************************************** * -* raymath +* raymath (header only file) * * Some useful functions to work with Vector3, Matrix and Quaternions * +* You must: +* #define RAYMATH_IMPLEMENTATION +* before you include this file in *only one* C or C++ file to create the implementation. +* +* Example: +* #define RAYMATH_IMPLEMENTATION +* #include "raymath.h" +* +* You can also use: +* #define RAYMATH_EXTERN_INLINE // Inlines all functions code, so it runs faster. +* // This requires lots of memory on system. +* #define RAYMATH_STANDALONE // Not dependent on raylib.h structs: Vector3, Matrix. +* +* * Copyright (c) 2015 Ramon Santamaria (@raysan5) * * This software is provided "as-is", without any express or implied warranty. In no event @@ -22,37 +36,21 @@ * 3. This notice may not be removed or altered from any source distribution. * **********************************************************************************************/ -//============================================================================ -// YOU MUST -// -// #define RAYMATH_DEFINE -// -// Like: -// -// #define RAYMATH_DEFINE -// #include "raymath.h" -// -// YOU CAN: -// #define RAYMATH_INLINE //inlines all code, so it runs faster. This requires lots of memory on system. -// AND -// #define RAYMATH_STANDALONE //not dependent on outside libs -// -// This needs to be done for every library/source file. -//============================================================================ - -#ifdef RAYMATH_INLINE - #define RMDEF static inline -#else - #define RMDEF static -#endif #ifndef RAYMATH_H #define RAYMATH_H -//#define RAYMATH_STANDALONE // NOTE: To use raymath as standalone lib, just uncomment this line +//#define RAYMATH_STANDALONE // NOTE: To use raymath as standalone lib, just uncomment this line +//#define RAYMATH_EXTERN_INLINE // NOTE: To compile functions as static inline, uncomment this line #ifndef RAYMATH_STANDALONE - #include "raylib.h" // Required for typedef: Vector3 + #include "raylib.h" // Required for structs: Vector3, Matrix +#endif + +#if defined(RAYMATH_EXTERN_INLINE) + #define RMDEF extern inline +#else + #define RMDEF extern #endif //---------------------------------------------------------------------------------- @@ -63,18 +61,18 @@ #endif #ifndef DEG2RAD - #define DEG2RAD (PI / 180.0f) + #define DEG2RAD (PI/180.0f) #endif #ifndef RAD2DEG - #define RAD2DEG (180.0f / PI) + #define RAD2DEG (180.0f/PI) #endif //---------------------------------------------------------------------------------- // Types and Structures Definition //---------------------------------------------------------------------------------- -#ifdef RAYMATH_STANDALONE +#if defined(RAYMATH_STANDALONE) // Vector2 type typedef struct Vector2 { float x; @@ -105,7 +103,77 @@ typedef struct Quaternion { float w; } Quaternion; -#ifdef RAYMATH_DEFINE +#ifndef RAYMATH_EXTERN_INLINE + +#ifdef __cplusplus +extern "C" { +#endif + +//------------------------------------------------------------------------------------ +// Functions Declaration to work with Vector3 +//------------------------------------------------------------------------------------ +RMDEF Vector3 VectorAdd(Vector3 v1, Vector3 v2); // Add two vectors +RMDEF Vector3 VectorSubtract(Vector3 v1, Vector3 v2); // Substract two vectors +RMDEF Vector3 VectorCrossProduct(Vector3 v1, Vector3 v2); // Calculate two vectors cross product +RMDEF Vector3 VectorPerpendicular(Vector3 v); // Calculate one vector perpendicular vector +RMDEF float VectorDotProduct(Vector3 v1, Vector3 v2); // Calculate two vectors dot product +RMDEF float VectorLength(const Vector3 v); // Calculate vector lenght +RMDEF void VectorScale(Vector3 *v, float scale); // Scale provided vector +RMDEF void VectorNegate(Vector3 *v); // Negate provided vector (invert direction) +RMDEF void VectorNormalize(Vector3 *v); // Normalize provided vector +RMDEF float VectorDistance(Vector3 v1, Vector3 v2); // Calculate distance between two points +RMDEF Vector3 VectorLerp(Vector3 v1, Vector3 v2, float amount); // Calculate linear interpolation between two vectors +RMDEF Vector3 VectorReflect(Vector3 vector, Vector3 normal); // Calculate reflected vector to normal +RMDEF void VectorTransform(Vector3 *v, Matrix mat); // Transforms a Vector3 by a given Matrix +RMDEF Vector3 VectorZero(void); // Return a Vector3 init to zero + +//------------------------------------------------------------------------------------ +// Functions Declaration to work with Matrix +//------------------------------------------------------------------------------------ +RMDEF float MatrixDeterminant(Matrix mat); // Compute matrix determinant +RMDEF float MatrixTrace(Matrix mat); // Returns the trace of the matrix (sum of the values along the diagonal) +RMDEF void MatrixTranspose(Matrix *mat); // Transposes provided matrix +RMDEF void MatrixInvert(Matrix *mat); // Invert provided matrix +RMDEF void MatrixNormalize(Matrix *mat); // Normalize provided matrix +RMDEF Matrix MatrixIdentity(void); // Returns identity matrix +RMDEF Matrix MatrixAdd(Matrix left, Matrix right); // Add two matrices +RMDEF Matrix MatrixSubstract(Matrix left, Matrix right); // Substract two matrices (left - right) +RMDEF Matrix MatrixTranslate(float x, float y, float z); // Returns translation matrix +RMDEF Matrix MatrixRotate(float angle, Vector3 axis); // Returns rotation matrix for an angle around an specified axis (angle in radians) +RMDEF Matrix MatrixRotateX(float angle); // Returns x-rotation matrix (angle in radians) +RMDEF Matrix MatrixRotateY(float angle); // Returns y-rotation matrix (angle in radians) +RMDEF Matrix MatrixRotateZ(float angle); // Returns z-rotation matrix (angle in radians) +RMDEF Matrix MatrixScale(float x, float y, float z); // Returns scaling matrix +RMDEF Matrix MatrixMultiply(Matrix left, Matrix right); // Returns two matrix multiplication +RMDEF Matrix MatrixFrustum(double left, double right, double bottom, double top, double near, double far); // Returns perspective projection matrix +RMDEF Matrix MatrixPerspective(double fovy, double aspect, double near, double far); // Returns perspective projection matrix +RMDEF Matrix MatrixOrtho(double left, double right, double bottom, double top, double near, double far); // Returns orthographic projection matrix +RMDEF Matrix MatrixLookAt(Vector3 position, Vector3 target, Vector3 up); // Returns camera look-at matrix (view matrix) +RMDEF void PrintMatrix(Matrix m); // Print matrix utility + +//------------------------------------------------------------------------------------ +// Functions Declaration to work with Quaternions +//------------------------------------------------------------------------------------ +RMDEF float QuaternionLength(Quaternion quat); // Compute the length of a quaternion +RMDEF void QuaternionNormalize(Quaternion *q); // Normalize provided quaternion +RMDEF Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2); // Calculate two quaternion multiplication +RMDEF Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float slerp); // Calculates spherical linear interpolation between two quaternions +RMDEF Quaternion QuaternionFromMatrix(Matrix matrix); // Returns a quaternion for a given rotation matrix +RMDEF Matrix QuaternionToMatrix(Quaternion q); // Returns a matrix for a given quaternion +RMDEF Quaternion QuaternionFromAxisAngle(float angle, Vector3 axis); // Returns rotation quaternion for an angle and axis +RMDEF void QuaternionToAxisAngle(Quaternion q, float *outAngle, Vector3 *outAxis); // Returns the rotation angle and axis for a given quaternion +RMDEF void QuaternionTransform(Quaternion *q, Matrix mat); // Transform a quaternion given a transformation matrix + +#ifdef __cplusplus +} +#endif + +#endif // notdef RAYMATH_EXTERN_INLINE + +//////////////////////////////////////////////////////////////////// end of header file + +#if defined(RAYMATH_IMPLEMENTATION) || defined(RAYMATH_EXTERN_INLINE) + #include // Used only on PrintMatrix() #include // Standard math libary: sin(), cos(), tan()... #include // Used for abs() @@ -114,18 +182,6 @@ typedef struct Quaternion { // Module Functions Definition - Vector3 math //---------------------------------------------------------------------------------- -// Converts Vector3 to float array -RMDEF float *VectorToFloat(Vector3 vec) -{ - static float buffer[3]; - - buffer[0] = vec.x; - buffer[1] = vec.y; - buffer[2] = vec.z; - - return buffer; -} - // Add two vectors RMDEF Vector3 VectorAdd(Vector3 v1, Vector3 v2) { @@ -229,9 +285,9 @@ RMDEF void VectorNormalize(Vector3 *v) length = VectorLength(*v); - if (length == 0) length = 1; + if (length == 0) length = 1.0f; - ilength = 1.0/length; + ilength = 1.0f/length; v->x *= ilength; v->y *= ilength; @@ -257,9 +313,9 @@ RMDEF Vector3 VectorLerp(Vector3 v1, Vector3 v2, float amount) { Vector3 result; - result.x = v1.x + amount * (v2.x - v1.x); - result.y = v1.y + amount * (v2.y - v1.y); - result.z = v1.z + amount * (v2.z - v1.z); + result.x = v1.x + amount*(v2.x - v1.x); + result.y = v1.y + amount*(v2.y - v1.y); + result.z = v1.z + amount*(v2.z - v1.z); return result; } @@ -269,15 +325,15 @@ RMDEF Vector3 VectorReflect(Vector3 vector, Vector3 normal) { // I is the original vector // N is the normal of the incident plane - // R = I - (2 * N * ( DotProduct[ I,N] )) + // R = I - (2*N*( DotProduct[ I,N] )) Vector3 result; float dotProduct = VectorDotProduct(vector, normal); - result.x = vector.x - (2.0 * normal.x) * dotProduct; - result.y = vector.y - (2.0 * normal.y) * dotProduct; - result.z = vector.z - (2.0 * normal.z) * dotProduct; + result.x = vector.x - (2.0f*normal.x)*dotProduct; + result.y = vector.y - (2.0f*normal.y)*dotProduct; + result.z = vector.z - (2.0f*normal.z)*dotProduct; return result; } @@ -308,34 +364,6 @@ RMDEF Vector3 VectorZero(void) // Module Functions Definition - Matrix math //---------------------------------------------------------------------------------- -// Converts Matrix to float array -// NOTE: Returned vector is a transposed version of the Matrix struct, -// it should be this way because, despite raymath use OpenGL column-major convention, -// Matrix struct memory alignment and variables naming are not coherent -RMDEF float *MatrixToFloat(Matrix mat) -{ - static float buffer[16]; - - buffer[0] = mat.m0; - buffer[1] = mat.m4; - buffer[2] = mat.m8; - buffer[3] = mat.m12; - buffer[4] = mat.m1; - buffer[5] = mat.m5; - buffer[6] = mat.m9; - buffer[7] = mat.m13; - buffer[8] = mat.m2; - buffer[9] = mat.m6; - buffer[10] = mat.m10; - buffer[11] = mat.m14; - buffer[12] = mat.m3; - buffer[13] = mat.m7; - buffer[14] = mat.m11; - buffer[15] = mat.m15; - - return buffer; -} - // Compute matrix determinant RMDEF float MatrixDeterminant(Matrix mat) { @@ -413,7 +441,7 @@ RMDEF void MatrixInvert(Matrix *mat) float b11 = a22*a33 - a23*a32; // Calculate the invert determinant (inlined to avoid double-caching) - float invDet = 1/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06); + float invDet = 1.0f/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06); temp.m0 = (a11*b11 - a12*b10 + a13*b09)*invDet; temp.m1 = (-a01*b11 + a02*b10 - a03*b09)*invDet; @@ -461,7 +489,10 @@ RMDEF void MatrixNormalize(Matrix *mat) // Returns identity matrix RMDEF Matrix MatrixIdentity(void) { - Matrix result = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; + Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f }; return result; } @@ -519,7 +550,10 @@ RMDEF Matrix MatrixSubstract(Matrix left, Matrix right) // Returns translation matrix RMDEF Matrix MatrixTranslate(float x, float y, float z) { - Matrix result = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }; + Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + x, y, z, 1.0f }; return result; } @@ -536,9 +570,9 @@ RMDEF Matrix MatrixRotate(float angle, Vector3 axis) float length = sqrt(x*x + y*y + z*z); - if ((length != 1) && (length != 0)) + if ((length != 1.0f) && (length != 0.0f)) { - length = 1/length; + length = 1.0f/length; x *= length; y *= length; z *= length; @@ -594,15 +628,15 @@ RMDEF Matrix MatrixRotate(float angle, float x, float y, float z) m2 = result.m2, m6 = result.m6, m10 = result.m10, m14 = result.m14; // build rotation matrix - float r0 = x * x * c1 + c; - float r1 = x * y * c1 + z * s; - float r2 = x * z * c1 - y * s; - float r4 = x * y * c1 - z * s; - float r5 = y * y * c1 + c; - float r6 = y * z * c1 + x * s; - float r8 = x * z * c1 + y * s; - float r9 = y * z * c1 - x * s; - float r10= z * z * c1 + c; + float r0 = x*x*c1 + c; + float r1 = x*y*c1 + z*s; + float r2 = x*z*c1 - y*s; + float r4 = x*y*c1 - z*s; + float r5 = y*y*c1 + c; + float r6 = y*z*c1 + x*s; + float r8 = x*z*c1 + y*s; + float r9 = y*z*c1 - x*s; + float r10= z*z*c1 + c; // multiply rotation matrix result.m0 = r0*m0 + r4*m1 + r8*m2; @@ -673,7 +707,10 @@ RMDEF Matrix MatrixRotateZ(float angle) // Returns scaling matrix RMDEF Matrix MatrixScale(float x, float y, float z) { - Matrix result = { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }; + Matrix result = { x, 0.0f, 0.0f, 0.0f, + 0.0f, y, 0.0f, 0.0f, + 0.0f, 0.0f, z, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f }; return result; } @@ -713,25 +750,25 @@ RMDEF Matrix MatrixFrustum(double left, double right, double bottom, double top, float tb = (top - bottom); float fn = (far - near); - result.m0 = (near*2.0f) / rl; - result.m1 = 0; - result.m2 = 0; - result.m3 = 0; + result.m0 = (near*2.0f)/rl; + result.m1 = 0.0f; + result.m2 = 0.0f; + result.m3 = 0.0f; - result.m4 = 0; - result.m5 = (near*2.0f) / tb; - result.m6 = 0; - result.m7 = 0; + result.m4 = 0.0f; + result.m5 = (near*2.0f)/tb; + result.m6 = 0.0f; + result.m7 = 0.0f; - result.m8 = (right + left) / rl; - result.m9 = (top + bottom) / tb; - result.m10 = -(far + near) / fn; + result.m8 = (right + left)/rl; + result.m9 = (top + bottom)/tb; + result.m10 = -(far + near)/fn; result.m11 = -1.0f; - result.m12 = 0; - result.m13 = 0; - result.m14 = -(far*near*2.0f) / fn; - result.m15 = 0; + result.m12 = 0.0f; + result.m13 = 0.0f; + result.m14 = -(far*near*2.0f)/fn; + result.m15 = 0.0f; return result; } @@ -739,7 +776,7 @@ RMDEF Matrix MatrixFrustum(double left, double right, double bottom, double top, // Returns perspective projection matrix RMDEF Matrix MatrixPerspective(double fovy, double aspect, double near, double far) { - double top = near*tanf(fovy*PI / 360.0f); + double top = near*tanf(fovy*PI/360.0f); double right = top*aspect; return MatrixFrustum(-right, right, -top, top, near, far); @@ -754,22 +791,22 @@ RMDEF Matrix MatrixOrtho(double left, double right, double bottom, double top, d float tb = (top - bottom); float fn = (far - near); - result.m0 = 2 / rl; - result.m1 = 0; - result.m2 = 0; - result.m3 = 0; - result.m4 = 0; - result.m5 = 2 / tb; - result.m6 = 0; - result.m7 = 0; - result.m8 = 0; - result.m9 = 0; - result.m10 = -2 / fn; - result.m11 = 0; - result.m12 = -(left + right) / rl; - result.m13 = -(top + bottom) / tb; - result.m14 = -(far + near) / fn; - result.m15 = 1; + result.m0 = 2.0f/rl; + result.m1 = 0.0f; + result.m2 = 0.0f; + result.m3 = 0.0f; + result.m4 = 0.0f; + result.m5 = 2.0f/tb; + result.m6 = 0.0f; + result.m7 = 0.0f; + result.m8 = 0.0f; + result.m9 = 0.0f; + result.m10 = -2.0f/fn; + result.m11 = 0.0f; + result.m12 = -(left + right)/rl; + result.m13 = -(top + bottom)/tb; + result.m14 = -(far + near)/fn; + result.m15 = 1.0f; return result; } @@ -789,19 +826,19 @@ RMDEF Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up) result.m0 = x.x; result.m1 = x.y; result.m2 = x.z; - result.m3 = -((x.x * eye.x) + (x.y * eye.y) + (x.z * eye.z)); + result.m3 = -((x.x*eye.x) + (x.y*eye.y) + (x.z*eye.z)); result.m4 = y.x; result.m5 = y.y; result.m6 = y.z; - result.m7 = -((y.x * eye.x) + (y.y * eye.y) + (y.z * eye.z)); + result.m7 = -((y.x*eye.x) + (y.y*eye.y) + (y.z*eye.z)); result.m8 = z.x; result.m9 = z.y; result.m10 = z.z; - result.m11 = -((z.x * eye.x) + (z.y * eye.y) + (z.z * eye.z)); - result.m12 = 0; - result.m13 = 0; - result.m14 = 0; - result.m15 = 1; + result.m11 = -((z.x*eye.x) + (z.y*eye.y) + (z.z*eye.z)); + result.m12 = 0.0f; + result.m13 = 0.0f; + result.m14 = 0.0f; + result.m15 = 1.0f; return result; } @@ -834,9 +871,9 @@ RMDEF void QuaternionNormalize(Quaternion *q) length = QuaternionLength(*q); - if (length == 0) length = 1; + if (length == 0.0f) length = 1.0f; - ilength = 1.0/length; + ilength = 1.0f/length; q->x *= ilength; q->y *= ilength; @@ -882,8 +919,8 @@ RMDEF Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount) } else { - float ratioA = sin((1 - amount)*halfTheta) / sinHalfTheta; - float ratioB = sin(amount*halfTheta) / sinHalfTheta; + float ratioA = sin((1 - amount)*halfTheta)/sinHalfTheta; + float ratioB = sin(amount*halfTheta)/sinHalfTheta; result.x = (q1.x*ratioA + q2.x*ratioB); result.y = (q1.y*ratioA + q2.y*ratioB); @@ -902,15 +939,15 @@ RMDEF Quaternion QuaternionFromMatrix(Matrix matrix) float trace = MatrixTrace(matrix); - if (trace > 0) + if (trace > 0.0f) { - float s = (float)sqrt(trace + 1) * 2; - float invS = 1 / s; + float s = (float)sqrt(trace + 1)*2.0f; + float invS = 1.0f/s; - result.w = s * 0.25; - result.x = (matrix.m6 - matrix.m9) * invS; - result.y = (matrix.m8 - matrix.m2) * invS; - result.z = (matrix.m1 - matrix.m4) * invS; + result.w = s*0.25f; + result.x = (matrix.m6 - matrix.m9)*invS; + result.y = (matrix.m8 - matrix.m2)*invS; + result.z = (matrix.m1 - matrix.m4)*invS; } else { @@ -918,33 +955,33 @@ RMDEF Quaternion QuaternionFromMatrix(Matrix matrix) if (m00 > m11 && m00 > m22) { - float s = (float)sqrt(1 + m00 - m11 - m22) * 2; - float invS = 1 / s; + float s = (float)sqrt(1.0f + m00 - m11 - m22)*2.0f; + float invS = 1.0f/s; - result.w = (matrix.m6 - matrix.m9) * invS; - result.x = s * 0.25; - result.y = (matrix.m4 + matrix.m1) * invS; - result.z = (matrix.m8 + matrix.m2) * invS; + result.w = (matrix.m6 - matrix.m9)*invS; + result.x = s*0.25f; + result.y = (matrix.m4 + matrix.m1)*invS; + result.z = (matrix.m8 + matrix.m2)*invS; } else if (m11 > m22) { - float s = (float)sqrt(1 + m11 - m00 - m22) * 2; - float invS = 1 / s; + float s = (float)sqrt(1.0f + m11 - m00 - m22)*2.0f; + float invS = 1.0f/s; - result.w = (matrix.m8 - matrix.m2) * invS; - result.x = (matrix.m4 + matrix.m1) * invS; - result.y = s * 0.25; - result.z = (matrix.m9 + matrix.m6) * invS; + result.w = (matrix.m8 - matrix.m2)*invS; + result.x = (matrix.m4 + matrix.m1)*invS; + result.y = s*0.25f; + result.z = (matrix.m9 + matrix.m6)*invS; } else { - float s = (float)sqrt(1 + m22 - m00 - m11) * 2; - float invS = 1 / s; + float s = (float)sqrt(1.0f + m22 - m00 - m11)*2.0f; + float invS = 1.0f/s; - result.w = (matrix.m1 - matrix.m4) * invS; - result.x = (matrix.m8 + matrix.m2) * invS; - result.y = (matrix.m9 + matrix.m6) * invS; - result.z = s * 0.25; + result.w = (matrix.m1 - matrix.m4)*invS; + result.x = (matrix.m8 + matrix.m2)*invS; + result.y = (matrix.m9 + matrix.m6)*invS; + result.z = s*0.25f; } } @@ -974,22 +1011,22 @@ RMDEF Matrix QuaternionToMatrix(Quaternion q) float wy = w*y2; float wz = w*z2; - result.m0 = 1 - (yy + zz); + result.m0 = 1.0f - (yy + zz); result.m1 = xy - wz; result.m2 = xz + wy; - result.m3 = 0; + result.m3 = 0.0f; result.m4 = xy + wz; - result.m5 = 1 - (xx + zz); + result.m5 = 1.0f - (xx + zz); result.m6 = yz - wx; - result.m7 = 0; + result.m7 = 0.0f; result.m8 = xz - wy; result.m9 = yz + wx; - result.m10 = 1 - (xx + yy); - result.m11 = 0; - result.m12 = 0; - result.m13 = 0; - result.m14 = 0; - result.m15 = 1; + result.m10 = 1.0f - (xx + yy); + result.m11 = 0.0f; + result.m12 = 0.0f; + result.m13 = 0.0f; + result.m14 = 0.0f; + result.m15 = 1.0f; return result; } @@ -998,17 +1035,17 @@ RMDEF Matrix QuaternionToMatrix(Quaternion q) // NOTE: angle must be provided in radians RMDEF Quaternion QuaternionFromAxisAngle(float angle, Vector3 axis) { - Quaternion result = { 0, 0, 0, 1 }; + Quaternion result = { 0.0f, 0.0f, 0.0f, 1.0f }; - if (VectorLength(axis) != 0.0) + if (VectorLength(axis) != 0.0f) - angle *= 0.5; + angle *= 0.5f; VectorNormalize(&axis); - result.x = axis.x * (float)sin(angle); - result.y = axis.y * (float)sin(angle); - result.z = axis.z * (float)sin(angle); + result.x = axis.x*(float)sin(angle); + result.y = axis.y*(float)sin(angle); + result.z = axis.z*(float)sin(angle); result.w = (float)cos(angle); QuaternionNormalize(&result); @@ -1021,23 +1058,23 @@ RMDEF void QuaternionToAxisAngle(Quaternion q, float *outAngle, Vector3 *outAxis { if (fabs(q.w) > 1.0f) QuaternionNormalize(&q); - Vector3 resAxis = { 0, 0, 0 }; - float resAngle = 0; + Vector3 resAxis = { 0.0f, 0.0f, 0.0f }; + float resAngle = 0.0f; - resAngle = 2.0f * (float)acos(q.w); - float den = (float)sqrt(1.0 - q.w * q.w); + resAngle = 2.0f*(float)acos(q.w); + float den = (float)sqrt(1.0f - q.w*q.w); if (den > 0.0001f) { - resAxis.x = q.x / den; - resAxis.y = q.y / den; - resAxis.z = q.z / den; + resAxis.x = q.x/den; + resAxis.y = q.y/den; + resAxis.z = q.z/den; } else { // This occurs when the angle is zero. // Not a problem: just set an arbitrary normalized axis. - resAxis.x = 1.0; + resAxis.x = 1.0f; } *outAxis = resAxis; @@ -1058,5 +1095,6 @@ RMDEF void QuaternionTransform(Quaternion *q, Matrix mat) q->w = mat.m3*x + mat.m7*y + mat.m11*z + mat.m15*w; } -#endif // RAYMATH_DEFINE -#endif // RAYMATH_H \ No newline at end of file +#endif // RAYMATH_IMPLEMENTATION + +#endif // RAYMATH_H \ No newline at end of file diff --git a/src/rlgl.c b/src/rlgl.c index ca08e1a2..dbcbc35f 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -32,6 +32,8 @@ #include // Declares malloc() and free() for memory management, rand() #include // Declares strcmp(), strlen(), strtok() +#include "raymath.h" // Required for Vector3 and Matrix functions + #if defined(GRAPHICS_API_OPENGL_11) #ifdef __APPLE__ // OpenGL include for OSX #include diff --git a/src/rlgl.h b/src/rlgl.h index 066e0339..fbece962 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -37,11 +37,12 @@ #endif #if defined(RLGL_STANDALONE) - #define RAYMATH_STANDALONE + #define RAYMATH_IMPLEMENTATION // Use raymath as a header-only library (includes implementation) + #define RAYMATH_EXTERN_INLINE // Compile raymath functions as static inline (remember, it's a compiler hint) + #define RAYMATH_STANDALONE // Not dependent on raylib.h structs: Vector3, Matrix + #include "raymath.h" // Required for Vector3 and Matrix functions #endif -#include "raymath.h" // Required for data type Matrix and Matrix functions - // Select desired OpenGL version // NOTE: Those preprocessor defines are only used on rlgl module, // if OpenGL version is required by any other module, it uses rlGetVersion() @@ -92,7 +93,7 @@ typedef enum { RL_LINES, RL_TRIANGLES, RL_QUADS } DrawMode; typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; -#ifdef RLGL_STANDALONE +#if defined(RLGL_STANDALONE) #ifndef __cplusplus // Boolean type typedef enum { false, true } bool; -- cgit v1.2.3 From fcd30c5649f28d0d9c867712898fc5537f176c85 Mon Sep 17 00:00:00 2001 From: victorfisac Date: Wed, 20 Jan 2016 19:28:47 +0100 Subject: Added ray-sphere collision detection --- src/models.c | 37 ++++++++++++++++++++++++++++++++++++- src/raylib.h | 2 ++ 2 files changed, 38 insertions(+), 1 deletion(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index 3d228a30..80d9a13a 100644 --- a/src/models.c +++ b/src/models.c @@ -1341,7 +1341,42 @@ bool CheckCollisionRaySphere(Ray ray, Vector3 spherePosition, float sphereRadius { bool collision = false; - // TODO: implement collision... + Vector3 raySpherePos = VectorSubtract(spherePosition, ray.position); + float distance = VectorLength(raySpherePos); + float vector = VectorDotProduct(raySpherePos, ray.direction); + float d = sphereRadius*sphereRadius - (distance*distance - vector*vector); + + if(d >= 0.0f) collision = true; + + return collision; +} + +// Detect collision between ray and sphere with extended parameters and collision point detection +bool CheckCollisionRaySphereEx(Ray ray, Vector3 spherePosition, float sphereRadius, Vector3 *collisionPoint) +{ + bool collision = false; + + Vector3 raySpherePos = VectorSubtract(spherePosition, ray.position); + float distance = VectorLength(raySpherePos); + float vector = VectorDotProduct(raySpherePos, ray.direction); + float d = sphereRadius*sphereRadius - (distance*distance - vector*vector); + + if(d >= 0.0f) collision = true; + + // Calculate collision point + Vector3 offset = ray.direction; + float collisionDistance = 0; + + // Check if ray origin is inside the sphere to calculate the correct collision point + if(distance < sphereRadius) collisionDistance = vector + sqrt(d); + else collisionDistance = vector - sqrt(d); + + VectorScale(&offset, collisionDistance); + Vector3 cPoint = VectorAdd(ray.position, offset); + + collisionPoint->x = cPoint.x; + collisionPoint->y = cPoint.y; + collisionPoint->z = cPoint.z; return collision; } diff --git a/src/raylib.h b/src/raylib.h index 5798d907..bebf4bc5 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -770,6 +770,8 @@ void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vec bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB); // Detect collision between two spheres bool CheckCollisionBoxes(Vector3 minBBox1, Vector3 maxBBox1, Vector3 minBBox2, Vector3 maxBBox2); // Detect collision between two boxes bool CheckCollisionBoxSphere(Vector3 minBBox, Vector3 maxBBox, Vector3 centerSphere, float radiusSphere); // Detect collision between box and sphere +bool CheckCollisionRaySphere(Ray ray, Vector3 spherePosition, float sphereRadius); // Detect collision between ray and sphere +bool CheckCollisionRaySphereEx(Ray ray, Vector3 spherePosition, float sphereRadius, Vector3 *collisionPoint); // Detect collision between ray and sphere with extended parameters and collision point detection bool CheckCollisionRayBox(Ray ray, Vector3 minBBox, Vector3 maxBBox); // Detect collision between ray and box Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *playerPosition, float radius); // Detect collision of player radius with cubicmap // NOTE: Return the normal vector of the impacted surface -- cgit v1.2.3 From 08da91047e8a2c593c3bc40b31c4796ae6cbb26d Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 23 Jan 2016 13:22:13 +0100 Subject: Some code tweaks --- src/audio.c | 17 +++++------------ src/core.c | 2 ++ src/models.c | 2 +- src/raylib.h | 2 +- src/shapes.c | 4 ++-- 5 files changed, 11 insertions(+), 16 deletions(-) (limited to 'src/models.c') diff --git a/src/audio.c b/src/audio.c index 6313c9dc..e40fdd41 100644 --- a/src/audio.c +++ b/src/audio.c @@ -166,15 +166,8 @@ void CloseAudioDevice(void) // Load sound to memory Sound LoadSound(char *fileName) { - Sound sound; - Wave wave; - - // Init some default values for wave... - wave.data = NULL; - wave.dataSize = 0; - wave.sampleRate = 0; - wave.bitsPerSample = 0; - wave.channels = 0; + Sound sound = { 0 }; + Wave wave = { 0 }; // NOTE: The entire file is loaded to memory to play it all at once (no-streaming) @@ -236,7 +229,7 @@ Sound LoadSound(char *fileName) // Load sound from wave data Sound LoadSoundFromWave(Wave wave) { - Sound sound; + Sound sound = { 0 }; if (wave.data != NULL) { @@ -290,7 +283,7 @@ Sound LoadSoundFromWave(Wave wave) Sound LoadSoundFromRES(const char *rresName, int resId) { // NOTE: rresName could be directly a char array with all the data!!! --> TODO - Sound sound; + Sound sound = { 0 }; #if defined(AUDIO_STANDALONE) TraceLog(WARNING, "Sound loading from rRES resource file not supported on standalone mode"); @@ -791,7 +784,7 @@ static Wave LoadWAV(const char *fileName) WaveFormat waveFormat; WaveData waveData; - Wave wave; + Wave wave = { 0 }; FILE *wavFile; wavFile = fopen(fileName, "rb"); diff --git a/src/core.c b/src/core.c index 9b068300..7a5de04e 100644 --- a/src/core.c +++ b/src/core.c @@ -1230,7 +1230,9 @@ Vector2 GetTouchPosition(void) return position; } +#endif +#if defined(PLATFORM_ANDROID) // Detect if a button has been pressed once bool IsButtonPressed(int button) { diff --git a/src/models.c b/src/models.c index 80d9a13a..e90f455a 100644 --- a/src/models.c +++ b/src/models.c @@ -557,7 +557,7 @@ void DrawGizmo(Vector3 position) // Load a 3d model (from file) Model LoadModel(const char *fileName) { - Model model; + Model model = { 0 }; Mesh mesh = { 0 }; // NOTE: Initialize default data for model in case loading fails, maybe a cube? diff --git a/src/raylib.h b/src/raylib.h index 49a320d4..73200556 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -18,7 +18,7 @@ * * Used external libs: * GLFW3 (www.glfw.org) for window/context management and input -* GLAD for OpenGL extensions loading (3.3 Core profile) +* GLAD for OpenGL extensions loading (3.3 Core profile, only PLATFORM_DESKTOP) * stb_image (Sean Barret) for images loading (JPEG, PNG, BMP, TGA, PSD, GIF, HDR, PIC) * stb_image_write (Sean Barret) for image writting (PNG) * stb_vorbis (Sean Barret) for ogg audio loading diff --git a/src/shapes.c b/src/shapes.c index a4761536..3b4be071 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -397,8 +397,8 @@ bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec) int recCenterX = rec.x + rec.width/2; int recCenterY = rec.y + rec.height/2; - float dx = abs(center.x - recCenterX); - float dy = abs(center.y - recCenterY); + float dx = fabs(center.x - recCenterX); + float dy = fabs(center.y - recCenterY); if (dx > (rec.width/2 + radius)) { return false; } if (dy > (rec.height/2 + radius)) { return false; } -- cgit v1.2.3 From 3113a20390a1e4d81e9f832e7aa1d022afdb56d1 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 25 Jan 2016 11:12:31 +0100 Subject: Added bounding box calculation --- src/models.c | 29 ++++++++++++++++++----------- src/raylib.h | 2 ++ src/raymath.h | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 11 deletions(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index e90f455a..2d78963e 100644 --- a/src/models.c +++ b/src/models.c @@ -1275,6 +1275,20 @@ void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vec rlDisableTexture(); } +// Draw a bounding box with wires +void DrawBoundingBox(BoundingBox box) +{ + Vector3 size; + + size.x = fabsf(box.max.x - box.min.x); + size.y = fabsf(box.max.y - box.min.y); + size.z = fabsf(box.max.z - box.min.z); + + Vector3 center = { box.min.x + size.x/2.0f, box.min.y + size.y/2.0f, box.min.z + size.z/2.0f }; + + DrawCubeWires(center, size.x, size.y, size.z, GREEN); +} + // Detect collision between two spheres bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB) { @@ -1401,10 +1415,8 @@ bool CheckCollisionRayBox(Ray ray, Vector3 minBBox, Vector3 maxBBox) return collision; } -// TODO: Useful function to check collision area? -//BoundingBox GetCollisionArea(BoundingBox box1, BoundingBox box2) - // Calculate mesh bounding box limits +// NOTE: minVertex and maxVertex should be transformed by model transform matrix (position, scale, rotate) BoundingBox CalculateBoundingBox(Mesh mesh) { // Get min and max vertex to construct bounds (AABB) @@ -1413,15 +1425,10 @@ BoundingBox CalculateBoundingBox(Mesh mesh) for (int i = 1; i < mesh.vertexCount; i++) { - // TODO: Compare min and max with previous vertex - //minVertex = Vector3.Min(minVertex, mesh.vertices[i]); - //maxVertex = Vector3.Max(maxVertex, mesh.vertices[i]); + minVertex = VectorMin(minVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] }); + maxVertex = VectorMax(maxVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] }); } - - // NOTE: For OBB, transform mesh by model transform matrix - //minVertex = VectorTransform(meshMin, mesh.transform); - //maxVertex = VectorTransform(meshMax, mesh.transform); - + // Create the bounding box BoundingBox box; box.min = minVertex; diff --git a/src/raylib.h b/src/raylib.h index 41fa3f7d..097c8865 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -765,10 +765,12 @@ void DrawModel(Model model, Vector3 position, float scale, Color tint); void DrawModelEx(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color tint); // Draw a model with extended parameters void DrawModelWires(Model model, Vector3 position, float scale, Color color); // Draw a model wires (with texture if set) void DrawModelWiresEx(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color tint); // Draw a model wires (with texture if set) with extended parameters +void DrawBoundingBox(BoundingBox box); // Draw bounding box (wires) void DrawBillboard(Camera camera, Texture2D texture, Vector3 center, float size, Color tint); // Draw a billboard texture void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vector3 center, float size, Color tint); // Draw a billboard texture defined by sourceRec +BoundingBox CalculateBoundingBox(Mesh mesh); // Calculate mesh bounding box limits bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB); // Detect collision between two spheres bool CheckCollisionBoxes(Vector3 minBBox1, Vector3 maxBBox1, Vector3 minBBox2, Vector3 maxBBox2); // Detect collision between two boxes bool CheckCollisionBoxSphere(Vector3 minBBox, Vector3 maxBBox, Vector3 centerSphere, float radiusSphere); // Detect collision between box and sphere diff --git a/src/raymath.h b/src/raymath.h index f5448504..46fab356 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -126,6 +126,8 @@ RMDEF Vector3 VectorLerp(Vector3 v1, Vector3 v2, float amount); // Calculate lin RMDEF Vector3 VectorReflect(Vector3 vector, Vector3 normal); // Calculate reflected vector to normal RMDEF void VectorTransform(Vector3 *v, Matrix mat); // Transforms a Vector3 by a given Matrix RMDEF Vector3 VectorZero(void); // Return a Vector3 init to zero +RMDEF Vector3 VectorMin(Vector3 vec1, Vector3 vec2); // Return min value for each pair of components +RMDEF Vector3 VectorMax(Vector3 vec1, Vector3 vec2); // Return max value for each pair of components //------------------------------------------------------------------------------------ // Functions Declaration to work with Matrix @@ -361,6 +363,30 @@ RMDEF Vector3 VectorZero(void) return zero; } +// Return min value for each pair of components +RMDEF Vector3 VectorMin(Vector3 vec1, Vector3 vec2) +{ + Vector3 result; + + result.x = fminf(vec1.x, vec2.x); + result.y = fminf(vec1.y, vec2.y); + result.z = fminf(vec1.z, vec2.z); + + return result; +} + +// Return max value for each pair of components +RMDEF Vector3 VectorMax(Vector3 vec1, Vector3 vec2) +{ + Vector3 result; + + result.x = fmaxf(vec1.x, vec2.x); + result.y = fmaxf(vec1.y, vec2.y); + result.z = fmaxf(vec1.z, vec2.z); + + return result; +} + //---------------------------------------------------------------------------------- // Module Functions Definition - Matrix math //---------------------------------------------------------------------------------- -- cgit v1.2.3 From df5c64d0beee06df8c87a43e5341b6b98f82839f Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 2 Feb 2016 18:41:01 +0100 Subject: Functions parameters reorganize: Axis and Angle sin(), cos() functions cached and replaced by float c99 versions sinf(), cos() --- src/models.c | 12 ++++++------ src/raylib.h | 4 ++-- src/raymath.h | 47 +++++++++++++++++++++++++---------------------- src/rlgl.c | 8 ++++---- src/rlgl.h | 2 +- 5 files changed, 38 insertions(+), 35 deletions(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index 2d78963e..91cb5813 100644 --- a/src/models.c +++ b/src/models.c @@ -1149,14 +1149,14 @@ void DrawModel(Model model, Vector3 position, float scale, Color tint) Vector3 vScale = { scale, scale, scale }; Vector3 rotationAxis = { 0.0f, 0.0f, 0.0f }; - DrawModelEx(model, position, 0.0f, rotationAxis, vScale, tint); + DrawModelEx(model, position, rotationAxis, 0.0f, vScale, tint); } // Draw a model with extended parameters -void DrawModelEx(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color tint) +void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint) { // NOTE: Rotation must be provided in degrees, it's converted to radians inside rlglDrawModel() - rlglDrawModel(model, position, rotationAngle, rotationAxis, scale, tint, false); + rlglDrawModel(model, position, rotationAxis, rotationAngle, scale, tint, false); } // Draw a model wires (with texture if set) @@ -1165,14 +1165,14 @@ void DrawModelWires(Model model, Vector3 position, float scale, Color color) Vector3 vScale = { scale, scale, scale }; Vector3 rotationAxis = { 0.0f, 0.0f, 0.0f }; - rlglDrawModel(model, position, 0.0f, rotationAxis, vScale, color, true); + rlglDrawModel(model, position, rotationAxis, 0.0f, vScale, color, true); } // Draw a model wires (with texture if set) with extended parameters -void DrawModelWiresEx(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color tint) +void DrawModelWiresEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint) { // NOTE: Rotation must be provided in degrees, it's converted to radians inside rlglDrawModel() - rlglDrawModel(model, position, rotationAngle, rotationAxis, scale, tint, true); + rlglDrawModel(model, position, rotationAxis, rotationAngle, scale, tint, true); } // Draw a billboard diff --git a/src/raylib.h b/src/raylib.h index a22c3f83..c306518d 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -764,9 +764,9 @@ void UnloadModel(Model model); void SetModelTexture(Model *model, Texture2D texture); // Link a texture to a model void DrawModel(Model model, Vector3 position, float scale, Color tint); // Draw a model (with texture if set) -void DrawModelEx(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color tint); // Draw a model with extended parameters +void DrawModelEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint); // Draw a model with extended parameters void DrawModelWires(Model model, Vector3 position, float scale, Color color); // Draw a model wires (with texture if set) -void DrawModelWiresEx(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color tint); // Draw a model wires (with texture if set) with extended parameters +void DrawModelWiresEx(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint); // Draw a model wires (with texture if set) with extended parameters void DrawBoundingBox(BoundingBox box); // Draw bounding box (wires) void DrawBillboard(Camera camera, Texture2D texture, Vector3 center, float size, Color tint); // Draw a billboard texture diff --git a/src/raymath.h b/src/raymath.h index 46fab356..35cee39f 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -141,7 +141,7 @@ RMDEF Matrix MatrixIdentity(void); // Returns identit RMDEF Matrix MatrixAdd(Matrix left, Matrix right); // Add two matrices RMDEF Matrix MatrixSubstract(Matrix left, Matrix right); // Substract two matrices (left - right) RMDEF Matrix MatrixTranslate(float x, float y, float z); // Returns translation matrix -RMDEF Matrix MatrixRotate(float angle, Vector3 axis); // Returns rotation matrix for an angle around an specified axis (angle in radians) +RMDEF Matrix MatrixRotate(Vector3 axis, float angle); // Returns rotation matrix for an angle around an specified axis (angle in radians) RMDEF Matrix MatrixRotateX(float angle); // Returns x-rotation matrix (angle in radians) RMDEF Matrix MatrixRotateY(float angle); // Returns y-rotation matrix (angle in radians) RMDEF Matrix MatrixRotateZ(float angle); // Returns z-rotation matrix (angle in radians) @@ -162,8 +162,8 @@ RMDEF Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2); // Calcula RMDEF Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float slerp); // Calculates spherical linear interpolation between two quaternions RMDEF Quaternion QuaternionFromMatrix(Matrix matrix); // Returns a quaternion for a given rotation matrix RMDEF Matrix QuaternionToMatrix(Quaternion q); // Returns a matrix for a given quaternion -RMDEF Quaternion QuaternionFromAxisAngle(float angle, Vector3 axis); // Returns rotation quaternion for an angle and axis -RMDEF void QuaternionToAxisAngle(Quaternion q, float *outAngle, Vector3 *outAxis); // Returns the rotation angle and axis for a given quaternion +RMDEF Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle); // Returns rotation quaternion for an angle and axis +RMDEF void QuaternionToAxisAngle(Quaternion q, Vector3 *outAxis, float *outAngle); // Returns the rotation angle and axis for a given quaternion RMDEF void QuaternionTransform(Quaternion *q, Matrix mat); // Transform a quaternion given a transformation matrix #ifdef __cplusplus @@ -587,7 +587,7 @@ RMDEF Matrix MatrixTranslate(float x, float y, float z) // Create rotation matrix from axis and angle // NOTE: Angle should be provided in radians -RMDEF Matrix MatrixRotate(float angle, Vector3 axis) +RMDEF Matrix MatrixRotate(Vector3 axis, float angle) { Matrix result; @@ -605,9 +605,9 @@ RMDEF Matrix MatrixRotate(float angle, Vector3 axis) z *= length; } - float s = sinf(angle); - float c = cosf(angle); - float t = 1.0f - c; + float sinres = sinf(angle); + float cosres = cosf(angle); + float t = 1.0f - cosres; // Cache some matrix values (speed optimization) float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3; @@ -615,9 +615,9 @@ RMDEF Matrix MatrixRotate(float angle, Vector3 axis) float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11; // Construct the elements of the rotation matrix - float b00 = x*x*t + c, b01 = y*x*t + z*s, b02 = z*x*t - y*s; - float b10 = x*y*t - z*s, b11 = y*y*t + c, b12 = z*y*t + x*s; - float b20 = x*z*t + y*s, b21 = y*z*t - x*s, b22 = z*z*t + c; + float b00 = x*x*t + cosres, b01 = y*x*t + z*sinres, b02 = z*x*t - y*sinres; + float b10 = x*y*t - z*sinres, b11 = y*y*t + cosres, b12 = z*y*t + x*sinres; + float b20 = x*z*t + y*sinres, b21 = y*z*t - x*sinres, b22 = z*z*t + cosres; // Perform rotation-specific matrix multiplication result.m0 = a00*b00 + a10*b01 + a20*b02; @@ -688,8 +688,8 @@ RMDEF Matrix MatrixRotateX(float angle) { Matrix result = MatrixIdentity(); - float cosres = (float)cos(angle); - float sinres = (float)sin(angle); + float cosres = cosf(angle); + float sinres = sinf(angle); result.m5 = cosres; result.m6 = -sinres; @@ -720,8 +720,8 @@ RMDEF Matrix MatrixRotateZ(float angle) { Matrix result = MatrixIdentity(); - float cosres = (float)cos(angle); - float sinres = (float)sin(angle); + float cosres = cosf(angle); + float sinres = sinf(angle); result.m0 = cosres; result.m1 = -sinres; @@ -946,8 +946,8 @@ RMDEF Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount) } else { - float ratioA = sin((1 - amount)*halfTheta)/sinHalfTheta; - float ratioB = sin(amount*halfTheta)/sinHalfTheta; + float ratioA = sinf((1 - amount)*halfTheta)/sinHalfTheta; + float ratioB = sinf(amount*halfTheta)/sinHalfTheta; result.x = (q1.x*ratioA + q2.x*ratioB); result.y = (q1.y*ratioA + q2.y*ratioB); @@ -1060,7 +1060,7 @@ RMDEF Matrix QuaternionToMatrix(Quaternion q) // Returns rotation quaternion for an angle and axis // NOTE: angle must be provided in radians -RMDEF Quaternion QuaternionFromAxisAngle(float angle, Vector3 axis) +RMDEF Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle) { Quaternion result = { 0.0f, 0.0f, 0.0f, 1.0f }; @@ -1069,11 +1069,14 @@ RMDEF Quaternion QuaternionFromAxisAngle(float angle, Vector3 axis) angle *= 0.5f; VectorNormalize(&axis); + + float sinres = sinf(angle); + float cosres = cosf(angle); - result.x = axis.x*(float)sin(angle); - result.y = axis.y*(float)sin(angle); - result.z = axis.z*(float)sin(angle); - result.w = (float)cos(angle); + result.x = axis.x*sinres; + result.y = axis.y*sinres; + result.z = axis.z*sinres; + result.w = cosres; QuaternionNormalize(&result); @@ -1081,7 +1084,7 @@ RMDEF Quaternion QuaternionFromAxisAngle(float angle, Vector3 axis) } // Returns the rotation angle and axis for a given quaternion -RMDEF void QuaternionToAxisAngle(Quaternion q, float *outAngle, Vector3 *outAxis) +RMDEF void QuaternionToAxisAngle(Quaternion q, Vector3 *outAxis, float *outAngle) { if (fabs(q.w) > 1.0f) QuaternionNormalize(&q); diff --git a/src/rlgl.c b/src/rlgl.c index 49300054..48e6ac1b 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -411,7 +411,7 @@ void rlRotatef(float angleDeg, float x, float y, float z) Vector3 axis = (Vector3){ x, y, z }; VectorNormalize(&axis); - matRotation = MatrixRotate(angleDeg*DEG2RAD, axis); + matRotation = MatrixRotate(axis, angleDeg*DEG2RAD); MatrixTranspose(&matRotation); @@ -1406,13 +1406,13 @@ void rlglDrawPostpro(void) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glBindFramebuffer(GL_FRAMEBUFFER, 0); - rlglDrawModel(postproQuad, (Vector3){0,0,0}, 0.0f, (Vector3){0,0,0}, (Vector3){1.0f, 1.0f, 1.0f}, (Color){ 255, 255, 255, 255 }, false); + rlglDrawModel(postproQuad, (Vector3){0,0,0}, (Vector3){0,0,0}, 0.0f, (Vector3){1.0f, 1.0f, 1.0f}, (Color){ 255, 255, 255, 255 }, false); #endif } // Draw a 3d model // NOTE: Model transform can come within model struct -void rlglDrawModel(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color color, bool wires) +void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color color, bool wires) { #if defined (GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) // NOTE: glPolygonMode() not available on OpenGL ES @@ -1461,7 +1461,7 @@ void rlglDrawModel(Model model, Vector3 position, float rotationAngle, Vector3 r // Calculate transformation matrix from function parameters // Get transform matrix (rotation -> scale -> translation) - Matrix matRotation = MatrixRotate(rotationAngle*DEG2RAD, rotationAxis); + Matrix matRotation = MatrixRotate(rotationAxis, rotationAngle*DEG2RAD); Matrix matScale = MatrixScale(scale.x, scale.y, scale.z); Matrix matTranslation = MatrixTranslate(position.x, position.y, position.z); Matrix matTransform = MatrixMultiply(MatrixMultiply(matRotation, matScale), matTranslation); diff --git a/src/rlgl.h b/src/rlgl.h index 64581db8..9e0aaaaa 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -262,7 +262,7 @@ void rlglInitPostpro(void); // Initialize postprocessing sys void rlglDrawPostpro(void); // Draw with postprocessing shader Model rlglLoadModel(Mesh mesh); // Upload vertex data into GPU and provided VAO/VBO ids -void rlglDrawModel(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color color, bool wires); +void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color color, bool wires); Vector3 rlglUnproject(Vector3 source, Matrix proj, Matrix view); // Get world coordinates from screen coordinates -- cgit v1.2.3 From 685273675bc9247e215c213939c017e506296a70 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 11 Feb 2016 15:51:04 +0100 Subject: Improved LoadHeightmap() --- examples/models_heightmap.c | 16 ++++++----- examples/models_heightmap.png | Bin 123602 -> 96976 bytes src/models.c | 60 +++++++++++++++++++----------------------- src/raylib.h | 2 +- 4 files changed, 37 insertions(+), 41 deletions(-) (limited to 'src/models.c') diff --git a/examples/models_heightmap.c b/examples/models_heightmap.c index ac578c61..f1da3301 100644 --- a/examples/models_heightmap.c +++ b/examples/models_heightmap.c @@ -21,13 +21,13 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [models] example - heightmap loading and drawing"); // Define our custom camera to look into our 3d world - Camera camera = {{ 24.0f, 18.0f, 24.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }}; + Camera camera = {{ 18.0f, 16.0f, 18.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }}; - Image image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM) - Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM) - Model map = LoadHeightmap(image, 32); // Load heightmap model - SetModelTexture(&map, texture); // Bind texture to model - Vector3 mapPosition = { -16.0f, 0.0f, -16.0f }; // Set model position (depends on model scaling!) + Image image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM) + Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM) + Model map = LoadHeightmap(image, (Vector3){ 16, 8, 16 }); // Load heightmap model with defined size + SetModelTexture(&map, texture); // Bind texture to model + Vector3 mapPosition = { -8.0f, 0.0f, -8.0f }; // Set model position (depends on model scaling!) UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM @@ -54,7 +54,9 @@ int main() Begin3dMode(camera); // NOTE: Model is scaled to 1/4 of its original size (128x128 units) - DrawModel(map, mapPosition, 1/4.0f, RED); + DrawModel(map, mapPosition, 1.0f, RED); + + DrawGrid(20, 1.0f); End3dMode(); diff --git a/examples/models_heightmap.png b/examples/models_heightmap.png index 9ed04586..6dcf01f0 100644 Binary files a/examples/models_heightmap.png and b/examples/models_heightmap.png differ diff --git a/src/models.c b/src/models.c index 91cb5813..94e61d84 100644 --- a/src/models.c +++ b/src/models.c @@ -55,7 +55,6 @@ extern unsigned int whiteTexture; //---------------------------------------------------------------------------------- // Module specific Functions Declaration //---------------------------------------------------------------------------------- -static float GetHeightValue(Color pixel); static Mesh LoadOBJ(const char *fileName); //---------------------------------------------------------------------------------- @@ -608,17 +607,19 @@ Model LoadModelEx(Mesh data) } // Load a heightmap image as a 3d model -Model LoadHeightmap(Image heightmap, float maxHeight) +// NOTE: model map size is defined in generic units +Model LoadHeightmap(Image heightmap, Vector3 size) { + #define GRAY_VALUE(c) ((c.r+c.g+c.b)/3) + Mesh mesh; int mapX = heightmap.width; int mapZ = heightmap.height; - Color *heightmapPixels = GetImageData(heightmap); + Color *pixels = GetImageData(heightmap); // NOTE: One vertex per pixel - // TODO: Consider resolution when generating model data? int numTriangles = (mapX-1)*(mapZ-1)*2; // One quad every four pixels mesh.vertexCount = numTriangles*3; @@ -634,7 +635,7 @@ Model LoadHeightmap(Image heightmap, float maxHeight) int trisCounter = 0; - float scaleFactor = maxHeight/255; // TODO: Review scaleFactor calculation + Vector3 scaleFactor = { size.x/mapX, size.y/255.0f, size.z/mapZ }; for(int z = 0; z < mapZ-1; z++) { @@ -644,17 +645,17 @@ Model LoadHeightmap(Image heightmap, float maxHeight) //---------------------------------------------------------- // one triangle - 3 vertex - mesh.vertices[vCounter] = x; - mesh.vertices[vCounter + 1] = GetHeightValue(heightmapPixels[x + z*mapX])*scaleFactor; - mesh.vertices[vCounter + 2] = z; + mesh.vertices[vCounter] = (float)x*scaleFactor.x; + mesh.vertices[vCounter + 1] = (float)GRAY_VALUE(pixels[x + z*mapX])*scaleFactor.y; + mesh.vertices[vCounter + 2] = (float)z*scaleFactor.z; - mesh.vertices[vCounter + 3] = x; - mesh.vertices[vCounter + 4] = GetHeightValue(heightmapPixels[x + (z+1)*mapX])*scaleFactor; - mesh.vertices[vCounter + 5] = z+1; + mesh.vertices[vCounter + 3] = (float)x*scaleFactor.x; + mesh.vertices[vCounter + 4] = (float)GRAY_VALUE(pixels[x + (z + 1)*mapX])*scaleFactor.y; + mesh.vertices[vCounter + 5] = (float)(z + 1)*scaleFactor.z; - mesh.vertices[vCounter + 6] = x+1; - mesh.vertices[vCounter + 7] = GetHeightValue(heightmapPixels[(x+1) + z*mapX])*scaleFactor; - mesh.vertices[vCounter + 8] = z; + mesh.vertices[vCounter + 6] = (float)(x + 1)*scaleFactor.x; + mesh.vertices[vCounter + 7] = (float)GRAY_VALUE(pixels[(x + 1) + z*mapX])*scaleFactor.y; + mesh.vertices[vCounter + 8] = (float)z*scaleFactor.z; // another triangle - 3 vertex mesh.vertices[vCounter + 9] = mesh.vertices[vCounter + 6]; @@ -665,21 +666,21 @@ Model LoadHeightmap(Image heightmap, float maxHeight) mesh.vertices[vCounter + 13] = mesh.vertices[vCounter + 4]; mesh.vertices[vCounter + 14] = mesh.vertices[vCounter + 5]; - mesh.vertices[vCounter + 15] = x+1; - mesh.vertices[vCounter + 16] = GetHeightValue(heightmapPixels[(x+1) + (z+1)*mapX])*scaleFactor; - mesh.vertices[vCounter + 17] = z+1; + mesh.vertices[vCounter + 15] = (float)(x + 1)*scaleFactor.x; + mesh.vertices[vCounter + 16] = (float)GRAY_VALUE(pixels[(x + 1) + (z + 1)*mapX])*scaleFactor.y; + mesh.vertices[vCounter + 17] = (float)(z + 1)*scaleFactor.z; vCounter += 18; // 6 vertex, 18 floats // Fill texcoords array with data //-------------------------------------------------------------- - mesh.texcoords[tcCounter] = (float)x / (mapX-1); - mesh.texcoords[tcCounter + 1] = (float)z / (mapZ-1); + mesh.texcoords[tcCounter] = (float)x/(mapX - 1); + mesh.texcoords[tcCounter + 1] = (float)z/(mapZ - 1); - mesh.texcoords[tcCounter + 2] = (float)x / (mapX-1); - mesh.texcoords[tcCounter + 3] = (float)(z+1) / (mapZ-1); + mesh.texcoords[tcCounter + 2] = (float)x/(mapX - 1); + mesh.texcoords[tcCounter + 3] = (float)(z + 1)/(mapZ - 1); - mesh.texcoords[tcCounter + 4] = (float)(x+1) / (mapX-1); - mesh.texcoords[tcCounter + 5] = (float)z / (mapZ-1); + mesh.texcoords[tcCounter + 4] = (float)(x + 1)/(mapX - 1); + mesh.texcoords[tcCounter + 5] = (float)z/(mapZ - 1); mesh.texcoords[tcCounter + 6] = mesh.texcoords[tcCounter + 4]; mesh.texcoords[tcCounter + 7] = mesh.texcoords[tcCounter + 5]; @@ -687,13 +688,12 @@ Model LoadHeightmap(Image heightmap, float maxHeight) mesh.texcoords[tcCounter + 8] = mesh.texcoords[tcCounter + 2]; mesh.texcoords[tcCounter + 9] = mesh.texcoords[tcCounter + 3]; - mesh.texcoords[tcCounter + 10] = (float)(x+1) / (mapX-1); - mesh.texcoords[tcCounter + 11] = (float)(z+1) / (mapZ-1); + mesh.texcoords[tcCounter + 10] = (float)(x + 1)/(mapX - 1); + mesh.texcoords[tcCounter + 11] = (float)(z + 1)/(mapZ - 1); tcCounter += 12; // 6 texcoords, 12 floats // Fill normals array with data //-------------------------------------------------------------- - // NOTE: Current Model implementation doe not use normals! for (int i = 0; i < 18; i += 3) { mesh.normals[nCounter + i] = 0.0f; @@ -709,7 +709,7 @@ Model LoadHeightmap(Image heightmap, float maxHeight) } } - free(heightmapPixels); + free(pixels); // Fill color data // NOTE: Not used any more... just one plain color defined at DrawModel() @@ -1688,12 +1688,6 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p // Module specific Functions Definition //---------------------------------------------------------------------------------- -// Get current vertex y altitude (proportional to pixel colors in grayscale) -static float GetHeightValue(Color pixel) -{ - return (((float)pixel.r + (float)pixel.g + (float)pixel.b)/3); -} - // Load OBJ mesh data static Mesh LoadOBJ(const char *fileName) { diff --git a/src/raylib.h b/src/raylib.h index a5dd6ad2..f9241533 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -759,7 +759,7 @@ void DrawGizmo(Vector3 position); Model LoadModel(const char *fileName); // Load a 3d model (.OBJ) Model LoadModelEx(Mesh data); // Load a 3d model (from vertex data) //Model LoadModelFromRES(const char *rresName, int resId); // TODO: Load a 3d model from rRES file (raylib Resource) -Model LoadHeightmap(Image heightmap, float maxHeight); // Load a heightmap image as a 3d model +Model LoadHeightmap(Image heightmap, Vector3 size); // Load a heightmap image as a 3d model Model LoadCubicmap(Image cubicmap); // Load a map image as a 3d model (cubes based) void UnloadModel(Model model); // Unload 3d model from memory void SetModelTexture(Model *model, Texture2D texture); // Link a texture to a model -- cgit v1.2.3 From 823abf666e09e96ccbf5230c96f2d3a61ff60895 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 12 Feb 2016 12:22:56 +0100 Subject: Reviewed code TODOs --- src/audio.c | 2 +- src/camera.c | 2 +- src/core.c | 17 +++++++---------- src/models.c | 1 - src/rlgl.c | 10 +++------- src/text.c | 2 +- src/textures.c | 10 ++++++---- 7 files changed, 19 insertions(+), 25 deletions(-) (limited to 'src/models.c') diff --git a/src/audio.c b/src/audio.c index e40fdd41..260f6778 100644 --- a/src/audio.c +++ b/src/audio.c @@ -280,9 +280,9 @@ Sound LoadSoundFromWave(Wave wave) } // Load sound to memory from rRES file (raylib Resource) +// TODO: Maybe rresName could be directly a char array with all the data? Sound LoadSoundFromRES(const char *rresName, int resId) { - // NOTE: rresName could be directly a char array with all the data!!! --> TODO Sound sound = { 0 }; #if defined(AUDIO_STANDALONE) diff --git a/src/camera.c b/src/camera.c index 6539da5f..517e4a2b 100644 --- a/src/camera.c +++ b/src/camera.c @@ -375,7 +375,7 @@ static void ProcessCamera(Camera *camera, Vector3 *playerPosition) } // Focus to center - // TODO: Move this function out of the module? + // TODO: Move this function out of this module? if (IsKeyDown('Z')) camera->target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera position update diff --git a/src/core.c b/src/core.c index 05ec0c0a..413006e4 100644 --- a/src/core.c +++ b/src/core.c @@ -359,7 +359,7 @@ void InitWindow(int width, int height, struct android_app *state) if (orientation == ACONFIGURATION_ORIENTATION_PORT) TraceLog(INFO, "PORTRAIT window orientation"); else if (orientation == ACONFIGURATION_ORIENTATION_LAND) TraceLog(INFO, "LANDSCAPE window orientation"); - // TODO: Review, it doesn't work... + // TODO: Automatic orientation doesn't seem to work if (width <= height) { AConfiguration_setOrientation(app->config, ACONFIGURATION_ORIENTATION_PORT); @@ -1246,7 +1246,7 @@ int GetTouchY(void) } // Returns touch position XY -// TODO: touch position should be scaled depending on display size and render size +// TODO: Touch position should be scaled depending on display size and render size Vector2 GetTouchPosition(int index) { Vector2 position = { -1.0f, -1.0f }; @@ -1257,7 +1257,7 @@ Vector2 GetTouchPosition(int index) if ((screenWidth > displayWidth) || (screenHeight > displayHeight)) { - // TODO: Seems to work ok but... review! + // TODO: Review touch position scaling for screenSize vs displaySize position.x = position.x*((float)screenWidth/(float)(displayWidth - renderOffsetX)) - renderOffsetX/2; position.y = position.y*((float)screenHeight/(float)(displayHeight - renderOffsetY)) - renderOffsetY/2; } @@ -1668,8 +1668,7 @@ static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, i #endif else currentKeyState[key] = action; - // HACK for GuiTextBox, to deteck back key - // TODO: Review... + // TODO: Review (and remove) this HACK for GuiTextBox, to deteck back key if ((key == 259) && (action == GLFW_PRESS)) lastKeyPressed = 3; } @@ -1678,8 +1677,6 @@ static void MouseButtonCallback(GLFWwindow *window, int button, int action, int { currentMouseState[button] = action; - // TODO: Test mouse gestures - #define ENABLE_MOUSE_GESTURES #if defined(ENABLE_MOUSE_GESTURES) // Process mouse events as touches to be able to use mouse-gestures @@ -2046,7 +2043,7 @@ static bool GetKeyStatus(int key) #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) return glfwGetKey(window, key); #elif defined(PLATFORM_ANDROID) - // TODO: Check virtual keyboard (?) + // TODO: Check for virtual keyboard return false; #elif defined(PLATFORM_RPI) // NOTE: Keys states are filled in PollInputEvents() @@ -2061,7 +2058,7 @@ static bool GetMouseButtonStatus(int button) #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) return glfwGetMouseButton(window, button); #elif defined(PLATFORM_ANDROID) - // TODO: Check virtual keyboard (?) + // TODO: Check for virtual keyboard return false; #elif defined(PLATFORM_RPI) // NOTE: mouse buttons array is filled on PollInputEvents() @@ -2382,7 +2379,7 @@ static void RestoreKeyboard(void) // Init gamepad system static void InitGamepad(void) { - // TODO: Gamepad support + // TODO: Add Gamepad support if ((gamepadStream = open(DEFAULT_GAMEPAD_DEV, O_RDONLY|O_NONBLOCK)) < 0) TraceLog(WARNING, "Gamepad device could not be opened, no gamepad available"); else TraceLog(INFO, "Gamepad device initialized successfully"); } diff --git a/src/models.c b/src/models.c index 94e61d84..8a36c279 100644 --- a/src/models.c +++ b/src/models.c @@ -704,7 +704,6 @@ Model LoadHeightmap(Image heightmap, Vector3 size) // TODO: Calculate normals in an efficient way nCounter += 18; // 6 vertex, 18 floats - trisCounter += 2; } } diff --git a/src/rlgl.c b/src/rlgl.c index 2ea06e1c..5f8a5ea1 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -171,7 +171,7 @@ typedef struct { typedef struct { GLuint textureId; int vertexCount; - // TODO: DrawState state -> Blending mode, shader + // TODO: Store draw state -> blending mode, shader } DrawCall; // pixel type (same as Color type) @@ -1475,11 +1475,7 @@ void rlglDrawModel(Model model, Vector3 position, Vector3 rotationAxis, float ro // Calculate model-view-projection matrix (MVP) Matrix matMVP = MatrixMultiply(matModelView, matProjection); // Transform to screen-space coordinates - // NOTE: Drawing in OpenGL 3.3+, matrices are passed to shader - // TODO: Reduce number of matrices passed to shaders, use only matMVP - //glUniformMatrix4fv(model.material.shader.modelLoc, 1, false, MatrixToFloat(matModel)); - //glUniformMatrix4fv(model.material.shader.viewLoc, 1, false, MatrixToFloat(matView)); - + // Send combined model-view-projection matrix to shader glUniformMatrix4fv(model.shader.mvpLoc, 1, false, MatrixToFloat(matMVP)); // Apply color tinting to model @@ -1900,7 +1896,7 @@ void rlglGenerateMipmaps(Texture2D texture) int mipmapCount = GenerateMipmaps(data, texture.width, texture.height); // TODO: Adjust mipmap size depending on texture format! - int size = texture.width*texture.height*4; + int size = texture.width*texture.height*4; // RGBA 32bit only int offset = size; int mipWidth = texture.width/2; diff --git a/src/text.c b/src/text.c index 3755932d..e4c7bbf3 100644 --- a/src/text.c +++ b/src/text.c @@ -346,7 +346,7 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f for(int i = 0; i < length; i++) { - // TODO: Right now we are supposing characters follow a continous order and start at FONT_FIRST_CHAR, + // TODO: Right now we are supposing characters that follow a continous order and start at FONT_FIRST_CHAR, // this sytem can be improved to support any characters order and init value... // An intermediate table could be created to link char values with predefined char position index in chars rectangle array diff --git a/src/textures.c b/src/textures.c index f03d2d9a..36819daf 100644 --- a/src/textures.c +++ b/src/textures.c @@ -712,7 +712,7 @@ void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp) { oldpixel = pixels[y*image->width + x]; - // TODO: New pixel obtained by bits truncate, it would be better to round values (check ImageFormat()) + // NOTE: New pixel obtained by bits truncate, it would be better to round values (check ImageFormat()) newpixel.r = oldpixel.r>>(8 - rBpp); // R bits newpixel.g = oldpixel.g>>(8 - gBpp); // G bits newpixel.b = oldpixel.b>>(8 - bBpp); // B bits @@ -769,7 +769,7 @@ void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp) } // Convert image to POT (power-of-two) -// NOTE: Requirement on OpenGL ES 2.0 (RPI, HTML5) +// NOTE: It could be useful on OpenGL ES 2.0 (RPI, HTML5) void ImageToPOT(Image *image, Color fillColor) { Color *pixels = GetImageData(*image); // Get pixels data @@ -784,7 +784,7 @@ void ImageToPOT(Image *image, Color fillColor) Color *pixelsPOT = NULL; // Generate POT array from NPOT data - pixelsPOT = (Color *)malloc(potWidth * potHeight * sizeof(Color)); + pixelsPOT = (Color *)malloc(potWidth*potHeight*sizeof(Color)); for (int j = 0; j < potHeight; j++) { @@ -896,7 +896,9 @@ void ImageCrop(Image *image, Rectangle crop) } // Resize and image to new size -// NOTE: Uses stb default scaling filter +// NOTE: Uses stb default scaling filters (both bicubic): +// STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM +// STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL (high-quality Catmull-Rom) void ImageResize(Image *image, int newWidth, int newHeight) { // Get data as Color pixels array to work with it -- cgit v1.2.3