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
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