shaders_custom_uniform.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Apply a postprocessing shader and connect a 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. * 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_DESKTOP)
  20. #define GLSL_VERSION 330
  21. #else // PLATFORM_RPI, 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. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  34. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - custom uniform variable");
  35. // Define the camera to look into our 3d world
  36. Camera camera = { 0 };
  37. camera.position = (Vector3){ 8.0f, 8.0f, 8.0f };
  38. camera.target = (Vector3){ 0.0f, 1.5f, 0.0f };
  39. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  40. camera.fovy = 45.0f;
  41. camera.projection = CAMERA_PERSPECTIVE;
  42. Model model = LoadModel("resources/models/barracks.obj"); // Load OBJ model
  43. Texture2D texture = LoadTexture("resources/models/barracks_diffuse.png"); // Load model texture (diffuse map)
  44. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set model diffuse texture
  45. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  46. // Load postprocessing shader
  47. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  48. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/swirl.fs", GLSL_VERSION));
  49. // Get variable (uniform) location on the shader to connect with the program
  50. // NOTE: If uniform variable could not be found in the shader, function returns -1
  51. int swirlCenterLoc = GetShaderLocation(shader, "center");
  52. float swirlCenter[2] = { (float)screenWidth/2, (float)screenHeight/2 };
  53. // Create a RenderTexture2D to be used for render to texture
  54. RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
  55. // Setup orbital camera
  56. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  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. Vector2 mousePosition = GetMousePosition();
  65. swirlCenter[0] = mousePosition.x;
  66. swirlCenter[1] = screenHeight - mousePosition.y;
  67. // Send new value to the shader to be used on drawing
  68. SetShaderValue(shader, swirlCenterLoc, swirlCenter, SHADER_UNIFORM_VEC2);
  69. UpdateCamera(&camera); // Update camera
  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. }