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.

145 lines
5.4 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 (@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. #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.type = CAMERA_PERSPECTIVE;
  41. // Define our three models to show the shader on
  42. Mesh torus = GenMeshTorus(.3, 1, 16, 32);
  43. Model model1 = LoadModelFromMesh(torus);
  44. Mesh cube = GenMeshCube(.8,.8,.8);
  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[MAP_DIFFUSE].texture = texDiffuse;
  54. model2.materials[0].maps[MAP_DIFFUSE].texture = texDiffuse;
  55. // Using MAP_EMISSION as a spare slot to use for 2nd texture
  56. // NOTE: Don't use MAP_IRRADIANCE, MAP_PREFILTER or MAP_CUBEMAP
  57. // as they are bound as cube maps
  58. Texture texMask = LoadTexture("resources/mask.png");
  59. model1.materials[0].maps[MAP_EMISSION].texture = texMask;
  60. model2.materials[0].maps[MAP_EMISSION].texture = texMask;
  61. shader.locs[LOC_MAP_EMISSION] = GetShaderLocation(shader, "mask");
  62. // Frame is incremented each frame to animate the shader
  63. int shaderFrame = GetShaderLocation(shader, "frame");
  64. // Apply the shader to the two models
  65. model1.materials[0].shader = shader;
  66. model2.materials[0].shader = shader;
  67. int framesCounter = 0;
  68. Vector3 rotation = { 0 }; // Model rotation angles
  69. SetTargetFPS(60); // Set to run at 60 frames-per-second
  70. //--------------------------------------------------------------------------------------
  71. // Main game loop
  72. while (!WindowShouldClose()) // Detect window close button or ESC key
  73. {
  74. // Update
  75. //----------------------------------------------------------------------------------
  76. framesCounter++;
  77. rotation.x += 0.01f;
  78. rotation.y += 0.005f;
  79. rotation.z -= 0.0025f;
  80. // Send frames counter to shader for animation
  81. SetShaderValue(shader, shaderFrame, &framesCounter, UNIFORM_INT);
  82. // Rotate one of the models
  83. model1.transform = MatrixRotateXYZ(rotation);
  84. UpdateCamera(&camera);
  85. //----------------------------------------------------------------------------------
  86. // Draw
  87. //----------------------------------------------------------------------------------
  88. BeginDrawing();
  89. ClearBackground(DARKBLUE);
  90. BeginMode3D(camera);
  91. DrawModel(model1, (Vector3){0.5,0,0}, 1, WHITE);
  92. DrawModelEx(model2, (Vector3){-.5,0,0}, (Vector3){1,1,0}, 50, (Vector3){1,1,1}, WHITE);
  93. DrawModel(model3,(Vector3){0,0,-1.5}, 1, WHITE);
  94. DrawGrid(10, 1.0f); // Draw a grid
  95. EndMode3D();
  96. DrawRectangle(16, 698, MeasureText(TextFormat("Frame: %i", framesCounter), 20) + 8, 42, BLUE);
  97. DrawText(TextFormat("Frame: %i", framesCounter), 20, 700, 20, WHITE);
  98. DrawFPS(10, 10);
  99. EndDrawing();
  100. //----------------------------------------------------------------------------------
  101. }
  102. // De-Initialization
  103. //--------------------------------------------------------------------------------------
  104. UnloadModel(model1);
  105. UnloadModel(model2);
  106. UnloadModel(model3);
  107. UnloadTexture(texDiffuse); // Unload default diffuse texture
  108. UnloadTexture(texMask); // Unload texture mask
  109. UnloadShader(shader); // Unload shader
  110. CloseWindow(); // Close window and OpenGL context
  111. //--------------------------------------------------------------------------------------
  112. return 0;
  113. }