Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

150 рядки
6.2 KiB

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