diff options
Diffstat (limited to 'docs/examples/web/models_ray_picking.c')
| -rw-r--r-- | docs/examples/web/models_ray_picking.c | 322 |
1 files changed, 182 insertions, 140 deletions
diff --git a/docs/examples/web/models_ray_picking.c b/docs/examples/web/models_ray_picking.c index c578a185..ba50c570 100644 --- a/docs/examples/web/models_ray_picking.c +++ b/docs/examples/web/models_ray_picking.c @@ -11,12 +11,51 @@ ********************************************************************************************/ #include "raylib.h" -#include "../src/raymath.h" +#include "raymath.h" #include <stdio.h> #include <float.h> +#if defined(PLATFORM_WEB) + #include <emscripten/emscripten.h> +#endif +//---------------------------------------------------------------------------------- +// Global Variables Definition +//---------------------------------------------------------------------------------- +const int screenWidth = 800; +const int screenHeight = 450; + +Camera camera; + +Vector3 cubePosition = { 0.0f, 1.0f, 0.0f }; +Vector3 cubeSize = { 2.0f, 2.0f, 2.0f }; + +Ray ray; // Picking line ray + +Model tower; +Texture2D texture; + +Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; +BoundingBox towerBBox; +bool hitMeshBBox = false; +bool hitTriangle = false; + +// Test triangle +Vector3 ta = (Vector3){ -25.0, 0.5, 0.0 }; +Vector3 tb = (Vector3){ -4.0, 2.5, 1.0 }; +Vector3 tc = (Vector3){ -8.0, 6.5, 0.0 }; + +Vector3 bary = { 0.0f, 0.0f, 0.0f }; + +//---------------------------------------------------------------------------------- +// Module Functions Declaration +//---------------------------------------------------------------------------------- +void UpdateDrawFrame(void); // Update and Draw one frame + +//---------------------------------------------------------------------------------- +// Main Enry Point +//---------------------------------------------------------------------------------- int main() { // Initialization @@ -27,171 +66,174 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [models] example - 3d ray picking"); // Define the camera to look into our 3d world - Camera camera; - camera.position = (Vector3){ 10.0f, 8.0f, 10.0f }; // Camera position - camera.target = (Vector3){ 0.0f, 2.3f, 0.0f }; // Camera looking at point - camera.up = (Vector3){ 0.0f, 1.6f, 0.0f }; // Camera up vector (rotation towards target) - camera.fovy = 45.0f; // Camera field-of-view Y - - Vector3 cubePosition = { 0.0f, 1.0f, 0.0f }; - Vector3 cubeSize = { 2.0f, 2.0f, 2.0f }; + camera.position = (Vector3){ 10.0f, 8.0f, 10.0f }; // Camera position + camera.target = (Vector3){ 0.0f, 2.3f, 0.0f }; // Camera looking at point + camera.up = (Vector3){ 0.0f, 1.6f, 0.0f }; // Camera up vector (rotation towards target) + camera.fovy = 45.0f; // Camera field-of-view Y + + tower = LoadModel("resources/model/lowpoly-tower.obj"); // Load OBJ model + texture = LoadTexture("resources/model/lowpoly-tower.png"); // Load model texture + tower.material.texDiffuse = texture; // Set model diffuse texture - Ray ray; // Picking line ray + towerBBox = CalculateBoundingBox(tower.mesh); + + SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- - Model tower = LoadModel("resources/model/lowpoly-tower.obj"); // Load OBJ model - Texture2D texture = LoadTexture("resources/model/lowpoly-tower.png"); // Load model texture - tower.material.texDiffuse = texture; // Set model diffuse texture +#if defined(PLATFORM_WEB) + emscripten_set_main_loop(UpdateDrawFrame, 0, 1); +#else + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- - Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position - BoundingBox towerBBox = CalculateBoundingBox( tower.mesh ); - bool hitMeshBBox = false; - bool hitTriangle = false; + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + UpdateDrawFrame(); + } +#endif - // Test triangle - Vector3 ta = (Vector3){ -25.0, 0.5, 0.0 }; - Vector3 tb = (Vector3){ -4.0, 2.5, 1.0 }; - Vector3 tc = (Vector3){ -8.0, 6.5, 0.0 }; + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadModel(tower); // Unload model from GPU + UnloadTexture(texture); // Unload texture from GPU + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- - Vector3 bary = { 0.0f, 0.0f, 0.0f }; + return 0; +} - SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode +//---------------------------------------------------------------------------------- +// Module Functions Definition +//---------------------------------------------------------------------------------- +void UpdateDrawFrame(void) +{ + // Update + //---------------------------------------------------------------------------------- + UpdateCamera(&camera); // Update camera + + // Display information about closest hit + RayHitInfo nearestHit; + char *hitObjectName = "None"; + nearestHit.distance = FLT_MAX; + nearestHit.hit = false; + Color cursorColor = WHITE; + + // Get ray and test against ground, triangle, and mesh + ray = GetMouseRay(GetMousePosition(), camera); + + // Check ray collision aginst ground plane + RayHitInfo groundHitInfo = GetCollisionRayGround(ray, 0.0f); + + if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance)) + { + nearestHit = groundHitInfo; + cursorColor = GREEN; + hitObjectName = "Ground"; + } - SetTargetFPS(60); // Set our game to run at 60 frames-per-second - //-------------------------------------------------------------------------------------- - // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + // Check ray collision against test triangle + RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, ta, tb, tc); + + if ((triHitInfo.hit) && (triHitInfo.distance < nearestHit.distance)) { - // Update - //---------------------------------------------------------------------------------- - UpdateCamera(&camera); // Update camera - - // Display information about closest hit - RayHitInfo nearestHit; - char *hitObjectName = "None"; - nearestHit.distance = FLT_MAX; - nearestHit.hit = false; - Color cursorColor = WHITE; - - // Get ray and test against ground, triangle, and mesh - ray = GetMouseRay(GetMousePosition(), camera); + nearestHit = triHitInfo; + cursorColor = PURPLE; + hitObjectName = "Triangle"; + + bary = Barycenter(nearestHit.hitPosition, ta, tb, tc); + hitTriangle = true; + } + else hitTriangle = false; + + RayHitInfo meshHitInfo; + + // Check ray collision against bounding box first, before trying the full ray-mesh test + if (CheckCollisionRayBox(ray, towerBBox)) + { + hitMeshBBox = true; - // Check ray collision aginst ground plane - RayHitInfo groundHitInfo = GetCollisionRayGround(ray, 0.0f); + // Check ray collision against mesh + meshHitInfo = GetCollisionRayMesh(ray, &tower.mesh); - if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance)) + if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance)) { - nearestHit = groundHitInfo; - cursorColor = GREEN; - hitObjectName = "Ground"; + nearestHit = meshHitInfo; + cursorColor = ORANGE; + hitObjectName = "Mesh"; } - - // Check ray collision against test triangle - RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, ta, tb, tc); - if ((triHitInfo.hit) && (triHitInfo.distance < nearestHit.distance)) - { - nearestHit = triHitInfo; - cursorColor = PURPLE; - hitObjectName = "Triangle"; + } hitMeshBBox = false; + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); - bary = Barycenter(nearestHit.hitPosition, ta, tb, tc); - hitTriangle = true; - } - else hitTriangle = false; + ClearBackground(RAYWHITE); - RayHitInfo meshHitInfo; + Begin3dMode(camera); - // Check ray collision against bounding box first, before trying the full ray-mesh test - if (CheckCollisionRayBox(ray, towerBBox)) - { - hitMeshBBox = true; + // Draw the tower + DrawModel(tower, towerPos, 1.0, WHITE); - // Check ray collision against mesh - meshHitInfo = GetCollisionRayMesh(ray, &tower.mesh); - - if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance)) - { - nearestHit = meshHitInfo; - cursorColor = ORANGE; - hitObjectName = "Mesh"; - } - - } hitMeshBBox = false; - //---------------------------------------------------------------------------------- - - // Draw - //---------------------------------------------------------------------------------- - BeginDrawing(); - - ClearBackground(RAYWHITE); + // Draw the test triangle + DrawLine3D(ta, tb, PURPLE); + DrawLine3D(tb, tc, PURPLE); + DrawLine3D(tc, ta, PURPLE); - Begin3dMode(camera); - - // Draw the tower - DrawModel(tower, towerPos, 1.0, WHITE); - - // Draw the test triangle - DrawLine3D(ta, tb, PURPLE); - DrawLine3D(tb, tc, PURPLE); - DrawLine3D(tc, ta, PURPLE); - - // Draw the mesh bbox if we hit it - if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME); - - // If we hit something, draw the cursor at the hit point - if (nearestHit.hit) - { - DrawCube(nearestHit.hitPosition, 0.5, 0.5, 0.5, cursorColor); - DrawCubeWires(nearestHit.hitPosition, 0.5, 0.5, 0.5, YELLOW); - - Vector3 normalEnd; - normalEnd.x = nearestHit.hitPosition.x + nearestHit.hitNormal.x; - normalEnd.y = nearestHit.hitPosition.y + nearestHit.hitNormal.y; - normalEnd.z = nearestHit.hitPosition.z + nearestHit.hitNormal.z; - - DrawLine3D(nearestHit.hitPosition, normalEnd, YELLOW); - } - - DrawRay(ray, MAROON); - - DrawGrid(100, 1.0f); - - End3dMode(); - - // Draw some debug GUI text - DrawText(FormatText("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK); + // Draw the mesh bbox if we hit it + if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME); + // If we hit something, draw the cursor at the hit point if (nearestHit.hit) { - int ypos = 70; + DrawCube(nearestHit.hitPosition, 0.5, 0.5, 0.5, cursorColor); + DrawCubeWires(nearestHit.hitPosition, 0.5, 0.5, 0.5, YELLOW); - DrawText(FormatText("Distance: %3.2f", nearestHit.distance), 10, ypos, 10, BLACK); + Vector3 normalEnd; + normalEnd.x = nearestHit.hitPosition.x + nearestHit.hitNormal.x; + normalEnd.y = nearestHit.hitPosition.y + nearestHit.hitNormal.y; + normalEnd.z = nearestHit.hitPosition.z + nearestHit.hitNormal.z; - DrawText(FormatText("Hit Pos: %3.2f %3.2f %3.2f", - nearestHit.hitPosition.x, - nearestHit.hitPosition.y, - nearestHit.hitPosition.z), 10, ypos + 15, 10, BLACK); - - DrawText(FormatText("Hit Norm: %3.2f %3.2f %3.2f", - nearestHit.hitNormal.x, - nearestHit.hitNormal.y, - nearestHit.hitNormal.z), 10, ypos + 30, 10, BLACK); - - if (hitTriangle) DrawText(FormatText("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK); + DrawLine3D(nearestHit.hitPosition, normalEnd, YELLOW); } - DrawText("Use Mouse to Move Camera", 10, 430, 10, GRAY); + DrawRay(ray, MAROON); + + DrawGrid(100, 1.0f); - DrawFPS(10, 10); + End3dMode(); + + // Draw some debug GUI text + DrawText(FormatText("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK); - EndDrawing(); - //---------------------------------------------------------------------------------- - } + if (nearestHit.hit) + { + int ypos = 70; - // De-Initialization - //-------------------------------------------------------------------------------------- - CloseWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- + DrawText(FormatText("Distance: %3.2f", nearestHit.distance), 10, ypos, 10, BLACK); + + DrawText(FormatText("Hit Pos: %3.2f %3.2f %3.2f", + nearestHit.hitPosition.x, + nearestHit.hitPosition.y, + nearestHit.hitPosition.z), 10, ypos + 15, 10, BLACK); + + DrawText(FormatText("Hit Norm: %3.2f %3.2f %3.2f", + nearestHit.hitNormal.x, + nearestHit.hitNormal.y, + nearestHit.hitNormal.z), 10, ypos + 30, 10, BLACK); + + if (hitTriangle) DrawText(FormatText("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK); + } - return 0; -}
\ No newline at end of file + DrawText("Use Mouse to Move Camera", 10, 430, 10, GRAY); + + DrawFPS(10, 10); + + EndDrawing(); + //---------------------------------------------------------------------------------- +} |
