Libgdx中错误的阴影映射实现

时间:2019-06-30 20:41:15

标签: libgdx shader shadow shadow-mapping

Hi希望从阴影贴图开始学习计算机图形技术。 我试图根据Internet上的一些文章来实现它,但是它确实出错了。我真的很挣扎。有人可以帮助我提供代码吗? (否则,如果您在Libgdx中实现了Shadow Maps,我希望看到它)。

以下是深度图的两个着色器:

attribute vec3 a_position;
attribute vec3 a_normal;
attribute vec2 a_texCoord0;

uniform mat4 u_worldTrans;
uniform mat4 u_projViewTrans; //Light Camera

varying vec4 v_positionLightFrame;

void main() {
    v_positionLightFrame = u_projViewTrans * u_worldTrans * vec4(a_position, 1.0);
    gl_Position = v_positionLightFrame;
}
#ifdef GL_ES
precision mediump float;
#endif

varying vec4 v_positionLightFrame;
uniform float cameraFar;

void main()
{
    gl_FragColor = vec4(v_positionLightFrame.z/cameraFar, v_positionLightFrame.z/cameraFar, v_positionLightFrame.z/cameraFar, 1.0);
}

当前,我将所有内容存储在RGBA纹理中。我知道我应该只使用一个通道的简单纹理。 这是用于渲染的两个着色器:

attribute vec3 a_position;
attribute vec3 a_normal;
attribute vec2 a_texCoord0;

uniform mat4 u_worldTrans;
uniform mat4 u_projViewTrans; 
uniform mat4 dLightCamProjView; //"The Light Camera"

varying vec4 v_positionLS;
varying vec2 v_texCoords;

void main() {
    v_texCoords = a_texCoord0;
    v_positionLS =  dLightCamProjView*u_worldTrans * vec4(a_position, 1.0);
    gl_Position = u_projViewTrans* u_worldTrans * vec4(a_position, 1.0);
}
#ifdef GL_ES
precision mediump float;
#endif

varying vec4 v_positionLS;
varying vec2 v_texCoords;


uniform sampler2D depthMap;
uniform sampler2D diffuseTexture;
uniform float cameraFar;

void main()
{
    vec3 color = vec3(0.0f,0.0f,0.0f);
    vec4 pos_aux = v_positionLS;
    vec3 pos = pos_aux.xyz;
    pos.xy = pos.xy/pos_aux.w;
    pos.xy = pos.xy * 0.5 + 0.5;
    pos.z = pos.z/cameraFar;
    float diff = pos.z - texture(depthMap, pos.xy).z;
    if(diff< 0.002)
    {
        color = texture(diffuseTexture, v_texCoords).rgb;
    }
    gl_FragColor = vec4(color, 1.0f);
}

这是我得到的结果: image


如果您想要我的参数在libgdx中:

cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(255.f,255.f,-2.54f);
cam.lookAt(0,0,0);
cam.near = 1f;
cam.far = 1000f;
cam.update();

dLight = new DirectionalLight(new Vector3(-0.7f, -0.7f, 0.0f), new Vector3(0.8f, 0.8f, 0.8f));
dLightCam = new PerspectiveCamera(67, DEPTH_SIZE, DEPTH_SIZE);
dLightCam.position.set(255f,255f,-2.54f);
dLightCam.lookAt(new Vector3(0.0f, 0.0f, 0.0f));
dLightCam.far = 1000f;
dLightCam.update();
fboDLight = new FrameBuffer(Pixmap.Format.RGBA8888, DEPTH_SIZE, DEPTH_SIZE, true);
depthMap = new Texture(DEPTH_SIZE, DEPTH_SIZE, Pixmap.Format.RGBA8888);

我打算使用透视相机代替正射相机,因为我计划实现点光源。

谢谢。

0 个答案:

没有答案