shaders_shapes_textures.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Apply a shader to some shape or 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. * 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. #include <stdio.h>
  20. #include <stdlib.h>
  21. int main()
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. int screenWidth = 800;
  26. int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - shapes and texture shaders");
  28. Texture2D sonic = LoadTexture("resources/texture_formats/sonic.png");
  29. // NOTE: This shader is a bit different than model/postprocessing shaders,
  30. // it requires the color data for every vertice to use it in every shape or texture independently
  31. Shader shader = LoadShader("resources/shaders/shapes_base.vs",
  32. "resources/shaders/shapes_grayscale.fs");
  33. // Shader usage is also different than models/postprocessing, shader is just activated when required
  34. SetTargetFPS(60);
  35. //--------------------------------------------------------------------------------------
  36. // Main game loop
  37. while (!WindowShouldClose()) // Detect window close button or ESC key
  38. {
  39. // Update
  40. //----------------------------------------------------------------------------------
  41. // TODO: Update your variables here
  42. //----------------------------------------------------------------------------------
  43. // Draw
  44. //----------------------------------------------------------------------------------
  45. BeginDrawing();
  46. ClearBackground(RAYWHITE);
  47. // Start drawing with default shader
  48. DrawText("USING DEFAULT SHADER", 20, 40, 10, RED);
  49. DrawCircle(80, 120, 35, DARKBLUE);
  50. DrawCircleGradient(80, 220, 60, GREEN, SKYBLUE);
  51. DrawCircleLines(80, 340, 80, DARKBLUE);
  52. // Activate our custom shader to be applied on next shapes/textures drawings
  53. SetCustomShader(shader);
  54. DrawText("USING CUSTOM SHADER", 190, 40, 10, RED);
  55. DrawRectangle(250 - 60, 90, 120, 60, RED);
  56. DrawRectangleGradient(250 - 90, 170, 180, 130, MAROON, GOLD);
  57. DrawRectangleLines(250 - 40, 320, 80, 60, ORANGE);
  58. // Activate our default shader for next drawings
  59. SetDefaultShader();
  60. DrawText("USING DEFAULT SHADER", 370, 40, 10, RED);
  61. DrawTriangle((Vector2){430, 80},
  62. (Vector2){430 - 60, 150},
  63. (Vector2){430 + 60, 150}, VIOLET);
  64. DrawTriangleLines((Vector2){430, 160},
  65. (Vector2){430 - 20, 230},
  66. (Vector2){430 + 20, 230}, DARKBLUE);
  67. DrawPoly((Vector2){430, 320}, 6, 80, 0, BROWN);
  68. // Activate our custom shader to be applied on next shapes/textures drawings
  69. SetCustomShader(shader);
  70. DrawTexture(sonic, 380, -10, WHITE); // Using custom shader
  71. // Activate our default shader for next drawings
  72. SetDefaultShader();
  73. EndDrawing();
  74. //----------------------------------------------------------------------------------
  75. }
  76. // De-Initialization
  77. //--------------------------------------------------------------------------------------
  78. UnloadShader(shader); // Unload shader
  79. UnloadTexture(sonic); // Unload texture
  80. CloseWindow(); // Close window and OpenGL context
  81. //--------------------------------------------------------------------------------------
  82. return 0;
  83. }