how to generate a depthmap?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
xLainik
Prole
Posts: 2
Joined: Tue Jul 05, 2022 10:40 pm

how to generate a depthmap?

Post by xLainik »

Hi there!

I have been messing around with the g3d library (groverburger's 3D engine) and also learning to do some 3d shaders with it.
The thing is, I'm trying to generate a depthmap. I have been using a depth canvas, but it doesn't seems to work because the output texture is just an empty black image.

Any ideas about what I'm doing wrong? Maybe the love2d depth canvas is not supposed to be read like that? or did I set it wrong?

Here is the commented code for generating and visualizing the depthmap. Basiclly reading it in the shader as a sampler2DShadow just as the wiki says and then rendering it to the screen canvas to see how it looks like:

main.lua

Code: Select all

function love.load()
g3d = require("libs/g3d")
--Load the 3d models with g3d, which are internally löve meshes
player_model = g3d.newModel("assets/cat_model.obj", "assets/cat_texture.png", {0,0,5}, {0,0,0}, 1)
link_model = g3d.newModel("assets/link_wind_waker.obj", "assets/link_texture_flip.png", {1,1,2.5}, nil, 1)
shelf_model = g3d.newModel("assets/shelf_model.obj", "assets/shelf_texture.png", {0,2,0}, {0,0,0}, 1)

--Create the 2 canvases, the first one with depth pixel format, and the second one is just a standard canvas 
depth_buffer_canvas = love.graphics.newCanvas(1280, 1280, {format="depth24",readable=true})
shadow_buffer_canvas:setFilter("linear","linear")
depth_buffer_canvas:setDepthSampleMode("lequal")
test_canvas = love.graphics.newCanvas(1280, 1280)

--Load the fragment shader depth_map_view.glsl, that will read the depth_buffer_canvas and output it to the screen
depthMapTest_code = love.filesystem.read("shaders/depth_map_view.glsl")
depthMapTestShader = love.graphics.newShader(depthMapTest_code)

function love.draw()
    love.graphics.setMeshCullMode("front")
    love.graphics.setCanvas({depthstencil=shadow_buffer_canvas, depth=true})

    love.graphics.clear(1.0,1.0,1.0)
    love.graphics.setDepthMode("lequal", true)
    
    --Drawing the models to the depth_buffer_canvas
    player_model:draw()
    link_model :draw()
    shelf_model :draw()
    
    --Send the depthmap to the shader for rendering it to the screen
    love.graphics.setCanvas(test_canvas)
    depthMapTestShader:send("depthmap", depth_buffer_canvas)
    love.graphics.setCanvas()
end
depth_map_view.glsl

Code: Select all

#ifdef PIXEL

    uniform sampler2DShadow depthmap;
    
    vec4 effect( vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords )
    {
        float depthValue;
        depthValue = shadow2DProj(depthmap, vec4(screen_coords, 0.0, 1.0)).r;
        return vec4(vec3(depthValue), 1.0);
    }
#endif
Thanks in advance!
User avatar
Hydrogen Maniac
Citizen
Posts: 80
Joined: Sat Dec 19, 2015 9:59 pm

Re: how to generate a depthmap?

Post by Hydrogen Maniac »

When you draw your depthmap to the screen is it completely black or just very nearly completely black? The best way that I know of to check for this (aside from just squinting really hard at the screen) is to render the depthmap to the screen, take a screenshot, open said screenshot in your photo editing software of choice and use the color picker tool to see if any pixels have differnt color values.

Some pixels might have a value of (0,0,0) while others might have a value of (1,1,1) or (2,2,2). If this is the case then your code probably works just fine. I can also recomend looking at Alesan99's code for their game Hoarder's Horrible House of stuff which features some real time shadows.

I hope this helped :)
I'm way too sober for this...
User avatar
xLainik
Prole
Posts: 2
Joined: Tue Jul 05, 2022 10:40 pm

Re: how to generate a depthmap?

Post by xLainik »

Hydrogen Maniac wrote: Wed Jul 06, 2022 3:44 pm When you draw your depthmap to the screen is it completely black or just very nearly completely black? The best way that I know of to check for this (aside from just squinting really hard at the screen) is to render the depthmap to the screen, take a screenshot, open said screenshot in your photo editing software of choice and use the color picker tool to see if any pixels have differnt color values.

Some pixels might have a value of (0,0,0) while others might have a value of (1,1,1) or (2,2,2). If this is the case then your code probably works just fine. I can also recomend looking at Alesan99's code for their game Hoarder's Horrible House of stuff which features some real time shadows.

I hope this helped :)
Hey! Thank you so much for your guidance on the topic, now the depthmap works! But the solution was not what I expected: At the end I had to copy the code from Alesan99, because he has a very different solution in terms of how he handles the depthmap texture so I took the chance to learn how his method works, because I wasn't able to figure it out on my own.. Anyway the things I noted are:

First, he doesn't really sets a comparison mode for the depthmap texture with the "setDepthSampleMode()" functions. I'm not sure how that works, maybe by default the depthmap works without it. This alows you to sample the depthmap texture as a regular texture with the "Texel()" function.
Second, the shader that allows him to read and draw to the screen the depthmap actually doesn't have the depthmap as a uniform input as I thought. Rather the dephtmap comes in to the pixel shader as the default love2d parameter "Image texture". Also in this shader you must include a simple vertex shader that simply multiply the vertext position to clip space, that I didn't know was needed.

Also thanks for your advice on the pixels having values super closer to black like (3, 3, 3) or (5, 5, 5). Alesan converts those values to the corresponding [0, 255] range for actually being able to see them.

TL;DR: If you got stuck like me with the problem of generating a depthmap, you might check Alesan99's Hoarder's Horrible House of stuff code parts where he does all the depthmap related stuff.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 146 guests