shaders_simple_mask.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Simple shader mask
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 2.5, last time updated with raylib 3.7
  8. *
  9. * Example contributed by Chris Camacho (@chriscamacho) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2019-2024 Chris Camacho (@chriscamacho) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************
  17. *
  18. * After a model is loaded it has a default material, this material can be
  19. * modified in place rather than creating one from scratch...
  20. * While all of the maps have particular names, they can be used for any purpose
  21. * except for three maps that are applied as cubic maps (see below)
  22. *
  23. ********************************************************************************************/
  24. #include "raylib.h"
  25. #include "raymath.h"
  26. #if defined(PLATFORM_DESKTOP)
  27. #define GLSL_VERSION 330
  28. #else // PLATFORM_ANDROID, PLATFORM_WEB
  29. #define GLSL_VERSION 100
  30. #endif
  31. //------------------------------------------------------------------------------------
  32. // Program main entry point
  33. //------------------------------------------------------------------------------------
  34. int main(void)
  35. {
  36. // Initialization
  37. //--------------------------------------------------------------------------------------
  38. const int screenWidth = 800;
  39. const int screenHeight = 450;
  40. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - simple shader mask");
  41. // Define the camera to look into our 3d world
  42. Camera camera = { 0 };
  43. camera.position = (Vector3){ 0.0f, 1.0f, 2.0f }; // Camera position
  44. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  45. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  46. camera.fovy = 45.0f; // Camera field-of-view Y
  47. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  48. // Define our three models to show the shader on
  49. Mesh torus = GenMeshTorus(0.3f, 1, 16, 32);
  50. Model model1 = LoadModelFromMesh(torus);
  51. Mesh cube = GenMeshCube(0.8f,0.8f,0.8f);
  52. Model model2 = LoadModelFromMesh(cube);
  53. // Generate model to be shaded just to see the gaps in the other two
  54. Mesh sphere = GenMeshSphere(1, 16, 16);
  55. Model model3 = LoadModelFromMesh(sphere);
  56. // Load the shader
  57. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/mask.fs", GLSL_VERSION));
  58. // Load and apply the diffuse texture (colour map)
  59. Texture texDiffuse = LoadTexture("resources/plasma.png");
  60. model1.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texDiffuse;
  61. model2.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texDiffuse;
  62. // Using MATERIAL_MAP_EMISSION as a spare slot to use for 2nd texture
  63. // NOTE: Don't use MATERIAL_MAP_IRRADIANCE, MATERIAL_MAP_PREFILTER or MATERIAL_MAP_CUBEMAP as they are bound as cube maps
  64. Texture texMask = LoadTexture("resources/mask.png");
  65. model1.materials[0].maps[MATERIAL_MAP_EMISSION].texture = texMask;
  66. model2.materials[0].maps[MATERIAL_MAP_EMISSION].texture = texMask;
  67. shader.locs[SHADER_LOC_MAP_EMISSION] = GetShaderLocation(shader, "mask");
  68. // Frame is incremented each frame to animate the shader
  69. int shaderFrame = GetShaderLocation(shader, "frame");
  70. // Apply the shader to the two models
  71. model1.materials[0].shader = shader;
  72. model2.materials[0].shader = shader;
  73. int framesCounter = 0;
  74. Vector3 rotation = { 0 }; // Model rotation angles
  75. DisableCursor(); // Limit cursor to relative movement inside the window
  76. SetTargetFPS(60); // Set to run at 60 frames-per-second
  77. //--------------------------------------------------------------------------------------
  78. // Main game loop
  79. while (!WindowShouldClose()) // Detect window close button or ESC key
  80. {
  81. // Update
  82. //----------------------------------------------------------------------------------
  83. UpdateCamera(&camera, CAMERA_FIRST_PERSON);
  84. framesCounter++;
  85. rotation.x += 0.01f;
  86. rotation.y += 0.005f;
  87. rotation.z -= 0.0025f;
  88. // Send frames counter to shader for animation
  89. SetShaderValue(shader, shaderFrame, &framesCounter, SHADER_UNIFORM_INT);
  90. // Rotate one of the models
  91. model1.transform = MatrixRotateXYZ(rotation);
  92. //----------------------------------------------------------------------------------
  93. // Draw
  94. //----------------------------------------------------------------------------------
  95. BeginDrawing();
  96. ClearBackground(DARKBLUE);
  97. BeginMode3D(camera);
  98. DrawModel(model1, (Vector3){ 0.5f, 0.0f, 0.0f }, 1, WHITE);
  99. DrawModelEx(model2, (Vector3){ -0.5f, 0.0f, 0.0f }, (Vector3){ 1.0f, 1.0f, 0.0f }, 50, (Vector3){ 1.0f, 1.0f, 1.0f }, WHITE);
  100. DrawModel(model3,(Vector3){ 0.0f, 0.0f, -1.5f }, 1, WHITE);
  101. DrawGrid(10, 1.0f); // Draw a grid
  102. EndMode3D();
  103. DrawRectangle(16, 698, MeasureText(TextFormat("Frame: %i", framesCounter), 20) + 8, 42, BLUE);
  104. DrawText(TextFormat("Frame: %i", framesCounter), 20, 700, 20, WHITE);
  105. DrawFPS(10, 10);
  106. EndDrawing();
  107. //----------------------------------------------------------------------------------
  108. }
  109. // De-Initialization
  110. //--------------------------------------------------------------------------------------
  111. UnloadModel(model1);
  112. UnloadModel(model2);
  113. UnloadModel(model3);
  114. UnloadTexture(texDiffuse); // Unload default diffuse texture
  115. UnloadTexture(texMask); // Unload texture mask
  116. UnloadShader(shader); // Unload shader
  117. CloseWindow(); // Close window and OpenGL context
  118. //--------------------------------------------------------------------------------------
  119. return 0;
  120. }