diff options
| author | Ray <[email protected]> | 2021-06-23 01:25:09 +0200 |
|---|---|---|
| committer | Ray <[email protected]> | 2021-06-23 01:25:09 +0200 |
| commit | 716e26aa37e352f0188824bc2de7dd3035f7413c (patch) | |
| tree | 6239af3f4c247e30932b32c884baafb8b2eba8eb /examples/shaders | |
| parent | f989048bda60aeb74111ec687cd44ec92deacfe3 (diff) | |
| download | raylib-716e26aa37e352f0188824bc2de7dd3035f7413c.tar.gz raylib-716e26aa37e352f0188824bc2de7dd3035f7413c.zip | |
Review BeginTextureMode() usage
Moved outside BeginDrawing()/EndDrawing() to illustrate drawing is happening to an external texture (not screen)
Diffstat (limited to 'examples/shaders')
| -rw-r--r-- | examples/shaders/shaders_custom_uniform.c | 47 | ||||
| -rw-r--r-- | examples/shaders/shaders_eratosthenes.c | 32 | ||||
| -rw-r--r-- | examples/shaders/shaders_julia_set.c | 42 | ||||
| -rw-r--r-- | examples/shaders/shaders_postprocessing.c | 34 |
4 files changed, 65 insertions, 90 deletions
diff --git a/examples/shaders/shaders_custom_uniform.c b/examples/shaders/shaders_custom_uniform.c index 6efda727..60516c11 100644 --- a/examples/shaders/shaders_custom_uniform.c +++ b/examples/shaders/shaders_custom_uniform.c @@ -65,11 +65,11 @@ int main(void) // Setup orbital camera SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode - SetTargetFPS(60); // Set our game to run at 60 frames-per-second + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- @@ -81,55 +81,46 @@ int main(void) // Send new value to the shader to be used on drawing SetShaderValue(shader, swirlCenterLoc, swirlCenter, SHADER_UNIFORM_VEC2); - UpdateCamera(&camera); // Update camera + UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- - BeginDrawing(); - - ClearBackground(RAYWHITE); - - BeginTextureMode(target); // Enable drawing to texture - - ClearBackground(RAYWHITE); // Clear texture background - - BeginMode3D(camera); // Begin 3d mode drawing - - DrawModel(model, position, 0.5f, WHITE); // Draw 3d model with texture + BeginTextureMode(target); // Enable drawing to texture + ClearBackground(RAYWHITE); // Clear texture background - DrawGrid(10, 1.0f); // Draw a grid + BeginMode3D(camera); // Begin 3d mode drawing + DrawModel(model, position, 0.5f, WHITE); // Draw 3d model with texture + DrawGrid(10, 1.0f); // Draw a grid + EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode - 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) - DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED); - - EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) + BeginDrawing(); + ClearBackground(RAYWHITE); // Clear screen background + // Enable shader using the custom uniform BeginShaderMode(shader); - // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0, 0 }, WHITE); - EndShaderMode(); // 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(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- - UnloadShader(shader); // Unload shader - UnloadTexture(texture); // Unload texture - UnloadModel(model); // Unload model - UnloadRenderTexture(target); // Unload render texture + UnloadShader(shader); // Unload shader + UnloadTexture(texture); // Unload texture + UnloadModel(model); // Unload model + UnloadRenderTexture(target); // Unload render texture - CloseWindow(); // Close window and OpenGL context + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; diff --git a/examples/shaders/shaders_eratosthenes.c b/examples/shaders/shaders_eratosthenes.c index d5163a7f..65fd9f98 100644 --- a/examples/shaders/shaders_eratosthenes.c +++ b/examples/shaders/shaders_eratosthenes.c @@ -46,11 +46,11 @@ int main(void) // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/eratosthenes.fs", GLSL_VERSION)); - SetTargetFPS(60); // Set our game to run at 60 frames-per-second + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- @@ -59,35 +59,33 @@ int main(void) // Draw //---------------------------------------------------------------------------------- - BeginDrawing(); - - ClearBackground(RAYWHITE); + BeginTextureMode(target); // Enable drawing to texture + ClearBackground(BLACK); // Clear the render texture - BeginTextureMode(target); // Enable drawing to texture - ClearBackground(BLACK); // Clear the render texture + // Draw a rectangle in shader mode to be used as shader canvas + // NOTE: Rectangle uses font white character texture coordinates, + // so shader can not be applied here directly because input vertexTexCoord + // do not represent full screen coordinates (space where want to apply shader) + DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); + EndTextureMode(); // End drawing to texture (now we have a blank texture available for the shader) - // Draw a rectangle in shader mode to be used as shader canvas - // NOTE: Rectangle uses font white character texture coordinates, - // so shader can not be applied here directly because input vertexTexCoord - // do not represent full screen coordinates (space where want to apply shader) - DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); - EndTextureMode(); // End drawing to texture (now we have a blank texture available for the shader) + BeginDrawing(); + ClearBackground(RAYWHITE); // Clear screen background BeginShaderMode(shader); // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0.0f, 0.0f }, WHITE); EndShaderMode(); - EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- - UnloadShader(shader); // Unload shader - UnloadRenderTexture(target); // Unload texture + UnloadShader(shader); // Unload shader + UnloadRenderTexture(target); // Unload render texture - CloseWindow(); // Close window and OpenGL context + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index 4a12ba02..90c44cf5 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -75,15 +75,15 @@ int main(void) SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT); SetShaderValue(shader, offsetLoc, offset, SHADER_UNIFORM_VEC2); - int incrementSpeed = 0; // Multiplier of speed to change c value - bool showControls = true; // Show controls - bool pause = false; // Pause animation + int incrementSpeed = 0; // Multiplier of speed to change c value + bool showControls = true; // Show controls + bool pause = false; // Pause animation - SetTargetFPS(60); // Set our game to run at 60 frames-per-second + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- @@ -145,20 +145,19 @@ int main(void) // Draw //---------------------------------------------------------------------------------- + // 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 to be used as shader canvas + // NOTE: Rectangle uses font white character texture coordinates, + // so shader can not be applied here directly because input vertexTexCoord + // do not represent full screen coordinates (space where want to apply shader) + DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); + EndTextureMode(); + 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 to be used as shader canvas - // NOTE: Rectangle uses font white character texture coordinates, - // so shader can not be applied here directly because input vertexTexCoord - // do not represent full screen coordinates (space where want to apply shader) - DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); - EndTextureMode(); + ClearBackground(BLACK); // Clear screen background // Draw the saved texture and rendered julia set with shader // NOTE: We do not invert texture on Y, already considered inside shader @@ -176,17 +175,16 @@ int main(void) DrawText("Press KEY_LEFT | KEY_RIGHT to change speed", 10, 60, 10, RAYWHITE); DrawText("Press KEY_SPACE to pause movement animation", 10, 75, 10, RAYWHITE); } - EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- - UnloadShader(shader); // Unload shader - UnloadRenderTexture(target); // Unload render texture + UnloadShader(shader); // Unload shader + UnloadRenderTexture(target); // Unload render texture - CloseWindow(); // Close window and OpenGL context + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; diff --git a/examples/shaders/shaders_postprocessing.c b/examples/shaders/shaders_postprocessing.c index ef815391..ebe5fcdb 100644 --- a/examples/shaders/shaders_postprocessing.c +++ b/examples/shaders/shaders_postprocessing.c @@ -124,50 +124,38 @@ int main(void) // Draw //---------------------------------------------------------------------------------- + BeginTextureMode(target); // Enable drawing to texture + ClearBackground(RAYWHITE); // Clear texture background + + BeginMode3D(camera); // Begin 3d mode drawing + DrawModel(model, position, 0.1f, WHITE); // Draw 3d model with texture + DrawGrid(10, 1.0f); // Draw a grid + EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode + EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) + BeginDrawing(); + ClearBackground(RAYWHITE); // Clear screen background - ClearBackground(RAYWHITE); - - BeginTextureMode(target); // Enable drawing to texture - - ClearBackground(RAYWHITE); // Clear texture background - - BeginMode3D(camera); // Begin 3d mode drawing - - DrawModel(model, position, 0.1f, WHITE); // Draw 3d model with texture - - DrawGrid(10, 1.0f); // Draw a grid - - EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode - - EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) - - // Render previously generated texture using selected postpro shader + // Render generated texture using selected postprocessing shader BeginShaderMode(shaders[currentShader]); - // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0, 0 }, WHITE); - EndShaderMode(); // Draw 2d shapes and text over drawn texture DrawRectangle(0, 9, 580, 30, Fade(LIGHTGRAY, 0.7f)); 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); DrawText("< >", 540, 10, 30, DARKBLUE); - DrawFPS(700, 15); - EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- - // Unload all postpro shaders for (int i = 0; i < MAX_POSTPRO_SHADERS; i++) UnloadShader(shaders[i]); |
