shaders_shapes_textures.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Apply a shader to some shape or texture (adapted for HTML5 platform)
  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. * NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example
  9. * on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders
  10. * raylib comes with shaders ready for both versions, check raylib/shaders install folder
  11. *
  12. * This example has been created using raylib 1.3 (www.raylib.com)
  13. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  14. *
  15. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  16. *
  17. ********************************************************************************************/
  18. #include "raylib.h"
  19. #if defined(PLATFORM_WEB)
  20. #include <emscripten/emscripten.h>
  21. #endif
  22. //----------------------------------------------------------------------------------
  23. // Global Variables Definition
  24. //----------------------------------------------------------------------------------
  25. int screenWidth = 800;
  26. int screenHeight = 450;
  27. Texture2D fudesumi;
  28. Shader shader;
  29. //----------------------------------------------------------------------------------
  30. // Module Functions Declaration
  31. //----------------------------------------------------------------------------------
  32. void UpdateDrawFrame(void); // Update and Draw one frame
  33. //----------------------------------------------------------------------------------
  34. // Main Enry Point
  35. //----------------------------------------------------------------------------------
  36. int main()
  37. {
  38. // Initialization
  39. //--------------------------------------------------------------------------------------
  40. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - shapes and texture shaders");
  41. fudesumi = LoadTexture("resources/fudesumi.png");
  42. // NOTE: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version
  43. shader = LoadShader("resources/shaders/glsl100/base.vs",
  44. "resources/shaders/glsl100/grayscale.fs");
  45. // Shader usage is also different than models/postprocessing, shader is just activated when required
  46. #if defined(PLATFORM_WEB)
  47. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  48. #else
  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. UpdateDrawFrame();
  55. }
  56. #endif
  57. // De-Initialization
  58. //--------------------------------------------------------------------------------------
  59. UnloadShader(shader); // Unload shader
  60. UnloadTexture(fudesumi); // Unload texture
  61. CloseWindow(); // Close window and OpenGL context
  62. //--------------------------------------------------------------------------------------
  63. return 0;
  64. }
  65. //----------------------------------------------------------------------------------
  66. // Module Functions Definition
  67. //----------------------------------------------------------------------------------
  68. void UpdateDrawFrame(void)
  69. {
  70. // Update
  71. //----------------------------------------------------------------------------------
  72. // TODO: Update your variables here
  73. //----------------------------------------------------------------------------------
  74. // Draw
  75. //----------------------------------------------------------------------------------
  76. BeginDrawing();
  77. ClearBackground(RAYWHITE);
  78. // Start drawing with default shader
  79. DrawText("USING DEFAULT SHADER", 20, 40, 10, RED);
  80. DrawCircle(80, 120, 35, DARKBLUE);
  81. DrawCircleGradient(80, 220, 60, GREEN, SKYBLUE);
  82. DrawCircleLines(80, 340, 80, DARKBLUE);
  83. // Activate our custom shader to be applied on next shapes/textures drawings
  84. BeginShaderMode(shader);
  85. DrawText("USING CUSTOM SHADER", 190, 40, 10, RED);
  86. DrawRectangle(250 - 60, 90, 120, 60, RED);
  87. DrawRectangleGradient(250 - 90, 170, 180, 130, MAROON, GOLD);
  88. DrawRectangleLines(250 - 40, 320, 80, 60, ORANGE);
  89. // Activate our default shader for next drawings
  90. EndShaderMode();
  91. DrawText("USING DEFAULT SHADER", 370, 40, 10, RED);
  92. DrawTriangle((Vector2){430, 80},
  93. (Vector2){430 - 60, 150},
  94. (Vector2){430 + 60, 150}, VIOLET);
  95. DrawTriangleLines((Vector2){430, 160},
  96. (Vector2){430 - 20, 230},
  97. (Vector2){430 + 20, 230}, DARKBLUE);
  98. DrawPoly((Vector2){430, 320}, 6, 80, 0, BROWN);
  99. // Activate our custom shader to be applied on next shapes/textures drawings
  100. BeginShaderMode(shader);
  101. DrawTexture(fudesumi, 500, -30, WHITE); // Using custom shader
  102. // Activate our default shader for next drawings
  103. EndShaderMode();
  104. EndDrawing();
  105. //----------------------------------------------------------------------------------
  106. }