I am trying to recreate the rendered depth map from this Learn OpenGL tutorial but when running the project, the depth map renders very briefly at the beginning before the whole screen goes blank. I’ve been trying to figure out the reason for this bug. I would appreciate any help I can get to figure this out. Thanks in advance.
Here’s the render script:
local function create_depth_buffer(w, h)
local color_params = {
format = graphics.TEXTURE_FORMAT_RGBA,
width = w,
height = h,
min_filter = graphics.TEXTURE_FILTER_NEAREST,
mag_filter = graphics.TEXTURE_FILTER_NEAREST,
u_wrap = graphics.TEXTURE_WRAP_REPEAT,
v_wrap = graphics.TEXTURE_WRAP_REPEAT
}
local depth_params = {
format = graphics.TEXTURE_FORMAT_DEPTH,
width = w,
height = h,
min_filter = graphics.TEXTURE_FILTER_NEAREST,
mag_filter = graphics.TEXTURE_FILTER_NEAREST,
u_wrap = graphics.TEXTURE_WRAP_REPEAT,
v_wrap = graphics.TEXTURE_WRAP_REPEAT
}
return render.render_target("shadow_buffer", {[graphics.BUFFER_TYPE_COLOR0_BIT] = color_params, [graphics.BUFFER_TYPE_DEPTH_BIT] = depth_params })
end
function init(self)
self.model_pred = render.predicate({"model"})
self.shadow_debug_pred = render.predicate({"shadow_debug"})
-- lighting info
self.z_buffer = create_depth_buffer(1024, 1024)
self.light_pos = vmath.vector3(2, 4, -1)
end
local frame = 0
local function clear_buffers()
render.clear({[graphics.BUFFER_TYPE_COLOR0_BIT] = vmath.vector4(0.1, 0.1, 0.1, 0.1), [graphics.BUFFER_TYPE_DEPTH_BIT] = 1})
end
local function render_depth_to_rt(self)
-- Set the render targer (framebuffer).
render.set_render_target(self.z_buffer)
render.set_viewport(0, 0, 1024, 1024)
--clear_buffers()
render.clear({[graphics.BUFFER_TYPE_COLOR0_BIT] = vmath.vector4(0.1, 0.1, 0.1, 0.1), [graphics.BUFFER_TYPE_DEPTH_BIT] = 1})
-- enable depth buffer for writing
render.set_depth_mask(true)
render.enable_state(graphics.STATE_DEPTH_TEST)
-- render depth of scene to texture from the light's perspective
local mtx_light_proj, mtx_light_view, mtx_light_space_matrix
local near_plane, far_plane = 1, 7.5
mtx_light_proj = vmath.matrix4_orthographic(-10, 10, -10, 10, near_plane, far_plane)
mtx_light_view = vmath.matrix4_look_at(self.light_pos, vmath.vector3(), vmath.vector3(0, 1, 0))
mtx_light_space_matrix = mtx_light_proj * mtx_light_view
local mtx_constants = render.constant_buffer()
mtx_constants.mtx_light_space_matrix = mtx_light_space_matrix
-- draw the predicate with the simple depth shader
render.enable_material("simple_depth")
render.draw(self.model_pred, {constants = mtx_constants})
render.disable_material()
render.disable_state(graphics.STATE_DEPTH_TEST)
render.set_depth_mask(false)
-- set the render target back to default
render.set_render_target(render.RENDER_TARGET_DEFAULT)
end
local function render_rt_to_screen(self)
render.set_viewport(0, 0, render.get_window_width(), render.get_window_height())
clear_buffers()
render.set_view(vmath.matrix4())
render.set_projection(vmath.matrix4())
--local plane_constants = render.constant_buffer()
--plane_constants.near_far_planes = vmath.vector4(1, 7.5, 0, 0)
render.enable_texture(0, self.z_buffer, graphics.BUFFER_TYPE_COLOR0_BIT)
render.draw(self.shadow_debug_pred)--, {constants = plane_constants})
render.disable_texture(0)
end
function update(self, dt)
-- debugging
frame = frame + 1
--clear_buffers("update")
render_depth_to_rt(self)
render_rt_to_screen(self)
end
Here’s the project
Depth Map Render.zip (2.2 MB)