1 | /******************************************************************************************* |
2 | * |
3 | * raylib example - procedural mesh generation |
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) 2017 Ramon Santamaria (Ray San) |
9 | * |
10 | ********************************************************************************************/ |
11 | |
12 | #include "raylib.h" |
13 | |
14 | #define NUM_MODELS 8 // Parametric 3d shapes to generate |
15 | |
16 | int main(void) |
17 | { |
18 | // Initialization |
19 | //-------------------------------------------------------------------------------------- |
20 | const int screenWidth = 800; |
21 | const int screenHeight = 450; |
22 | |
23 | InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation" ); |
24 | |
25 | // We generate a checked image for texturing |
26 | Image checked = GenImageChecked(2, 2, 1, 1, RED, GREEN); |
27 | Texture2D texture = LoadTextureFromImage(checked); |
28 | UnloadImage(checked); |
29 | |
30 | Model models[NUM_MODELS] = { 0 }; |
31 | |
32 | models[0] = LoadModelFromMesh(GenMeshPlane(2, 2, 5, 5)); |
33 | models[1] = LoadModelFromMesh(GenMeshCube(2.0f, 1.0f, 2.0f)); |
34 | models[2] = LoadModelFromMesh(GenMeshSphere(2, 32, 32)); |
35 | models[3] = LoadModelFromMesh(GenMeshHemiSphere(2, 16, 16)); |
36 | models[4] = LoadModelFromMesh(GenMeshCylinder(1, 2, 16)); |
37 | models[5] = LoadModelFromMesh(GenMeshTorus(0.25f, 4.0f, 16, 32)); |
38 | models[6] = LoadModelFromMesh(GenMeshKnot(1.0f, 2.0f, 16, 128)); |
39 | models[7] = LoadModelFromMesh(GenMeshPoly(5, 2.0f)); |
40 | |
41 | // Set checked texture as default diffuse component for all models material |
42 | for (int i = 0; i < NUM_MODELS; i++) models[i].materials[0].maps[MAP_DIFFUSE].texture = texture; |
43 | |
44 | // Define the camera to look into our 3d world |
45 | Camera camera = { { 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; |
46 | |
47 | // Model drawing position |
48 | Vector3 position = { 0.0f, 0.0f, 0.0f }; |
49 | |
50 | int currentModel = 0; |
51 | |
52 | SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode |
53 | |
54 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
55 | //-------------------------------------------------------------------------------------- |
56 | |
57 | // Main game loop |
58 | while (!WindowShouldClose()) // Detect window close button or ESC key |
59 | { |
60 | // Update |
61 | //---------------------------------------------------------------------------------- |
62 | UpdateCamera(&camera); // Update internal camera and our camera |
63 | |
64 | if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) |
65 | { |
66 | currentModel = (currentModel + 1)%NUM_MODELS; // Cycle between the textures |
67 | } |
68 | |
69 | if (IsKeyPressed(KEY_RIGHT)) |
70 | { |
71 | currentModel++; |
72 | if (currentModel >= NUM_MODELS) currentModel = 0; |
73 | } |
74 | else if (IsKeyPressed(KEY_LEFT)) |
75 | { |
76 | currentModel--; |
77 | if (currentModel < 0) currentModel = NUM_MODELS - 1; |
78 | } |
79 | //---------------------------------------------------------------------------------- |
80 | |
81 | // Draw |
82 | //---------------------------------------------------------------------------------- |
83 | BeginDrawing(); |
84 | |
85 | ClearBackground(RAYWHITE); |
86 | |
87 | BeginMode3D(camera); |
88 | |
89 | DrawModel(models[currentModel], position, 1.0f, WHITE); |
90 | |
91 | DrawGrid(10, 1.0); |
92 | |
93 | EndMode3D(); |
94 | |
95 | DrawRectangle(30, 400, 310, 30, Fade(SKYBLUE, 0.5f)); |
96 | DrawRectangleLines(30, 400, 310, 30, Fade(DARKBLUE, 0.5f)); |
97 | DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL MODELS" , 40, 410, 10, BLUE); |
98 | |
99 | switch(currentModel) |
100 | { |
101 | case 0: DrawText("PLANE" , 680, 10, 20, DARKBLUE); break; |
102 | case 1: DrawText("CUBE" , 680, 10, 20, DARKBLUE); break; |
103 | case 2: DrawText("SPHERE" , 680, 10, 20, DARKBLUE); break; |
104 | case 3: DrawText("HEMISPHERE" , 640, 10, 20, DARKBLUE); break; |
105 | case 4: DrawText("CYLINDER" , 680, 10, 20, DARKBLUE); break; |
106 | case 5: DrawText("TORUS" , 680, 10, 20, DARKBLUE); break; |
107 | case 6: DrawText("KNOT" , 680, 10, 20, DARKBLUE); break; |
108 | case 7: DrawText("POLY" , 680, 10, 20, DARKBLUE); break; |
109 | default: break; |
110 | } |
111 | |
112 | EndDrawing(); |
113 | //---------------------------------------------------------------------------------- |
114 | } |
115 | |
116 | // De-Initialization |
117 | //-------------------------------------------------------------------------------------- |
118 | UnloadTexture(texture); // Unload texture |
119 | |
120 | // Unload models data (GPU VRAM) |
121 | for (int i = 0; i < NUM_MODELS; i++) UnloadModel(models[i]); |
122 | |
123 | CloseWindow(); // Close window and OpenGL context |
124 | //-------------------------------------------------------------------------------------- |
125 | |
126 | return 0; |
127 | } |