2
0

shaders_custom_uniform.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. int main()
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. int screenWidth = 800;
  24. int screenHeight = 450;
  25. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  26. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - custom uniform variable");
  27. // Define the camera to look into our 3d world
  28. Camera camera = { 0 };
  29. camera.position = (Vector3){ 8.0f, 8.0f, 8.0f };
  30. camera.target = (Vector3){ 0.0f, 1.5f, 0.0f };
  31. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  32. camera.fovy = 45.0f;
  33. camera.type = CAMERA_PERSPECTIVE;
  34. Model model = LoadModel("resources/models/barracks.obj"); // Load OBJ model
  35. Texture2D texture = LoadTexture("resources/models/barracks_diffuse.png"); // Load model texture (diffuse map)
  36. model.material.maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture
  37. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  38. Shader shader = LoadShader("resources/shaders/glsl330/base.vs",
  39. "resources/shaders/glsl330/swirl.fs"); // Load postpro shader
  40. // Get variable (uniform) location on the shader to connect with the program
  41. // NOTE: If uniform variable could not be found in the shader, function returns -1
  42. int swirlCenterLoc = GetShaderLocation(shader, "center");
  43. float swirlCenter[2] = { (float)screenWidth/2, (float)screenHeight/2 };
  44. // Create a RenderTexture2D to be used for render to texture
  45. RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
  46. // Setup orbital camera
  47. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  48. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  49. //--------------------------------------------------------------------------------------
  50. // Main game loop
  51. while (!WindowShouldClose()) // Detect window close button or ESC key
  52. {
  53. // Update
  54. //----------------------------------------------------------------------------------
  55. Vector2 mousePosition = GetMousePosition();
  56. swirlCenter[0] = mousePosition.x;
  57. swirlCenter[1] = screenHeight - mousePosition.y;
  58. // Send new value to the shader to be used on drawing
  59. SetShaderValue(shader, swirlCenterLoc, swirlCenter, 2);
  60. UpdateCamera(&camera); // Update camera
  61. //----------------------------------------------------------------------------------
  62. // Draw
  63. //----------------------------------------------------------------------------------
  64. BeginDrawing();
  65. ClearBackground(RAYWHITE);
  66. BeginTextureMode(target); // Enable drawing to texture
  67. BeginMode3D(camera);
  68. DrawModel(model, position, 0.5f, WHITE); // Draw 3d model with texture
  69. DrawGrid(10, 1.0f); // Draw a grid
  70. EndMode3D();
  71. DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED);
  72. EndTextureMode(); // End drawing to texture (now we have a texture available for next passes)
  73. BeginShaderMode(shader);
  74. // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  75. DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE);
  76. EndShaderMode();
  77. DrawText("(c) Barracks 3D model by Alberto Cano", screenWidth - 220, screenHeight - 20, 10, GRAY);
  78. DrawFPS(10, 10);
  79. EndDrawing();
  80. //----------------------------------------------------------------------------------
  81. }
  82. // De-Initialization
  83. //--------------------------------------------------------------------------------------
  84. UnloadShader(shader); // Unload shader
  85. UnloadTexture(texture); // Unload texture
  86. UnloadModel(model); // Unload model
  87. UnloadRenderTexture(target); // Unload render texture
  88. CloseWindow(); // Close window and OpenGL context
  89. //--------------------------------------------------------------------------------------
  90. return 0;
  91. }