From 00c34a035c6cf32fb688dd06da39db32fa66bc70 Mon Sep 17 00:00:00 2001 From: Ray San Date: Wed, 20 Dec 2017 12:37:08 +0100 Subject: Updated copyright year --- src/models.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index 4b8a6731..002ffac3 100644 --- a/src/models.c +++ b/src/models.c @@ -17,7 +17,7 @@ * * LICENSE: zlib/libpng * -* Copyright (c) 2014-2017 Ramon Santamaria (@raysan5) +* Copyright (c) 2014-2018 Ramon Santamaria (@raysan5) * * This software is provided "as-is", without any express or implied warranty. In no event * will the authors be held liable for any damages arising from the use of this software. -- cgit v1.2.3 From e7cf03b1e4c1aa7872bda7b35a7d064815332759 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 28 Dec 2017 19:27:02 +0100 Subject: Minor tweaks --- src/models.c | 6 ++---- src/rlgl.c | 8 ++++---- src/textures.c | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index 002ffac3..1f37e345 100644 --- a/src/models.c +++ b/src/models.c @@ -45,9 +45,7 @@ #include "raylib.h" -#if defined(PLATFORM_ANDROID) - #include "utils.h" // Android fopen function map -#endif +#include "utils.h" // Required for: fopen() Android mapping #include // Required for: FILE, fopen(), fclose(), fscanf(), feof(), rewind(), fgets() #include // Required for: malloc(), free() @@ -57,7 +55,7 @@ #include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2 #define PAR_SHAPES_IMPLEMENTATION -#include "external/par_shapes.h" +#include "external/par_shapes.h" // Shapes 3d parametric generation //---------------------------------------------------------------------------------- // Defines and Macros diff --git a/src/rlgl.c b/src/rlgl.c index 58e9187d..4bbebf03 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1109,7 +1109,7 @@ void rlglInit(int width, int height) if (strcmp(extList[i], (const char *)"GL_OES_texture_npot") == 0) texNPOTSupported = true; // Check texture float support - if (strcmp(extList[i], (const char *)"OES_texture_float") == 0) texFloatSupported = true; + if (strcmp(extList[i], (const char *)"GL_OES_texture_float") == 0) texFloatSupported = true; #endif // DDS texture compression support @@ -1137,11 +1137,11 @@ void rlglInit(int width, int height) glGetFloatv(0x84FF, &maxAnisotropicLevel); // GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT } - // Debug marker support - if(strcmp(extList[i], (const char *)"GL_EXT_debug_marker") == 0) debugMarkerSupported = true; - // Clamp mirror wrap mode supported if (strcmp(extList[i], (const char *)"GL_EXT_texture_mirror_clamp") == 0) texClampMirrorSupported = true; + + // Debug marker support + if(strcmp(extList[i], (const char *)"GL_EXT_debug_marker") == 0) debugMarkerSupported = true; } #ifdef _MSC_VER diff --git a/src/textures.c b/src/textures.c index 34f2c323..fd2fae0d 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1681,7 +1681,7 @@ Image GenImageCellular(int width, int height, int tileSize) // Generate GPU mipmaps for a texture void GenTextureMipmaps(Texture2D *texture) { -#if PLATFORM_WEB +#if defined(PLATFORM_WEB) // Calculate next power-of-two values int potWidth = (int)powf(2, ceilf(logf((float)texture->width)/logf(2))); int potHeight = (int)powf(2, ceilf(logf((float)texture->height)/logf(2))); -- cgit v1.2.3 From 6d64327a874b09e93290d1525edd5347a9787b84 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 22 Feb 2018 12:39:17 +0100 Subject: Reviewed unloading model data When UnloadModel() --> UnloadMaterial(), avoid unloading default shader (if used) and avoid unlaoding default texture (if used), that data is managed by raylib internally. The question is... should UnloadModel() also UnloadMaterial()? --- src/models.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index 1f37e345..02e69c0c 100644 --- a/src/models.c +++ b/src/models.c @@ -1671,14 +1671,13 @@ Material LoadMaterialDefault(void) // Unload material from memory void UnloadMaterial(Material material) { - // Unload material shader - UnloadShader(material.shader); + // Unload material shader (avoid unloading default shader, managed by raylib) + if (material.shader.id != GetShaderDefault().id) UnloadShader(material.shader); - // Unload loaded texture maps + // Unload loaded texture maps (avoid unloading default texture, managed by raylib) for (int i = 0; i < MAX_MATERIAL_MAPS; i++) { - // NOTE: We already check for (tex.id > 0) inside function - rlDeleteTextures(material.maps[i].texture.id); + if (material.maps[i].texture.id != GetTextureDefault().id) rlDeleteTextures(material.maps[i].texture.id); } } -- cgit v1.2.3 From 077bef42861644f1f25070d23363a1bfa2e7a4a6 Mon Sep 17 00:00:00 2001 From: Ray Date: Sat, 24 Feb 2018 12:31:32 +0100 Subject: Support 4 components mesh.tangent data Added struct Vector4 for convenience --- src/models.c | 23 ++++++++++++++--------- src/raylib.h | 10 +++++++++- src/rlgl.c | 14 +++++++------- src/rlgl.h | 2 +- 4 files changed, 31 insertions(+), 18 deletions(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index 02e69c0c..f3a168c9 100644 --- a/src/models.c +++ b/src/models.c @@ -2329,12 +2329,12 @@ static Mesh LoadOBJ(const char *fileName) else { // Attempt to calculate mesh tangents and binormals using positions and texture coordinates - mesh.tangents = (float *)malloc(mesh.vertexCount*3*sizeof(float)); + mesh.tangents = (float *)malloc(mesh.vertexCount*4*sizeof(float)); // mesh.binormals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); int vCount = 0; int uvCount = 0; - while (vCount < mesh.vertexCount*3) + while (vCount < mesh.vertexCount*4) { // Calculate mesh vertex positions as Vector3 Vector3 v0 = { mesh.vertices[vCount], mesh.vertices[vCount + 1], mesh.vertices[vCount + 2] }; @@ -2363,17 +2363,22 @@ static Mesh LoadOBJ(const char *fileName) // Calculate vertex tangent Vector3 tangent = Vector3Subtract(t1, t2); Vector3Scale(&tangent, r); + + // TODO: Calculate tangent 4th component // Apply calculated tangents data to mesh struct mesh.tangents[vCount + 0] = tangent.x; mesh.tangents[vCount + 1] = tangent.y; mesh.tangents[vCount + 2] = tangent.z; - mesh.tangents[vCount + 3] = tangent.x; - mesh.tangents[vCount + 4] = tangent.y; - mesh.tangents[vCount + 5] = tangent.z; - mesh.tangents[vCount + 6] = tangent.x; - mesh.tangents[vCount + 7] = tangent.y; - mesh.tangents[vCount + 8] = tangent.z; + mesh.tangents[vCount + 3] = 0.0f; + mesh.tangents[vCount + 4] = tangent.x; + mesh.tangents[vCount + 5] = tangent.y; + mesh.tangents[vCount + 6] = tangent.z; + mesh.tangents[vCount + 7] = 0.0f; + mesh.tangents[vCount + 8] = tangent.x; + mesh.tangents[vCount + 9] = tangent.y; + mesh.tangents[vCount + 10] = tangent.z; + mesh.tangents[vCount + 11] = 0.0f; // TODO: add binormals to mesh struct and assign buffers id and locations properly /* // Calculate vertex binormal @@ -2392,7 +2397,7 @@ static Mesh LoadOBJ(const char *fileName) mesh.binormals[vCount + 8] = binormal.z; */ // Update vertex position and texture coordinates counters - vCount += 9; + vCount += 12; uvCount += 6; } } diff --git a/src/raylib.h b/src/raylib.h index 782dd4e5..41755dc3 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -322,6 +322,14 @@ typedef struct Vector3 { float z; } Vector3; +// Vector4 type +typedef struct Vector4 { + float x; + float y; + float z; + float w; +} Vector4; + // Matrix type (OpenGL style 4x4 - right handed, column major) typedef struct Matrix { float m0, m4, m8, m12; @@ -422,7 +430,7 @@ typedef struct Mesh { float *texcoords; // Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) float *texcoords2; // Vertex second texture coordinates (useful for lightmaps) (shader-location = 5) float *normals; // Vertex normals (XYZ - 3 components per vertex) (shader-location = 2) - float *tangents; // Vertex tangents (XYZ - 3 components per vertex) (shader-location = 4) + float *tangents; // Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4) unsigned char *colors; // Vertex colors (RGBA - 4 components per vertex) (shader-location = 3) unsigned short *indices;// Vertex indices (in case vertex data comes indexed) diff --git a/src/rlgl.c b/src/rlgl.c index 2cb9d54e..46e0668d 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1782,14 +1782,14 @@ void rlLoadMesh(Mesh *mesh, bool dynamic) { glGenBuffers(1, &mesh->vboId[4]); glBindBuffer(GL_ARRAY_BUFFER, mesh->vboId[4]); - glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->vertexCount, mesh->tangents, drawHint); - glVertexAttribPointer(4, 3, GL_FLOAT, 0, 0, 0); + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*mesh->vertexCount, mesh->tangents, drawHint); + glVertexAttribPointer(4, 4, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(4); } else { // Default tangents vertex attribute - glVertexAttrib3f(4, 0.0f, 0.0f, 0.0f); + glVertexAttrib4f(4, 0.0f, 0.0f, 0.0f, 0.0f); glDisableVertexAttribArray(4); } @@ -1804,7 +1804,7 @@ void rlLoadMesh(Mesh *mesh, bool dynamic) } else { - // Default tangents vertex attribute + // Default texcoord2 vertex attribute glVertexAttrib2f(5, 0.0f, 0.0f); glDisableVertexAttribArray(5); } @@ -1868,8 +1868,8 @@ void rlUpdateMesh(Mesh mesh, int buffer, int numVertex) case 4: // Update tangents (vertex tangents) { glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[4]); - if (numVertex >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*numVertex, mesh.tangents, GL_DYNAMIC_DRAW); - else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*3*numVertex, mesh.tangents); + if (numVertex >= mesh.vertexCount) glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*numVertex, mesh.tangents, GL_DYNAMIC_DRAW); + else glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*4*numVertex, mesh.tangents); } break; case 5: // Update texcoords2 (vertex second texture coordinates) { @@ -2019,7 +2019,7 @@ void rlDrawMesh(Mesh mesh, Material material, Matrix transform) if (material.shader.locs[LOC_VERTEX_TANGENT] != -1) { glBindBuffer(GL_ARRAY_BUFFER, mesh.vboId[4]); - glVertexAttribPointer(material.shader.locs[LOC_VERTEX_TANGENT], 3, GL_FLOAT, 0, 0, 0); + glVertexAttribPointer(material.shader.locs[LOC_VERTEX_TANGENT], 4, GL_FLOAT, 0, 0, 0); glEnableVertexAttribArray(material.shader.locs[LOC_VERTEX_TANGENT]); } diff --git a/src/rlgl.h b/src/rlgl.h index 68d8d4eb..94331389 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -187,7 +187,7 @@ typedef unsigned char byte; float *texcoords; // vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) float *texcoords2; // vertex second texture coordinates (useful for lightmaps) (shader-location = 5) float *normals; // vertex normals (XYZ - 3 components per vertex) (shader-location = 2) - float *tangents; // vertex tangents (XYZ - 3 components per vertex) (shader-location = 4) + float *tangents; // vertex tangents (XYZW - 4 components per vertex) (shader-location = 4) unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) (shader-location = 3) unsigned short *indices;// vertex indices (in case vertex data comes indexed) -- cgit v1.2.3 From a7207dc6d4bc4fa7deb21bb090d4d6692e86fdf3 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 26 Feb 2018 12:02:05 +0100 Subject: Removed tangents generation It has no sense to be inside LoadOBJ(), mesh processing moved to own functions: MeshTangents() and MeshBinormals(). Not exposed to user yet. --- src/models.c | 90 ++++++++---------------------------------------------------- 1 file changed, 12 insertions(+), 78 deletions(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index f3a168c9..63d227e3 100644 --- a/src/models.c +++ b/src/models.c @@ -2083,6 +2083,18 @@ BoundingBox CalculateBoundingBox(Mesh mesh) return box; } +// Compute provided mesh tangents +void MeshTangents(Mesh *mesh) +{ + // TODO: Calculate mesh tangents +} + +// Compute provided mesh binormals +void MeshBinormals(Mesh *mesh) +{ + // TODO: Calculate mesh binormals +} + //---------------------------------------------------------------------------------- // Module specific Functions Definition //---------------------------------------------------------------------------------- @@ -2324,84 +2336,6 @@ static Mesh LoadOBJ(const char *fileName) fclose(objFile); - // Security check, just in case no normals or no texcoords defined in OBJ - if (texcoordCount == 0) for (int i = 0; i < (2*mesh.vertexCount); i++) mesh.texcoords[i] = 0.0f; - else - { - // Attempt to calculate mesh tangents and binormals using positions and texture coordinates - mesh.tangents = (float *)malloc(mesh.vertexCount*4*sizeof(float)); - // mesh.binormals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); - - int vCount = 0; - int uvCount = 0; - while (vCount < mesh.vertexCount*4) - { - // Calculate mesh vertex positions as Vector3 - Vector3 v0 = { mesh.vertices[vCount], mesh.vertices[vCount + 1], mesh.vertices[vCount + 2] }; - Vector3 v1 = { mesh.vertices[vCount + 3], mesh.vertices[vCount + 4], mesh.vertices[vCount + 5] }; - Vector3 v2 = { mesh.vertices[vCount + 6], mesh.vertices[vCount + 7], mesh.vertices[vCount + 8] }; - - // Calculate mesh texture coordinates as Vector2 - Vector2 uv0 = { mesh.texcoords[uvCount + 0], mesh.texcoords[uvCount + 1] }; - Vector2 uv1 = { mesh.texcoords[uvCount + 2], mesh.texcoords[uvCount + 3] }; - Vector2 uv2 = { mesh.texcoords[uvCount + 4], mesh.texcoords[uvCount + 5] }; - - // Calculate edges of the triangle (position delta) - Vector3 deltaPos1 = Vector3Subtract(v1, v0); - Vector3 deltaPos2 = Vector3Subtract(v2, v0); - - // UV delta - Vector2 deltaUV1 = { uv1.x - uv0.x, uv1.y - uv0.y }; - Vector2 deltaUV2 = { uv2.x - uv0.x, uv2.y - uv0.y }; - - float r = 1.0f/(deltaUV1.x*deltaUV2.y - deltaUV1.y*deltaUV2.x); - Vector3 t1 = { deltaPos1.x*deltaUV2.y, deltaPos1.y*deltaUV2.y, deltaPos1.z*deltaUV2.y }; - Vector3 t2 = { deltaPos2.x*deltaUV1.y, deltaPos2.y*deltaUV1.y, deltaPos2.z*deltaUV1.y }; - // Vector3 b1 = { deltaPos2.x*deltaUV1.x, deltaPos2.y*deltaUV1.x, deltaPos2.z*deltaUV1.x }; - // Vector3 b2 = { deltaPos1.x*deltaUV2.x, deltaPos1.y*deltaUV2.x, deltaPos1.z*deltaUV2.x }; - - // Calculate vertex tangent - Vector3 tangent = Vector3Subtract(t1, t2); - Vector3Scale(&tangent, r); - - // TODO: Calculate tangent 4th component - - // Apply calculated tangents data to mesh struct - mesh.tangents[vCount + 0] = tangent.x; - mesh.tangents[vCount + 1] = tangent.y; - mesh.tangents[vCount + 2] = tangent.z; - mesh.tangents[vCount + 3] = 0.0f; - mesh.tangents[vCount + 4] = tangent.x; - mesh.tangents[vCount + 5] = tangent.y; - mesh.tangents[vCount + 6] = tangent.z; - mesh.tangents[vCount + 7] = 0.0f; - mesh.tangents[vCount + 8] = tangent.x; - mesh.tangents[vCount + 9] = tangent.y; - mesh.tangents[vCount + 10] = tangent.z; - mesh.tangents[vCount + 11] = 0.0f; - - // TODO: add binormals to mesh struct and assign buffers id and locations properly - /* // Calculate vertex binormal - Vector3 binormal = Vector3Subtract(b1, b2); - Vector3Scale(&binormal, r); - - // Apply calculated binormals data to mesh struct - mesh.binormals[vCount + 0] = binormal.x; - mesh.binormals[vCount + 1] = binormal.y; - mesh.binormals[vCount + 2] = binormal.z; - mesh.binormals[vCount + 3] = binormal.x; - mesh.binormals[vCount + 4] = binormal.y; - mesh.binormals[vCount + 5] = binormal.z; - mesh.binormals[vCount + 6] = binormal.x; - mesh.binormals[vCount + 7] = binormal.y; - mesh.binormals[vCount + 8] = binormal.z; */ - - // Update vertex position and texture coordinates counters - vCount += 12; - uvCount += 6; - } - } - // Now we can free temp mid* arrays free(midVertices); free(midNormals); -- cgit v1.2.3 From fd2adbe62ded6cb91ae7684331381afe0675df69 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 26 Feb 2018 12:10:45 +0100 Subject: Renamed CalculateBoundingBox() to MeshBoundingBox() Renamed function for consistency with a possible Mesh manipulation functions (maybe added in a future). Naming follows Image*() manipulation functions. --- examples/models/models_mesh_picking.c | 2 +- src/models.c | 12 ++++++------ src/raylib.h | 6 +++++- 3 files changed, 12 insertions(+), 8 deletions(-) (limited to 'src/models.c') diff --git a/examples/models/models_mesh_picking.c b/examples/models/models_mesh_picking.c index e150fe92..1cc38b45 100644 --- a/examples/models/models_mesh_picking.c +++ b/examples/models/models_mesh_picking.c @@ -38,7 +38,7 @@ int main() tower.material.maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position - BoundingBox towerBBox = CalculateBoundingBox(tower.mesh); + BoundingBox towerBBox = MeshBoundingBox(tower.mesh); // Get mesh bounding box bool hitMeshBBox = false; bool hitTriangle = false; diff --git a/src/models.c b/src/models.c index 63d227e3..8e9a6586 100644 --- a/src/models.c +++ b/src/models.c @@ -2055,9 +2055,9 @@ RayHitInfo GetCollisionRayGround(Ray ray, float groundHeight) return result; } -// Calculate mesh bounding box limits +// Compute mesh bounding box limits // NOTE: minVertex and maxVertex should be transformed by model transform matrix -BoundingBox CalculateBoundingBox(Mesh mesh) +BoundingBox MeshBoundingBox(Mesh mesh) { // Get min and max vertex to construct bounds (AABB) Vector3 minVertex = { 0 }; @@ -2083,16 +2083,16 @@ BoundingBox CalculateBoundingBox(Mesh mesh) return box; } -// Compute provided mesh tangents +// Compute mesh tangents void MeshTangents(Mesh *mesh) { - // TODO: Calculate mesh tangents + // TODO: Compute mesh tangents } -// Compute provided mesh binormals +// Compute mesh binormals void MeshBinormals(Mesh *mesh) { - // TODO: Calculate mesh binormals + // TODO: Compute mesh binormals } //---------------------------------------------------------------------------------- diff --git a/src/raylib.h b/src/raylib.h index 41755dc3..c1d383fd 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1006,6 +1006,11 @@ RLAPI void UnloadModel(Model model); RLAPI Mesh LoadMesh(const char *fileName); // Load mesh from file RLAPI void UnloadMesh(Mesh *mesh); // Unload mesh from memory (RAM and/or VRAM) +// Mesh manipulation functions +RLAPI BoundingBox MeshBoundingBox(Mesh mesh); // Compute mesh bounding box limits +RLAPI void MeshTangents(Mesh *mesh); // Compute mesh tangents +RLAPI void MeshBinormals(Mesh *mesh); // Compute mesh binormals + // Mesh generation functions RLAPI Mesh GenMeshPlane(float width, float length, int resX, int resZ); // Generate plane mesh (with subdivisions) RLAPI Mesh GenMeshCube(float width, float height, float length); // Generate cuboid mesh @@ -1035,7 +1040,6 @@ RLAPI void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRe Vector3 center, float size, Color tint); // Draw a billboard texture defined by sourceRec // Collision detection functions -RLAPI BoundingBox CalculateBoundingBox(Mesh mesh); // Calculate mesh bounding box limits RLAPI bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB); // Detect collision between two spheres RLAPI bool CheckCollisionBoxes(BoundingBox box1, BoundingBox box2); // Detect collision between two bounding boxes RLAPI bool CheckCollisionBoxSphere(BoundingBox box, Vector3 centerSphere, float radiusSphere); // Detect collision between box and sphere -- cgit v1.2.3 From fd5e457bb49b4a87ca9dc9880f3b8df1683a594d Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 11 Mar 2018 10:41:49 +0100 Subject: Correct issue with triangleCount --- src/models.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index 8e9a6586..47302a1d 100644 --- a/src/models.c +++ b/src/models.c @@ -1179,9 +1179,9 @@ Mesh GenMeshHeightmap(Image heightmap, Vector3 size) Color *pixels = GetImageData(heightmap); // NOTE: One vertex per pixel - int triangleCount = (mapX-1)*(mapZ-1)*2; // One quad every four pixels + mesh.triangleCount = (mapX-1)*(mapZ-1)*2; // One quad every four pixels - mesh.vertexCount = triangleCount*3; + mesh.vertexCount = mesh.triangleCount*3; mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float)); mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); @@ -1584,6 +1584,7 @@ Mesh GenMeshCubicmap(Image cubicmap, Vector3 cubeSize) // Move data from mapVertices temp arays to vertices float array mesh.vertexCount = vCounter; + mesh.triangleCount = vCounter/3; mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float)); mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float)); -- cgit v1.2.3 From 61e0e4b4f37cc66135445bc87af7c92399fa69ee Mon Sep 17 00:00:00 2001 From: Ray Date: Fri, 16 Mar 2018 13:47:01 +0100 Subject: Complete review of raymath for API consistency --- release/include/raylib.h | 61 ++-- release/libs/win32/mingw32/libraylib.a | Bin 973406 -> 1092978 bytes src/core.c | 10 +- src/models.c | 14 +- src/raylib.h | 2 +- src/raymath.h | 507 +++++++++++++++++---------------- src/rlgl.c | 8 +- 7 files changed, 316 insertions(+), 286 deletions(-) (limited to 'src/models.c') diff --git a/release/include/raylib.h b/release/include/raylib.h index dc02370d..c94f4477 100644 --- a/release/include/raylib.h +++ b/release/include/raylib.h @@ -1,6 +1,6 @@ /********************************************************************************************** * -* raylib v1.9-dev +* raylib v1.9.6-dev * * A simple and easy-to-use library to learn videogames programming (www.raylib.com) * @@ -103,11 +103,23 @@ #define KEY_SPACE 32 #define KEY_ESCAPE 256 #define KEY_ENTER 257 +#define KEY_TAB 258 #define KEY_BACKSPACE 259 +#define KEY_INSERT 260 +#define KEY_DELETE 261 #define KEY_RIGHT 262 #define KEY_LEFT 263 #define KEY_DOWN 264 #define KEY_UP 265 +#define KEY_PAGE_UP 266 +#define KEY_PAGE_DOWN 267 +#define KEY_HOME 268 +#define KEY_END 269 +#define KEY_CAPS_LOCK 280 +#define KEY_SCROLL_LOCK 281 +#define KEY_NUM_LOCK 282 +#define KEY_PRINT_SCREEN 283 +#define KEY_PAUSE 284 #define KEY_F1 290 #define KEY_F2 291 #define KEY_F3 292 @@ -310,6 +322,14 @@ typedef struct Vector3 { float z; } Vector3; +// Vector4 type +typedef struct Vector4 { + float x; + float y; + float z; + float w; +} Vector4; + // Matrix type (OpenGL style 4x4 - right handed, column major) typedef struct Matrix { float m0, m4, m8, m12; @@ -410,7 +430,7 @@ typedef struct Mesh { float *texcoords; // Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) float *texcoords2; // Vertex second texture coordinates (useful for lightmaps) (shader-location = 5) float *normals; // Vertex normals (XYZ - 3 components per vertex) (shader-location = 2) - float *tangents; // Vertex tangents (XYZ - 3 components per vertex) (shader-location = 4) + float *tangents; // Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4) unsigned char *colors; // Vertex colors (RGBA - 4 components per vertex) (shader-location = 3) unsigned short *indices;// Vertex indices (in case vertex data comes indexed) @@ -541,7 +561,7 @@ typedef enum { LOC_MAP_METALNESS, // LOC_MAP_SPECULAR LOC_MAP_NORMAL, LOC_MAP_ROUGHNESS, - LOC_MAP_OCCUSION, + LOC_MAP_OCCLUSION, LOC_MAP_EMISSION, LOC_MAP_HEIGHT, LOC_MAP_CUBEMAP, @@ -674,6 +694,7 @@ extern "C" { // Prevents name mangling of functions // Window-related functions RLAPI void InitWindow(int width, int height, void *data); // Initialize window and OpenGL context RLAPI void CloseWindow(void); // Close window and unload OpenGL context +RLAPI bool IsWindowReady(void); // Check if window has been initialized successfully RLAPI bool WindowShouldClose(void); // Check if KEY_ESCAPE pressed or Close icon pressed RLAPI bool IsWindowMinimized(void); // Check if window has been minimized (or lost focus) RLAPI void ToggleFullscreen(void); // Toggle fullscreen mode (only PLATFORM_DESKTOP) @@ -682,6 +703,7 @@ RLAPI void SetWindowTitle(const char *title); // Set title f RLAPI void SetWindowPosition(int x, int y); // Set window position on screen (only PLATFORM_DESKTOP) RLAPI void SetWindowMonitor(int monitor); // Set monitor for the current window (fullscreen mode) RLAPI void SetWindowMinSize(int width, int height); // Set window minimum dimensions (for FLAG_WINDOW_RESIZABLE) +RLAPI void SetWindowSize(int width, int height); // Set window dimensions RLAPI int GetScreenWidth(void); // Get current screen width RLAPI int GetScreenHeight(void); // Get current screen height @@ -715,17 +737,11 @@ RLAPI float GetFrameTime(void); // Returns tim RLAPI double GetTime(void); // Returns elapsed time in seconds since InitWindow() // Color-related functions -RLAPI int GetHexValue(Color color); // Returns hexadecimal value for a Color +RLAPI float *ColorToFloat(Color color); // Returns normalized float array for a Color +RLAPI int ColorToInt(Color color); // Returns hexadecimal value for a Color +RLAPI Vector3 ColorToHSV(Color color); // Returns HSV values for a Color RLAPI Color GetColor(int hexValue); // Returns a Color struct from hexadecimal value RLAPI Color Fade(Color color, float alpha); // Color fade-in or fade-out, alpha goes from 0.0f to 1.0f -RLAPI float *ColorToFloat(Color color); // Converts Color to float array and normalizes - -// Math useful functions (available from raymath.h) -RLAPI float *Vector3ToFloat(Vector3 vec); // Returns Vector3 as float array -RLAPI float *MatrixToFloat(Matrix mat); // Returns Matrix as float array -RLAPI Vector3 Vector3Zero(void); // Vector with components value 0.0f -RLAPI Vector3 Vector3One(void); // Vector with components value 1.0f -RLAPI Matrix MatrixIdentity(void); // Returns identity matrix // Misc. functions RLAPI void ShowLogo(void); // Activate raylib logo at startup (can be done with flags) @@ -783,6 +799,7 @@ RLAPI int GetMouseX(void); // Returns mouse p RLAPI int GetMouseY(void); // Returns mouse position Y RLAPI Vector2 GetMousePosition(void); // Returns mouse position XY RLAPI void SetMousePosition(Vector2 position); // Set mouse position XY +RLAPI void SetMouseScale(float scale); // Set mouse scaling RLAPI int GetMouseWheelMove(void); // Returns mouse wheel movement Y // Input-related functions: touch @@ -839,6 +856,7 @@ RLAPI void DrawRectangleGradientV(int posX, int posY, int width, int height, Col RLAPI void DrawRectangleGradientH(int posX, int posY, int width, int height, Color color1, Color color2);// Draw a horizontal-gradient-filled rectangle RLAPI void DrawRectangleGradientEx(Rectangle rec, Color col1, Color col2, Color col3, Color col4); // Draw a gradient-filled rectangle with custom vertex colors RLAPI void DrawRectangleLines(int posX, int posY, int width, int height, Color color); // Draw rectangle outline +RLAPI void DrawRectangleLinesEx(Rectangle rec, int lineThick, Color color); // Draw rectangle outline with extended parameters RLAPI void DrawTriangle(Vector2 v1, Vector2 v2, Vector2 v3, Color color); // Draw a color-filled triangle RLAPI void DrawTriangleLines(Vector2 v1, Vector2 v2, Vector2 v3, Color color); // Draw triangle outline RLAPI void DrawPoly(Vector2 center, int sides, float radius, float rotation, Color color); // Draw a regular polygon (Vector version) @@ -886,6 +904,7 @@ RLAPI void ImageAlphaPremultiply(Image *image); RLAPI void ImageCrop(Image *image, Rectangle crop); // Crop an image to a defined rectangle RLAPI void ImageResize(Image *image, int newWidth, int newHeight); // Resize and image (bilinear filtering) RLAPI void ImageResizeNN(Image *image,int newWidth,int newHeight); // Resize and image (Nearest-Neighbor scaling algorithm) +RLAPI void ImageMipmaps(Image *image); // Generate all mipmap levels for a provided image RLAPI void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp); // Dither image data to 16bpp or lower (Floyd-Steinberg dithering) RLAPI Image ImageText(const char *text, int fontSize, Color color); // Create an image from text (default font) RLAPI Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing, Color tint); // Create an image from text (custom sprite font) @@ -908,7 +927,7 @@ RLAPI Image GenImageGradientH(int width, int height, Color left, Color right); RLAPI Image GenImageGradientRadial(int width, int height, float density, Color inner, Color outer); // Generate image: radial gradient RLAPI Image GenImageChecked(int width, int height, int checksX, int checksY, Color col1, Color col2); // Generate image: checked RLAPI Image GenImageWhiteNoise(int width, int height, float factor); // Generate image: white noise -RLAPI Image GenImagePerlinNoise(int width, int height, float scale); // Generate image: perlin noise +RLAPI Image GenImagePerlinNoise(int width, int height, int offsetX, int offsetY, float scale); // Generate image: perlin noise RLAPI Image GenImageCellular(int width, int height, int tileSize); // Generate image: cellular algorithm. Bigger tileSize means bigger cells // Texture2D configuration functions @@ -932,19 +951,20 @@ RLAPI void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle dest RLAPI SpriteFont GetDefaultFont(void); // Get the default SpriteFont RLAPI SpriteFont LoadSpriteFont(const char *fileName); // Load SpriteFont from file into GPU memory (VRAM) RLAPI SpriteFont LoadSpriteFontEx(const char *fileName, int fontSize, int charsCount, int *fontChars); // Load SpriteFont from file with extended parameters -RLAPI void UnloadSpriteFont(SpriteFont spriteFont); // Unload SpriteFont from GPU memory (VRAM) +RLAPI void UnloadSpriteFont(SpriteFont font); // Unload SpriteFont from GPU memory (VRAM) // Text drawing functions RLAPI void DrawFPS(int posX, int posY); // Shows current FPS RLAPI void DrawText(const char *text, int posX, int posY, int fontSize, Color color); // Draw text (using default font) -RLAPI void DrawTextEx(SpriteFont spriteFont, const char* text, Vector2 position, // Draw text using SpriteFont and additional parameters +RLAPI void DrawTextEx(SpriteFont font, const char* text, Vector2 position, // Draw text using SpriteFont and additional parameters float fontSize, int spacing, Color tint); // Text misc. functions RLAPI int MeasureText(const char *text, int fontSize); // Measure string width for default font -RLAPI Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, float fontSize, int spacing); // Measure string size for SpriteFont +RLAPI Vector2 MeasureTextEx(SpriteFont font, const char *text, float fontSize, int spacing); // Measure string size for SpriteFont RLAPI const char *FormatText(const char *text, ...); // Formatting of text with variables to 'embed' RLAPI const char *SubText(const char *text, int position, int length); // Get a piece of a text string +RLAPI int GetGlyphIndex(SpriteFont font, int character); // Returns index position for a unicode character on sprite font //------------------------------------------------------------------------------------ // Basic 3d Shapes Drawing Functions (Module: models) @@ -981,6 +1001,11 @@ RLAPI void UnloadModel(Model model); RLAPI Mesh LoadMesh(const char *fileName); // Load mesh from file RLAPI void UnloadMesh(Mesh *mesh); // Unload mesh from memory (RAM and/or VRAM) +// Mesh manipulation functions +RLAPI BoundingBox MeshBoundingBox(Mesh mesh); // Compute mesh bounding box limits +RLAPI void MeshTangents(Mesh *mesh); // Compute mesh tangents +RLAPI void MeshBinormals(Mesh *mesh); // Compute mesh binormals + // Mesh generation functions RLAPI Mesh GenMeshPlane(float width, float length, int resX, int resZ); // Generate plane mesh (with subdivisions) RLAPI Mesh GenMeshCube(float width, float height, float length); // Generate cuboid mesh @@ -1010,7 +1035,6 @@ RLAPI void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRe Vector3 center, float size, Color tint); // Draw a billboard texture defined by sourceRec // Collision detection functions -RLAPI BoundingBox CalculateBoundingBox(Mesh mesh); // Calculate mesh bounding box limits RLAPI bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB); // Detect collision between two spheres RLAPI bool CheckCollisionBoxes(BoundingBox box1, BoundingBox box2); // Detect collision between two bounding boxes RLAPI bool CheckCollisionBoxSphere(BoundingBox box, Vector3 centerSphere, float radiusSphere); // Detect collision between box and sphere @@ -1029,7 +1053,8 @@ RLAPI RayHitInfo GetCollisionRayGround(Ray ray, float groundHeight); // Shader loading/unloading functions RLAPI char *LoadText(const char *fileName); // Load chars array from text file -RLAPI Shader LoadShader(char *vsFileName, char *fsFileName); // Load shader from files and bind default locations +RLAPI Shader LoadShader(const char *vsFileName, const char *fsFileName); // Load shader from files and bind default locations +RLAPI Shader LoadShaderCode(char *vsCode, char *fsCode); // Load shader from code strings and bind default locations RLAPI void UnloadShader(Shader shader); // Unload shader from GPU memory (VRAM) RLAPI Shader GetShaderDefault(void); // Get default shader diff --git a/release/libs/win32/mingw32/libraylib.a b/release/libs/win32/mingw32/libraylib.a index 7653d1d0..e015495a 100644 Binary files a/release/libs/win32/mingw32/libraylib.a and b/release/libs/win32/mingw32/libraylib.a differ diff --git a/src/core.c b/src/core.c index 2f79d5be..0532c3c4 100644 --- a/src/core.c +++ b/src/core.c @@ -432,7 +432,7 @@ static void *GamepadThread(void *arg); // Mouse reading thread // NOTE: data parameter could be used to pass any kind of required data to the initialization void InitWindow(int width, int height, void *data) { - TraceLog(LOG_INFO, "Initializing raylib (v1.9.5-dev)"); + TraceLog(LOG_INFO, "Initializing raylib (v1.9.6-dev)"); #if defined(PLATFORM_DESKTOP) windowTitle = (char *)data; @@ -503,7 +503,7 @@ void InitWindow(int width, int height, void *data) // NOTE: data parameter could be used to pass any kind of required data to the initialization void InitWindow(int width, int height, void *data) { - TraceLog(LOG_INFO, "Initializing raylib (v1.9.5-dev)"); + TraceLog(LOG_INFO, "Initializing raylib (v1.9.6-dev)"); screenWidth = width; screenHeight = height; @@ -1025,7 +1025,7 @@ Ray GetMouseRay(Vector2 mousePosition, Camera camera) // Calculate normalized direction vector Vector3 direction = Vector3Subtract(farPoint, nearPoint); - Vector3Normalize(&direction); + direction = Vector3Normalize(direction); // Apply calculated vectors to ray ray.position = camera.position; @@ -1047,10 +1047,10 @@ Vector2 GetWorldToScreen(Vector3 position, Camera camera) Quaternion worldPos = { position.x, position.y, position.z, 1.0f }; // Transform world position to view - QuaternionTransform(&worldPos, matView); + worldPos = QuaternionTransform(worldPos, matView); // Transform result to projection (clip space position) - QuaternionTransform(&worldPos, matProj); + worldPos = QuaternionTransform(worldPos, matProj); // Calculate normalized device coordinates (inverted y) Vector3 ndcPos = { worldPos.x/worldPos.w, -worldPos.y/worldPos.w, worldPos.z/worldPos.w }; diff --git a/src/models.c b/src/models.c index 47302a1d..b4f02d1b 100644 --- a/src/models.c +++ b/src/models.c @@ -1759,8 +1759,8 @@ void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vec | | d-------c */ - Vector3Scale(&right, sizeRatio.x/2); - Vector3Scale(&up, sizeRatio.y/2); + right = Vector3Scale(right, sizeRatio.x/2); + up = Vector3Scale(up, sizeRatio.y/2); Vector3 p1 = Vector3Add(right, up); Vector3 p2 = Vector3Subtract(right, up); @@ -1897,7 +1897,7 @@ bool CheckCollisionRaySphereEx(Ray ray, Vector3 spherePosition, float sphereRadi if (distance < sphereRadius) collisionDistance = vector + sqrtf(d); else collisionDistance = vector - sqrtf(d); - Vector3Scale(&offset, collisionDistance); + offset = Vector3Scale(offset, collisionDistance); Vector3 cPoint = Vector3Add(ray.position, offset); collisionPoint->x = cPoint.x; @@ -2022,9 +2022,9 @@ RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3) result.distance = t; result.hit = true; result.normal = Vector3CrossProduct(edge1, edge2); - Vector3Normalize(&result.normal); + result.normal = Vector3Normalize(result.normal); Vector3 rayDir = ray.direction; - Vector3Scale(&rayDir, t); + rayDir = Vector3Scale(rayDir, t); result.position = Vector3Add(ray.position, rayDir); } @@ -2045,7 +2045,7 @@ RayHitInfo GetCollisionRayGround(Ray ray, float groundHeight) if (t >= 0.0) { Vector3 rayDir = ray.direction; - Vector3Scale(&rayDir, t); + rayDir = Vector3Scale(rayDir, t); result.hit = true; result.distance = t; result.normal = (Vector3){ 0.0, 1.0, 0.0 }; @@ -2300,7 +2300,7 @@ static Mesh LoadOBJ(const char *fileName) { // If normals not defined, they are calculated from the 3 vertices [N = (V2 - V1) x (V3 - V1)] Vector3 norm = Vector3CrossProduct(Vector3Subtract(midVertices[vCount[1]-1], midVertices[vCount[0]-1]), Vector3Subtract(midVertices[vCount[2]-1], midVertices[vCount[0]-1])); - Vector3Normalize(&norm); + norm = Vector3Normalize(norm); mesh.normals[nCounter] = norm.x; mesh.normals[nCounter + 1] = norm.y; diff --git a/src/raylib.h b/src/raylib.h index ba2ebc36..c94f4477 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1,6 +1,6 @@ /********************************************************************************************** * -* raylib v1.9.5-dev +* raylib v1.9.6-dev * * A simple and easy-to-use library to learn videogames programming (www.raylib.com) * diff --git a/src/raymath.h b/src/raymath.h index 3dbbd2be..22adfe52 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -83,11 +83,16 @@ #define RAD2DEG (180.0f/PI) #endif -// Return float vector +// Return float vector for Matrix #ifndef MatrixToFloat #define MatrixToFloat(mat) (MatrixToFloatV(mat).v) #endif +// Return float vector for Vector3 +#ifndef Vector3ToFloat + #define Vector3ToFloat(vec) (Vector3ToFloatV(vec).v) +#endif + //---------------------------------------------------------------------------------- // Types and Structures Definition //---------------------------------------------------------------------------------- @@ -147,84 +152,86 @@ RMDEF float Clamp(float value, float min, float max) // Vector with components value 0.0f RMDEF Vector2 Vector2Zero(void) { - Vector2 tmp = {0.0f, 0.0f}; - return tmp; + Vector2 result = { 0.0f, 0.0f }; + return result; } // Vector with components value 1.0f RMDEF Vector2 Vector2One(void) { - Vector2 tmp = {1.0f, 1.0f}; - return tmp; + Vector2 result = { 1.0f, 1.0f }; + return result; } // Add two vectors (v1 + v2) RMDEF Vector2 Vector2Add(Vector2 v1, Vector2 v2) { - Vector2 tmp = { v1.x + v2.x, v1.y + v2.y }; - return tmp; + Vector2 result = { v1.x + v2.x, v1.y + v2.y }; + return result; } // Subtract two vectors (v1 - v2) RMDEF Vector2 Vector2Subtract(Vector2 v1, Vector2 v2) { - Vector2 tmp = { v1.x - v2.x, v1.y - v2.y }; - return tmp; + Vector2 result = { v1.x - v2.x, v1.y - v2.y }; + return result; } // Calculate vector length RMDEF float Vector2Length(Vector2 v) { - return sqrtf((v.x*v.x) + (v.y*v.y)); + float result = sqrtf((v.x*v.x) + (v.y*v.y)); + return result; } // Calculate two vectors dot product RMDEF float Vector2DotProduct(Vector2 v1, Vector2 v2) { - return (v1.x*v2.x + v1.y*v2.y); + float result = (v1.x*v2.x + v1.y*v2.y); + return result; } // Calculate distance between two vectors RMDEF float Vector2Distance(Vector2 v1, Vector2 v2) { - return sqrtf((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y)); + float result = sqrtf((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y)); + return result; } // Calculate angle from two vectors in X-axis RMDEF float Vector2Angle(Vector2 v1, Vector2 v2) { - float angle = atan2f(v2.y - v1.y, v2.x - v1.x)*(180.0f/PI); - - if (angle < 0) angle += 360.0f; - - return angle; + float result = atan2f(v2.y - v1.y, v2.x - v1.x)*(180.0f/PI); + if (result < 0) result += 360.0f; + return result; } // Scale vector (multiply by value) -RMDEF void Vector2Scale(Vector2 *v, float scale) +RMDEF Vector2 Vector2Scale(Vector2 v, float scale) { - v->x *= scale; - v->y *= scale; + Vector2 result = { v.x*scale, v.y*scale }; + return result; } // Negate vector -RMDEF void Vector2Negate(Vector2 *v) +RMDEF Vector2 Vector2Negate(Vector2 v) { - v->x = -v->x; - v->y = -v->y; + Vector2 result = { -v.x, -v.y }; + return result; } // Divide vector by a float value -RMDEF void Vector2Divide(Vector2 *v, float div) +RMDEF Vector2 Vector2Divide(Vector2 v, float div) { - Vector2 tmp = {v->x/div, v->y/div}; - *v = tmp; + Vector2 result = { v.x/div, v.y/div }; + return result; } // Normalize provided vector -RMDEF void Vector2Normalize(Vector2 *v) +RMDEF Vector2 Vector2Normalize(Vector2 v) { - Vector2Divide(v, Vector2Length(*v)); + Vector2 result = Vector2Divide(v, Vector2Length(v)); + return result; } //---------------------------------------------------------------------------------- @@ -234,69 +241,56 @@ RMDEF void Vector2Normalize(Vector2 *v) // Vector with components value 0.0f RMDEF Vector3 Vector3Zero(void) { - Vector3 tmp = { 0.0f, 0.0f, 0.0f }; - return tmp; + Vector3 result = { 0.0f, 0.0f, 0.0f }; + return result; } // Vector with components value 1.0f RMDEF Vector3 Vector3One(void) { - Vector3 tmp = { 1.0f, 1.0f, 1.0f }; - return tmp; + Vector3 result = { 1.0f, 1.0f, 1.0f }; + return result; } // Add two vectors RMDEF Vector3 Vector3Add(Vector3 v1, Vector3 v2) { - Vector3 tmp = { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z }; - return tmp; + Vector3 result = { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z }; + return result; } // Substract two vectors RMDEF Vector3 Vector3Subtract(Vector3 v1, Vector3 v2) { - Vector3 tmp = { v1.x - v2.x, v1.y - v2.y, v1.z - v2.z }; - return tmp; + Vector3 result = { v1.x - v2.x, v1.y - v2.y, v1.z - v2.z }; + return result; } // Multiply vector by scalar RMDEF Vector3 Vector3Multiply(Vector3 v, float scalar) -{ - v.x *= scalar; - v.y *= scalar; - v.z *= scalar; - - return v; +{ + Vector3 result = { v.x*scalar, v.y*scalar, v.z*scalar }; + return result; } // Multiply vector by vector RMDEF Vector3 Vector3MultiplyV(Vector3 v1, Vector3 v2) { - Vector3 result; - - result.x = v1.x * v2.x; - result.y = v1.y * v2.y; - result.z = v1.z * v2.z; - + Vector3 result = { v1.x*v2.x, v1.y*v2.y, v1.z*v2.z }; return result; } // Calculate two vectors cross product RMDEF Vector3 Vector3CrossProduct(Vector3 v1, Vector3 v2) { - Vector3 result; - - result.x = v1.y*v2.z - v1.z*v2.y; - result.y = v1.z*v2.x - v1.x*v2.z; - result.z = v1.x*v2.y - v1.y*v2.x; - + Vector3 result = { v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x }; return result; } // Calculate one vector perpendicular vector RMDEF Vector3 Vector3Perpendicular(Vector3 v) { - Vector3 result; + Vector3 result = { 0 }; float min = fabsf(v.x); Vector3 cardinalAxis = {1.0f, 0.0f, 0.0f}; @@ -322,13 +316,15 @@ RMDEF Vector3 Vector3Perpendicular(Vector3 v) // Calculate vector length RMDEF float Vector3Length(const Vector3 v) { - return sqrtf(v.x*v.x + v.y*v.y + v.z*v.z); + float result = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z); + return result; } // Calculate two vectors dot product RMDEF float Vector3DotProduct(Vector3 v1, Vector3 v2) { - return (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z); + float result = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z); + return result; } // Calculate distance between two vectors @@ -337,58 +333,60 @@ RMDEF float Vector3Distance(Vector3 v1, Vector3 v2) float dx = v2.x - v1.x; float dy = v2.y - v1.y; float dz = v2.z - v1.z; - - return sqrtf(dx*dx + dy*dy + dz*dz); + float result = sqrtf(dx*dx + dy*dy + dz*dz); + return result; } // Scale provided vector -RMDEF void Vector3Scale(Vector3 *v, float scale) +RMDEF Vector3 Vector3Scale(Vector3 v, float scale) { - v->x *= scale; - v->y *= scale; - v->z *= scale; + Vector3 result = { v.x*scale, v.y*scale, v.z*scale }; + return result; } // Negate provided vector (invert direction) -RMDEF void Vector3Negate(Vector3 *v) +RMDEF Vector3 Vector3Negate(Vector3 v) { - v->x = -v->x; - v->y = -v->y; - v->z = -v->z; + Vector3 result = { -v.x, -v.y, -v.z }; + return result; } // Normalize provided vector -RMDEF void Vector3Normalize(Vector3 *v) +RMDEF Vector3 Vector3Normalize(Vector3 v) { + Vector3 result = v; + float length, ilength; - - length = Vector3Length(*v); - + length = Vector3Length(v); if (length == 0.0f) length = 1.0f; - ilength = 1.0f/length; - v->x *= ilength; - v->y *= ilength; - v->z *= ilength; + result.x *= ilength; + result.y *= ilength; + result.z *= ilength; + + return result; } // Transforms a Vector3 by a given Matrix -RMDEF void Vector3Transform(Vector3 *v, Matrix mat) +RMDEF Vector3 Vector3Transform(Vector3 v, Matrix mat) { - float x = v->x; - float y = v->y; - float z = v->z; + Vector3 result = { 0 }; + float x = v.x; + float y = v.y; + float z = v.z; - v->x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12; - v->y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13; - v->z = mat.m2*x + mat.m6*y + mat.m10*z + mat.m14; + result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12; + result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13; + result.z = mat.m2*x + mat.m6*y + mat.m10*z + mat.m14; + + return result; }; // Calculate linear interpolation between two vectors RMDEF Vector3 Vector3Lerp(Vector3 v1, Vector3 v2, float amount) { - Vector3 result; + Vector3 result = { 0 }; result.x = v1.x + amount*(v2.x - v1.x); result.y = v1.y + amount*(v2.y - v1.y); @@ -398,43 +396,43 @@ RMDEF Vector3 Vector3Lerp(Vector3 v1, Vector3 v2, float amount) } // Calculate reflected vector to normal -RMDEF Vector3 Vector3Reflect(Vector3 vector, Vector3 normal) +RMDEF Vector3 Vector3Reflect(Vector3 v, Vector3 normal) { // I is the original vector // N is the normal of the incident plane // R = I - (2*N*( DotProduct[ I,N] )) - Vector3 result; + Vector3 result = { 0 }; - float dotProduct = Vector3DotProduct(vector, normal); + float dotProduct = Vector3DotProduct(v, normal); - 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; + result.x = v.x - (2.0f*normal.x)*dotProduct; + result.y = v.y - (2.0f*normal.y)*dotProduct; + result.z = v.z - (2.0f*normal.z)*dotProduct; return result; } // Return min value for each pair of components -RMDEF Vector3 Vector3Min(Vector3 vec1, Vector3 vec2) +RMDEF Vector3 Vector3Min(Vector3 v1, Vector3 v2) { - Vector3 result; + Vector3 result = { 0 }; - result.x = fminf(vec1.x, vec2.x); - result.y = fminf(vec1.y, vec2.y); - result.z = fminf(vec1.z, vec2.z); + result.x = fminf(v1.x, v2.x); + result.y = fminf(v1.y, v2.y); + result.z = fminf(v1.z, v2.z); return result; } // Return max value for each pair of components -RMDEF Vector3 Vector3Max(Vector3 vec1, Vector3 vec2) +RMDEF Vector3 Vector3Max(Vector3 v1, Vector3 v2) { - Vector3 result; + Vector3 result = { 0 }; - result.x = fmaxf(vec1.x, vec2.x); - result.y = fmaxf(vec1.y, vec2.y); - result.z = fmaxf(vec1.z, vec2.z); + result.x = fmaxf(v1.x, v2.x); + result.y = fmaxf(v1.y, v2.y); + result.z = fmaxf(v1.z, v2.z); return result; } @@ -456,7 +454,7 @@ RMDEF Vector3 Vector3Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c) float denom = d00*d11 - d01*d01; - Vector3 result; + Vector3 result = { 0 }; result.y = (d11*d20 - d01*d21)/denom; result.z = (d00*d21 - d01*d20)/denom; @@ -466,19 +464,16 @@ RMDEF Vector3 Vector3Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c) } // Returns Vector3 as float array -RMDEF float3 Vector3ToFloat_(Vector3 vec) +RMDEF float3 Vector3ToFloatV(Vector3 v) { - float3 buffer; + float3 buffer = { 0 }; - buffer.v[0] = vec.x; - buffer.v[1] = vec.y; - buffer.v[2] = vec.z; + buffer.v[0] = v.x; + buffer.v[1] = v.y; + buffer.v[2] = v.z; return buffer; } -#ifndef Vector3ToFloat -#define Vector3ToFloat(vec) (Vector3ToFloat_(vec).v) -#endif //---------------------------------------------------------------------------------- // Module Functions Definition - Matrix math @@ -487,7 +482,7 @@ RMDEF float3 Vector3ToFloat_(Vector3 vec) // Compute matrix determinant RMDEF float MatrixDeterminant(Matrix mat) { - float result; + float result = { 0 }; // Cache the matrix values (speed optimization) float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3; @@ -508,44 +503,45 @@ RMDEF float MatrixDeterminant(Matrix mat) // Returns the trace of the matrix (sum of the values along the diagonal) RMDEF float MatrixTrace(Matrix mat) { - return (mat.m0 + mat.m5 + mat.m10 + mat.m15); + float result = (mat.m0 + mat.m5 + mat.m10 + mat.m15); + return result; } // Transposes provided matrix -RMDEF void MatrixTranspose(Matrix *mat) -{ - Matrix temp; - - temp.m0 = mat->m0; - temp.m1 = mat->m4; - temp.m2 = mat->m8; - temp.m3 = mat->m12; - temp.m4 = mat->m1; - temp.m5 = mat->m5; - temp.m6 = mat->m9; - temp.m7 = mat->m13; - temp.m8 = mat->m2; - temp.m9 = mat->m6; - temp.m10 = mat->m10; - temp.m11 = mat->m14; - temp.m12 = mat->m3; - temp.m13 = mat->m7; - temp.m14 = mat->m11; - temp.m15 = mat->m15; - - *mat = temp; +RMDEF Matrix MatrixTranspose(Matrix mat) +{ + Matrix result = { 0 }; + + result.m0 = mat.m0; + result.m1 = mat.m4; + result.m2 = mat.m8; + result.m3 = mat.m12; + result.m4 = mat.m1; + result.m5 = mat.m5; + result.m6 = mat.m9; + result.m7 = mat.m13; + result.m8 = mat.m2; + result.m9 = mat.m6; + result.m10 = mat.m10; + result.m11 = mat.m14; + result.m12 = mat.m3; + result.m13 = mat.m7; + result.m14 = mat.m11; + result.m15 = mat.m15; + + return result; } // Invert provided matrix -RMDEF void MatrixInvert(Matrix *mat) +RMDEF Matrix MatrixInvert(Matrix mat) { - Matrix temp; + Matrix result = { 0 }; // Cache the matrix values (speed optimization) - float a00 = mat->m0, a01 = mat->m1, a02 = mat->m2, a03 = mat->m3; - float a10 = mat->m4, a11 = mat->m5, a12 = mat->m6, a13 = mat->m7; - float a20 = mat->m8, a21 = mat->m9, a22 = mat->m10, a23 = mat->m11; - float a30 = mat->m12, a31 = mat->m13, a32 = mat->m14, a33 = mat->m15; + float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3; + float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7; + float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11; + float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15; float b00 = a00*a11 - a01*a10; float b01 = a00*a12 - a02*a10; @@ -563,47 +559,51 @@ RMDEF void MatrixInvert(Matrix *mat) // Calculate the invert determinant (inlined to avoid double-caching) 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; - temp.m2 = (a31*b05 - a32*b04 + a33*b03)*invDet; - temp.m3 = (-a21*b05 + a22*b04 - a23*b03)*invDet; - temp.m4 = (-a10*b11 + a12*b08 - a13*b07)*invDet; - temp.m5 = (a00*b11 - a02*b08 + a03*b07)*invDet; - temp.m6 = (-a30*b05 + a32*b02 - a33*b01)*invDet; - temp.m7 = (a20*b05 - a22*b02 + a23*b01)*invDet; - temp.m8 = (a10*b10 - a11*b08 + a13*b06)*invDet; - temp.m9 = (-a00*b10 + a01*b08 - a03*b06)*invDet; - temp.m10 = (a30*b04 - a31*b02 + a33*b00)*invDet; - temp.m11 = (-a20*b04 + a21*b02 - a23*b00)*invDet; - temp.m12 = (-a10*b09 + a11*b07 - a12*b06)*invDet; - temp.m13 = (a00*b09 - a01*b07 + a02*b06)*invDet; - temp.m14 = (-a30*b03 + a31*b01 - a32*b00)*invDet; - temp.m15 = (a20*b03 - a21*b01 + a22*b00)*invDet; - - *mat = temp; + result.m0 = (a11*b11 - a12*b10 + a13*b09)*invDet; + result.m1 = (-a01*b11 + a02*b10 - a03*b09)*invDet; + result.m2 = (a31*b05 - a32*b04 + a33*b03)*invDet; + result.m3 = (-a21*b05 + a22*b04 - a23*b03)*invDet; + result.m4 = (-a10*b11 + a12*b08 - a13*b07)*invDet; + result.m5 = (a00*b11 - a02*b08 + a03*b07)*invDet; + result.m6 = (-a30*b05 + a32*b02 - a33*b01)*invDet; + result.m7 = (a20*b05 - a22*b02 + a23*b01)*invDet; + result.m8 = (a10*b10 - a11*b08 + a13*b06)*invDet; + result.m9 = (-a00*b10 + a01*b08 - a03*b06)*invDet; + result.m10 = (a30*b04 - a31*b02 + a33*b00)*invDet; + result.m11 = (-a20*b04 + a21*b02 - a23*b00)*invDet; + result.m12 = (-a10*b09 + a11*b07 - a12*b06)*invDet; + result.m13 = (a00*b09 - a01*b07 + a02*b06)*invDet; + result.m14 = (-a30*b03 + a31*b01 - a32*b00)*invDet; + result.m15 = (a20*b03 - a21*b01 + a22*b00)*invDet; + + return result; } // Normalize provided matrix -RMDEF void MatrixNormalize(Matrix *mat) -{ - float det = MatrixDeterminant(*mat); - - mat->m0 /= det; - mat->m1 /= det; - mat->m2 /= det; - mat->m3 /= det; - mat->m4 /= det; - mat->m5 /= det; - mat->m6 /= det; - mat->m7 /= det; - mat->m8 /= det; - mat->m9 /= det; - mat->m10 /= det; - mat->m11 /= det; - mat->m12 /= det; - mat->m13 /= det; - mat->m14 /= det; - mat->m15 /= det; +RMDEF Matrix MatrixNormalize(Matrix mat) +{ + Matrix result = { 0 }; + + float det = MatrixDeterminant(mat); + + result.m0 = mat.m0/det; + result.m1 = mat.m1/det; + result.m2 = mat.m2/det; + result.m3 = mat.m3/det; + result.m4 = mat.m4/det; + result.m5 = mat.m5/det; + result.m6 = mat.m6/det; + result.m7 = mat.m7/det; + result.m8 = mat.m8/det; + result.m9 = mat.m9/det; + result.m10 = mat.m10/det; + result.m11 = mat.m11/det; + result.m12 = mat.m12/det; + result.m13 = mat.m13/det; + result.m14 = mat.m14/det; + result.m15 = mat.m15/det; + + return result; } // Returns identity matrix @@ -682,7 +682,7 @@ RMDEF Matrix MatrixTranslate(float x, float y, float z) // NOTE: Angle should be provided in radians RMDEF Matrix MatrixRotate(Vector3 axis, float angle) { - Matrix result; + Matrix result = { 0 }; float x = axis.x, y = axis.y, z = axis.z; @@ -786,7 +786,7 @@ RMDEF Matrix MatrixScale(float x, float y, float z) // NOTE: When multiplying matrices... the order matters! RMDEF Matrix MatrixMultiply(Matrix left, Matrix right) { - Matrix result; + Matrix result = { 0 }; result.m0 = left.m0*right.m0 + left.m1*right.m4 + left.m2*right.m8 + left.m3*right.m12; result.m1 = left.m0*right.m1 + left.m1*right.m5 + left.m2*right.m9 + left.m3*right.m13; @@ -811,7 +811,7 @@ RMDEF Matrix MatrixMultiply(Matrix left, Matrix right) // Returns perspective projection matrix RMDEF Matrix MatrixFrustum(double left, double right, double bottom, double top, double near, double far) { - Matrix result; + Matrix result = { 0 }; float rl = (right - left); float tb = (top - bottom); @@ -846,14 +846,15 @@ RMDEF Matrix MatrixPerspective(double fovy, double aspect, double near, double f { double top = near*tan(fovy*0.5); double right = top*aspect; + Matrix result = MatrixFrustum(-right, right, -top, top, near, far); - return MatrixFrustum(-right, right, -top, top, near, far); + return result; } // Returns orthographic projection matrix RMDEF Matrix MatrixOrtho(double left, double right, double bottom, double top, double near, double far) { - Matrix result; + Matrix result = { 0 }; float rl = (right - left); float tb = (top - bottom); @@ -882,14 +883,14 @@ RMDEF Matrix MatrixOrtho(double left, double right, double bottom, double top, d // Returns camera look-at matrix (view matrix) RMDEF Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up) { - Matrix result; + Matrix result = { 0 }; Vector3 z = Vector3Subtract(eye, target); - Vector3Normalize(&z); + z = Vector3Normalize(z); Vector3 x = Vector3CrossProduct(up, z); - Vector3Normalize(&x); + x = Vector3Normalize(x); Vector3 y = Vector3CrossProduct(z, x); - Vector3Normalize(&y); + y = Vector3Normalize(y); result.m0 = x.x; result.m1 = x.y; @@ -908,7 +909,7 @@ RMDEF Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up) result.m14 = eye.z; result.m15 = 1.0f; - MatrixInvert(&result); + result = MatrixInvert(result); return result; } @@ -916,7 +917,7 @@ RMDEF Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up) // Returns float array of matrix data RMDEF float16 MatrixToFloatV(Matrix mat) { - float16 buffer; + float16 buffer = { 0 }; buffer.v[0] = mat.m0; buffer.v[1] = mat.m1; @@ -945,54 +946,59 @@ RMDEF float16 MatrixToFloatV(Matrix mat) // Returns identity quaternion RMDEF Quaternion QuaternionIdentity(void) { - Quaternion q = { 0.0f, 0.0f, 0.0f, 1.0f }; - return q; + Quaternion result = { 0.0f, 0.0f, 0.0f, 1.0f }; + return result; } // Computes the length of a quaternion -RMDEF float QuaternionLength(Quaternion quat) +RMDEF float QuaternionLength(Quaternion q) { - return sqrt(quat.x*quat.x + quat.y*quat.y + quat.z*quat.z + quat.w*quat.w); + float result = sqrt(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w); + return result; } // Normalize provided quaternion -RMDEF void QuaternionNormalize(Quaternion *q) +RMDEF Quaternion QuaternionNormalize(Quaternion q) { + Quaternion result = { 0 }; + float length, ilength; - - length = QuaternionLength(*q); - + length = QuaternionLength(q); if (length == 0.0f) length = 1.0f; - ilength = 1.0f/length; - q->x *= ilength; - q->y *= ilength; - q->z *= ilength; - q->w *= ilength; + result.x = q.x*ilength; + result.y = q.y*ilength; + result.z = q.z*ilength; + result.w = q.w*ilength; + + return result; } // Invert provided quaternion -RMDEF void QuaternionInvert(Quaternion *quat) +RMDEF Quaternion QuaternionInvert(Quaternion q) { - float length = QuaternionLength(*quat); + Quaternion result = q; + float length = QuaternionLength(q); float lengthSq = length*length; if (lengthSq != 0.0) { float i = 1.0f/lengthSq; - quat->x *= -i; - quat->y *= -i; - quat->z *= -i; - quat->w *= i; + result.x *= -i; + result.y *= -i; + result.z *= -i; + result.w *= i; } + + return result; } // Calculate two quaternion multiplication RMDEF Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2) { - Quaternion result; + Quaternion result = { 0 }; float qax = q1.x, qay = q1.y, qaz = q1.z, qaw = q1.w; float qbx = q2.x, qby = q2.y, qbz = q2.z, qbw = q2.w; @@ -1008,7 +1014,7 @@ RMDEF Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2) // Calculate linear interpolation between two quaternions RMDEF Quaternion QuaternionLerp(Quaternion q1, Quaternion q2, float amount) { - Quaternion result; + Quaternion result = { 0 }; result.x = q1.x + amount*(q2.x - q1.x); result.y = q1.y + amount*(q2.y - q1.y); @@ -1022,7 +1028,7 @@ RMDEF Quaternion QuaternionLerp(Quaternion q1, Quaternion q2, float amount) RMDEF Quaternion QuaternionNlerp(Quaternion q1, Quaternion q2, float amount) { Quaternion result = QuaternionLerp(q1, q2, amount); - QuaternionNormalize(&result); + result = QuaternionNormalize(result); return result; } @@ -1030,7 +1036,7 @@ RMDEF Quaternion QuaternionNlerp(Quaternion q1, Quaternion q2, float amount) // Calculates spherical linear interpolation between two quaternions RMDEF Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount) { - Quaternion result; + Quaternion result = { 0 }; float cosHalfTheta = q1.x*q2.x + q1.y*q2.y + q1.z*q2.z + q1.w*q2.w; @@ -1066,31 +1072,31 @@ RMDEF Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount) // Calculate quaternion based on the rotation from one vector to another RMDEF Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to) { - Quaternion q = { 0 }; + Quaternion result = { 0 }; float cos2Theta = Vector3DotProduct(from, to); Vector3 cross = Vector3CrossProduct(from, to); - q.x = cross.x; - q.y = cross.y; - q.z = cross.y; - q.w = 1.0f + cos2Theta; // NOTE: Added QuaternioIdentity() + result.x = cross.x; + result.y = cross.y; + result.z = cross.y; + result.w = 1.0f + cos2Theta; // NOTE: Added QuaternioIdentity() // Normalize to essentially nlerp the original and identity to 0.5 - QuaternionNormalize(&q); + result = QuaternionNormalize(result); // Above lines are equivalent to: //Quaternion result = QuaternionNlerp(q, QuaternionIdentity(), 0.5f); - return q; + return result; } // Returns a quaternion for a given rotation matrix -RMDEF Quaternion QuaternionFromMatrix(Matrix matrix) +RMDEF Quaternion QuaternionFromMatrix(Matrix mat) { - Quaternion result; + Quaternion result = { 0 }; - float trace = MatrixTrace(matrix); + float trace = MatrixTrace(mat); if (trace > 0.0f) { @@ -1098,42 +1104,42 @@ RMDEF Quaternion QuaternionFromMatrix(Matrix matrix) float invS = 1.0f/s; 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; + result.x = (mat.m6 - mat.m9)*invS; + result.y = (mat.m8 - mat.m2)*invS; + result.z = (mat.m1 - mat.m4)*invS; } else { - float m00 = matrix.m0, m11 = matrix.m5, m22 = matrix.m10; + float m00 = mat.m0, m11 = mat.m5, m22 = mat.m10; if (m00 > m11 && m00 > m22) { float s = (float)sqrt(1.0f + m00 - m11 - m22)*2.0f; float invS = 1.0f/s; - result.w = (matrix.m6 - matrix.m9)*invS; + result.w = (mat.m6 - mat.m9)*invS; result.x = s*0.25f; - result.y = (matrix.m4 + matrix.m1)*invS; - result.z = (matrix.m8 + matrix.m2)*invS; + result.y = (mat.m4 + mat.m1)*invS; + result.z = (mat.m8 + mat.m2)*invS; } else if (m11 > m22) { 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.w = (mat.m8 - mat.m2)*invS; + result.x = (mat.m4 + mat.m1)*invS; result.y = s*0.25f; - result.z = (matrix.m9 + matrix.m6)*invS; + result.z = (mat.m9 + mat.m6)*invS; } else { 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.w = (mat.m1 - mat.m4)*invS; + result.x = (mat.m8 + mat.m2)*invS; + result.y = (mat.m9 + mat.m6)*invS; result.z = s*0.25f; } } @@ -1144,7 +1150,7 @@ RMDEF Quaternion QuaternionFromMatrix(Matrix matrix) // Returns a matrix for a given quaternion RMDEF Matrix QuaternionToMatrix(Quaternion q) { - Matrix result; + Matrix result = { 0 }; float x = q.x, y = q.y, z = q.z, w = q.w; @@ -1197,7 +1203,7 @@ RMDEF Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle) angle *= 0.5f; - Vector3Normalize(&axis); + axis = Vector3Normalize(axis); float sinres = sinf(angle); float cosres = cosf(angle); @@ -1207,7 +1213,7 @@ RMDEF Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle) result.z = axis.z*sinres; result.w = cosres; - QuaternionNormalize(&result); + result = QuaternionNormalize(result); return result; } @@ -1215,7 +1221,7 @@ RMDEF Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle) // Returns the rotation angle and axis for a given quaternion RMDEF void QuaternionToAxisAngle(Quaternion q, Vector3 *outAxis, float *outAngle) { - if (fabs(q.w) > 1.0f) QuaternionNormalize(&q); + if (fabs(q.w) > 1.0f) q = QuaternionNormalize(q); Vector3 resAxis = { 0.0f, 0.0f, 0.0f }; float resAngle = 0.0f; @@ -1264,39 +1270,38 @@ RMDEF Quaternion QuaternionFromEuler(float roll, float pitch, float yaw) // NOTE: Angles are returned in a Vector3 struct in degrees RMDEF Vector3 QuaternionToEuler(Quaternion q) { - Vector3 v = { 0 }; + Vector3 result = { 0 }; // roll (x-axis rotation) float x0 = 2.0f*(q.w*q.x + q.y*q.z); float x1 = 1.0f - 2.0f*(q.x*q.x + q.y*q.y); - v.x = atan2f(x0, x1)*RAD2DEG; + result.x = atan2f(x0, x1)*RAD2DEG; // pitch (y-axis rotation) float y0 = 2.0f*(q.w*q.y - q.z*q.x); y0 = y0 > 1.0f ? 1.0f : y0; y0 = y0 < -1.0f ? -1.0f : y0; - v.y = asinf(y0)*RAD2DEG; + result.y = asinf(y0)*RAD2DEG; // yaw (z-axis rotation) float z0 = 2.0f*(q.w*q.z + q.x*q.y); float z1 = 1.0f - 2.0f*(q.y*q.y + q.z*q.z); - v.z = atan2f(z0, z1)*RAD2DEG; + result.z = atan2f(z0, z1)*RAD2DEG; - return v; + return result; } // Transform a quaternion given a transformation matrix -RMDEF void QuaternionTransform(Quaternion *q, Matrix mat) +RMDEF Quaternion QuaternionTransform(Quaternion q, Matrix mat) { - float x = q->x; - float y = q->y; - float z = q->z; - float w = q->w; + Quaternion result = { 0 }; - q->x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12*w; - q->y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13*w; - q->z = mat.m2*x + mat.m6*y + mat.m10*z + mat.m14*w; - q->w = mat.m3*x + mat.m7*y + mat.m11*z + mat.m15*w; + result.x = mat.m0*q.x + mat.m4*q.y + mat.m8*q.z + mat.m12*q.w; + result.y = mat.m1*q.x + mat.m5*q.y + mat.m9*q.z + mat.m13*q.w; + result.z = mat.m2*q.x + mat.m6*q.y + mat.m10*q.z + mat.m14*q.w; + result.w = mat.m3*q.x + mat.m7*q.y + mat.m11*q.z + mat.m15*q.w; + + return result; } #endif // RAYMATH_H diff --git a/src/rlgl.c b/src/rlgl.c index 46e0668d..8b9f18e8 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -470,7 +470,7 @@ void rlRotatef(float angleDeg, float x, float y, float z) Matrix matRotation = MatrixIdentity(); Vector3 axis = (Vector3){ x, y, z }; - Vector3Normalize(&axis); + axis = Vector3Normalize(axis); matRotation = MatrixRotate(axis, angleDeg*DEG2RAD); // NOTE: We transpose matrix with multiplication order @@ -570,7 +570,7 @@ void rlEnd(void) // This way, rlTranslatef(), rlRotatef()... behaviour is the same than OpenGL 1.1 // Apply transformation matrix to all temp vertices - for (int i = 0; i < tempBufferCount; i++) Vector3Transform(&tempBuffer[i], *currentMatrix); + for (int i = 0; i < tempBufferCount; i++) tempBuffer[i] = Vector3Transform(tempBuffer[i], *currentMatrix); // Deactivate tempBuffer usage to allow rlVertex3f do its job useTempBuffer = false; @@ -1356,13 +1356,13 @@ Vector3 rlUnproject(Vector3 source, Matrix proj, Matrix view) // Calculate unproject matrix (multiply view patrix by projection matrix) and invert it Matrix matViewProj = MatrixMultiply(view, proj); - MatrixInvert(&matViewProj); + matViewProj= MatrixInvert(matViewProj); // Create quaternion from source point Quaternion quat = { source.x, source.y, source.z, 1.0f }; // Multiply quat point by unproject matrix - QuaternionTransform(&quat, matViewProj); + quat = QuaternionTransform(quat, matViewProj); // Normalized world points in vectors result.x = quat.x/quat.w; -- cgit v1.2.3 From 201007e426816eea145c6ed516165d8a10fc4301 Mon Sep 17 00:00:00 2001 From: "maficccc@gmail.com" Date: Fri, 16 Mar 2018 16:26:02 +0100 Subject: Fix sscanf() without field limits can crash with huge input data --- src/models.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index 8e9a6586..aae3bd86 100644 --- a/src/models.c +++ b/src/models.c @@ -2353,7 +2353,7 @@ static Mesh LoadOBJ(const char *fileName) // NOTE: Texture map parameters are not supported static Material LoadMTL(const char *fileName) { - #define MAX_BUFFER_SIZE 128 + #define MAX_BUFFER_SIZE 128 Material material = { 0 }; @@ -2381,7 +2381,7 @@ static Material LoadMTL(const char *fileName) case 'n': // newmtl string Material name. Begins a new material description. { // TODO: Support multiple materials in a single .mtl - sscanf(buffer, "newmtl %s", mapFileName); + sscanf(buffer, "newmtl %127s", mapFileName); TraceLog(LOG_INFO, "[%s] Loading material...", mapFileName); } @@ -2446,12 +2446,12 @@ static Material LoadMTL(const char *fileName) { if (buffer[5] == 'd') // map_Kd string Diffuse color texture map. { - result = sscanf(buffer, "map_Kd %s", mapFileName); + result = sscanf(buffer, "map_Kd %127s", mapFileName); if (result != EOF) material.maps[MAP_DIFFUSE].texture = LoadTexture(mapFileName); } else if (buffer[5] == 's') // map_Ks string Specular color texture map. { - result = sscanf(buffer, "map_Ks %s", mapFileName); + result = sscanf(buffer, "map_Ks %127s", mapFileName); if (result != EOF) material.maps[MAP_SPECULAR].texture = LoadTexture(mapFileName); } else if (buffer[5] == 'a') // map_Ka string Ambient color texture map. @@ -2461,12 +2461,12 @@ static Material LoadMTL(const char *fileName) } break; case 'B': // map_Bump string Bump texture map. { - result = sscanf(buffer, "map_Bump %s", mapFileName); + result = sscanf(buffer, "map_Bump %127s", mapFileName); if (result != EOF) material.maps[MAP_NORMAL].texture = LoadTexture(mapFileName); } break; case 'b': // map_bump string Bump texture map. { - result = sscanf(buffer, "map_bump %s", mapFileName); + result = sscanf(buffer, "map_bump %127s", mapFileName); if (result != EOF) material.maps[MAP_NORMAL].texture = LoadTexture(mapFileName); } break; case 'd': // map_d string Opacity texture map. @@ -2491,7 +2491,7 @@ static Material LoadMTL(const char *fileName) } break; case 'b': // bump string Bump texture map { - result = sscanf(buffer, "bump %s", mapFileName); + result = sscanf(buffer, "bump %127s", mapFileName); if (result != EOF) material.maps[MAP_NORMAL].texture = LoadTexture(mapFileName); } break; case 'T': // Tr float Transparency Tr (alpha). Tr is inverse of d -- cgit v1.2.3 From 375adf86a61be8f3b23c253672dfd3c8df805784 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 2 Apr 2018 15:16:45 +0200 Subject: Review math usage to reduce temp variables --- src/core.c | 3 +-- src/models.c | 27 ++++++++++----------------- src/rlgl.c | 5 ++--- 3 files changed, 13 insertions(+), 22 deletions(-) (limited to 'src/models.c') diff --git a/src/core.c b/src/core.c index 2171835a..3692845f 100644 --- a/src/core.c +++ b/src/core.c @@ -1061,8 +1061,7 @@ Ray GetMouseRay(Vector2 mousePosition, Camera camera) Vector3 cameraPlanePointerPos = rlUnproject((Vector3){ deviceCoords.x, deviceCoords.y, -1.0f }, matProj, matView); // Calculate normalized direction vector - Vector3 direction = Vector3Subtract(farPoint, nearPoint); - direction = Vector3Normalize(direction); + Vector3 direction = Vector3Normalize(Vector3Subtract(farPoint, nearPoint)); if(camera.type == CAMERA_PERSPECTIVE) { diff --git a/src/models.c b/src/models.c index b4f02d1b..0cfcf486 100644 --- a/src/models.c +++ b/src/models.c @@ -1889,16 +1889,14 @@ bool CheckCollisionRaySphereEx(Ray ray, Vector3 spherePosition, float sphereRadi if (d >= 0.0f) collision = true; - // Calculate collision point - Vector3 offset = ray.direction; + // Check if ray origin is inside the sphere to calculate the correct collision point float collisionDistance = 0; - // Check if ray origin is inside the sphere to calculate the correct collision point if (distance < sphereRadius) collisionDistance = vector + sqrtf(d); else collisionDistance = vector - sqrtf(d); - - offset = Vector3Scale(offset, collisionDistance); - Vector3 cPoint = Vector3Add(ray.position, offset); + + // Calculate collision point + Vector3 cPoint = Vector3Add(ray.position, Vector3Scale(ray.direction, collisionDistance)); collisionPoint->x = cPoint.x; collisionPoint->y = cPoint.y; @@ -2021,11 +2019,8 @@ RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3) result.hit = true; result.distance = t; result.hit = true; - result.normal = Vector3CrossProduct(edge1, edge2); - result.normal = Vector3Normalize(result.normal); - Vector3 rayDir = ray.direction; - rayDir = Vector3Scale(rayDir, t); - result.position = Vector3Add(ray.position, rayDir); + result.normal = Vector3Normalize(Vector3CrossProduct(edge1, edge2)); + result.position = Vector3Add(ray.position, Vector3Scale(ray.direction, t)); } return result; @@ -2040,16 +2035,14 @@ RayHitInfo GetCollisionRayGround(Ray ray, float groundHeight) if (fabsf(ray.direction.y) > EPSILON) { - float t = (ray.position.y - groundHeight)/-ray.direction.y; + float distance = (ray.position.y - groundHeight)/-ray.direction.y; - if (t >= 0.0) + if (distance >= 0.0) { - Vector3 rayDir = ray.direction; - rayDir = Vector3Scale(rayDir, t); result.hit = true; - result.distance = t; + result.distance = distance; result.normal = (Vector3){ 0.0, 1.0, 0.0 }; - result.position = Vector3Add(ray.position, rayDir); + result.position = Vector3Add(ray.position, Vector3Scale(ray.direction, distance)); } } diff --git a/src/rlgl.c b/src/rlgl.c index 8b9f18e8..f25340fa 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -470,8 +470,7 @@ void rlRotatef(float angleDeg, float x, float y, float z) Matrix matRotation = MatrixIdentity(); Vector3 axis = (Vector3){ x, y, z }; - axis = Vector3Normalize(axis); - matRotation = MatrixRotate(axis, angleDeg*DEG2RAD); + matRotation = MatrixRotate(Vector3Normalize(axis), angleDeg*DEG2RAD); // NOTE: We transpose matrix with multiplication order *currentMatrix = MatrixMultiply(matRotation, *currentMatrix); @@ -1356,7 +1355,7 @@ Vector3 rlUnproject(Vector3 source, Matrix proj, Matrix view) // Calculate unproject matrix (multiply view patrix by projection matrix) and invert it Matrix matViewProj = MatrixMultiply(view, proj); - matViewProj= MatrixInvert(matViewProj); + matViewProj = MatrixInvert(matViewProj); // Create quaternion from source point Quaternion quat = { source.x, source.y, source.z, 1.0f }; -- cgit v1.2.3 From 6edf15b9f9d761536906b9d1144fc49610881ea6 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 4 Apr 2018 12:00:54 +0200 Subject: Added funtion: ExportMesh() --- src/models.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index f9aa1805..ae1fc968 100644 --- a/src/models.c +++ b/src/models.c @@ -650,6 +650,47 @@ void UnloadMesh(Mesh *mesh) rlUnloadMesh(mesh); } +// Export mesh as an OBJ file +void ExportMesh(const char *fileName, Mesh mesh) +{ + FILE *objFile = fopen(fileName, "wt"); + + fprintf(objFile, "# raylib Mesh OBJ exporter v1.0\n\n"); + fprintf(objFile, "# Mesh exported as triangle faces and not optimized.\n"); + fprintf(objFile, "# Vertex Count: %i\n", mesh.vertexCount); + fprintf(objFile, "# Triangle Count: %i\n\n", mesh.triangleCount); + fprintf(objFile, "# LICENSE: zlib/libpng\n"); + fprintf(objFile, "# Copyright (c) 2018 Ramon Santamaria (@raysan5)\n\n"); + + fprintf(objFile, "g mesh\n"); + + for (int i = 0, v = 0; i < mesh.vertexCount; i++, v += 3) + { + fprintf(objFile, "v %.2f %.2f %.2f\n", mesh.vertices[v], mesh.vertices[v + 1], mesh.vertices[v + 2]); + } + + for (int i = 0, v = 0; i < mesh.vertexCount; i++, v += 2) + { + fprintf(objFile, "vt %.2f %.2f\n", mesh.texcoords[v], mesh.texcoords[v + 1]); + } + + for (int i = 0, v = 0; i < mesh.vertexCount; i++, v += 3) + { + fprintf(objFile, "vn %.2f %.2f %.2f\n", mesh.normals[v], mesh.normals[v + 1], mesh.normals[v + 2]); + } + + for (int i = 0; i < mesh.triangleCount; i += 3) + { + fprintf(objFile, "f %i/%i/%i %i/%i/%i %i/%i/%i\n", i, i, i, i + 1, i + 1, i + 1, i + 2, i + 2, i + 2); + } + + fprintf(objFile, "\n"); + + fclose(objFile); + + TraceLog(LOG_INFO, "Mesh saved: %s", fileName); +} + #if defined(SUPPORT_MESH_GENERATION) // Generate plane mesh (with subdivisions) Mesh GenMeshPlane(float width, float length, int resX, int resZ) -- cgit v1.2.3 From d003c23ecf6ec22f4811d07cb30fd2c86cf1d190 Mon Sep 17 00:00:00 2001 From: lumaio teon Date: Fri, 6 Apr 2018 12:04:09 -0400 Subject: Added GetCollisionRayModel --- release/libs/win32/mingw32/libraylib.a | Bin 1056510 -> 1061358 bytes src/models.c | 47 +++++++++++++++++++++++++++++++++ src/raylib.h | 1 + 3 files changed, 48 insertions(+) (limited to 'src/models.c') diff --git a/release/libs/win32/mingw32/libraylib.a b/release/libs/win32/mingw32/libraylib.a index 361aa73c..ed0003c3 100644 Binary files a/release/libs/win32/mingw32/libraylib.a and b/release/libs/win32/mingw32/libraylib.a differ diff --git a/src/models.c b/src/models.c index ae1fc968..0db46fc1 100644 --- a/src/models.c +++ b/src/models.c @@ -2008,6 +2008,53 @@ RayHitInfo GetCollisionRayMesh(Ray ray, Mesh *mesh) return result; } +// Get collision info between ray and model +// NOTE: This is an exact clone of GetCollisionRayMesh but applies transformation matrix from the model to the vertices +RayHitInfo GetCollisionRayModel(Ray ray, Model *model) +{ + RayHitInfo result = { 0 }; + + // If mesh doesn't have vertex data on CPU, can't test it. + if (!model->mesh.vertices) return result; + + // model->mesh.triangleCount may not be set, vertexCount is more reliable + int triangleCount = model->mesh.vertexCount/3; + + // Test against all triangles in mesh + for (int i = 0; i < triangleCount; i++) + { + Vector3 a, b, c; + Vector3 *vertdata = (Vector3 *)model->mesh.vertices; + + if (model->mesh.indices) + { + a = vertdata[model->mesh.indices[i*3 + 0]]; + b = vertdata[model->mesh.indices[i*3 + 1]]; + c = vertdata[model->mesh.indices[i*3 + 2]]; + } + else + { + a = vertdata[i*3 + 0]; + b = vertdata[i*3 + 1]; + c = vertdata[i*3 + 2]; + } + + a = Vector3Transform(a, model->transform); + b = Vector3Transform(b, model->transform); + c = Vector3Transform(c, model->transform); + + RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, a, b, c); + + if (triHitInfo.hit) + { + // Save the closest hit triangle + if ((!result.hit) || (result.distance > triHitInfo.distance)) result = triHitInfo; + } + } + + return result; +} + // Get collision info between ray and triangle // NOTE: Based on https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3) diff --git a/src/raylib.h b/src/raylib.h index 3986ebcf..12ec5e24 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1053,6 +1053,7 @@ RLAPI bool CheckCollisionRaySphereEx(Ray ray, Vector3 spherePosition, float sphe Vector3 *collisionPoint); // Detect collision between ray and sphere, returns collision point RLAPI bool CheckCollisionRayBox(Ray ray, BoundingBox box); // Detect collision between ray and box RLAPI RayHitInfo GetCollisionRayMesh(Ray ray, Mesh *mesh); // Get collision info between ray and mesh +RLAPI RayHitInfo GetCollisionRayModel(Ray ray, Model *model); // Get collision info between ray and model RLAPI RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3); // Get collision info between ray and triangle RLAPI RayHitInfo GetCollisionRayGround(Ray ray, float groundHeight); // Get collision info between ray and ground plane (Y-normal plane) -- cgit v1.2.3 From d2cc5b88dfa3ff8bbcd66a4849cd3be1d553b6f1 Mon Sep 17 00:00:00 2001 From: lumaio teon Date: Sat, 7 Apr 2018 03:49:56 -0400 Subject: Removed useless GetCollisionRayMesh and libraylib.a --- release/libs/win32/mingw32/libraylib.a | Bin 1061358 -> 1056510 bytes src/models.c | 43 --------------------------------- src/raylib.h | 1 - 3 files changed, 44 deletions(-) (limited to 'src/models.c') diff --git a/release/libs/win32/mingw32/libraylib.a b/release/libs/win32/mingw32/libraylib.a index ed0003c3..361aa73c 100644 Binary files a/release/libs/win32/mingw32/libraylib.a and b/release/libs/win32/mingw32/libraylib.a differ diff --git a/src/models.c b/src/models.c index 0db46fc1..ccf2d05b 100644 --- a/src/models.c +++ b/src/models.c @@ -1966,50 +1966,7 @@ bool CheckCollisionRayBox(Ray ray, BoundingBox box) return collision; } -// Get collision info between ray and mesh -RayHitInfo GetCollisionRayMesh(Ray ray, Mesh *mesh) -{ - RayHitInfo result = { 0 }; - - // If mesh doesn't have vertex data on CPU, can't test it. - if (!mesh->vertices) return result; - - // mesh->triangleCount may not be set, vertexCount is more reliable - int triangleCount = mesh->vertexCount/3; - - // Test against all triangles in mesh - for (int i = 0; i < triangleCount; i++) - { - Vector3 a, b, c; - Vector3 *vertdata = (Vector3 *)mesh->vertices; - - if (mesh->indices) - { - a = vertdata[mesh->indices[i*3 + 0]]; - b = vertdata[mesh->indices[i*3 + 1]]; - c = vertdata[mesh->indices[i*3 + 2]]; - } - else - { - a = vertdata[i*3 + 0]; - b = vertdata[i*3 + 1]; - c = vertdata[i*3 + 2]; - } - - RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, a, b, c); - - if (triHitInfo.hit) - { - // Save the closest hit triangle - if ((!result.hit) || (result.distance > triHitInfo.distance)) result = triHitInfo; - } - } - - return result; -} - // Get collision info between ray and model -// NOTE: This is an exact clone of GetCollisionRayMesh but applies transformation matrix from the model to the vertices RayHitInfo GetCollisionRayModel(Ray ray, Model *model) { RayHitInfo result = { 0 }; diff --git a/src/raylib.h b/src/raylib.h index 12ec5e24..a5963905 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1052,7 +1052,6 @@ RLAPI bool CheckCollisionRaySphere(Ray ray, Vector3 spherePosition, float sphere RLAPI bool CheckCollisionRaySphereEx(Ray ray, Vector3 spherePosition, float sphereRadius, Vector3 *collisionPoint); // Detect collision between ray and sphere, returns collision point RLAPI bool CheckCollisionRayBox(Ray ray, BoundingBox box); // Detect collision between ray and box -RLAPI RayHitInfo GetCollisionRayMesh(Ray ray, Mesh *mesh); // Get collision info between ray and mesh RLAPI RayHitInfo GetCollisionRayModel(Ray ray, Model *model); // Get collision info between ray and model RLAPI RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3); // Get collision info between ray and triangle RLAPI RayHitInfo GetCollisionRayGround(Ray ray, float groundHeight); // Get collision info between ray and ground plane (Y-normal plane) -- cgit v1.2.3 From 1841afad11892bab16976b976d69b7757b617c8b Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Sat, 7 Apr 2018 22:29:53 +0200 Subject: Refactor all #define SUPPORT_* into a config.h That way, a user needs only to touch a single file to configure what features raylib is built with. Include guards are left out intentionally, because config.h should only be included in source files, not headers. Later on, config.h can also define the raylib version (#461). --- src/audio.c | 12 +++------- src/config.h | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/core.c | 12 ++-------- src/models.c | 7 +----- src/rlgl.c | 6 +---- src/rlgl.h | 4 ++-- src/shapes.c | 8 +------ src/text.c | 9 ++----- src/textures.c | 12 +--------- src/utils.c | 2 +- src/utils.h | 6 +++-- 11 files changed, 92 insertions(+), 60 deletions(-) create mode 100644 src/config.h (limited to 'src/models.c') diff --git a/src/audio.c b/src/audio.c index 1fcf6f91..3ee1fe81 100644 --- a/src/audio.c +++ b/src/audio.c @@ -11,7 +11,7 @@ * - Manage raw audio context * * CONFIGURATION: -* +* * #define AUDIO_STANDALONE * Define to use the module as standalone library (independently of raylib). * Required types and functions are defined in the same module. @@ -24,7 +24,7 @@ * #define SUPPORT_FILEFORMAT_XM * #define SUPPORT_FILEFORMAT_MOD * #define SUPPORT_FILEFORMAT_FLAC -* Selected desired fileformats to be supported for loading. Some of those formats are +* Selected desired fileformats to be supported for loading. Some of those formats are * supported by default, to remove support, just comment unrequired #define in this module * * LIMITATIONS (only OpenAL Soft): @@ -72,13 +72,7 @@ * **********************************************************************************************/ -// Default configuration flags (supported features) -//------------------------------------------------- -#define SUPPORT_FILEFORMAT_WAV -#define SUPPORT_FILEFORMAT_OGG -#define SUPPORT_FILEFORMAT_XM -#define SUPPORT_FILEFORMAT_MOD -//------------------------------------------------- +#include "config.h" #if !defined(USE_OPENAL_BACKEND) #define USE_MINI_AL 1 // Set to 1 to use mini_al; 0 to use OpenAL. diff --git a/src/config.h b/src/config.h new file mode 100644 index 00000000..40b9d7c4 --- /dev/null +++ b/src/config.h @@ -0,0 +1,74 @@ +/* Edit to control what features raylib is compiled with. */ + +// text.c +/* Default font is loaded on window initialization to be available for the user to render simple text. NOTE: If enabled, uses external module functions to load default raylib font (module: text) */ +#define SUPPORT_DEFAULT_FONT 1 +/* Selected desired fileformats to be supported for loading. */ +#define SUPPORT_FILEFORMAT_FNT 1 +#define SUPPORT_FILEFORMAT_TTF 1 + +// textures.c +/* Selecte desired fileformats to be supported for image data loading. */ +#define SUPPORT_FILEFORMAT_PNG 1 +#define SUPPORT_FILEFORMAT_DDS 1 +#define SUPPORT_FILEFORMAT_HDR 1 +#define SUPPORT_FILEFORMAT_KTX 1 +#define SUPPORT_FILEFORMAT_ASTC 1 +/* #undef SUPPORT_FILEFORMAT_BMP */ +/* #undef SUPPORT_FILEFORMAT_TGA */ +/* #undef SUPPORT_FILEFORMAT_JPG */ +/* #undef SUPPORT_FILEFORMAT_GIF */ +/* #undef SUPPORT_FILEFORMAT_PSD */ +/* #undef SUPPORT_FILEFORMAT_PKM */ +/* #undef SUPPORT_FILEFORMAT_PVR */ + +/* Support multiple image editing functions to scale, adjust colors, flip, draw on images, crop... If not defined only three image editing functions supported: ImageFormat(), ImageAlphaMask(), ImageToPOT() */ +#define SUPPORT_IMAGE_MANIPULATION 1 + +/* Support proedural image generation functionality (gradient, spot, perlin-noise, cellular) */ +#define SUPPORT_IMAGE_GENERATION 1 + +// rlgl.c +/* Support VR simulation functionality (stereo rendering) */ +#define SUPPORT_VR_SIMULATOR 1 +/* Include stereo rendering distortion shader (shader_distortion.h) */ +#define SUPPORT_DISTORTION_SHADER 1 + +// core.c +/* Camera module is included (camera.h) and multiple predefined cameras are available: free, 1st/3rd person, orbital */ +#define SUPPORT_CAMERA_SYSTEM 1 +/* Gestures module is included (gestures.h) to support gestures detection: tap, hold, swipe, drag */ +#define SUPPORT_GESTURES_SYSTEM 1 +/* Mouse gestures are directly mapped like touches and processed by gestures system. */ +#define SUPPORT_MOUSE_GESTURES 1 +/* Use busy wait loop for timing sync, if not defined, a high-resolution timer is setup and used */ +#define SUPPORT_BUSY_WAIT_LOOP 1 +/* Allow automatic gif recording of current screen pressing CTRL+F12, defined in KeyCallback() */ +#define SUPPORT_GIF_RECORDING 1 + +// audio.c +/* Desired fileformats to be supported for loading. */ +#define SUPPORT_FILEFORMAT_WAV 1 +#define SUPPORT_FILEFORMAT_OGG 1 +#define SUPPORT_FILEFORMAT_XM 1 +#define SUPPORT_FILEFORMAT_MOD 1 +/* #undef SUPPORT_FILEFORMAT_FLAC */ + +// models.c +/* Selected desired fileformats to be supported for loading. */ +#define SUPPORT_FILEFORMAT_OBJ 1 +#define SUPPORT_FILEFORMAT_MTL 1 + +/* Support procedural mesh generation functions, uses external par_shapes.h library + * NOTE: Some generated meshes DO NOT include generated texture coordinates + */ +#define SUPPORT_MESH_GENERATION 1 + +// utils.c +/* Show TraceLog() output messages. NOTE: By default LOG_DEBUG traces not shown */ +#define SUPPORT_TRACELOG 1 + +/* Support saving image data as PNG fileformat. NOTE: Requires stb_image_write library */ +#define SUPPORT_SAVE_PNG 1 +/* Support saving image data as PMP fileformat. NOTE: Requires stb_image_write library */ +/* #undef SUPPORT_SAVE_BMP */ diff --git a/src/core.c b/src/core.c index e10494c1..0265afd0 100644 --- a/src/core.c +++ b/src/core.c @@ -48,7 +48,7 @@ * Mouse gestures are directly mapped like touches and processed by gestures system. * * #define SUPPORT_BUSY_WAIT_LOOP -* Use busy wait loop for timming sync, if not defined, a high-resolution timer is setup and used +* Use busy wait loop for timing sync, if not defined, a high-resolution timer is setup and used * * #define SUPPORT_GIF_RECORDING * Allow automatic gif recording of current screen pressing CTRL+F12, defined in KeyCallback() @@ -81,15 +81,7 @@ * **********************************************************************************************/ -// Default configuration flags (supported features) -//------------------------------------------------- -#define SUPPORT_DEFAULT_FONT -#define SUPPORT_MOUSE_GESTURES -#define SUPPORT_CAMERA_SYSTEM -#define SUPPORT_GESTURES_SYSTEM -#define SUPPORT_BUSY_WAIT_LOOP -#define SUPPORT_GIF_RECORDING -//------------------------------------------------- +#include "config.h" #include "raylib.h" diff --git a/src/models.c b/src/models.c index ae1fc968..0486d17c 100644 --- a/src/models.c +++ b/src/models.c @@ -36,12 +36,7 @@ * **********************************************************************************************/ -// Default configuration flags (supported features) -//------------------------------------------------- -#define SUPPORT_FILEFORMAT_OBJ -#define SUPPORT_FILEFORMAT_MTL -#define SUPPORT_MESH_GENERATION -//------------------------------------------------- +#include "config.h" #include "raylib.h" diff --git a/src/rlgl.c b/src/rlgl.c index 118823db..68bd3670 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -54,11 +54,7 @@ * **********************************************************************************************/ -// Default configuration flags (supported features) -//------------------------------------------------- -#define SUPPORT_VR_SIMULATOR -#define SUPPORT_DISTORTION_SHADER -//------------------------------------------------- +#include "config.h" #include "rlgl.h" diff --git a/src/rlgl.h b/src/rlgl.h index 01278699..c071acac 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -24,7 +24,7 @@ * #define RLGL_STANDALONE * Use rlgl as standalone library (no raylib dependency) * -* #define SUPPORT_VR_SIMULATION / SUPPORT_STEREO_RENDERING +* #define SUPPORT_VR_SIMULATOR * Support VR simulation functionality (stereo rendering) * * #define SUPPORT_DISTORTION_SHADER @@ -496,4 +496,4 @@ void TraceLog(int msgType, const char *text, ...); // Show trace log messag } #endif -#endif // RLGL_H \ No newline at end of file +#endif // RLGL_H diff --git a/src/shapes.c b/src/shapes.c index 693463ff..a1bc7098 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -4,12 +4,6 @@ * * CONFIGURATION: * -* #define SUPPORT_QUADS_ONLY -* Draw shapes using only QUADS, vertex are accumulated in QUADS arrays (like textures) -* -* #define SUPPORT_TRIANGLES_ONLY -* Draw shapes using only TRIANGLES, vertex are accumulated in TRIANGLES arrays -* * #define USE_DEFAULT_FONT_TEXTURE * Draw rectangle shapes using font texture white character instead of default white texture * Allows drawing rectangles and text with a single draw call, very useful for GUI systems! @@ -36,7 +30,7 @@ * **********************************************************************************************/ -#define USE_DEFAULT_FONT_TEXTURE +#include "config.h" #include "raylib.h" diff --git a/src/text.c b/src/text.c index d053be30..1a9d386a 100644 --- a/src/text.c +++ b/src/text.c @@ -6,7 +6,7 @@ * * #define SUPPORT_FILEFORMAT_FNT * #define SUPPORT_FILEFORMAT_TTF -* Selected desired fileformats to be supported for loading. Some of those formats are +* Selected desired fileformats to be supported for loading. Some of those formats are * supported by default, to remove support, just comment unrequired #define in this module * * #define SUPPORT_DEFAULT_FONT @@ -36,12 +36,7 @@ * **********************************************************************************************/ -// Default supported features -//------------------------------------- -#define SUPPORT_DEFAULT_FONT -#define SUPPORT_FILEFORMAT_FNT -#define SUPPORT_FILEFORMAT_TTF -//------------------------------------- +#include "config.h" #include "raylib.h" diff --git a/src/textures.c b/src/textures.c index 8a562887..43453f73 100644 --- a/src/textures.c +++ b/src/textures.c @@ -52,17 +52,7 @@ * 3. This notice may not be removed or altered from any source distribution. * **********************************************************************************************/ - -// Default configuration flags (supported features) -//------------------------------------------------- -#define SUPPORT_FILEFORMAT_PNG -#define SUPPORT_FILEFORMAT_DDS -#define SUPPORT_FILEFORMAT_HDR -#define SUPPORT_FILEFORMAT_KTX -#define SUPPORT_FILEFORMAT_ASTC -#define SUPPORT_IMAGE_MANIPULATION -#define SUPPORT_IMAGE_GENERATION -//------------------------------------------------- +#include "config.h" #include "raylib.h" diff --git a/src/utils.c b/src/utils.c index e37b4ff7..9d9d8b55 100644 --- a/src/utils.c +++ b/src/utils.c @@ -41,7 +41,7 @@ * **********************************************************************************************/ -#define SUPPORT_TRACELOG // Output tracelog messages +#include "config.h" #include "raylib.h" // WARNING: Required for: LogType enum #include "utils.h" diff --git a/src/utils.h b/src/utils.h index f4a1a01a..ed75eb68 100644 --- a/src/utils.h +++ b/src/utils.h @@ -32,7 +32,9 @@ #include // Required for: AAssetManager #endif -#define SUPPORT_SAVE_PNG +#ifndef SUPPORT_SAVE_PNG +#define SUPPORT_SAVE_PNG 1 +#endif //---------------------------------------------------------------------------------- // Some basic Defines @@ -74,4 +76,4 @@ FILE *android_fopen(const char *fileName, const char *mode); // Replacement f } #endif -#endif // UTILS_H \ No newline at end of file +#endif // UTILS_H -- cgit v1.2.3 From 847bdaf68287f70fbeb5599361257b6f982e48c5 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 19 Apr 2018 20:20:34 +0200 Subject: Implemented default mesh In case mesh loading fails, a cube is generated instead! --- src/models.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/models.c') diff --git a/src/models.c b/src/models.c index eadbf658..4793ee0a 100644 --- a/src/models.c +++ b/src/models.c @@ -631,11 +631,13 @@ Mesh LoadMesh(const char *fileName) TraceLog(LOG_WARNING, "[%s] Mesh fileformat not supported, it can't be loaded", fileName); #endif - if (mesh.vertexCount == 0) TraceLog(LOG_WARNING, "Mesh could not be loaded"); + if (mesh.vertexCount == 0) + { + TraceLog(LOG_WARNING, "Mesh could not be loaded! Let's load a cube to replace it!"); + mesh = GenMeshCube(1.0f, 1.0f, 1.0f); + } else rlLoadMesh(&mesh, false); // Upload vertex data to GPU (static mesh) - // TODO: Initialize default mesh data in case loading fails, maybe a cube? - return mesh; } -- cgit v1.2.3