shaders_postprocessing.bmx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. SuperStrict
  2. Framework Ray.Lib
  3. ?Not opengles
  4. Const GLSL_VERSION:Int = 330
  5. ?opengles
  6. Const GLSL_VERSION:Int = 100
  7. ?
  8. Const MAX_POSTPRO_SHADERS:Int = 12
  9. Const FX_GRAYSCALE:Int = 0
  10. Const FX_POSTERIZATION:Int = 1
  11. Const FX_DREAM_VISION:Int = 2
  12. Const FX_PIXELIZER:Int = 3
  13. Const FX_CROSS_HATCHING:Int = 4
  14. Const FX_CROSS_STITCHING:Int = 5
  15. Const FX_PREDATOR_VIEW:Int = 6
  16. Const FX_SCANLINES:Int = 7
  17. Const FX_FISHEYE:Int = 8
  18. Const FX_SOBEL:Int = 9
  19. Const FX_BLOOM:Int = 10
  20. Const FX_BLUR:Int = 11
  21. Local postproShaderText:String[] = [ ..
  22. "GRAYSCALE", "POSTERIZATION", "DREAM_VISION", "PIXELIZER", "CROSS_HATCHING", "CROSS_STITCHING", ..
  23. "PREDATOR_VIEW", "SCANLINES", "FISHEYE", "SOBEL", "BLOOM", "BLUR"]
  24. ' Initialization
  25. '--------------------------------------------------------------------------------------
  26. Const screenWidth:Int = 800
  27. Const screenHeight:Int = 450
  28. SetConfigFlags(FLAG_MSAA_4X_HINT) ' Enable Multi Sampling Anti Aliasing 4x (if available)
  29. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - postprocessing shader")
  30. ' Define the camera to look into our 3d world
  31. Local camera:RCamera
  32. camera.position = New RVector3(2.0, 3.0, 2.0) ' Camera position
  33. camera.target = New RVector3(0.0, 1.0, 0.0) ' Camera looking at point
  34. camera.up = New RVector3(0.0, 1.0, 0.0) ' Camera up vector (rotation towards target)
  35. camera.fovy = 45.0 ' Camera field-of-view Y
  36. camera.projection = CAMERA_PERSPECTIVE ' Camera projection type
  37. Local model:RModel = LoadModel("../../lib.mod/raylib/examples/shaders/resources/models/church.obj") ' Load OBJ model
  38. Local texture:RTexture2D = LoadTexture("../../lib.mod/raylib/examples/shaders/resources/models/church_diffuse.png") ' Load model texture (diffuse map)
  39. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture ' Set model diffuse texture
  40. Local position:RVector3 = New RVector3(0.0, 0.0, 0.0) ' Set model position
  41. ' Load all postpro shaders
  42. ' NOTE 1: All postpro shader use the base vertex shader (DEFAULT_VERTEX_SHADER)
  43. ' NOTE 2: We load the correct shader depending on GLSL version
  44. Local shaders:RShader[MAX_POSTPRO_SHADERS]
  45. ' NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  46. shaders[FX_GRAYSCALE] = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl" + GLSL_VERSION + "/grayscale.fs")
  47. shaders[FX_POSTERIZATION] = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl" + GLSL_VERSION + "/posterization.fs")
  48. shaders[FX_DREAM_VISION] = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl" + GLSL_VERSION + "/dream_vision.fs")
  49. shaders[FX_PIXELIZER] = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl" + GLSL_VERSION + "/pixelizer.fs")
  50. shaders[FX_CROSS_HATCHING] = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl" + GLSL_VERSION + "/cross_hatching.fs")
  51. shaders[FX_CROSS_STITCHING] = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl" + GLSL_VERSION + "/cross_stitching.fs")
  52. shaders[FX_PREDATOR_VIEW] = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl" + GLSL_VERSION + "/predator.fs")
  53. shaders[FX_SCANLINES] = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl" + GLSL_VERSION + "/scanlines.fs")
  54. shaders[FX_FISHEYE] = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl" + GLSL_VERSION + "/fisheye.fs")
  55. shaders[FX_SOBEL] = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl" + GLSL_VERSION + "/sobel.fs")
  56. shaders[FX_BLOOM] = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl" + GLSL_VERSION + "/bloom.fs")
  57. shaders[FX_BLUR] = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl" + GLSL_VERSION + "/blur.fs")
  58. Local currentShader:Int = FX_GRAYSCALE
  59. ' Create a RenderTexture2D to be used for render to texture
  60. Local target:RRenderTexture2D = LoadRenderTexture(screenWidth, screenHeight)
  61. SetTargetFPS(60) ' Set our game to run at 60 frames-per-second
  62. '--------------------------------------------------------------------------------------
  63. ' Main game loop
  64. While Not WindowShouldClose() ' Detect window close button or ESC key
  65. ' Update
  66. '----------------------------------------------------------------------------------
  67. UpdateCamera(camera, CAMERA_ORBITAL) ' Update camera
  68. If IsKeyPressed(KEY_RIGHT) Then
  69. currentShader :+ 1
  70. Else If IsKeyPressed(KEY_LEFT) Then
  71. currentShader :- 1
  72. End If
  73. If currentShader >= MAX_POSTPRO_SHADERS Then
  74. currentShader = 0
  75. Else If currentShader < 0 Then
  76. currentShader = MAX_POSTPRO_SHADERS - 1
  77. End If
  78. '----------------------------------------------------------------------------------
  79. ' Draw
  80. '----------------------------------------------------------------------------------
  81. BeginTextureMode(target) ' Enable drawing to texture
  82. ClearBackground(RAYWHITE) ' Clear texture background
  83. BeginMode3D(camera) ' Begin 3d mode drawing
  84. DrawModel(model, position, 0.1, WHITE) ' Draw 3d model with texture
  85. DrawGrid(10, 1.0) ' Draw a grid
  86. EndMode3D() ' End 3d mode drawing, returns to orthographic 2d mode
  87. EndTextureMode() ' End drawing to texture (now we have a texture available for next passes)
  88. BeginDrawing()
  89. ClearBackground(RAYWHITE) ' Clear screen background
  90. ' Render previously generated texture using selected postpro shader
  91. BeginShaderMode(shaders[currentShader])
  92. ' NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  93. DrawTextureRec(target.texture, New RRectangle(0, 0, target.texture.width, -target.texture.height), New RVector2(0, 0), WHITE)
  94. EndShaderMode()
  95. ' Draw 2d shapes and text over drawn texture
  96. DrawRectangle(0, 9, 580, 30, Fade(LIGHTGRAY, 0.7))
  97. DrawText("(c) Church 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY)
  98. DrawText("CURRENT POSTPRO SHADER:", 10, 15, 20, BLACK)
  99. DrawText(postproShaderText[currentShader], 330, 15, 20, RED)
  100. DrawText("< >", 540, 10, 30, DARKBLUE)
  101. DrawFPS(700, 15)
  102. EndDrawing()
  103. '----------------------------------------------------------------------------------
  104. Wend
  105. ' De-Initialization
  106. '--------------------------------------------------------------------------------------
  107. ' Unload all postpro shaders
  108. For Local i:Int = 0 Until MAX_POSTPRO_SHADERS
  109. UnloadShader(shaders[i])
  110. Next
  111. UnloadTexture(texture) ' Unload texture
  112. UnloadModel(model) ' Unload model
  113. UnloadRenderTexture(target) ' Unload render texture
  114. CloseWindow() ' Close window and OpenGL context
  115. '--------------------------------------------------------------------------------------