shaders_texture_outline.c 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Apply an shdrOutline to a texture
  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. * This example has been created using raylib 3.8 (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Example contributed by Samuel Skiff (@GoldenThumbs) and reviewed by Ramon Santamaria (@raysan5)
  12. *
  13. * Copyright (c) 2021 Samuel SKiff (@GoldenThumbs) and Ramon Santamaria (@raysan5)
  14. *
  15. ********************************************************************************************/
  16. #include "raylib.h"
  17. #if defined(PLATFORM_DESKTOP)
  18. #define GLSL_VERSION 330
  19. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  20. #define GLSL_VERSION 100
  21. #endif
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - Apply an outline to a texture");
  29. Texture2D texture = LoadTexture("resources/fudesumi.png");
  30. Shader shdrOutline = LoadShader(0, TextFormat("resources/shaders/glsl%i/outline.fs", GLSL_VERSION));
  31. float outlineSize = 2.0f;
  32. float outlineColor[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; // Normalized RED color
  33. float textureSize[2] = { (float)texture.width, (float)texture.height };
  34. // Get shader locations
  35. int outlineSizeLoc = GetShaderLocation(shdrOutline, "outlineSize");
  36. int outlineColorLoc = GetShaderLocation(shdrOutline, "outlineColor");
  37. int textureSizeLoc = GetShaderLocation(shdrOutline, "textureSize");
  38. // Set shader values (they can be changed later)
  39. SetShaderValue(shdrOutline, outlineSizeLoc, &outlineSize, SHADER_UNIFORM_FLOAT);
  40. SetShaderValue(shdrOutline, outlineColorLoc, outlineColor, SHADER_UNIFORM_VEC4);
  41. SetShaderValue(shdrOutline, textureSizeLoc, textureSize, SHADER_UNIFORM_VEC2);
  42. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  43. //--------------------------------------------------------------------------------------
  44. // Main game loop
  45. while (!WindowShouldClose()) // Detect window close button or ESC key
  46. {
  47. // Update
  48. //----------------------------------------------------------------------------------
  49. outlineSize += GetMouseWheelMove();
  50. if (outlineSize < 1.0f) outlineSize = 1.0f;
  51. SetShaderValue(shdrOutline, outlineSizeLoc, &outlineSize, SHADER_UNIFORM_FLOAT);
  52. //----------------------------------------------------------------------------------
  53. // Draw
  54. //----------------------------------------------------------------------------------
  55. BeginDrawing();
  56. ClearBackground(RAYWHITE);
  57. BeginShaderMode(shdrOutline);
  58. DrawTexture(texture, GetScreenWidth()/2 - texture.width/2, -30, WHITE);
  59. EndShaderMode();
  60. DrawText("Shader-based\ntexture\noutline", 10, 10, 20, GRAY);
  61. DrawText(TextFormat("Outline size: %i px", (int)outlineSize), 10, 120, 20, MAROON);
  62. DrawFPS(710, 10);
  63. EndDrawing();
  64. //----------------------------------------------------------------------------------
  65. }
  66. // De-Initialization
  67. //--------------------------------------------------------------------------------------
  68. UnloadTexture(texture);
  69. UnloadShader(shdrOutline);
  70. CloseWindow(); // Close window and OpenGL context
  71. //--------------------------------------------------------------------------------------
  72. return 0;
  73. }