2017-01-01 02:06:39 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
2017-04-16 20:08:19 +03:00
|
|
|
* raylib [models] example - Mesh picking in 3d mode, ground plane, triangle, mesh
|
2017-01-01 02:06:39 +03:00
|
|
|
*
|
2017-01-05 21:33:05 +03:00
|
|
|
* This example has been created using raylib 1.7 (www.raylib.com)
|
2017-01-01 02:06:39 +03:00
|
|
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
2017-01-03 08:56:25 +03:00
|
|
|
* Example contributed by Joel Davis (@joeld42)
|
2017-01-01 02:06:39 +03:00
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
2017-04-09 00:31:58 +03:00
|
|
|
#include "raymath.h"
|
2017-01-03 08:56:25 +03:00
|
|
|
|
2018-07-30 14:02:20 +03:00
|
|
|
#define FLT_MAX 340282346638528859811704183484516925440.0 // Maximum value of a float, from bit pattern 01111111011111111111111111111111
|
2017-01-01 02:06:39 +03:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
int screenWidth = 800;
|
|
|
|
int screenHeight = 450;
|
|
|
|
|
2017-04-16 20:08:19 +03:00
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh picking");
|
2017-01-01 02:06:39 +03:00
|
|
|
|
|
|
|
// Define the camera to look into our 3d world
|
|
|
|
Camera camera;
|
2018-06-30 21:02:32 +03:00
|
|
|
camera.position = (Vector3){ 20.0f, 20.0f, 20.0f }; // Camera position
|
|
|
|
camera.target = (Vector3){ 0.0f, 8.0f, 0.0f }; // Camera looking at point
|
2017-01-03 08:56:25 +03:00
|
|
|
camera.up = (Vector3){ 0.0f, 1.6f, 0.0f }; // Camera up vector (rotation towards target)
|
2017-01-01 02:06:39 +03:00
|
|
|
camera.fovy = 45.0f; // Camera field-of-view Y
|
2018-05-11 19:14:51 +03:00
|
|
|
camera.type = CAMERA_PERSPECTIVE; // Camera mode type
|
2017-01-01 02:06:39 +03:00
|
|
|
|
2017-04-16 20:08:19 +03:00
|
|
|
Ray ray; // Picking ray
|
2017-01-01 02:06:39 +03:00
|
|
|
|
2018-06-30 21:02:32 +03:00
|
|
|
Model tower = LoadModel("resources/models/turret.obj"); // Load OBJ model
|
|
|
|
Texture2D texture = LoadTexture("resources/models/turret_diffuse.png"); // Load model texture
|
|
|
|
tower.material.maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture
|
2017-01-05 21:33:05 +03:00
|
|
|
|
2017-04-09 00:31:58 +03:00
|
|
|
Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position
|
2018-02-26 14:10:45 +03:00
|
|
|
BoundingBox towerBBox = MeshBoundingBox(tower.mesh); // Get mesh bounding box
|
2017-01-05 21:33:05 +03:00
|
|
|
bool hitMeshBBox = false;
|
|
|
|
bool hitTriangle = false;
|
2017-01-03 08:56:25 +03:00
|
|
|
|
|
|
|
// 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 };
|
|
|
|
|
2017-01-05 21:33:05 +03:00
|
|
|
Vector3 bary = { 0.0f, 0.0f, 0.0f };
|
2017-01-03 08:56:25 +03:00
|
|
|
|
2017-01-01 02:06:39 +03:00
|
|
|
SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
|
|
|
|
|
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
2017-01-03 08:56:25 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
2017-01-01 02:06:39 +03:00
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
UpdateCamera(&camera); // Update camera
|
|
|
|
|
2017-01-03 08:56:25 +03:00
|
|
|
// 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
|
2017-01-01 02:06:39 +03:00
|
|
|
ray = GetMouseRay(GetMousePosition(), camera);
|
2017-01-03 08:56:25 +03:00
|
|
|
|
2017-01-05 21:33:05 +03:00
|
|
|
// Check ray collision aginst ground plane
|
|
|
|
RayHitInfo groundHitInfo = GetCollisionRayGround(ray, 0.0f);
|
|
|
|
|
|
|
|
if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance))
|
|
|
|
{
|
2017-01-03 08:56:25 +03:00
|
|
|
nearestHit = groundHitInfo;
|
|
|
|
cursorColor = GREEN;
|
|
|
|
hitObjectName = "Ground";
|
|
|
|
}
|
|
|
|
|
2017-01-05 21:33:05 +03:00
|
|
|
// Check ray collision against test triangle
|
|
|
|
RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, ta, tb, tc);
|
|
|
|
|
|
|
|
if ((triHitInfo.hit) && (triHitInfo.distance < nearestHit.distance))
|
|
|
|
{
|
2017-01-03 08:56:25 +03:00
|
|
|
nearestHit = triHitInfo;
|
|
|
|
cursorColor = PURPLE;
|
|
|
|
hitObjectName = "Triangle";
|
|
|
|
|
2017-10-14 01:11:37 +03:00
|
|
|
bary = Vector3Barycenter(nearestHit.position, ta, tb, tc);
|
2017-01-03 08:56:25 +03:00
|
|
|
hitTriangle = true;
|
2017-01-05 21:33:05 +03:00
|
|
|
}
|
|
|
|
else hitTriangle = false;
|
2017-01-03 08:56:25 +03:00
|
|
|
|
|
|
|
RayHitInfo meshHitInfo;
|
|
|
|
|
2017-01-05 21:33:05 +03:00
|
|
|
// Check ray collision against bounding box first, before trying the full ray-mesh test
|
|
|
|
if (CheckCollisionRayBox(ray, towerBBox))
|
|
|
|
{
|
2017-01-03 08:56:25 +03:00
|
|
|
hitMeshBBox = true;
|
2017-01-05 21:33:05 +03:00
|
|
|
|
2018-04-07 11:00:06 +03:00
|
|
|
// Check ray collision against model
|
2018-06-30 21:02:32 +03:00
|
|
|
// NOTE: It considers model.transform matrix!
|
2018-04-07 11:00:06 +03:00
|
|
|
meshHitInfo = GetCollisionRayModel(ray, &tower);
|
2017-01-05 21:33:05 +03:00
|
|
|
|
|
|
|
if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance))
|
|
|
|
{
|
2017-01-03 08:56:25 +03:00
|
|
|
nearestHit = meshHitInfo;
|
|
|
|
cursorColor = ORANGE;
|
|
|
|
hitObjectName = "Mesh";
|
|
|
|
}
|
2017-01-05 21:33:05 +03:00
|
|
|
|
|
|
|
} hitMeshBBox = false;
|
2017-01-01 02:06:39 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
2017-01-05 21:33:05 +03:00
|
|
|
|
2017-01-01 02:06:39 +03:00
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
|
|
|
|
|
|
|
ClearBackground(RAYWHITE);
|
|
|
|
|
2018-05-04 17:54:05 +03:00
|
|
|
BeginMode3D(camera);
|
2017-01-01 02:06:39 +03:00
|
|
|
|
2017-01-03 08:56:25 +03:00
|
|
|
// Draw the tower
|
2018-06-30 21:02:32 +03:00
|
|
|
// WARNING: If scale is different than 1.0f,
|
|
|
|
// not considered by GetCollisionRayModel()
|
|
|
|
DrawModel(tower, towerPos, 1.0f, WHITE);
|
2017-01-03 08:56:25 +03:00
|
|
|
|
|
|
|
// Draw the test triangle
|
2017-01-05 21:33:05 +03:00
|
|
|
DrawLine3D(ta, tb, PURPLE);
|
|
|
|
DrawLine3D(tb, tc, PURPLE);
|
|
|
|
DrawLine3D(tc, ta, PURPLE);
|
2017-01-03 08:56:25 +03:00
|
|
|
|
|
|
|
// Draw the mesh bbox if we hit it
|
2017-01-05 21:33:05 +03:00
|
|
|
if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME);
|
2017-01-01 02:06:39 +03:00
|
|
|
|
2017-01-03 08:56:25 +03:00
|
|
|
// If we hit something, draw the cursor at the hit point
|
2017-01-05 21:33:05 +03:00
|
|
|
if (nearestHit.hit)
|
|
|
|
{
|
2017-05-16 16:23:01 +03:00
|
|
|
DrawCube(nearestHit.position, 0.3, 0.3, 0.3, cursorColor);
|
|
|
|
DrawCubeWires(nearestHit.position, 0.3, 0.3, 0.3, RED);
|
2017-01-01 02:06:39 +03:00
|
|
|
|
2017-01-03 08:56:25 +03:00
|
|
|
Vector3 normalEnd;
|
2017-05-16 16:23:01 +03:00
|
|
|
normalEnd.x = nearestHit.position.x + nearestHit.normal.x;
|
|
|
|
normalEnd.y = nearestHit.position.y + nearestHit.normal.y;
|
|
|
|
normalEnd.z = nearestHit.position.z + nearestHit.normal.z;
|
2017-01-05 21:33:05 +03:00
|
|
|
|
2017-05-16 16:23:01 +03:00
|
|
|
DrawLine3D(nearestHit.position, normalEnd, RED);
|
2017-01-01 02:06:39 +03:00
|
|
|
}
|
2017-01-03 08:56:25 +03:00
|
|
|
|
2017-01-01 02:06:39 +03:00
|
|
|
DrawRay(ray, MAROON);
|
|
|
|
|
2018-06-30 21:02:32 +03:00
|
|
|
DrawGrid(10, 10.0f);
|
2017-01-01 02:06:39 +03:00
|
|
|
|
2018-05-04 17:54:05 +03:00
|
|
|
EndMode3D();
|
2017-01-01 02:06:39 +03:00
|
|
|
|
2017-01-05 21:33:05 +03:00
|
|
|
// 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",
|
2017-05-16 16:23:01 +03:00
|
|
|
nearestHit.position.x,
|
|
|
|
nearestHit.position.y,
|
|
|
|
nearestHit.position.z), 10, ypos + 15, 10, BLACK);
|
2017-01-05 21:33:05 +03:00
|
|
|
|
|
|
|
DrawText(FormatText("Hit Norm: %3.2f %3.2f %3.2f",
|
2017-05-16 16:23:01 +03:00
|
|
|
nearestHit.normal.x,
|
|
|
|
nearestHit.normal.y,
|
|
|
|
nearestHit.normal.z), 10, ypos + 30, 10, BLACK);
|
2017-01-05 21:33:05 +03:00
|
|
|
|
|
|
|
if (hitTriangle) DrawText(FormatText("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK);
|
2017-01-03 08:56:25 +03:00
|
|
|
}
|
|
|
|
|
2017-01-05 21:33:05 +03:00
|
|
|
DrawText("Use Mouse to Move Camera", 10, 430, 10, GRAY);
|
2018-06-30 21:02:32 +03:00
|
|
|
|
|
|
|
DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
|
2017-01-01 02:06:39 +03:00
|
|
|
|
|
|
|
DrawFPS(10, 10);
|
|
|
|
|
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2017-04-09 00:31:58 +03:00
|
|
|
UnloadModel(tower); // Unload model
|
|
|
|
UnloadTexture(texture); // Unload texture
|
|
|
|
|
|
|
|
CloseWindow(); // Close window and OpenGL context
|
2017-01-01 02:06:39 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|