shaders_texture_outline.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Apply an shdrOutline to a texture
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
  8. * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
  9. *
  10. * Example originally created with raylib 4.0, last time updated with raylib 4.0
  11. *
  12. * Example contributed by Samuel Skiff (@GoldenThumbs) and reviewed by Ramon Santamaria (@raysan5)
  13. *
  14. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  15. * BSD-like license that allows static linking with closed source software
  16. *
  17. * Copyright (c) 2021-2025 Samuel SKiff (@GoldenThumbs) and Ramon Santamaria (@raysan5)
  18. *
  19. ********************************************************************************************/
  20. #include "raylib.h"
  21. #if defined(PLATFORM_DESKTOP)
  22. #define GLSL_VERSION 330
  23. #else // PLATFORM_ANDROID, PLATFORM_WEB
  24. #define GLSL_VERSION 100
  25. #endif
  26. //------------------------------------------------------------------------------------
  27. // Program main entry point
  28. //------------------------------------------------------------------------------------
  29. int main(void)
  30. {
  31. // Initialization
  32. //--------------------------------------------------------------------------------------
  33. const int screenWidth = 800;
  34. const int screenHeight = 450;
  35. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - Apply an outline to a texture");
  36. Texture2D texture = LoadTexture("resources/fudesumi.png");
  37. Shader shdrOutline = LoadShader(0, TextFormat("resources/shaders/glsl%i/outline.fs", GLSL_VERSION));
  38. float outlineSize = 2.0f;
  39. float outlineColor[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; // Normalized RED color
  40. float textureSize[2] = { (float)texture.width, (float)texture.height };
  41. // Get shader locations
  42. int outlineSizeLoc = GetShaderLocation(shdrOutline, "outlineSize");
  43. int outlineColorLoc = GetShaderLocation(shdrOutline, "outlineColor");
  44. int textureSizeLoc = GetShaderLocation(shdrOutline, "textureSize");
  45. // Set shader values (they can be changed later)
  46. SetShaderValue(shdrOutline, outlineSizeLoc, &outlineSize, SHADER_UNIFORM_FLOAT);
  47. SetShaderValue(shdrOutline, outlineColorLoc, outlineColor, SHADER_UNIFORM_VEC4);
  48. SetShaderValue(shdrOutline, textureSizeLoc, textureSize, SHADER_UNIFORM_VEC2);
  49. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  50. //--------------------------------------------------------------------------------------
  51. // Main game loop
  52. while (!WindowShouldClose()) // Detect window close button or ESC key
  53. {
  54. // Update
  55. //----------------------------------------------------------------------------------
  56. outlineSize += GetMouseWheelMove();
  57. if (outlineSize < 1.0f) outlineSize = 1.0f;
  58. SetShaderValue(shdrOutline, outlineSizeLoc, &outlineSize, SHADER_UNIFORM_FLOAT);
  59. //----------------------------------------------------------------------------------
  60. // Draw
  61. //----------------------------------------------------------------------------------
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. BeginShaderMode(shdrOutline);
  65. DrawTexture(texture, GetScreenWidth()/2 - texture.width/2, -30, WHITE);
  66. EndShaderMode();
  67. DrawText("Shader-based\ntexture\noutline", 10, 10, 20, GRAY);
  68. DrawText("Scroll mouse wheel to\nchange outline size", 10, 72, 20, GRAY);
  69. DrawText(TextFormat("Outline size: %i px", (int)outlineSize), 10, 120, 20, MAROON);
  70. DrawFPS(710, 10);
  71. EndDrawing();
  72. //----------------------------------------------------------------------------------
  73. }
  74. // De-Initialization
  75. //--------------------------------------------------------------------------------------
  76. UnloadTexture(texture);
  77. UnloadShader(shdrOutline);
  78. CloseWindow(); // Close window and OpenGL context
  79. //--------------------------------------------------------------------------------------
  80. return 0;
  81. }