1 | /******************************************************************************************* |
2 | * |
3 | * raylib [models] example - Heightmap loading and drawing |
4 | * |
5 | * This example has been created using raylib 1.8 (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 [models] example - heightmap loading and drawing" ); |
22 | |
23 | // Define our custom camera to look into our 3d world |
24 | Camera camera = { { 18.0f, 18.0f, 18.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; |
25 | |
26 | Image image = LoadImage("resources/heightmap.png" ); // Load heightmap image (RAM) |
27 | Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM) |
28 | |
29 | Mesh mesh = GenMeshHeightmap(image, (Vector3){ 16, 8, 16 }); // Generate heightmap mesh (RAM and VRAM) |
30 | Model model = LoadModelFromMesh(mesh); // Load model from generated mesh |
31 | |
32 | model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set map diffuse texture |
33 | Vector3 mapPosition = { -8.0f, 0.0f, -8.0f }; // Define model position |
34 | |
35 | UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM |
36 | |
37 | SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode |
38 | |
39 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
40 | //-------------------------------------------------------------------------------------- |
41 | |
42 | // Main game loop |
43 | while (!WindowShouldClose()) // Detect window close button or ESC key |
44 | { |
45 | // Update |
46 | //---------------------------------------------------------------------------------- |
47 | UpdateCamera(&camera); // Update camera |
48 | //---------------------------------------------------------------------------------- |
49 | |
50 | // Draw |
51 | //---------------------------------------------------------------------------------- |
52 | BeginDrawing(); |
53 | |
54 | ClearBackground(RAYWHITE); |
55 | |
56 | BeginMode3D(camera); |
57 | |
58 | DrawModel(model, mapPosition, 1.0f, RED); |
59 | |
60 | DrawGrid(20, 1.0f); |
61 | |
62 | EndMode3D(); |
63 | |
64 | DrawTexture(texture, screenWidth - texture.width - 20, 20, WHITE); |
65 | DrawRectangleLines(screenWidth - texture.width - 20, 20, texture.width, texture.height, GREEN); |
66 | |
67 | DrawFPS(10, 10); |
68 | |
69 | EndDrawing(); |
70 | //---------------------------------------------------------------------------------- |
71 | } |
72 | |
73 | // De-Initialization |
74 | //-------------------------------------------------------------------------------------- |
75 | UnloadTexture(texture); // Unload texture |
76 | UnloadModel(model); // Unload model |
77 | |
78 | CloseWindow(); // Close window and OpenGL context |
79 | //-------------------------------------------------------------------------------------- |
80 | |
81 | return 0; |
82 | } |