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.

135 lines
5.8 KiB

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