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.

128 lines
6.2 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Postprocessing with custom uniform variable
  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. * Example originally created with raylib 1.3, last time updated with raylib 4.0
  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) 2015-2024 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. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  36. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - custom uniform variable");
  37. // Define the camera to look into our 3d world
  38. Camera camera = { 0 };
  39. camera.position = (Vector3){ 8.0f, 8.0f, 8.0f }; // Camera position
  40. camera.target = (Vector3){ 0.0f, 1.5f, 0.0f }; // Camera looking at point
  41. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  42. camera.fovy = 45.0f; // Camera field-of-view Y
  43. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  44. Model model = LoadModel("resources/models/barracks.obj"); // Load OBJ model
  45. Texture2D texture = LoadTexture("resources/models/barracks_diffuse.png"); // Load model texture (diffuse map)
  46. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set model diffuse texture
  47. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  48. // Load postprocessing shader
  49. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  50. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/swirl.fs", GLSL_VERSION));
  51. // Get variable (uniform) location on the shader to connect with the program
  52. // NOTE: If uniform variable could not be found in the shader, function returns -1
  53. int swirlCenterLoc = GetShaderLocation(shader, "center");
  54. float swirlCenter[2] = { (float)screenWidth/2, (float)screenHeight/2 };
  55. // Create a RenderTexture2D to be used for render to texture
  56. RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
  57. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  58. //--------------------------------------------------------------------------------------
  59. // Main game loop
  60. while (!WindowShouldClose()) // Detect window close button or ESC key
  61. {
  62. // Update
  63. //----------------------------------------------------------------------------------
  64. UpdateCamera(&camera, CAMERA_ORBITAL);
  65. Vector2 mousePosition = GetMousePosition();
  66. swirlCenter[0] = mousePosition.x;
  67. swirlCenter[1] = screenHeight - mousePosition.y;
  68. // Send new value to the shader to be used on drawing
  69. SetShaderValue(shader, swirlCenterLoc, swirlCenter, SHADER_UNIFORM_VEC2);
  70. //----------------------------------------------------------------------------------
  71. // Draw
  72. //----------------------------------------------------------------------------------
  73. BeginTextureMode(target); // Enable drawing to texture
  74. ClearBackground(RAYWHITE); // Clear texture background
  75. BeginMode3D(camera); // Begin 3d mode drawing
  76. DrawModel(model, position, 0.5f, WHITE); // Draw 3d model with texture
  77. DrawGrid(10, 1.0f); // Draw a grid
  78. EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode
  79. DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED);
  80. EndTextureMode(); // End drawing to texture (now we have a texture available for next passes)
  81. BeginDrawing();
  82. ClearBackground(RAYWHITE); // Clear screen background
  83. // Enable shader using the custom uniform
  84. BeginShaderMode(shader);
  85. // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  86. DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0, 0 }, WHITE);
  87. EndShaderMode();
  88. // Draw some 2d text over drawn texture
  89. DrawText("(c) Barracks 3D model by Alberto Cano", screenWidth - 220, screenHeight - 20, 10, GRAY);
  90. DrawFPS(10, 10);
  91. EndDrawing();
  92. //----------------------------------------------------------------------------------
  93. }
  94. // De-Initialization
  95. //--------------------------------------------------------------------------------------
  96. UnloadShader(shader); // Unload shader
  97. UnloadTexture(texture); // Unload texture
  98. UnloadModel(model); // Unload model
  99. UnloadRenderTexture(target); // Unload render texture
  100. CloseWindow(); // Close window and OpenGL context
  101. //--------------------------------------------------------------------------------------
  102. return 0;
  103. }