1 | /******************************************************************************************* |
2 | * |
3 | * raylib [shaders] example - Apply a shader to a 3d model |
4 | * |
5 | * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, |
6 | * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. |
7 | * |
8 | * NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example |
9 | * on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders |
10 | * raylib comes with shaders ready for both versions, check raylib/shaders install folder |
11 | * |
12 | * This example has been created using raylib 1.3 (www.raylib.com) |
13 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
14 | * |
15 | * Copyright (c) 2014 Ramon Santamaria (@raysan5) |
16 | * |
17 | ********************************************************************************************/ |
18 | |
19 | #include "raylib.h" |
20 | |
21 | #if defined(PLATFORM_DESKTOP) |
22 | #define GLSL_VERSION 330 |
23 | #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB |
24 | #define GLSL_VERSION 100 |
25 | #endif |
26 | |
27 | int main(void) |
28 | { |
29 | // Initialization |
30 | //-------------------------------------------------------------------------------------- |
31 | const int screenWidth = 800; |
32 | const int screenHeight = 450; |
33 | |
34 | SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) |
35 | |
36 | InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader" ); |
37 | |
38 | // Define the camera to look into our 3d world |
39 | Camera camera = { 0 }; |
40 | camera.position = (Vector3){ 4.0f, 4.0f, 4.0f }; |
41 | camera.target = (Vector3){ 0.0f, 1.0f, -1.0f }; |
42 | camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; |
43 | camera.fovy = 45.0f; |
44 | camera.type = CAMERA_PERSPECTIVE; |
45 | |
46 | Model model = LoadModel("resources/models/watermill.obj" ); // Load OBJ model |
47 | Texture2D texture = LoadTexture("resources/models/watermill_diffuse.png" ); // Load model texture |
48 | |
49 | // Load shader for model |
50 | // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader |
51 | Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/grayscale.fs" , GLSL_VERSION)); |
52 | |
53 | model.materials[0].shader = shader; // Set shader effect to 3d model |
54 | model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Bind texture to model |
55 | |
56 | Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position |
57 | |
58 | SetCameraMode(camera, CAMERA_FREE); // Set an orbital camera mode |
59 | |
60 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
61 | //-------------------------------------------------------------------------------------- |
62 | |
63 | // Main game loop |
64 | while (!WindowShouldClose()) // Detect window close button or ESC key |
65 | { |
66 | // Update |
67 | //---------------------------------------------------------------------------------- |
68 | UpdateCamera(&camera); // Update camera |
69 | //---------------------------------------------------------------------------------- |
70 | |
71 | // Draw |
72 | //---------------------------------------------------------------------------------- |
73 | BeginDrawing(); |
74 | |
75 | ClearBackground(RAYWHITE); |
76 | |
77 | BeginMode3D(camera); |
78 | |
79 | DrawModel(model, position, 0.2f, WHITE); // Draw 3d model with texture |
80 | |
81 | DrawGrid(10, 1.0f); // Draw a grid |
82 | |
83 | EndMode3D(); |
84 | |
85 | DrawText("(c) Watermill 3D model by Alberto Cano" , screenWidth - 210, screenHeight - 20, 10, GRAY); |
86 | |
87 | DrawFPS(10, 10); |
88 | |
89 | EndDrawing(); |
90 | //---------------------------------------------------------------------------------- |
91 | } |
92 | |
93 | // De-Initialization |
94 | //-------------------------------------------------------------------------------------- |
95 | UnloadShader(shader); // Unload shader |
96 | UnloadTexture(texture); // Unload texture |
97 | UnloadModel(model); // Unload model |
98 | |
99 | CloseWindow(); // Close window and OpenGL context |
100 | //-------------------------------------------------------------------------------------- |
101 | |
102 | return 0; |
103 | } |