shaders_custom_uniform.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 = {{ 3.0, 3.0, 3.0 }, { 0.0, 1.5, 0.0 }, { 0.0, 1.0, 0.0 }};
  29. Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model
  30. Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture
  31. SetModelTexture(&dwarf, texture); // Bind texture to model
  32. Vector3 position = { 0.0, 0.0, 0.0 }; // Set model position
  33. Shader shader = LoadShader("resources/shaders/base.vs",
  34. "resources/shaders/swirl.fs"); // Load postpro shader
  35. // Get variable (uniform) location on the shader to connect with the program
  36. // NOTE: If uniform variable could not be found in the shader, function returns -1
  37. int swirlCenterLoc = GetShaderLocation(shader, "center");
  38. float swirlCenter[2] = { screenWidth/2, screenHeight/2 };
  39. SetPostproShader(shader); // Set fullscreen postprocessing shader
  40. // Setup orbital camera
  41. SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode
  42. SetCameraPosition(camera.position); // Set internal camera position to match our camera position
  43. SetCameraTarget(camera.target); // Set internal camera target to match our camera target
  44. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. Vector2 mousePosition = GetMousePosition();
  52. swirlCenter[0] = mousePosition.x;
  53. swirlCenter[1] = screenHeight - mousePosition.y;
  54. // Send new value to the shader to be used on drawing
  55. SetShaderValue(shader, swirlCenterLoc, swirlCenter, 2);
  56. UpdateCamera(&camera); // Update internal camera and our camera
  57. //----------------------------------------------------------------------------------
  58. // Draw
  59. //----------------------------------------------------------------------------------
  60. BeginDrawing();
  61. ClearBackground(RAYWHITE);
  62. Begin3dMode(camera);
  63. DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture
  64. DrawGrid(10.0, 1.0); // Draw a grid
  65. End3dMode();
  66. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY);
  67. DrawFPS(10, 10);
  68. EndDrawing();
  69. //----------------------------------------------------------------------------------
  70. }
  71. // De-Initialization
  72. //--------------------------------------------------------------------------------------
  73. UnloadShader(shader); // Unload shader
  74. UnloadTexture(texture); // Unload texture
  75. UnloadModel(dwarf); // Unload model
  76. CloseWindow(); // Close window and OpenGL context
  77. //--------------------------------------------------------------------------------------
  78. return 0;
  79. }