1/*******************************************************************************************
2*
3* raylib [shaders] example - Apply a postprocessing shader to a scene
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#define MAX_POSTPRO_SHADERS 12
28
29typedef enum {
30 FX_GRAYSCALE = 0,
31 FX_POSTERIZATION,
32 FX_DREAM_VISION,
33 FX_PIXELIZER,
34 FX_CROSS_HATCHING,
35 FX_CROSS_STITCHING,
36 FX_PREDATOR_VIEW,
37 FX_SCANLINES,
38 FX_FISHEYE,
39 FX_SOBEL,
40 FX_BLOOM,
41 FX_BLUR,
42 //FX_FXAA
43} PostproShader;
44
45static const char *postproShaderText[] = {
46 "GRAYSCALE",
47 "POSTERIZATION",
48 "DREAM_VISION",
49 "PIXELIZER",
50 "CROSS_HATCHING",
51 "CROSS_STITCHING",
52 "PREDATOR_VIEW",
53 "SCANLINES",
54 "FISHEYE",
55 "SOBEL",
56 "BLOOM",
57 "BLUR",
58 //"FXAA"
59};
60
61int main(void)
62{
63 // Initialization
64 //--------------------------------------------------------------------------------------
65 const int screenWidth = 800;
66 const int screenHeight = 450;
67
68 SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
69
70 InitWindow(screenWidth, screenHeight, "raylib [shaders] example - postprocessing shader");
71
72 // Define the camera to look into our 3d world
73 Camera camera = { { 2.0f, 3.0f, 2.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
74
75 Model model = LoadModel("resources/models/church.obj"); // Load OBJ model
76 Texture2D texture = LoadTexture("resources/models/church_diffuse.png"); // Load model texture (diffuse map)
77 model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture
78
79 Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
80
81 // Load all postpro shaders
82 // NOTE 1: All postpro shader use the base vertex shader (DEFAULT_VERTEX_SHADER)
83 // NOTE 2: We load the correct shader depending on GLSL version
84 Shader shaders[MAX_POSTPRO_SHADERS] = { 0 };
85
86 // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
87 shaders[FX_GRAYSCALE] = LoadShader(0, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION));
88 shaders[FX_POSTERIZATION] = LoadShader(0, FormatText("resources/shaders/glsl%i/posterization.fs", GLSL_VERSION));
89 shaders[FX_DREAM_VISION] = LoadShader(0, FormatText("resources/shaders/glsl%i/dream_vision.fs", GLSL_VERSION));
90 shaders[FX_PIXELIZER] = LoadShader(0, FormatText("resources/shaders/glsl%i/pixelizer.fs", GLSL_VERSION));
91 shaders[FX_CROSS_HATCHING] = LoadShader(0, FormatText("resources/shaders/glsl%i/cross_hatching.fs", GLSL_VERSION));
92 shaders[FX_CROSS_STITCHING] = LoadShader(0, FormatText("resources/shaders/glsl%i/cross_stitching.fs", GLSL_VERSION));
93 shaders[FX_PREDATOR_VIEW] = LoadShader(0, FormatText("resources/shaders/glsl%i/predator.fs", GLSL_VERSION));
94 shaders[FX_SCANLINES] = LoadShader(0, FormatText("resources/shaders/glsl%i/scanlines.fs", GLSL_VERSION));
95 shaders[FX_FISHEYE] = LoadShader(0, FormatText("resources/shaders/glsl%i/fisheye.fs", GLSL_VERSION));
96 shaders[FX_SOBEL] = LoadShader(0, FormatText("resources/shaders/glsl%i/sobel.fs", GLSL_VERSION));
97 shaders[FX_BLOOM] = LoadShader(0, FormatText("resources/shaders/glsl%i/bloom.fs", GLSL_VERSION));
98 shaders[FX_BLUR] = LoadShader(0, FormatText("resources/shaders/glsl%i/blur.fs", GLSL_VERSION));
99
100 int currentShader = FX_GRAYSCALE;
101
102 // Create a RenderTexture2D to be used for render to texture
103 RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
104
105 // Setup orbital camera
106 SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
107
108 SetTargetFPS(60); // Set our game to run at 60 frames-per-second
109 //--------------------------------------------------------------------------------------
110
111 // Main game loop
112 while (!WindowShouldClose()) // Detect window close button or ESC key
113 {
114 // Update
115 //----------------------------------------------------------------------------------
116 UpdateCamera(&camera); // Update camera
117
118 if (IsKeyPressed(KEY_RIGHT)) currentShader++;
119 else if (IsKeyPressed(KEY_LEFT)) currentShader--;
120
121 if (currentShader >= MAX_POSTPRO_SHADERS) currentShader = 0;
122 else if (currentShader < 0) currentShader = MAX_POSTPRO_SHADERS - 1;
123 //----------------------------------------------------------------------------------
124
125 // Draw
126 //----------------------------------------------------------------------------------
127 BeginDrawing();
128
129 ClearBackground(RAYWHITE);
130
131 BeginTextureMode(target); // Enable drawing to texture
132
133 ClearBackground(RAYWHITE); // Clear texture background
134
135 BeginMode3D(camera); // Begin 3d mode drawing
136
137 DrawModel(model, position, 0.1f, WHITE); // Draw 3d model with texture
138
139 DrawGrid(10, 1.0f); // Draw a grid
140
141 EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode
142
143 EndTextureMode(); // End drawing to texture (now we have a texture available for next passes)
144
145 // Render previously generated texture using selected postpro shader
146 BeginShaderMode(shaders[currentShader]);
147
148 // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
149 DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE);
150
151 EndShaderMode();
152
153 // Draw 2d shapes and text over drawn texture
154 DrawRectangle(0, 9, 580, 30, Fade(LIGHTGRAY, 0.7f));
155
156 DrawText("(c) Church 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
157
158 DrawText("CURRENT POSTPRO SHADER:", 10, 15, 20, BLACK);
159 DrawText(postproShaderText[currentShader], 330, 15, 20, RED);
160 DrawText("< >", 540, 10, 30, DARKBLUE);
161
162 DrawFPS(700, 15);
163
164 EndDrawing();
165 //----------------------------------------------------------------------------------
166 }
167
168 // De-Initialization
169 //--------------------------------------------------------------------------------------
170
171 // Unload all postpro shaders
172 for (int i = 0; i < MAX_POSTPRO_SHADERS; i++) UnloadShader(shaders[i]);
173
174 UnloadTexture(texture); // Unload texture
175 UnloadModel(model); // Unload model
176 UnloadRenderTexture(target); // Unload render texture
177
178 CloseWindow(); // Close window and OpenGL context
179 //--------------------------------------------------------------------------------------
180
181 return 0;
182}
183