shaders_simple_mask.bmx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. SuperStrict
  2. Framework Ray.Lib
  3. Import Ray.Math
  4. ' Initialization
  5. '--------------------------------------------------------------------------------------
  6. Const screenWidth:Int = 800
  7. Const screenHeight:Int = 450
  8. InitWindow(screenWidth, screenHeight, "raylib - simple shader mask")
  9. ' Define the camera to look into our 3d world
  10. Local camera:RCamera
  11. camera.position = New RVector3(0.0, 1.0, 2.0)
  12. camera.target = New RVector3(0.0, 0.0, 0.0)
  13. camera.up = New RVector3(0.0, 1.0, 0.0)
  14. camera.fovy = 45.0
  15. camera.projection = CAMERA_PERSPECTIVE
  16. ' Define our three models to show the shader on
  17. Local torus:RMesh = GenMeshTorus(0.3, 1, 16, 32)
  18. Local model1:RModel = LoadModelFromMesh(torus)
  19. Local cube:RMesh = GenMeshCube(0.8,0.8,0.8)
  20. Local model2:RModel = LoadModelFromMesh(cube)
  21. ' Generate model to be shaded just to see the gaps in the other two
  22. Local sphere:RMesh = GenMeshSphere(1, 16, 16)
  23. Local model3:RModel = LoadModelFromMesh(sphere)
  24. ' Load the shader
  25. Local shader:RShader = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl330/mask.fs")
  26. ' Load and apply the diffuse texture (colour map)
  27. Local texDiffuse:RTexture2D = LoadTexture("../../lib.mod/raylib/examples/shaders/resources/plasma.png")
  28. model1.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texDiffuse
  29. model2.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texDiffuse
  30. ' Using MAP_EMISSION as a spare slot to use for 2nd texture
  31. ' NOTE: Don't use MAP_IRRADIANCE, MAP_PREFILTER or MAP_CUBEMAP
  32. ' as they are bound as cube maps
  33. Local texMask:RTexture2D = LoadTexture("../../lib.mod/raylib/examples/shaders/resources/mask.png")
  34. model1.materials[0].maps[MATERIAL_MAP_EMISSION].texture = texMask
  35. model2.materials[0].maps[MATERIAL_MAP_EMISSION].texture = texMask
  36. shader.locs[SHADER_LOC_MAP_EMISSION] = GetShaderLocation(shader, "mask")
  37. ' Frame is incremented each frame to animate the shader
  38. Local shaderFrame:Int = GetShaderLocation(shader, "frame")
  39. ' Apply the shader to the two models
  40. model1.materials[0].shader = shader
  41. model2.materials[0].shader = shader
  42. Local framesCounter:Int = 0
  43. Local Rotation:RVector3 ' Model rotation angles
  44. DisableCursor() ' Limit cursor to relative movement inside the window
  45. SetTargetFPS(60) ' Set to run at 60 frames-per-second
  46. '--------------------------------------------------------------------------------------
  47. ' Main game loop
  48. While Not WindowShouldClose() ' Detect window close button or ESC key
  49. ' Update
  50. '----------------------------------------------------------------------------------
  51. UpdateCamera(camera, CAMERA_FIRST_PERSON)
  52. framesCounter :+ 1
  53. rotation.x :+ 0.01
  54. rotation.y :+ 0.005
  55. rotation.z :- 0.0025
  56. ' Send frames counter to shader for animation
  57. SetShaderValue(shader, shaderFrame, Varptr framesCounter, SHADER_UNIFORM_INT)
  58. ' Rotate one of the models
  59. model1.transform = MatrixRotateXYZ(rotation)
  60. '----------------------------------------------------------------------------------
  61. ' Draw
  62. '----------------------------------------------------------------------------------
  63. BeginDrawing()
  64. ClearBackground(DARKBLUE)
  65. BeginMode3D(camera)
  66. DrawModel(model1, New RVector3(0.5,0,0), 1, WHITE)
  67. DrawModelEx(model2, New RVector3(-0.5,0,0), New RVector3(1,1,0), 50, New RVector3(1,1,1), WHITE)
  68. DrawModel(model3, New RVector3(0,0,-1.5), 1, WHITE)
  69. DrawGrid(10, 1.0) ' Draw a grid
  70. EndMode3D()
  71. DrawRectangle(16, 420, MeasureText("Frame: " + framesCounter, 20) + 8, 22, BLUE)
  72. DrawText("Frame: " + framesCounter, 20, 422, 20, WHITE)
  73. DrawFPS(10, 10)
  74. EndDrawing()
  75. '----------------------------------------------------------------------------------
  76. Wend
  77. ' De-Initialization
  78. '--------------------------------------------------------------------------------------
  79. UnloadModel(model1)
  80. UnloadModel(model2)
  81. UnloadModel(model3)
  82. UnloadTexture(texDiffuse) ' Unload default diffuse texture
  83. UnloadTexture(texMask) ' Unload texture mask
  84. UnloadShader(shader) ' Unload shader
  85. CloseWindow() ' Close window and OpenGL context
  86. '--------------------------------------------------------------------------------------