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 ++---- 1 file changed, 2 insertions(+), 4 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 -- 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 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 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; } } -- 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. --- src/models.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/models.c') 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 } //---------------------------------------------------------------------------------- -- 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 --- 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 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; -- 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/models.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) (limited to 'src/models.c') 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)); } } -- 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 --- src/models.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src/models.c') 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) -- 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 --- src/models.c | 43 ------------------------------------------- 1 file changed, 43 deletions(-) (limited to 'src/models.c') 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 }; -- 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/models.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'src/models.c') 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" -- 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