1 | /******************************************************************************************* |
2 | * |
3 | * raylib [models] example - Mesh picking in 3d mode, ground plane, triangle, mesh |
4 | * |
5 | * This example has been created using raylib 1.7 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Example contributed by Joel Davis (@joeld42) and reviewed by Ramon Santamaria (@raysan5) |
9 | * |
10 | * Copyright (c) 2017 Joel Davis (@joeld42) and Ramon Santamaria (@raysan5) |
11 | * |
12 | ********************************************************************************************/ |
13 | |
14 | #include "raylib.h" |
15 | #include "raymath.h" |
16 | |
17 | #define FLT_MAX 340282346638528859811704183484516925440.0f // Maximum value of a float, from bit pattern 01111111011111111111111111111111 |
18 | |
19 | int main(void) |
20 | { |
21 | // Initialization |
22 | //-------------------------------------------------------------------------------------- |
23 | const int screenWidth = 800; |
24 | const int screenHeight = 450; |
25 | |
26 | InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh picking" ); |
27 | |
28 | // Define the camera to look into our 3d world |
29 | Camera camera = { 0 }; |
30 | camera.position = (Vector3){ 20.0f, 20.0f, 20.0f }; // Camera position |
31 | camera.target = (Vector3){ 0.0f, 8.0f, 0.0f }; // Camera looking at point |
32 | camera.up = (Vector3){ 0.0f, 1.6f, 0.0f }; // Camera up vector (rotation towards target) |
33 | camera.fovy = 45.0f; // Camera field-of-view Y |
34 | camera.type = CAMERA_PERSPECTIVE; // Camera mode type |
35 | |
36 | Ray ray = { 0 }; // Picking ray |
37 | |
38 | Model tower = LoadModel("resources/models/turret.obj" ); // Load OBJ model |
39 | Texture2D texture = LoadTexture("resources/models/turret_diffuse.png" ); // Load model texture |
40 | tower.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture |
41 | |
42 | Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position |
43 | BoundingBox towerBBox = MeshBoundingBox(tower.meshes[0]); // Get mesh bounding box |
44 | bool hitMeshBBox = false; |
45 | bool hitTriangle = false; |
46 | |
47 | // Test triangle |
48 | Vector3 ta = (Vector3){ -25.0, 0.5, 0.0 }; |
49 | Vector3 tb = (Vector3){ -4.0, 2.5, 1.0 }; |
50 | Vector3 tc = (Vector3){ -8.0, 6.5, 0.0 }; |
51 | |
52 | Vector3 bary = { 0.0f, 0.0f, 0.0f }; |
53 | |
54 | SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode |
55 | |
56 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
57 | //-------------------------------------------------------------------------------------- |
58 | // Main game loop |
59 | while (!WindowShouldClose()) // Detect window close button or ESC key |
60 | { |
61 | // Update |
62 | //---------------------------------------------------------------------------------- |
63 | UpdateCamera(&camera); // Update camera |
64 | |
65 | // Display information about closest hit |
66 | RayHitInfo nearestHit = { 0 }; |
67 | char *hitObjectName = "None" ; |
68 | nearestHit.distance = FLT_MAX; |
69 | nearestHit.hit = false; |
70 | Color cursorColor = WHITE; |
71 | |
72 | // Get ray and test against ground, triangle, and mesh |
73 | ray = GetMouseRay(GetMousePosition(), camera); |
74 | |
75 | // Check ray collision aginst ground plane |
76 | RayHitInfo groundHitInfo = GetCollisionRayGround(ray, 0.0f); |
77 | |
78 | if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance)) |
79 | { |
80 | nearestHit = groundHitInfo; |
81 | cursorColor = GREEN; |
82 | hitObjectName = "Ground" ; |
83 | } |
84 | |
85 | // Check ray collision against test triangle |
86 | RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, ta, tb, tc); |
87 | |
88 | if ((triHitInfo.hit) && (triHitInfo.distance < nearestHit.distance)) |
89 | { |
90 | nearestHit = triHitInfo; |
91 | cursorColor = PURPLE; |
92 | hitObjectName = "Triangle" ; |
93 | |
94 | bary = Vector3Barycenter(nearestHit.position, ta, tb, tc); |
95 | hitTriangle = true; |
96 | } |
97 | else hitTriangle = false; |
98 | |
99 | RayHitInfo meshHitInfo = { 0 }; |
100 | |
101 | // Check ray collision against bounding box first, before trying the full ray-mesh test |
102 | if (CheckCollisionRayBox(ray, towerBBox)) |
103 | { |
104 | hitMeshBBox = true; |
105 | |
106 | // Check ray collision against model |
107 | // NOTE: It considers model.transform matrix! |
108 | meshHitInfo = GetCollisionRayModel(ray, tower); |
109 | |
110 | if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance)) |
111 | { |
112 | nearestHit = meshHitInfo; |
113 | cursorColor = ORANGE; |
114 | hitObjectName = "Mesh" ; |
115 | } |
116 | } |
117 | |
118 | hitMeshBBox = false; |
119 | //---------------------------------------------------------------------------------- |
120 | |
121 | // Draw |
122 | //---------------------------------------------------------------------------------- |
123 | BeginDrawing(); |
124 | |
125 | ClearBackground(RAYWHITE); |
126 | |
127 | BeginMode3D(camera); |
128 | |
129 | // Draw the tower |
130 | // WARNING: If scale is different than 1.0f, |
131 | // not considered by GetCollisionRayModel() |
132 | DrawModel(tower, towerPos, 1.0f, WHITE); |
133 | |
134 | // Draw the test triangle |
135 | DrawLine3D(ta, tb, PURPLE); |
136 | DrawLine3D(tb, tc, PURPLE); |
137 | DrawLine3D(tc, ta, PURPLE); |
138 | |
139 | // Draw the mesh bbox if we hit it |
140 | if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME); |
141 | |
142 | // If we hit something, draw the cursor at the hit point |
143 | if (nearestHit.hit) |
144 | { |
145 | DrawCube(nearestHit.position, 0.3, 0.3, 0.3, cursorColor); |
146 | DrawCubeWires(nearestHit.position, 0.3, 0.3, 0.3, RED); |
147 | |
148 | Vector3 normalEnd; |
149 | normalEnd.x = nearestHit.position.x + nearestHit.normal.x; |
150 | normalEnd.y = nearestHit.position.y + nearestHit.normal.y; |
151 | normalEnd.z = nearestHit.position.z + nearestHit.normal.z; |
152 | |
153 | DrawLine3D(nearestHit.position, normalEnd, RED); |
154 | } |
155 | |
156 | DrawRay(ray, MAROON); |
157 | |
158 | DrawGrid(10, 10.0f); |
159 | |
160 | EndMode3D(); |
161 | |
162 | // Draw some debug GUI text |
163 | DrawText(FormatText("Hit Object: %s" , hitObjectName), 10, 50, 10, BLACK); |
164 | |
165 | if (nearestHit.hit) |
166 | { |
167 | int ypos = 70; |
168 | |
169 | DrawText(FormatText("Distance: %3.2f" , nearestHit.distance), 10, ypos, 10, BLACK); |
170 | |
171 | DrawText(FormatText("Hit Pos: %3.2f %3.2f %3.2f" , |
172 | nearestHit.position.x, |
173 | nearestHit.position.y, |
174 | nearestHit.position.z), 10, ypos + 15, 10, BLACK); |
175 | |
176 | DrawText(FormatText("Hit Norm: %3.2f %3.2f %3.2f" , |
177 | nearestHit.normal.x, |
178 | nearestHit.normal.y, |
179 | nearestHit.normal.z), 10, ypos + 30, 10, BLACK); |
180 | |
181 | if (hitTriangle) DrawText(FormatText("Barycenter: %3.2f %3.2f %3.2f" , bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK); |
182 | } |
183 | |
184 | DrawText("Use Mouse to Move Camera" , 10, 430, 10, GRAY); |
185 | |
186 | DrawText("(c) Turret 3D model by Alberto Cano" , screenWidth - 200, screenHeight - 20, 10, GRAY); |
187 | |
188 | DrawFPS(10, 10); |
189 | |
190 | EndDrawing(); |
191 | //---------------------------------------------------------------------------------- |
192 | } |
193 | |
194 | // De-Initialization |
195 | //-------------------------------------------------------------------------------------- |
196 | UnloadModel(tower); // Unload model |
197 | UnloadTexture(texture); // Unload texture |
198 | |
199 | CloseWindow(); // Close window and OpenGL context |
200 | //-------------------------------------------------------------------------------------- |
201 | |
202 | return 0; |
203 | } |