shaders_simple_mask.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Simple shader mask
  4. *
  5. * This example has been created using raylib 2.5 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by Chris Camacho (@codifies) and reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2019 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************
  13. *
  14. * After a model is loaded it has a default material, this material can be
  15. * modified in place rather than creating one from scratch...
  16. * While all of the maps have particular names, they can be used for any purpose
  17. * except for three maps that are applied as cubic maps (see below)
  18. *
  19. ********************************************************************************************/
  20. #include "raylib.h"
  21. #include "raymath.h"
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. InitWindow(screenWidth, screenHeight, "raylib - simple shader mask");
  29. // Define the camera to look into our 3d world
  30. Camera camera = { 0 };
  31. camera.position = (Vector3){ 0.0f, 1.0f, 2.0f };
  32. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  33. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  34. camera.fovy = 45.0f;
  35. camera.type = CAMERA_PERSPECTIVE;
  36. // Define our three models to show the shader on
  37. Mesh torus = GenMeshTorus(.3, 1, 16, 32);
  38. Model model1 = LoadModelFromMesh(torus);
  39. Mesh cube = GenMeshCube(.8,.8,.8);
  40. Model model2 = LoadModelFromMesh(cube);
  41. // Generate model to be shaded just to see the gaps in the other two
  42. Mesh sphere = GenMeshSphere(1, 16, 16);
  43. Model model3 = LoadModelFromMesh(sphere);
  44. // Load the shader
  45. Shader shader = LoadShader("resources/shaders/glsl330/mask.vs", "resources/shaders/glsl330/mask.fs");
  46. // Load and apply the diffuse texture (colour map)
  47. Texture texDiffuse = LoadTexture("resources/plasma.png");
  48. model1.materials[0].maps[MAP_DIFFUSE].texture = texDiffuse;
  49. model2.materials[0].maps[MAP_DIFFUSE].texture = texDiffuse;
  50. // Using MAP_EMISSION as a spare slot to use for 2nd texture
  51. // NOTE: Don't use MAP_IRRADIANCE, MAP_PREFILTER or MAP_CUBEMAP
  52. // as they are bound as cube maps
  53. Texture texMask = LoadTexture("resources/mask.png");
  54. model1.materials[0].maps[MAP_EMISSION].texture = texMask;
  55. model2.materials[0].maps[MAP_EMISSION].texture = texMask;
  56. shader.locs[LOC_MAP_EMISSION] = GetShaderLocation(shader, "mask");
  57. // Frame is incremented each frame to animate the shader
  58. int shaderFrame = GetShaderLocation(shader, "framesCounter");
  59. // Apply the shader to the two models
  60. model1.materials[0].shader = shader;
  61. model2.materials[0].shader = shader;
  62. int framesCounter = 0;
  63. Vector3 rotation = { 0 }; // Model rotation angles
  64. SetTargetFPS(60); // Set to run at 60 frames-per-second
  65. //--------------------------------------------------------------------------------------
  66. // Main game loop
  67. while (!WindowShouldClose()) // Detect window close button or ESC key
  68. {
  69. // Update
  70. //----------------------------------------------------------------------------------
  71. framesCounter++;
  72. rotation.x += 0.01f;
  73. rotation.y += 0.005f;
  74. rotation.z -= 0.0025f;
  75. // Send frames counter to shader for animation
  76. SetShaderValue(shader, shaderFrame, &framesCounter, UNIFORM_INT);
  77. // Rotate one of the models
  78. model1.transform = MatrixRotateXYZ(rotation);
  79. UpdateCamera(&camera);
  80. //----------------------------------------------------------------------------------
  81. // Draw
  82. //----------------------------------------------------------------------------------
  83. BeginDrawing();
  84. ClearBackground(DARKBLUE);
  85. BeginMode3D(camera);
  86. DrawModel(model1, (Vector3){0.5,0,0}, 1, WHITE);
  87. DrawModelEx(model2, (Vector3){-.5,0,0}, (Vector3){1,1,0}, 50, (Vector3){1,1,1}, WHITE);
  88. DrawModel(model3,(Vector3){0,0,-1.5}, 1, WHITE);
  89. DrawGrid(10, 1.0f); // Draw a grid
  90. EndMode3D();
  91. DrawRectangle(16, 698, MeasureText(FormatText("Frame: %i", framesCounter), 20) + 8, 42, BLUE);
  92. DrawText(FormatText("Frame: %i", framesCounter), 20, 700, 20, WHITE);
  93. DrawFPS(10, 10);
  94. EndDrawing();
  95. //----------------------------------------------------------------------------------
  96. }
  97. // De-Initialization
  98. //--------------------------------------------------------------------------------------
  99. UnloadModel(model1);
  100. UnloadModel(model2);
  101. UnloadModel(model3);
  102. UnloadTexture(texDiffuse); // Unload default diffuse texture
  103. UnloadTexture(texMask); // Unload texture mask
  104. UnloadShader(shader); // Unload shader
  105. CloseWindow(); // Close window and OpenGL context
  106. //--------------------------------------------------------------------------------------
  107. return 0;
  108. }