I need a bit of help with Render Targets

I am trying to master the Defold render script by recreating the exercises on learnopengl.com. I am currently trying to recreate the exercises in the shadow mapping chapter. On recreating this in defold, the render target does not fill the window. Here’s a screenshot:

Generally, I am a bit confused about how to use render targets to create shadow mapping in defold and I have spent a long time trying to figure it out. I would appreciate any kind of help. Thanks in advance.

Here’s the project:

Shadows Depth.zip (5.1 MB)

2 Likes

Also, there’s this black rectangle in the editor view. I haven’t seen anything like it in the examples I follow.

Change to the quad2x2 mesh instead

I took a quick look and it looks like that rectangle is your depthmap model quad.

1 Like

Thanks. This fixed it.

1 Like

I am now trying to render the scene but it renders a grey screen. I know I am missing a simple thing. I can’t just figure out what the problem is. I still struggle a bit with the render script. I would appreciate any help I can get. Thanks in advance.

local function create_depth_buffer(w, h)
	local color_params = {
		format = graphics.TEXTURE_FORMAT_RGBA, -- R32F
		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"})

	self.scr_width = render.get_window_width()
	self.scr_height = render.get_window_height()

	self.shadow_width = 1024
	self.shadow_height = 1024

	-- lighting info
	self.z_buffer = create_depth_buffer(self.shadow_width, self.shadow_height)
	self.light_pos = vmath.vector3(2, 4, -1)

	self.mtx_constants = render.constant_buffer()
	self.camera = "main:/camera#camera"
end

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_pass(self)
	-- render depth of scene to texture from the light's perspective
	local near_plane, far_plane = 1, 7.5
	local mtx_light_proj = vmath.matrix4_orthographic(-10, 10, -10, 10, near_plane, far_plane)
	local mtx_light_view = vmath.matrix4_look_at(self.light_pos, vmath.vector3(), vmath.vector3(0, 1, 0))
	local mtx_light_space_matrix = mtx_light_proj * mtx_light_view

	self.mtx_constants.mtx_light_space_matrix = mtx_light_space_matrix

	render.set_view(vmath.matrix4())
	render.set_projection(vmath.matrix4())

	render.set_viewport(0, 0, self.shadow_width, self.shadow_height)

	-- enable depth buffer for writing
	render.set_depth_mask(true)
	render.enable_state(graphics.STATE_DEPTH_TEST)

	-- Set the render targer (framebuffer).
	render.set_render_target(self.z_buffer)--, {transient = { graphics.BUFFER_TYPE_DEPTH_BIT}} )

	clear_buffers()

	-- draw the predicate with the simple depth shader
	render.enable_material("simple_depth")

	render.draw(self.model_pred, {constants = self.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_shadows(self)
	render.set_camera(self.camera, { use_frustum = true })

	render.set_viewport(0, 0, self.scr_width, self.scr_height)

	clear_buffers()
	render.set_depth_mask(true)
	render.enable_state(graphics.STATE_DEPTH_TEST)

	render.enable_texture(1, self.z_buffer, graphics.BUFFER_TYPE_COLOR0_BIT)

	-- set light uniforms. Already done in the material
	--self.mtx_constants.view_pos = vmath.vector4(0, 11, 30, 0)
	--self.mtx_constants.light_pos = self.light_pos

 	render.draw(self.shadow_debug_pred)

 	render.disable_texture(1)

	render.set_depth_mask(true)
	render.disable_state(graphics.STATE_DEPTH_TEST)
end

function update(self, dt)
	render_depth_pass(self)
	render_shadows(self)
end

Project files:

Shadow Mapping.zip (5.1 MB)

Won’t you just put you code in a github/gitlab repo instead of sending zip files…? could help us to help you