diff options
| author | Ray <[email protected]> | 2019-05-14 17:57:45 +0200 |
|---|---|---|
| committer | Ray <[email protected]> | 2019-05-14 17:57:45 +0200 |
| commit | 5a27bcaf7115e46a5123e9857007d940998f0650 (patch) | |
| tree | 7179c6a1b4d700c9685dd51cc76cb2b2c90b9609 /examples/src/shaders | |
| parent | 423fdd699225ee9686c44a606bf83272b4c77802 (diff) | |
| download | raylib.com-5a27bcaf7115e46a5123e9857007d940998f0650.tar.gz raylib.com-5a27bcaf7115e46a5123e9857007d940998f0650.zip | |
Review examples collection -WIP-
WARNING: Examples list has been reviewed but examples haven't been recompiled yet... that's not trivial...
Diffstat (limited to 'examples/src/shaders')
| -rw-r--r-- | examples/src/shaders/shaders_custom_uniform.c | 51 | ||||
| -rw-r--r-- | examples/src/shaders/shaders_julia_set.c | 179 | ||||
| -rw-r--r-- | examples/src/shaders/shaders_model_shader.c | 43 | ||||
| -rw-r--r-- | examples/src/shaders/shaders_palette_switch.c | 147 | ||||
| -rw-r--r-- | examples/src/shaders/shaders_postprocessing.c | 56 | ||||
| -rw-r--r-- | examples/src/shaders/shaders_raymarching.c | 112 | ||||
| -rw-r--r-- | examples/src/shaders/shaders_shapes_textures.c | 13 | ||||
| -rw-r--r-- | examples/src/shaders/shaders_texture_drawing.c | 79 | ||||
| -rw-r--r-- | examples/src/shaders/shaders_texture_waves.c | 115 |
9 files changed, 732 insertions, 63 deletions
diff --git a/examples/src/shaders/shaders_custom_uniform.c b/examples/src/shaders/shaders_custom_uniform.c index a0f6fd2..74b9e77 100644 --- a/examples/src/shaders/shaders_custom_uniform.c +++ b/examples/src/shaders/shaders_custom_uniform.c @@ -18,6 +18,12 @@ #include "raylib.h" +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + int main() { // Initialization @@ -30,16 +36,22 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [shaders] example - custom uniform variable"); // Define the camera to look into our 3d world - Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; - - Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model - Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture (diffuse map) - dwarf.material.maps[MAP_DIFFUSE].texture = texture; // Set dwarf model diffuse texture - - Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position + Camera camera = { 0 }; + camera.position = (Vector3){ 8.0f, 8.0f, 8.0f }; + camera.target = (Vector3){ 0.0f, 1.5f, 0.0f }; + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; + camera.fovy = 45.0f; + camera.type = CAMERA_PERSPECTIVE; + + Model model = LoadModel("resources/models/barracks.obj"); // Load OBJ model + Texture2D texture = LoadTexture("resources/models/barracks_diffuse.png"); // Load model texture (diffuse map) + model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture + + Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position - Shader shader = LoadShader("resources/shaders/glsl330/base.vs", - "resources/shaders/glsl330/swirl.fs"); // Load postpro shader + // Load postprocessing shader + // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader + Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/swirl.fs", GLSL_VERSION)); // Get variable (uniform) location on the shader to connect with the program // NOTE: If uniform variable could not be found in the shader, function returns -1 @@ -67,7 +79,7 @@ int main() swirlCenter[1] = screenHeight - mousePosition.y; // Send new value to the shader to be used on drawing - SetShaderValue(shader, swirlCenterLoc, swirlCenter, 2); + SetShaderValue(shader, swirlCenterLoc, swirlCenter, UNIFORM_VEC2); UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- @@ -78,19 +90,21 @@ int main() ClearBackground(RAYWHITE); - BeginTextureMode(target); // Enable drawing to texture + BeginTextureMode(target); // Enable drawing to texture + + ClearBackground(RAYWHITE); // Clear texture background - Begin3dMode(camera); + BeginMode3D(camera); // Begin 3d mode drawing - DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture + DrawModel(model, position, 0.5f, WHITE); // Draw 3d model with texture DrawGrid(10, 1.0f); // Draw a grid - End3dMode(); + EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED); - EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) + EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) BeginShaderMode(shader); @@ -99,8 +113,9 @@ int main() EndShaderMode(); - DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY); - + // Draw some 2d text over drawn texture + DrawText("(c) Barracks 3D model by Alberto Cano", screenWidth - 220, screenHeight - 20, 10, GRAY); + DrawFPS(10, 10); EndDrawing(); @@ -111,7 +126,7 @@ int main() //-------------------------------------------------------------------------------------- UnloadShader(shader); // Unload shader UnloadTexture(texture); // Unload texture - UnloadModel(dwarf); // Unload model + UnloadModel(model); // Unload model UnloadRenderTexture(target); // Unload render texture CloseWindow(); // Close window and OpenGL context diff --git a/examples/src/shaders/shaders_julia_set.c b/examples/src/shaders/shaders_julia_set.c new file mode 100644 index 0000000..c4dac6d --- /dev/null +++ b/examples/src/shaders/shaders_julia_set.c @@ -0,0 +1,179 @@ +/******************************************************************************************* +* +* raylib [shaders] example - julia sets +* +* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, +* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. +* +* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3). +* +* This example has been created using raylib 2.5 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Example contributed by eggmund (@eggmund) and reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2019 eggmund (@eggmund) and Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#include "raymath.h" + +// A few good julia sets +const float POINTS_OF_INTEREST[6][2] = +{ + { -0.348827, 0.607167 }, + { -0.786268, 0.169728 }, + { -0.8, 0.156 }, + { 0.285, 0.0 }, + { -0.835, -0.2321 }, + { -0.70176, -0.3842 }, +}; + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - julia sets"); + + // Load julia set shader + // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader + Shader shader = LoadShader(0, "resources/shaders/glsl330/julia_shader.fs"); + + // c constant to use in z^2 + c + float c[2] = { POINTS_OF_INTEREST[0][0], POINTS_OF_INTEREST[0][1] }; + + // Offset and zoom to draw the julia set at. (centered on screen and 1.6 times smaller) + float offset[2] = { -(float)screenWidth/2, -(float)screenHeight/2 }; + float zoom = 1.6f; + + // Get variable (uniform) locations on the shader to connect with the program + // NOTE: If uniform variable could not be found in the shader, function returns -1 + int cLoc = GetShaderLocation(shader, "c"); + int zoomLoc = GetShaderLocation(shader, "zoom"); + int offsetLoc = GetShaderLocation(shader, "offset"); + + // Tell the shader what the screen dimensions, zoom, offset and c are + float screenDims[2] = { (float)screenWidth, (float)screenHeight }; + SetShaderValue(shader, GetShaderLocation(shader, "screenDims"), screenDims, UNIFORM_VEC2); + + SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); + SetShaderValue(shader, zoomLoc, &zoom, UNIFORM_FLOAT); + SetShaderValue(shader, offsetLoc, offset, UNIFORM_VEC2); + + // Create a RenderTexture2D to be used for render to texture + RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); + + int incrementSpeed = 3; // Multiplier of speed to change c value + bool showControls = true; // Show controls + bool pause = false; // Pause animation + + SetTargetFPS(60); // Set the window to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + + // Press [1 - 6] to reset c to a point of interest + if (IsKeyPressed(KEY_ONE) || + IsKeyPressed(KEY_TWO) || + IsKeyPressed(KEY_THREE) || + IsKeyPressed(KEY_FOUR) || + IsKeyPressed(KEY_FIVE) || + IsKeyPressed(KEY_SIX)) + { + if (IsKeyPressed(KEY_ONE)) c[0] = POINTS_OF_INTEREST[0][0], c[1] = POINTS_OF_INTEREST[0][1]; + else if (IsKeyPressed(KEY_TWO)) c[0] = POINTS_OF_INTEREST[1][0], c[1] = POINTS_OF_INTEREST[1][1]; + else if (IsKeyPressed(KEY_THREE)) c[0] = POINTS_OF_INTEREST[2][0], c[1] = POINTS_OF_INTEREST[2][1]; + else if (IsKeyPressed(KEY_FOUR)) c[0] = POINTS_OF_INTEREST[3][0], c[1] = POINTS_OF_INTEREST[3][1]; + else if (IsKeyPressed(KEY_FIVE)) c[0] = POINTS_OF_INTEREST[4][0], c[1] = POINTS_OF_INTEREST[4][1]; + else if (IsKeyPressed(KEY_SIX)) c[0] = POINTS_OF_INTEREST[5][0], c[1] = POINTS_OF_INTEREST[5][1]; + + SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); + } + + if (IsKeyPressed(KEY_P)) pause = !pause; // Pause animation (c change) + if (IsKeyPressed(KEY_F1)) showControls = !showControls; // Toggle whether or not to show controls + + if (!pause) + { + if (IsKeyDown(KEY_RIGHT)) incrementSpeed++; + else if (IsKeyDown(KEY_LEFT)) incrementSpeed--; + + // Use mouse wheel to change zoom + zoom -= (float)GetMouseWheelMove()/10.f; + SetShaderValue(shader, zoomLoc, &zoom, UNIFORM_FLOAT); + + // Use mouse button to change offset + if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) + { + // TODO: Logic is not correct, the idea is getting zoom focus to pointed area + Vector2 mousePos = GetMousePosition(); + + offset[0] = mousePos.x -(float)screenWidth; + offset[1] = mousePos.y -(float)screenHeight; + + SetShaderValue(shader, offsetLoc, offset, UNIFORM_VEC2); + } + + // Increment c value with time + float amount = GetFrameTime()*incrementSpeed*0.0005f; + c[0] += amount; + c[1] += amount; + + SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(BLACK); // Clear the screen of the previous frame. + + // Using a render texture to draw Julia set + BeginTextureMode(target); // Enable drawing to texture + ClearBackground(BLACK); // Clear the render texture + + // Draw a rectangle in shader mode + // NOTE: This acts as a canvas for the shader to draw on + BeginShaderMode(shader); + DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); + EndShaderMode(); + EndTextureMode(); + + // Draw the saved texture (rendered julia set) + DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, Vector2Zero(), WHITE); + + // Draw information + //DrawText( FormatText("cx: %f\ncy: %f\nspeed: %d", c[0], c[1], incrementSpeed), 10, 10, 10, RAYWHITE); + + if (showControls) + { + DrawText("Press keys [1 - 6] to change point of interest", 10, GetScreenHeight() - 60, 10, RAYWHITE); + DrawText("Press KEY_LEFT | KEY_RIGHT to change speed", 10, GetScreenHeight() - 45, 10, RAYWHITE); + DrawText("Press KEY_P to pause movement animation", 10, GetScreenHeight() - 30, 10, RAYWHITE); + DrawText("Press KEY_F1 to toggle these controls", 10, GetScreenHeight() - 15, 10, RAYWHITE); + } + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(shader); // Unload shader + UnloadRenderTexture(target); // Unload render texture + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/src/shaders/shaders_model_shader.c b/examples/src/shaders/shaders_model_shader.c index b0ad1fd..2717c19 100644 --- a/examples/src/shaders/shaders_model_shader.c +++ b/examples/src/shaders/shaders_model_shader.c @@ -12,12 +12,18 @@ * This example has been created using raylib 1.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2014-2017 Ramon Santamaria (@raysan5) +* Copyright (c) 2014 Ramon Santamaria (@raysan5) * ********************************************************************************************/ #include "raylib.h" +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + int main() { // Initialization @@ -30,19 +36,26 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader"); // Define the camera to look into our 3d world - Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; - - Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model - Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture - Shader shader = LoadShader("resources/shaders/glsl330/base.vs", - "resources/shaders/glsl330/grayscale.fs"); // Load model shader + Camera camera = { 0 }; + camera.position = (Vector3){ 4.0f, 4.0f, 4.0f }; + camera.target = (Vector3){ 0.0f, 1.0f, -1.0f }; + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; + camera.fovy = 45.0f; + camera.type = CAMERA_PERSPECTIVE; + + Model model = LoadModel("resources/models/watermill.obj"); // Load OBJ model + Texture2D texture = LoadTexture("resources/models/watermill_diffuse.png"); // Load model texture + + // Load shader for model + // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader + Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION)); - dwarf.material.shader = shader; // Set shader effect to 3d model - dwarf.material.maps[MAP_DIFFUSE].texture = texture; // Bind texture to model + model.materials[0].shader = shader; // Set shader effect to 3d model + model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Bind texture to model Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position - SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode + SetCameraMode(camera, CAMERA_FREE); // Set an orbital camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -61,15 +74,15 @@ int main() ClearBackground(RAYWHITE); - Begin3dMode(camera); + BeginMode3D(camera); - DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture + DrawModel(model, position, 0.2f, WHITE); // Draw 3d model with texture DrawGrid(10, 1.0f); // Draw a grid - End3dMode(); + EndMode3D(); - DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY); + DrawText("(c) Watermill 3D model by Alberto Cano", screenWidth - 210, screenHeight - 20, 10, GRAY); DrawText(FormatText("Camera position: (%.2f, %.2f, %.2f)", camera.position.x, camera.position.y, camera.position.z), 600, 20, 10, BLACK); DrawText(FormatText("Camera target: (%.2f, %.2f, %.2f)", camera.target.x, camera.target.y, camera.target.z), 600, 40, 10, GRAY); @@ -84,7 +97,7 @@ int main() //-------------------------------------------------------------------------------------- UnloadShader(shader); // Unload shader UnloadTexture(texture); // Unload texture - UnloadModel(dwarf); // Unload model + UnloadModel(model); // Unload model CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- diff --git a/examples/src/shaders/shaders_palette_switch.c b/examples/src/shaders/shaders_palette_switch.c new file mode 100644 index 0000000..197ae54 --- /dev/null +++ b/examples/src/shaders/shaders_palette_switch.c @@ -0,0 +1,147 @@ +/******************************************************************************************* +* +* raylib [shaders] example - Color palette switch +* +* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, +* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. +* +* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example +* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders +* raylib comes with shaders ready for both versions, check raylib/shaders install folder +* +* This example has been created using raylib 2.3 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Example contributed by Marco Lizza (@MarcoLizza) and reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2019 Marco Lizza (@MarcoLizza) and Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + +#define MAX_PALETTES 3 +#define COLORS_PER_PALETTE 8 +#define VALUES_PER_COLOR 3 + +static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE*VALUES_PER_COLOR] = { + { // 3-BIT RGB + 0, 0, 0, + 255, 0, 0, + 0, 255, 0, + 0, 0, 255, + 0, 255, 255, + 255, 0, 255, + 255, 255, 0, + 255, 255, 255, + }, + { // AMMO-8 (GameBoy-like) + 4, 12, 6, + 17, 35, 24, + 30, 58, 41, + 48, 93, 66, + 77, 128, 97, + 137, 162, 87, + 190, 220, 127, + 238, 255, 204, + }, + { // RKBV (2-strip film) + 21, 25, 26, + 138, 76, 88, + 217, 98, 117, + 230, 184, 193, + 69, 107, 115, + 75, 151, 166, + 165, 189, 194, + 255, 245, 247, + } +}; + +static const char *paletteText[] = { + "3-BIT RGB", + "AMMO-8 (GameBoy-like)", + "RKBV (2-strip film)" +}; + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - color palette switch"); + + // Load shader to be used on some parts drawing + // NOTE 1: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version + // NOTE 2: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader + Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/palette-switch.fs", GLSL_VERSION)); + + // Get variable (uniform) location on the shader to connect with the program + // NOTE: If uniform variable could not be found in the shader, function returns -1 + int paletteLoc = GetShaderLocation(shader, "palette"); + + int currentPalette = 0; + int lineHeight = screenHeight/COLORS_PER_PALETTE; + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + if (IsKeyPressed(KEY_RIGHT)) currentPalette++; + else if (IsKeyPressed(KEY_LEFT)) currentPalette--; + + if (currentPalette >= MAX_PALETTES) currentPalette = 0; + else if (currentPalette < 0) currentPalette = MAX_PALETTES - 1; + + // Send new value to the shader to be used on drawing. + // NOTE: We are sending RGB triplets w/o the alpha channel + SetShaderValueV(shader, paletteLoc, palettes[currentPalette], UNIFORM_IVEC3, COLORS_PER_PALETTE); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + BeginShaderMode(shader); + + for (int i = 0; i < COLORS_PER_PALETTE; i++) + { + // Draw horizontal screen-wide rectangles with increasing "palette index" + // The used palette index is encoded in the RGB components of the pixel + DrawRectangle(0, lineHeight*i, GetScreenWidth(), lineHeight, (Color){ i, i, i, 255 }); + } + + EndShaderMode(); + + DrawText("< >", 10, 10, 30, DARKBLUE); + DrawText("CURRENT PALETTE:", 60, 15, 20, RAYWHITE); + DrawText(paletteText[currentPalette], 300, 15, 20, RED); + + DrawFPS(700, 15); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(shader); // Unload shader + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/src/shaders/shaders_postprocessing.c b/examples/src/shaders/shaders_postprocessing.c index 4aac5f9..7c14641 100644 --- a/examples/src/shaders/shaders_postprocessing.c +++ b/examples/src/shaders/shaders_postprocessing.c @@ -20,10 +20,8 @@ #if defined(PLATFORM_DESKTOP) #define GLSL_VERSION 330 - #define DEFAULT_VERTEX_SHADER "resources/shaders/glsl330/base.vs" #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB #define GLSL_VERSION 100 - #define DEFAULT_VERTEX_SHADER "resources/shaders/glsl100/base.vs" #endif #define MAX_POSTPRO_SHADERS 12 @@ -72,31 +70,32 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [shaders] example - postprocessing shader"); // Define the camera to look into our 3d world - Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; + Camera camera = {{ 2.0f, 3.0f, 2.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; - Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model - Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture (diffuse map) - dwarf.material.maps[MAP_DIFFUSE].texture = texture; // Set dwarf model diffuse texture + Model model = LoadModel("resources/models/church.obj"); // Load OBJ model + Texture2D texture = LoadTexture("resources/models/church_diffuse.png"); // Load model texture (diffuse map) + model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture - Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position + Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position // Load all postpro shaders // NOTE 1: All postpro shader use the base vertex shader (DEFAULT_VERTEX_SHADER) // NOTE 2: We load the correct shader depending on GLSL version Shader shaders[MAX_POSTPRO_SHADERS]; - shaders[FX_GRAYSCALE] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION)); - shaders[FX_POSTERIZATION] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/posterization.fs", GLSL_VERSION)); - shaders[FX_DREAM_VISION] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/dream_vision.fs", GLSL_VERSION)); - shaders[FX_PIXELIZER] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/pixelizer.fs", GLSL_VERSION)); - shaders[FX_CROSS_HATCHING] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/cross_hatching.fs", GLSL_VERSION)); - shaders[FX_CROSS_STITCHING] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/cross_stitching.fs", GLSL_VERSION)); - shaders[FX_PREDATOR_VIEW] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/predator.fs", GLSL_VERSION)); - shaders[FX_SCANLINES] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/scanlines.fs", GLSL_VERSION)); - shaders[FX_FISHEYE] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/fisheye.fs", GLSL_VERSION)); - shaders[FX_SOBEL] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/sobel.fs", GLSL_VERSION)); - shaders[FX_BLOOM] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/bloom.fs", GLSL_VERSION)); - shaders[FX_BLUR] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/blur.fs", GLSL_VERSION)); + // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader + shaders[FX_GRAYSCALE] = LoadShader(0, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION)); + shaders[FX_POSTERIZATION] = LoadShader(0, FormatText("resources/shaders/glsl%i/posterization.fs", GLSL_VERSION)); + shaders[FX_DREAM_VISION] = LoadShader(0, FormatText("resources/shaders/glsl%i/dream_vision.fs", GLSL_VERSION)); + shaders[FX_PIXELIZER] = LoadShader(0, FormatText("resources/shaders/glsl%i/pixelizer.fs", GLSL_VERSION)); + shaders[FX_CROSS_HATCHING] = LoadShader(0, FormatText("resources/shaders/glsl%i/cross_hatching.fs", GLSL_VERSION)); + shaders[FX_CROSS_STITCHING] = LoadShader(0, FormatText("resources/shaders/glsl%i/cross_stitching.fs", GLSL_VERSION)); + shaders[FX_PREDATOR_VIEW] = LoadShader(0, FormatText("resources/shaders/glsl%i/predator.fs", GLSL_VERSION)); + shaders[FX_SCANLINES] = LoadShader(0, FormatText("resources/shaders/glsl%i/scanlines.fs", GLSL_VERSION)); + shaders[FX_FISHEYE] = LoadShader(0, FormatText("resources/shaders/glsl%i/fisheye.fs", GLSL_VERSION)); + shaders[FX_SOBEL] = LoadShader(0, FormatText("resources/shaders/glsl%i/sobel.fs", GLSL_VERSION)); + shaders[FX_BLOOM] = LoadShader(0, FormatText("resources/shaders/glsl%i/bloom.fs", GLSL_VERSION)); + shaders[FX_BLUR] = LoadShader(0, FormatText("resources/shaders/glsl%i/blur.fs", GLSL_VERSION)); int currentShader = FX_GRAYSCALE; @@ -129,17 +128,19 @@ int main() ClearBackground(RAYWHITE); - BeginTextureMode(target); // Enable drawing to texture + BeginTextureMode(target); // Enable drawing to texture - Begin3dMode(camera); + ClearBackground(RAYWHITE); // Clear texture background - DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture + BeginMode3D(camera); // Begin 3d mode drawing + + DrawModel(model, position, 0.1f, WHITE); // Draw 3d model with texture DrawGrid(10, 1.0f); // Draw a grid - End3dMode(); + EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode - EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) + EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) // Render previously generated texture using selected postpro shader BeginShaderMode(shaders[currentShader]); @@ -149,9 +150,10 @@ int main() EndShaderMode(); + // Draw 2d shapes and text over drawn texture DrawRectangle(0, 9, 580, 30, Fade(LIGHTGRAY, 0.7f)); - DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, DARKGRAY); + DrawText("(c) Church 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY); DrawText("CURRENT POSTPRO SHADER:", 10, 15, 20, BLACK); DrawText(postproShaderText[currentShader], 330, 15, 20, RED); @@ -170,11 +172,11 @@ int main() for (int i = 0; i < MAX_POSTPRO_SHADERS; i++) UnloadShader(shaders[i]); UnloadTexture(texture); // Unload texture - UnloadModel(dwarf); // Unload model + UnloadModel(model); // Unload model UnloadRenderTexture(target); // Unload render texture CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; -}
\ No newline at end of file +} diff --git a/examples/src/shaders/shaders_raymarching.c b/examples/src/shaders/shaders_raymarching.c new file mode 100644 index 0000000..f68222b --- /dev/null +++ b/examples/src/shaders/shaders_raymarching.c @@ -0,0 +1,112 @@ +/******************************************************************************************* +* +* raylib [shaders] example - Raymarching shapes generation +* +* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, +* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. +* +* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example +* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders +* raylib comes with shaders ready for both versions, check raylib/shaders install folder +* +* This example has been created using raylib 2.0 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2018 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - raymarching shapes"); + + Camera camera = { 0 }; + camera.position = (Vector3){ 2.5f, 2.5f, 3.0f }; // Camera position + camera.target = (Vector3){ 0.0f, 0.0f, 0.7f }; // Camera looking at point + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target) + camera.fovy = 65.0f; // Camera field-of-view Y + + SetCameraMode(camera, CAMERA_FREE); // Set camera mode + + // Load raymarching shader + // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader + Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/raymarching.fs", GLSL_VERSION)); + + // Get shader locations for required uniforms + int viewEyeLoc = GetShaderLocation(shader, "viewEye"); + int viewCenterLoc = GetShaderLocation(shader, "viewCenter"); + int viewUpLoc = GetShaderLocation(shader, "viewUp"); + int deltaTimeLoc = GetShaderLocation(shader, "deltaTime"); + int runTimeLoc = GetShaderLocation(shader, "runTime"); + int resolutionLoc = GetShaderLocation(shader, "resolution"); + + float resolution[2] = { screenWidth, screenHeight }; + SetShaderValue(shader, resolutionLoc, resolution, UNIFORM_VEC2); + + float runTime = 0.0f; + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + UpdateCamera(&camera); // Update camera + + float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z }; + float cameraTarget[3] = { camera.target.x, camera.target.y, camera.target.z }; + float cameraUp[3] = { camera.up.x, camera.up.y, camera.up.z }; + + float deltaTime = GetFrameTime(); + runTime += deltaTime; + + // Set shader required uniform values + SetShaderValue(shader, viewEyeLoc, cameraPos, UNIFORM_VEC3); + SetShaderValue(shader, viewCenterLoc, cameraTarget, UNIFORM_VEC3); + SetShaderValue(shader, viewUpLoc, cameraUp, UNIFORM_VEC3); + SetShaderValue(shader, deltaTimeLoc, &deltaTime, UNIFORM_FLOAT); + SetShaderValue(shader, runTimeLoc, &runTime, UNIFORM_FLOAT); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + // We only draw a white full-screen rectangle, + // frame is generated in shader using raymarching + BeginShaderMode(shader); + DrawRectangle(0, 0, screenWidth, screenHeight, WHITE); + EndShaderMode(); + + DrawText("(c) Raymarching shader by Iñigo Quilez. MIT License.", screenWidth - 280, screenHeight - 20, 10, GRAY); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(shader); // Unload shader + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file diff --git a/examples/src/shaders/shaders_shapes_textures.c b/examples/src/shaders/shaders_shapes_textures.c index e8c36a1..5ee5d56 100644 --- a/examples/src/shaders/shaders_shapes_textures.c +++ b/examples/src/shaders/shaders_shapes_textures.c @@ -18,6 +18,12 @@ #include "raylib.h" +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + int main() { // Initialization @@ -29,9 +35,10 @@ int main() Texture2D fudesumi = LoadTexture("resources/fudesumi.png"); - // NOTE: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version - Shader shader = LoadShader("resources/shaders/glsl330/base.vs", - "resources/shaders/glsl330/grayscale.fs"); + // Load shader to be used on some parts drawing + // NOTE 1: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version + // NOTE 2: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader + Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION)); SetTargetFPS(60); //-------------------------------------------------------------------------------------- diff --git a/examples/src/shaders/shaders_texture_drawing.c b/examples/src/shaders/shaders_texture_drawing.c new file mode 100644 index 0000000..3a54009 --- /dev/null +++ b/examples/src/shaders/shaders_texture_drawing.c @@ -0,0 +1,79 @@ +/******************************************************************************************* +* +* raylib [textures] example - Texture drawing +* +* This example illustrates how to draw on a blank texture using a shader +* +* This example has been created using raylib 2.0 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Example contributed by Michał Ciesielski and reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2019 Michał Ciesielski and Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture drawing"); + + Image imBlank = GenImageColor(1024, 1024, BLANK); + Texture2D texture = LoadTextureFromImage(imBlank); // Load blank texture to fill on shader + UnloadImage(imBlank); + + // NOTE: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version + Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/cubes_panning.fs", GLSL_VERSION)); + + float time = 0.0f; + int timeLoc = GetShaderLocation(shader, "uTime"); + SetShaderValue(shader, timeLoc, &time, UNIFORM_FLOAT); + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + while (!WindowShouldClose()) + { + // Update + //---------------------------------------------------------------------------------- + time = GetTime(); + SetShaderValue(shader, timeLoc, &time, UNIFORM_FLOAT); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + BeginShaderMode(shader); // Enable our custom shader for next shapes/textures drawings + DrawTexture(texture, 0, 0, WHITE); // Drawing BLANK texture, all magic happens on shader + EndShaderMode(); // Disable our custom shader, return to default shader + + DrawText("BACKGROUND is PAINTED and ANIMATED on SHADER!", 10, 10, 20, MAROON); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(shader); + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/src/shaders/shaders_texture_waves.c b/examples/src/shaders/shaders_texture_waves.c new file mode 100644 index 0000000..bc677c7 --- /dev/null +++ b/examples/src/shaders/shaders_texture_waves.c @@ -0,0 +1,115 @@ +/******************************************************************************************* +* +* raylib [shaders] example - Texture Waves +* +* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, +* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. +* +* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example +* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders +* raylib comes with shaders ready for both versions, check raylib/shaders install folder +* +* This example has been created using raylib 2.5 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Example contributed by Anata (@anatagawa) and reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2019 Anata (@anatagawa) and Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + +// ------------------------------------------------------------------------------------------------------------- +// Main Entry point +// ------------------------------------------------------------------------------------------------------------- +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture waves"); + + // Load space texture to apply shaders + Texture2D space = LoadTexture("resources/space.png"); + + // Load shader and setup location points and values + Shader wave = LoadShader(0, FormatText("resources/shaders/glsl%i/wave.fs", GLSL_VERSION)); + + float screenSizeLoc = GetShaderLocation(wave, "size"); + float secondsLoc = GetShaderLocation(wave, "secondes"); + float freqXLoc = GetShaderLocation(wave, "freqX"); + float freqYLoc = GetShaderLocation(wave, "freqY"); + float ampXLoc = GetShaderLocation(wave, "ampX"); + float ampYLoc = GetShaderLocation(wave, "ampY"); + float speedXLoc = GetShaderLocation(wave, "speedX"); + float speedYLoc = GetShaderLocation(wave, "speedY"); + + float screenSize[2] = { 800, 450 }; + + // Shader uniform values that can be updated at any time + float freqX = 25.0f; + float freqY = 25.0f; + float ampX = 5.0f; + float ampY = 5.0f; + float speedX = 8.0f; + float speedY = 8.0f; + + SetShaderValue(wave, screenSizeLoc, &screenSize, UNIFORM_VEC2); + SetShaderValue(wave, freqXLoc, &freqX, UNIFORM_FLOAT); + SetShaderValue(wave, freqYLoc, &freqY, UNIFORM_FLOAT); + SetShaderValue(wave, ampXLoc, &X, UNIFORM_FLOAT); + SetShaderValue(wave, ampYLoc, &Y, UNIFORM_FLOAT); + SetShaderValue(wave, speedXLoc, &speedX, UNIFORM_FLOAT); + SetShaderValue(wave, speedYLoc, &speedY, UNIFORM_FLOAT); + + float seconds = 0.0f; + + SetTargetFPS(60); + // ------------------------------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + seconds += GetFrameTime(); + + SetShaderValue(wave, secondsLoc, &seconds, UNIFORM_FLOAT); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + BeginShaderMode(wave); + + DrawTexture(space, 0, 0, WHITE); + DrawTexture(space, space.width, 0, WHITE); + + EndShaderMode(); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(wave); // Unload shader + UnloadTexture(space); // Unload texture + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} |
