|
| 1 | +from demosys.opengl import FBO |
| 2 | +from demosys.opengl import Texture |
| 3 | +from OpenGL import GL |
| 4 | + |
| 5 | + |
| 6 | +class DeferredRenderer: |
| 7 | + |
| 8 | + def __init__(self, width, height, gbuffer=None, lightbuffer=None): |
| 9 | + self.gbuffer = gbuffer |
| 10 | + self.lightbuffer = lightbuffer |
| 11 | + |
| 12 | + # FIXME: We might want double buffering here as well |
| 13 | + # Create geometry buffer if not supplied |
| 14 | + if not self.gbuffer: |
| 15 | + self.gbuffer = FBO() |
| 16 | + # RGBA color attachment |
| 17 | + self.gbuffer.add_color_attachment( |
| 18 | + Texture.create_2d(width=width, height=height, |
| 19 | + internal_format=GL.GL_RGBA8, format=GL.GL_RGBA, |
| 20 | + min_filter=GL.GL_NEAREST, mag_filter=GL.GL_NEAREST, |
| 21 | + wrap_s=GL.GL_CLAMP_TO_EDGE, wrap_t=GL.GL_CLAMP_TO_EDGE) |
| 22 | + ) |
| 23 | + # 16 bit RGB float buffer for normals |
| 24 | + self.gbuffer.add_color_attachment( |
| 25 | + Texture.create_2d(width=width, height=height, |
| 26 | + format=GL.GL_RGB, internal_format=GL.GL_RGB16F, type=GL.GL_FLOAT, |
| 27 | + min_filter=GL.GL_NEAREST, mag_filter=GL.GL_NEAREST, |
| 28 | + wrap_s=GL.GL_CLAMP_TO_EDGE, wrap_t=GL.GL_CLAMP_TO_EDGE) |
| 29 | + ) |
| 30 | + # 24 bit depth, 8 bit stencil |
| 31 | + self.gbuffer.set_depth_attachment( |
| 32 | + Texture.create_2d(width=width, height=height, |
| 33 | + internal_format=GL.GL_DEPTH24_STENCIL8, format=GL.GL_DEPTH_COMPONENT, |
| 34 | + min_filter=GL.GL_NEAREST, mag_filter=GL.GL_NEAREST, |
| 35 | + wrap_s=GL.GL_CLAMP_TO_EDGE, wrap_t=GL.GL_CLAMP_TO_EDGE, wrap_r=GL.GL_CLAMP_TO_EDGE) |
| 36 | + ) |
| 37 | + if not self.lightbuffer: |
| 38 | + self.lightbuffer = FBO() |
| 39 | + # 8 bit light accumulation buffer |
| 40 | + self.lightbuffer.add_color_attachment( |
| 41 | + Texture.create_2d(width=width, height=height, |
| 42 | + internal_format=GL.GL_RED, format=GL.GL_RED, type=GL.GL_UNSIGNED_BYTE, |
| 43 | + min_filter=GL.GL_NEAREST, mag_filter=GL.GL_NEAREST, |
| 44 | + wrap_s=GL.GL_CLAMP_TO_EDGE, wrap_t=GL.GL_CLAMP_TO_EDGE) |
| 45 | + ) |
| 46 | + # Attach the same depth buffer as the geometry buffer |
| 47 | + self.lightbuffer.set_depth_attachment(self.gbuffer.depth_buffer) |
| 48 | + |
| 49 | + def render_geometry(self): |
| 50 | + pass |
0 commit comments