1 | /******************************************************************************************* |
2 | * |
3 | * raylib [shaders] example - Apply a postprocessing shader and connect a custom uniform variable |
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) 2015 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 - custom uniform variable" ); |
37 | |
38 | // Define the camera to look into our 3d world |
39 | Camera camera = { 0 }; |
40 | camera.position = (Vector3){ 8.0f, 8.0f, 8.0f }; |
41 | camera.target = (Vector3){ 0.0f, 1.5f, 0.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/barracks.obj" ); // Load OBJ model |
47 | Texture2D texture = LoadTexture("resources/models/barracks_diffuse.png" ); // Load model texture (diffuse map) |
48 | model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture |
49 | |
50 | Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position |
51 | |
52 | // Load postprocessing shader |
53 | // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader |
54 | Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/swirl.fs" , GLSL_VERSION)); |
55 | |
56 | // Get variable (uniform) location on the shader to connect with the program |
57 | // NOTE: If uniform variable could not be found in the shader, function returns -1 |
58 | int swirlCenterLoc = GetShaderLocation(shader, "center" ); |
59 | |
60 | float swirlCenter[2] = { (float)screenWidth/2, (float)screenHeight/2 }; |
61 | |
62 | // Create a RenderTexture2D to be used for render to texture |
63 | RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); |
64 | |
65 | // Setup orbital camera |
66 | SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode |
67 | |
68 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
69 | //-------------------------------------------------------------------------------------- |
70 | |
71 | // Main game loop |
72 | while (!WindowShouldClose()) // Detect window close button or ESC key |
73 | { |
74 | // Update |
75 | //---------------------------------------------------------------------------------- |
76 | Vector2 mousePosition = GetMousePosition(); |
77 | |
78 | swirlCenter[0] = mousePosition.x; |
79 | swirlCenter[1] = screenHeight - mousePosition.y; |
80 | |
81 | // Send new value to the shader to be used on drawing |
82 | SetShaderValue(shader, swirlCenterLoc, swirlCenter, UNIFORM_VEC2); |
83 | |
84 | UpdateCamera(&camera); // Update camera |
85 | //---------------------------------------------------------------------------------- |
86 | |
87 | // Draw |
88 | //---------------------------------------------------------------------------------- |
89 | BeginDrawing(); |
90 | |
91 | ClearBackground(RAYWHITE); |
92 | |
93 | BeginTextureMode(target); // Enable drawing to texture |
94 | |
95 | ClearBackground(RAYWHITE); // Clear texture background |
96 | |
97 | BeginMode3D(camera); // Begin 3d mode drawing |
98 | |
99 | DrawModel(model, position, 0.5f, WHITE); // Draw 3d model with texture |
100 | |
101 | DrawGrid(10, 1.0f); // Draw a grid |
102 | |
103 | EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode |
104 | |
105 | DrawText("TEXT DRAWN IN RENDER TEXTURE" , 200, 10, 30, RED); |
106 | |
107 | EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) |
108 | |
109 | BeginShaderMode(shader); |
110 | |
111 | // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) |
112 | DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); |
113 | |
114 | EndShaderMode(); |
115 | |
116 | // Draw some 2d text over drawn texture |
117 | DrawText("(c) Barracks 3D model by Alberto Cano" , screenWidth - 220, screenHeight - 20, 10, GRAY); |
118 | |
119 | DrawFPS(10, 10); |
120 | |
121 | EndDrawing(); |
122 | //---------------------------------------------------------------------------------- |
123 | } |
124 | |
125 | // De-Initialization |
126 | //-------------------------------------------------------------------------------------- |
127 | UnloadShader(shader); // Unload shader |
128 | UnloadTexture(texture); // Unload texture |
129 | UnloadModel(model); // Unload model |
130 | UnloadRenderTexture(target); // Unload render texture |
131 | |
132 | CloseWindow(); // Close window and OpenGL context |
133 | //-------------------------------------------------------------------------------------- |
134 | |
135 | return 0; |
136 | } |