|
@@ -1,8 +1,8 @@
|
|
/*******************************************************************************************
|
|
/*******************************************************************************************
|
|
*
|
|
*
|
|
-* raylib [core] example - Ray-Picking in 3d mode, ground plane, triangle, mesh
|
|
|
|
|
|
+* raylib [models] example - Ray picking in 3d mode, ground plane, triangle, mesh
|
|
*
|
|
*
|
|
-* This example has been created using raylib 1.3 (www.raylib.com)
|
|
|
|
|
|
+* This example has been created using raylib 1.7 (www.raylib.com)
|
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
|
*
|
|
*
|
|
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
|
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
|
@@ -11,7 +11,7 @@
|
|
********************************************************************************************/
|
|
********************************************************************************************/
|
|
|
|
|
|
#include "raylib.h"
|
|
#include "raylib.h"
|
|
-#include "raymath.h"
|
|
|
|
|
|
+#include "../src/raymath.h"
|
|
|
|
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
#include <float.h>
|
|
#include <float.h>
|
|
@@ -24,7 +24,7 @@ int main()
|
|
int screenWidth = 800;
|
|
int screenWidth = 800;
|
|
int screenHeight = 450;
|
|
int screenHeight = 450;
|
|
|
|
|
|
- InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d ray picking");
|
|
|
|
|
|
+ InitWindow(screenWidth, screenHeight, "raylib [models] example - 3d ray picking");
|
|
|
|
|
|
// Define the camera to look into our 3d world
|
|
// Define the camera to look into our 3d world
|
|
Camera camera;
|
|
Camera camera;
|
|
@@ -41,22 +41,22 @@ int main()
|
|
Model tower = LoadModel("resources/model/lowpoly-tower.obj"); // Load OBJ model
|
|
Model tower = LoadModel("resources/model/lowpoly-tower.obj"); // Load OBJ model
|
|
Texture2D texture = LoadTexture("resources/model/lowpoly-tower.png"); // Load model texture
|
|
Texture2D texture = LoadTexture("resources/model/lowpoly-tower.png"); // Load model texture
|
|
tower.material.texDiffuse = texture; // Set model diffuse texture
|
|
tower.material.texDiffuse = texture; // Set model diffuse texture
|
|
|
|
+
|
|
Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position
|
|
Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position
|
|
BoundingBox towerBBox = CalculateBoundingBox( tower.mesh );
|
|
BoundingBox towerBBox = CalculateBoundingBox( tower.mesh );
|
|
- bool hitMeshBBox;
|
|
|
|
- bool hitTriangle;
|
|
|
|
|
|
+ bool hitMeshBBox = false;
|
|
|
|
+ bool hitTriangle = false;
|
|
|
|
|
|
// Test triangle
|
|
// Test triangle
|
|
Vector3 ta = (Vector3){ -25.0, 0.5, 0.0 };
|
|
Vector3 ta = (Vector3){ -25.0, 0.5, 0.0 };
|
|
Vector3 tb = (Vector3){ -4.0, 2.5, 1.0 };
|
|
Vector3 tb = (Vector3){ -4.0, 2.5, 1.0 };
|
|
Vector3 tc = (Vector3){ -8.0, 6.5, 0.0 };
|
|
Vector3 tc = (Vector3){ -8.0, 6.5, 0.0 };
|
|
|
|
|
|
- Vector3 bary = {0};
|
|
|
|
|
|
+ Vector3 bary = { 0.0f, 0.0f, 0.0f };
|
|
|
|
|
|
SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
|
|
SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
|
|
|
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
|
-
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
//--------------------------------------------------------------------------------------
|
|
// Main game loop
|
|
// Main game loop
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
@@ -65,7 +65,6 @@ int main()
|
|
//----------------------------------------------------------------------------------
|
|
//----------------------------------------------------------------------------------
|
|
UpdateCamera(&camera); // Update camera
|
|
UpdateCamera(&camera); // Update camera
|
|
|
|
|
|
-
|
|
|
|
// Display information about closest hit
|
|
// Display information about closest hit
|
|
RayHitInfo nearestHit;
|
|
RayHitInfo nearestHit;
|
|
char *hitObjectName = "None";
|
|
char *hitObjectName = "None";
|
|
@@ -76,41 +75,50 @@ int main()
|
|
// Get ray and test against ground, triangle, and mesh
|
|
// Get ray and test against ground, triangle, and mesh
|
|
ray = GetMouseRay(GetMousePosition(), camera);
|
|
ray = GetMouseRay(GetMousePosition(), camera);
|
|
|
|
|
|
- RayHitInfo groundHitInfo = RaycastGroundPlane( ray, 0.0 );
|
|
|
|
- if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance)) {
|
|
|
|
|
|
+ // Check ray collision aginst ground plane
|
|
|
|
+ RayHitInfo groundHitInfo = GetCollisionRayGround(ray, 0.0f);
|
|
|
|
+
|
|
|
|
+ if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance))
|
|
|
|
+ {
|
|
nearestHit = groundHitInfo;
|
|
nearestHit = groundHitInfo;
|
|
cursorColor = GREEN;
|
|
cursorColor = GREEN;
|
|
hitObjectName = "Ground";
|
|
hitObjectName = "Ground";
|
|
}
|
|
}
|
|
|
|
|
|
- RayHitInfo triHitInfo = RaycastTriangle( ray, ta, tb, tc );
|
|
|
|
- if ((triHitInfo.hit) && (triHitInfo.distance < nearestHit.distance)) {
|
|
|
|
|
|
+ // Check ray collision against test triangle
|
|
|
|
+ RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, ta, tb, tc);
|
|
|
|
+
|
|
|
|
+ if ((triHitInfo.hit) && (triHitInfo.distance < nearestHit.distance))
|
|
|
|
+ {
|
|
nearestHit = triHitInfo;
|
|
nearestHit = triHitInfo;
|
|
cursorColor = PURPLE;
|
|
cursorColor = PURPLE;
|
|
hitObjectName = "Triangle";
|
|
hitObjectName = "Triangle";
|
|
|
|
|
|
- bary = Barycentric( nearestHit.hitPosition, ta, tb, tc );
|
|
|
|
|
|
+ bary = Barycenter(nearestHit.hitPosition, ta, tb, tc);
|
|
hitTriangle = true;
|
|
hitTriangle = true;
|
|
- } else {
|
|
|
|
- hitTriangle = false;
|
|
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
+ else hitTriangle = false;
|
|
|
|
|
|
RayHitInfo meshHitInfo;
|
|
RayHitInfo meshHitInfo;
|
|
|
|
|
|
- // check the bounding box first, before trying the full ray/mesh test
|
|
|
|
- if (CheckCollisionRayBox( ray, towerBBox )) {
|
|
|
|
|
|
+ // Check ray collision against bounding box first, before trying the full ray-mesh test
|
|
|
|
+ if (CheckCollisionRayBox(ray, towerBBox))
|
|
|
|
+ {
|
|
hitMeshBBox = true;
|
|
hitMeshBBox = true;
|
|
- meshHitInfo = RaycastMesh( ray, &tower.mesh );
|
|
|
|
- if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance)) {
|
|
|
|
|
|
+
|
|
|
|
+ // Check ray collision against mesh
|
|
|
|
+ meshHitInfo = GetCollisionRayMesh(ray, &tower.mesh);
|
|
|
|
+
|
|
|
|
+ if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance))
|
|
|
|
+ {
|
|
nearestHit = meshHitInfo;
|
|
nearestHit = meshHitInfo;
|
|
cursorColor = ORANGE;
|
|
cursorColor = ORANGE;
|
|
hitObjectName = "Mesh";
|
|
hitObjectName = "Mesh";
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- hitMeshBBox = false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ } hitMeshBBox = false;
|
|
//----------------------------------------------------------------------------------
|
|
//----------------------------------------------------------------------------------
|
|
|
|
+
|
|
// Draw
|
|
// Draw
|
|
//----------------------------------------------------------------------------------
|
|
//----------------------------------------------------------------------------------
|
|
BeginDrawing();
|
|
BeginDrawing();
|
|
@@ -120,65 +128,59 @@ int main()
|
|
Begin3dMode(camera);
|
|
Begin3dMode(camera);
|
|
|
|
|
|
// Draw the tower
|
|
// Draw the tower
|
|
- DrawModel( tower, towerPos, 1.0, WHITE );
|
|
|
|
|
|
+ DrawModel(tower, towerPos, 1.0, WHITE);
|
|
|
|
|
|
// Draw the test triangle
|
|
// Draw the test triangle
|
|
- DrawLine3D( ta, tb, PURPLE );
|
|
|
|
- DrawLine3D( tb, tc, PURPLE );
|
|
|
|
- DrawLine3D( tc, ta, PURPLE );
|
|
|
|
|
|
+ DrawLine3D(ta, tb, PURPLE);
|
|
|
|
+ DrawLine3D(tb, tc, PURPLE);
|
|
|
|
+ DrawLine3D(tc, ta, PURPLE);
|
|
|
|
|
|
// Draw the mesh bbox if we hit it
|
|
// Draw the mesh bbox if we hit it
|
|
- if (hitMeshBBox) {
|
|
|
|
- DrawBoundingBox( towerBBox, LIME );
|
|
|
|
- }
|
|
|
|
|
|
+ if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME);
|
|
|
|
|
|
// If we hit something, draw the cursor at the hit point
|
|
// 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 );
|
|
|
|
|
|
+ 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;
|
|
Vector3 normalEnd;
|
|
normalEnd.x = nearestHit.hitPosition.x + nearestHit.hitNormal.x;
|
|
normalEnd.x = nearestHit.hitPosition.x + nearestHit.hitNormal.x;
|
|
normalEnd.y = nearestHit.hitPosition.y + nearestHit.hitNormal.y;
|
|
normalEnd.y = nearestHit.hitPosition.y + nearestHit.hitNormal.y;
|
|
normalEnd.z = nearestHit.hitPosition.z + nearestHit.hitNormal.z;
|
|
normalEnd.z = nearestHit.hitPosition.z + nearestHit.hitNormal.z;
|
|
- DrawLine3D( nearestHit.hitPosition, normalEnd, YELLOW );
|
|
|
|
|
|
+
|
|
|
|
+ DrawLine3D(nearestHit.hitPosition, normalEnd, YELLOW);
|
|
}
|
|
}
|
|
|
|
|
|
DrawRay(ray, MAROON);
|
|
DrawRay(ray, MAROON);
|
|
|
|
|
|
- DrawGrid(10, 1.0f);
|
|
|
|
|
|
+ DrawGrid(100, 1.0f);
|
|
|
|
|
|
End3dMode();
|
|
End3dMode();
|
|
|
|
|
|
- // Show some debug text
|
|
|
|
- char line[1024];
|
|
|
|
- sprintf( line, "Hit Object: %s\n", hitObjectName );
|
|
|
|
- DrawText( line, 10, 30, 15, BLACK );
|
|
|
|
-
|
|
|
|
- if (nearestHit.hit) {
|
|
|
|
- int ypos = 45;
|
|
|
|
- sprintf( line, "Distance: %3.2f", nearestHit.distance );
|
|
|
|
- DrawText( line, 10, ypos, 15, BLACK );
|
|
|
|
- ypos += 15;
|
|
|
|
-
|
|
|
|
- sprintf( line, "Hit Pos: %3.2f %3.2f %3.2f",
|
|
|
|
- nearestHit.hitPosition.x, nearestHit.hitPosition.y, nearestHit.hitPosition.z );
|
|
|
|
- DrawText( line, 10, ypos, 15, BLACK );
|
|
|
|
- ypos += 15;
|
|
|
|
-
|
|
|
|
- sprintf( line, "Hit Norm: %3.2f %3.2f %3.2f",
|
|
|
|
- nearestHit.hitNormal.x, nearestHit.hitNormal.y, nearestHit.hitNormal.z );
|
|
|
|
- DrawText( line, 10, ypos, 15, BLACK );
|
|
|
|
- ypos += 15;
|
|
|
|
-
|
|
|
|
- if (hitTriangle) {
|
|
|
|
- sprintf( line, "Barycentric: %3.2f %3.2f %3.2f",
|
|
|
|
- bary.x, bary.y, bary.z );
|
|
|
|
- DrawText( line, 10, ypos, 15, BLACK );
|
|
|
|
- }
|
|
|
|
|
|
+ // Draw some debug GUI text
|
|
|
|
+ DrawText(FormatText("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK);
|
|
|
|
+
|
|
|
|
+ if (nearestHit.hit)
|
|
|
|
+ {
|
|
|
|
+ int ypos = 70;
|
|
|
|
+
|
|
|
|
+ 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);
|
|
}
|
|
}
|
|
|
|
|
|
- DrawText( "Use Mouse to Move Camera", 10, 420, 15, LIGHTGRAY );
|
|
|
|
|
|
+ DrawText("Use Mouse to Move Camera", 10, 430, 10, GRAY);
|
|
|
|
|
|
DrawFPS(10, 10);
|
|
DrawFPS(10, 10);
|
|
|
|
|