1 | /******************************************************************************************* |
2 | * |
3 | * raylib [core] example - World to screen |
4 | * |
5 | * This example has been created using raylib 1.3 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Copyright (c) 2015 Ramon Santamaria (@raysan5) |
9 | * |
10 | ********************************************************************************************/ |
11 | |
12 | #include "raylib.h" |
13 | |
14 | int main(void) |
15 | { |
16 | // Initialization |
17 | //-------------------------------------------------------------------------------------- |
18 | const int screenWidth = 800; |
19 | const int screenHeight = 450; |
20 | |
21 | InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free" ); |
22 | |
23 | // Define the camera to look into our 3d world |
24 | Camera camera = { 0 }; |
25 | camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; |
26 | camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; |
27 | camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; |
28 | camera.fovy = 45.0f; |
29 | camera.type = CAMERA_PERSPECTIVE; |
30 | |
31 | Vector3 cubePosition = { 0.0f, 0.0f, 0.0f }; |
32 | Vector2 cubeScreenPosition = { 0.0f, 0.0f }; |
33 | |
34 | SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode |
35 | |
36 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
37 | //-------------------------------------------------------------------------------------- |
38 | |
39 | // Main game loop |
40 | while (!WindowShouldClose()) // Detect window close button or ESC key |
41 | { |
42 | // Update |
43 | //---------------------------------------------------------------------------------- |
44 | UpdateCamera(&camera); // Update camera |
45 | |
46 | // Calculate cube screen space position (with a little offset to be in top) |
47 | cubeScreenPosition = GetWorldToScreen((Vector3){cubePosition.x, cubePosition.y + 2.5f, cubePosition.z}, camera); |
48 | //---------------------------------------------------------------------------------- |
49 | |
50 | // Draw |
51 | //---------------------------------------------------------------------------------- |
52 | BeginDrawing(); |
53 | |
54 | ClearBackground(RAYWHITE); |
55 | |
56 | BeginMode3D(camera); |
57 | |
58 | DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED); |
59 | DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON); |
60 | |
61 | DrawGrid(10, 1.0f); |
62 | |
63 | EndMode3D(); |
64 | |
65 | DrawText("Enemy: 100 / 100" , cubeScreenPosition.x - MeasureText("Enemy: 100/100" , 20)/2, cubeScreenPosition.y, 20, BLACK); |
66 | DrawText("Text is always on top of the cube" , (screenWidth - MeasureText("Text is always on top of the cube" , 20))/2, 25, 20, GRAY); |
67 | |
68 | EndDrawing(); |
69 | //---------------------------------------------------------------------------------- |
70 | } |
71 | |
72 | // De-Initialization |
73 | //-------------------------------------------------------------------------------------- |
74 | CloseWindow(); // Close window and OpenGL context |
75 | //-------------------------------------------------------------------------------------- |
76 | |
77 | return 0; |
78 | } |