1 | /******************************************************************************************* |
2 | * |
3 | * raylib [models] example - Cubicmap 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 - cubesmap loading and drawing" ); |
22 | |
23 | // Define the camera to look into our 3d world |
24 | Camera camera = { { 16.0f, 14.0f, 16.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; |
25 | |
26 | Image image = LoadImage("resources/cubicmap.png" ); // Load cubicmap image (RAM) |
27 | Texture2D cubicmap = LoadTextureFromImage(image); // Convert image to texture to display (VRAM) |
28 | |
29 | Mesh mesh = GenMeshCubicmap(image, (Vector3){ 1.0f, 1.0f, 1.0f }); |
30 | Model model = LoadModelFromMesh(mesh); |
31 | |
32 | // NOTE: By default each cube is mapped to one part of texture atlas |
33 | Texture2D texture = LoadTexture("resources/cubicmap_atlas.png" ); // Load map texture |
34 | model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set map diffuse texture |
35 | |
36 | Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position |
37 | |
38 | UnloadImage(image); // Unload cubesmap image from RAM, already uploaded to VRAM |
39 | |
40 | SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode |
41 | |
42 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
43 | //-------------------------------------------------------------------------------------- |
44 | |
45 | // Main game loop |
46 | while (!WindowShouldClose()) // Detect window close button or ESC key |
47 | { |
48 | // Update |
49 | //---------------------------------------------------------------------------------- |
50 | UpdateCamera(&camera); // Update camera |
51 | //---------------------------------------------------------------------------------- |
52 | |
53 | // Draw |
54 | //---------------------------------------------------------------------------------- |
55 | BeginDrawing(); |
56 | |
57 | ClearBackground(RAYWHITE); |
58 | |
59 | BeginMode3D(camera); |
60 | |
61 | DrawModel(model, mapPosition, 1.0f, WHITE); |
62 | |
63 | EndMode3D(); |
64 | |
65 | DrawTextureEx(cubicmap, (Vector2){ screenWidth - cubicmap.width*4 - 20, 20 }, 0.0f, 4.0f, WHITE); |
66 | DrawRectangleLines(screenWidth - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN); |
67 | |
68 | DrawText("cubicmap image used to" , 658, 90, 10, GRAY); |
69 | DrawText("generate map 3d model" , 658, 104, 10, GRAY); |
70 | |
71 | DrawFPS(10, 10); |
72 | |
73 | EndDrawing(); |
74 | //---------------------------------------------------------------------------------- |
75 | } |
76 | |
77 | // De-Initialization |
78 | //-------------------------------------------------------------------------------------- |
79 | UnloadTexture(cubicmap); // Unload cubicmap texture |
80 | UnloadTexture(texture); // Unload map texture |
81 | UnloadModel(model); // Unload map model |
82 | |
83 | CloseWindow(); // Close window and OpenGL context |
84 | //-------------------------------------------------------------------------------------- |
85 | |
86 | return 0; |
87 | } |
88 | |