You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
4.1 KiB

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