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.

144 lines
5.5 KiB

5 years ago
5 years ago
  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 (@chriscamacho) and reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2019 Chris Camacho (@chriscamacho) 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. #if defined(PLATFORM_DESKTOP)
  23. #define GLSL_VERSION 330
  24. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  25. #define GLSL_VERSION 100
  26. #endif
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. InitWindow(screenWidth, screenHeight, "raylib - simple shader mask");
  34. // Define the camera to look into our 3d world
  35. Camera camera = { 0 };
  36. camera.position = (Vector3){ 0.0f, 1.0f, 2.0f };
  37. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  38. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  39. camera.fovy = 45.0f;
  40. camera.projection = CAMERA_PERSPECTIVE;
  41. // Define our three models to show the shader on
  42. Mesh torus = GenMeshTorus(0.3f, 1, 16, 32);
  43. Model model1 = LoadModelFromMesh(torus);
  44. Mesh cube = GenMeshCube(0.8f,0.8f,0.8f);
  45. Model model2 = LoadModelFromMesh(cube);
  46. // Generate model to be shaded just to see the gaps in the other two
  47. Mesh sphere = GenMeshSphere(1, 16, 16);
  48. Model model3 = LoadModelFromMesh(sphere);
  49. // Load the shader
  50. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/mask.fs", GLSL_VERSION));
  51. // Load and apply the diffuse texture (colour map)
  52. Texture texDiffuse = LoadTexture("resources/plasma.png");
  53. model1.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texDiffuse;
  54. model2.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texDiffuse;
  55. // Using MATERIAL_MAP_EMISSION as a spare slot to use for 2nd texture
  56. // NOTE: Don't use MATERIAL_MAP_IRRADIANCE, MATERIAL_MAP_PREFILTER or MATERIAL_MAP_CUBEMAP as they are bound as cube maps
  57. Texture texMask = LoadTexture("resources/mask.png");
  58. model1.materials[0].maps[MATERIAL_MAP_EMISSION].texture = texMask;
  59. model2.materials[0].maps[MATERIAL_MAP_EMISSION].texture = texMask;
  60. shader.locs[SHADER_LOC_MAP_EMISSION] = GetShaderLocation(shader, "mask");
  61. // Frame is incremented each frame to animate the shader
  62. int shaderFrame = GetShaderLocation(shader, "frame");
  63. // Apply the shader to the two models
  64. model1.materials[0].shader = shader;
  65. model2.materials[0].shader = shader;
  66. int framesCounter = 0;
  67. Vector3 rotation = { 0 }; // Model rotation angles
  68. SetTargetFPS(60); // Set to run at 60 frames-per-second
  69. //--------------------------------------------------------------------------------------
  70. // Main game loop
  71. while (!WindowShouldClose()) // Detect window close button or ESC key
  72. {
  73. // Update
  74. //----------------------------------------------------------------------------------
  75. framesCounter++;
  76. rotation.x += 0.01f;
  77. rotation.y += 0.005f;
  78. rotation.z -= 0.0025f;
  79. // Send frames counter to shader for animation
  80. SetShaderValue(shader, shaderFrame, &framesCounter, SHADER_UNIFORM_INT);
  81. // Rotate one of the models
  82. model1.transform = MatrixRotateXYZ(rotation);
  83. UpdateCamera(&camera);
  84. //----------------------------------------------------------------------------------
  85. // Draw
  86. //----------------------------------------------------------------------------------
  87. BeginDrawing();
  88. ClearBackground(DARKBLUE);
  89. BeginMode3D(camera);
  90. DrawModel(model1, (Vector3){0.5,0,0}, 1, WHITE);
  91. DrawModelEx(model2, (Vector3){-.5,0,0}, (Vector3){1,1,0}, 50, (Vector3){1,1,1}, WHITE);
  92. DrawModel(model3,(Vector3){0,0,-1.5}, 1, WHITE);
  93. DrawGrid(10, 1.0f); // Draw a grid
  94. EndMode3D();
  95. DrawRectangle(16, 698, MeasureText(TextFormat("Frame: %i", framesCounter), 20) + 8, 42, BLUE);
  96. DrawText(TextFormat("Frame: %i", framesCounter), 20, 700, 20, WHITE);
  97. DrawFPS(10, 10);
  98. EndDrawing();
  99. //----------------------------------------------------------------------------------
  100. }
  101. // De-Initialization
  102. //--------------------------------------------------------------------------------------
  103. UnloadModel(model1);
  104. UnloadModel(model2);
  105. UnloadModel(model3);
  106. UnloadTexture(texDiffuse); // Unload default diffuse texture
  107. UnloadTexture(texMask); // Unload texture mask
  108. UnloadShader(shader); // Unload shader
  109. CloseWindow(); // Close window and OpenGL context
  110. //--------------------------------------------------------------------------------------
  111. return 0;
  112. }