diff options
| author | Joel Davis <[email protected]> | 2017-01-02 21:56:25 -0800 |
|---|---|---|
| committer | Joel Davis <[email protected]> | 2017-01-02 21:56:25 -0800 |
| commit | d5d391faaf69027b8fecb26f30754c3bff83c311 (patch) | |
| tree | 5129f65135b521e2825af5cc09b3d7e33a2ce59a /src/models.c | |
| parent | 037da8879a3ae61b09d8388bc2b4a2fe5359256a (diff) | |
| download | raylib-d5d391faaf69027b8fecb26f30754c3bff83c311.tar.gz raylib-d5d391faaf69027b8fecb26f30754c3bff83c311.zip | |
Added RaycastMesh function and example test case
Diffstat (limited to 'src/models.c')
| -rw-r--r-- | src/models.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/models.c b/src/models.c index a2043913..41e527dc 100644 --- a/src/models.c +++ b/src/models.c @@ -1918,3 +1918,41 @@ static Material LoadMTL(const char *fileName) return material; } + +RayHitInfo RaycastMesh( 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 = RaycastTriangle( ray, a, b, c ); + if (triHitInfo.hit) { + // Save the closest hit triangle + if ((!result.hit)||(result.distance > triHitInfo.distance)) { + result = triHitInfo; + } + } + } + + return result; +} |
