shaders_texture_drawing.c 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Texture drawing
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * NOTE: This example illustrates how to draw into a blank texture using a shader
  8. *
  9. * Example originally created with raylib 2.0, last time updated with raylib 3.7
  10. *
  11. * Example contributed by Michał Ciesielski (@ciessielski) and reviewed by Ramon Santamaria (@raysan5)
  12. *
  13. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  14. * BSD-like license that allows static linking with closed source software
  15. *
  16. * Copyright (c) 2019-2025 Michał Ciesielski (@ciessielski) and Ramon Santamaria (@raysan5)
  17. *
  18. ********************************************************************************************/
  19. #include "raylib.h"
  20. #if defined(PLATFORM_DESKTOP)
  21. #define GLSL_VERSION 330
  22. #else // PLATFORM_ANDROID, PLATFORM_WEB
  23. #define GLSL_VERSION 100
  24. #endif
  25. //------------------------------------------------------------------------------------
  26. // Program main entry point
  27. //------------------------------------------------------------------------------------
  28. int main(void)
  29. {
  30. // Initialization
  31. //--------------------------------------------------------------------------------------
  32. const int screenWidth = 800;
  33. const int screenHeight = 450;
  34. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture drawing");
  35. Image imBlank = GenImageColor(1024, 1024, BLANK);
  36. Texture2D texture = LoadTextureFromImage(imBlank); // Load blank texture to fill on shader
  37. UnloadImage(imBlank);
  38. // NOTE: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version
  39. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/cubes_panning.fs", GLSL_VERSION));
  40. float time = 0.0f;
  41. int timeLoc = GetShaderLocation(shader, "uTime");
  42. SetShaderValue(shader, timeLoc, &time, SHADER_UNIFORM_FLOAT);
  43. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  44. // -------------------------------------------------------------------------------------------------------------
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. // Update
  49. //----------------------------------------------------------------------------------
  50. time = (float)GetTime();
  51. SetShaderValue(shader, timeLoc, &time, SHADER_UNIFORM_FLOAT);
  52. //----------------------------------------------------------------------------------
  53. // Draw
  54. //----------------------------------------------------------------------------------
  55. BeginDrawing();
  56. ClearBackground(RAYWHITE);
  57. BeginShaderMode(shader); // Enable our custom shader for next shapes/textures drawings
  58. DrawTexture(texture, 0, 0, WHITE); // Drawing BLANK texture, all magic happens on shader
  59. EndShaderMode(); // Disable our custom shader, return to default shader
  60. DrawText("BACKGROUND is PAINTED and ANIMATED on SHADER!", 10, 10, 20, MAROON);
  61. EndDrawing();
  62. //----------------------------------------------------------------------------------
  63. }
  64. // De-Initialization
  65. //--------------------------------------------------------------------------------------
  66. UnloadShader(shader);
  67. UnloadTexture(texture);
  68. CloseWindow(); // Close window and OpenGL context
  69. //--------------------------------------------------------------------------------------
  70. return 0;
  71. }