shaders_custom_uniform.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Apply a postprocessing shader and connect a custom uniform variable (adapted for HTML5 platform)
  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_WEB)
  20. #include <emscripten/emscripten.h>
  21. #endif
  22. //----------------------------------------------------------------------------------
  23. // Global Variables Definition
  24. //----------------------------------------------------------------------------------
  25. int screenWidth = 800;
  26. int screenHeight = 450;
  27. // Define the camera to look into our 3d world
  28. Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
  29. Model dwarf; // OBJ model
  30. Texture2D texture; // Model texture
  31. Shader shader; // Postpro shader
  32. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  33. int swirlCenterLoc;
  34. float swirlCenter[2];
  35. RenderTexture2D target;
  36. //----------------------------------------------------------------------------------
  37. // Module Functions Declaration
  38. //----------------------------------------------------------------------------------
  39. void UpdateDrawFrame(void); // Update and Draw one frame
  40. //----------------------------------------------------------------------------------
  41. // Main Enry Point
  42. //----------------------------------------------------------------------------------
  43. int main()
  44. {
  45. // Initialization
  46. //--------------------------------------------------------------------------------------
  47. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  48. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - custom uniform variable");
  49. dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model
  50. texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture
  51. dwarf.material.texDiffuse = texture; // Set dwarf model diffuse texture
  52. shader = LoadShader("resources/shaders/glsl100/base.vs",
  53. "resources/shaders/glsl100/swirl.fs"); // Load postpro shader
  54. // Get variable (uniform) location on the shader to connect with the program
  55. // NOTE: If uniform variable could not be found in the shader, function returns -1
  56. swirlCenterLoc = GetShaderLocation(shader, "center");
  57. swirlCenter[0] = (float)screenWidth/2;
  58. swirlCenter[1] = (float)screenHeight/2;
  59. // Create a RenderTexture2D to be used for render to texture
  60. target = LoadRenderTexture(screenWidth, screenHeight);
  61. // Setup orbital camera
  62. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  63. #if defined(PLATFORM_WEB)
  64. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  65. #else
  66. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  67. //--------------------------------------------------------------------------------------
  68. // Main game loop
  69. while (!WindowShouldClose()) // Detect window close button or ESC key
  70. {
  71. UpdateDrawFrame();
  72. }
  73. #endif
  74. // De-Initialization
  75. //--------------------------------------------------------------------------------------
  76. UnloadShader(shader); // Unload shader
  77. UnloadTexture(texture); // Unload texture
  78. UnloadModel(dwarf); // Unload model
  79. CloseWindow(); // Close window and OpenGL context
  80. //--------------------------------------------------------------------------------------
  81. return 0;
  82. }
  83. //----------------------------------------------------------------------------------
  84. // Module Functions Definition
  85. //----------------------------------------------------------------------------------
  86. void UpdateDrawFrame(void)
  87. {
  88. // Update
  89. //----------------------------------------------------------------------------------
  90. Vector2 mousePosition = GetMousePosition();
  91. swirlCenter[0] = mousePosition.x;
  92. swirlCenter[1] = screenHeight - mousePosition.y;
  93. // Send new value to the shader to be used on drawing
  94. SetShaderValue(shader, swirlCenterLoc, swirlCenter, 2);
  95. UpdateCamera(&camera); // Update internal camera and our camera
  96. //----------------------------------------------------------------------------------
  97. // Draw
  98. //----------------------------------------------------------------------------------
  99. BeginDrawing();
  100. ClearBackground(RAYWHITE);
  101. BeginTextureMode(target); // Enable drawing to texture
  102. Begin3dMode(camera);
  103. DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture
  104. DrawGrid(10, 1.0f); // Draw a grid
  105. End3dMode();
  106. DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED);
  107. EndTextureMode(); // End drawing to texture (now we have a texture available for next passes)
  108. BeginShaderMode(shader);
  109. // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  110. DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE);
  111. EndShaderMode();
  112. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY);
  113. DrawFPS(10, 10);
  114. EndDrawing();
  115. //----------------------------------------------------------------------------------
  116. }