|
| 1 | +from pyray import * |
| 2 | +import raylib as rl |
| 3 | + |
| 4 | +#------------------------------------------------------------------------------------ |
| 5 | +# Define custom functions required for the example |
| 6 | +#------------------------------------------------------------------------------------ |
| 7 | +# Load custom render texture, create a writable depth texture buffer |
| 8 | +def LoadRenderTextureDepthTex(width, height): |
| 9 | + |
| 10 | + target = RenderTexture() |
| 11 | + |
| 12 | + target.id = rl_load_framebuffer(width, height) # Load an empty framebuffer |
| 13 | + |
| 14 | + if target.id > 0: |
| 15 | + |
| 16 | + rl_enable_framebuffer(target.id) |
| 17 | + |
| 18 | + # Create color texture (default to RGBA) |
| 19 | + target.texture.id = rl_load_texture(None, width, height, PixelFormat.PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1) |
| 20 | + target.texture.width = width |
| 21 | + target.texture.height = height |
| 22 | + target.texture.format = PixelFormat.PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 |
| 23 | + target.texture.mipmaps = 1 |
| 24 | + |
| 25 | + # Create depth texture buffer (instead of raylib default renderbuffer) |
| 26 | + target.depth.id = rl_load_texture_depth(width, height, False) |
| 27 | + target.depth.width = width |
| 28 | + target.depth.height = height |
| 29 | + target.depth.format = 19 #DEPTH_COMPONENT_24BIT? |
| 30 | + target.depth.mipmaps = 1 |
| 31 | + |
| 32 | + # Attach color texture and depth texture to FBO |
| 33 | + rl_framebuffer_attach(target.id, target.texture.id, rl.RL_ATTACHMENT_COLOR_CHANNEL0, rl.RL_ATTACHMENT_TEXTURE2D, 0) |
| 34 | + rl_framebuffer_attach(target.id, target.depth.id, rl.RL_ATTACHMENT_DEPTH, rl.RL_ATTACHMENT_TEXTURE2D, 0) |
| 35 | + |
| 36 | + # Check if fbo is complete with attachments (valid) |
| 37 | + if rl_framebuffer_complete(target.id): |
| 38 | + print(f"FBO: [{target.id}] Framebuffer object created successfully") |
| 39 | + |
| 40 | + rl_disable_framebuffer() |
| 41 | + |
| 42 | + else: |
| 43 | + print("FBO: Framebuffer object can not be created") |
| 44 | + |
| 45 | + return target |
| 46 | + |
| 47 | + |
| 48 | +# Unload render texture from GPU memory (VRAM) |
| 49 | +def UnloadRenderTextureDepthTex(target): |
| 50 | + |
| 51 | + if target.id > 0: |
| 52 | + |
| 53 | + # Color texture attached to FBO is deleted |
| 54 | + rl_unload_texture(target.texture.id) |
| 55 | + rl_unload_texture(target.depth.id) |
| 56 | + |
| 57 | + # NOTE: Depth texture is automatically |
| 58 | + # queried and deleted before deleting framebuffer |
| 59 | + rl_unload_framebuffer(target.id) |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +screenWidth = 800 |
| 64 | +screenHeight = 450 |
| 65 | + |
| 66 | +init_window(screenWidth, screenHeight, "raylib [shaders] example - write depth buffer") |
| 67 | + |
| 68 | +# The shader inverts the depth buffer by writing into it by `gl_FragDepth = 1 - gl_FragCoord.z` |
| 69 | +shader = load_shader("","resources/shaders/glsl330/write_depth.fs") |
| 70 | + |
| 71 | +# Use Customized function to create writable depth texture buffer |
| 72 | +target = LoadRenderTextureDepthTex(screenWidth, screenHeight) |
| 73 | + |
| 74 | +# Define the camera to look into our 3d world |
| 75 | +camera = Camera3D() |
| 76 | +camera.position = (2.0, 2.0, 3.0) # Camera position |
| 77 | +camera.target = (0.0, 0.5, 0.0) # Camera looking at point |
| 78 | +camera.up = (0.0, 1.0, 0.0) # Camera up vector (rotation towards target) |
| 79 | +camera.fovy = 45.0 # Camera field-of-view Y |
| 80 | +camera.projection = CameraProjection.CAMERA_PERSPECTIVE # Camera projection type |
| 81 | + |
| 82 | + |
| 83 | +set_target_fps(60) # Set our game to run at 60 frames-per-second |
| 84 | +#-------------------------------------------------------------------------------------- |
| 85 | + |
| 86 | +# Main game loop |
| 87 | +while not window_should_close(): # Detect window close button or ESC key |
| 88 | + |
| 89 | + # Update |
| 90 | + #---------------------------------------------------------------------------------- |
| 91 | + update_camera(camera, CameraMode.CAMERA_ORBITAL) |
| 92 | + #---------------------------------------------------------------------------------- |
| 93 | + |
| 94 | + # Draw |
| 95 | + #---------------------------------------------------------------------------------- |
| 96 | + |
| 97 | + # Draw into our custom render texture (framebuffer) |
| 98 | + begin_texture_mode(target) |
| 99 | + clear_background(WHITE) |
| 100 | + |
| 101 | + begin_mode_3d(camera) |
| 102 | + begin_shader_mode(shader) |
| 103 | + draw_cube_wires_v((0.0, 0.5, 1.0) , (1.0,1.0, 1.0), RED) |
| 104 | + draw_cube_v((0.0, 0.5, 1.0) , (1.0, 1.0, 1.0) , PURPLE) |
| 105 | + draw_cube_wires_v((0.0, 0.5, -1.0), (1.0, 1.0, 1.0) , DARKGREEN) |
| 106 | + draw_cube_v((0.0, 0.5, -1.0) , (1.0, 1.0, 1.0) , YELLOW) |
| 107 | + draw_grid(10, 1.0) |
| 108 | + end_shader_mode() |
| 109 | + end_mode_3d() |
| 110 | + end_texture_mode() |
| 111 | + |
| 112 | + # Draw into screen our custom render texture |
| 113 | + begin_drawing() |
| 114 | + clear_background(RAYWHITE) |
| 115 | + |
| 116 | + draw_texture_rec(target.texture, Rectangle(0, 0, screenWidth, -screenHeight) , (0, 0) , WHITE) |
| 117 | + draw_fps(10, 10) |
| 118 | + end_drawing() |
| 119 | + |
| 120 | + |
| 121 | +# De-Initialization |
| 122 | +#-------------------------------------------------------------------------------------- |
| 123 | +UnloadRenderTextureDepthTex(target) |
| 124 | +unload_shader(shader) |
| 125 | + |
| 126 | +close_window() # Close window and OpenGL context |
| 127 | + |
| 128 | + |
0 commit comments