JOGL 阴影映射

Posted

技术标签:

【中文标题】JOGL 阴影映射【英文标题】:JOGL shadow mapping 【发布时间】:2016-02-25 22:12:32 【问题描述】:

我正在使用 JOGL 将 OpenGL SuperBible 演示翻译成 Java。直到第 12 章的所有演示都进行得很顺利,但现在我陷入了阴影映射。这是我的设置:

public class ShadowMap implements GLEventListener

    public static void main(String[] args)
    
        ShadowMap rend = new ShadowMap();
        window.NEWTWindowStarter.init(rend, null, null);
    

    private int light_program, camera_program;
    private int[] obj, bkg;

    private int camera_mv_pointer, camera_proj_pointer, light_mvp_pointer, camera_shadow_pointer;

    private int fbo, depth_tex;
    private int screen_width, screen_height;
    private float aspect;

    private double[] light_position, camera_position;
    private double[][] light_proj_matrix, light_view_matrix;
    private double[][] camera_proj_matrix, camera_view_matrix;
    private double[][] bias_matrix, shadow_matrix;
    private double[][] obj_model_matrix, bkg_model_matrix;

    @Override
    public void init(GLAutoDrawable glAutoDrawable)
    
        GL4 gl = glAutoDrawable.getGL().getGL4bc();

        ObjectParser parser = new ObjectParser("pawn_s.obj");
        obj = BufferController.prepareVAO(gl, parser.getDataHolder());

        parser = new ObjectParser("bkg.obj");
        bkg = BufferController.prepareVAO(gl, parser.getDataHolder());

        light_program = ShaderController.init(gl, "shaders/demo/d25/light_v.glsl", "shaders/demo/d25/light_f.glsl");
        light_mvp_pointer = gl.glGetUniformLocation(light_program, "mvp");

        camera_program = ShaderController.init(gl, "shaders/demo/d25/camera_v.glsl", "shaders/demo/d25/camera_f.glsl");
        camera_mv_pointer = gl.glGetUniformLocation(camera_program, "mv_matrix");
        camera_proj_pointer = gl.glGetUniformLocation(camera_program, "proj_matrix");
        camera_shadow_pointer = gl.glGetUniformLocation(camera_program, "shadow_matrix");


        depth_tex = BufferController.generateTextureId(gl);
        gl.glBindTexture(gl.GL_TEXTURE_2D, depth_tex);
        gl.glTexStorage2D(gl.GL_TEXTURE_2D, 11, gl.GL_DEPTH_COMPONENT32F, 1024, 1024);

        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR);
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR);

        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_BORDER);
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_BORDER);

        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_COMPARE_MODE, gl.GL_COMPARE_REF_TO_TEXTURE);
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_COMPARE_FUNC, gl.GL_LEQUAL);

        gl.glActiveTexture(gl.GL_TEXTURE0);
        gl.glBindTexture(gl.GL_TEXTURE_2D, depth_tex);

        fbo = BufferController.generateFrameId(gl);
        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, fbo);
        gl.glFramebufferTexture(gl.GL_FRAMEBUFFER, gl.GL_DEPTH_ATTACHMENT, depth_tex, 0);
        gl.glDrawBuffer(gl.GL_NONE);
        gl.glReadBuffer(gl.GL_NONE);

        if(gl.glCheckFramebufferStatus(gl.GL_FRAMEBUFFER) == gl.GL_FRAMEBUFFER_COMPLETE)
            System.out.println("FrameBuffer OK!");
        else
            System.out.println("FrameBuffer FAIL!");

        gl.glBindTexture(gl.GL_TEXTURE_2D, 0);
        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0);

        gl.glEnable(gl.GL_DEPTH_TEST);
    


    @Override
    public void dispose(GLAutoDrawable glAutoDrawable)

    @Override
    public void display(GLAutoDrawable glAutoDrawable)
    
        GL4bc gl = glAutoDrawable.getGL().getGL4bc();

        computerMatrices();
        gl.glEnable(gl.GL_DEPTH_TEST);

        // depth pass
        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, fbo);
        gl.glViewport(0, 0, 1024, 1024);

        gl.glUseProgram(light_program);
        drawFromLight(gl, obj, obj_model_matrix);
        drawFromLight(gl, bkg, bkg_model_matrix);

        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0);


        // final pass
        gl.glViewport(0, 0, screen_width, screen_height);
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
        gl.glBindTexture(gl.GL_TEXTURE_2D, depth_tex);
        gl.glDrawBuffer(gl.GL_BACK);

        gl.glUseProgram(camera_program);
        drawFromCamera(gl, obj, obj_model_matrix);
        drawFromCamera(gl, bkg, bkg_model_matrix);

        gl.glBindTexture(gl.GL_TEXTURE_2D, 0);
    

    private void computerMatrices()
    
        bkg_model_matrix = Matrix.model(0, 0, 0, 0, 0, 0, 1);
        obj_model_matrix = Matrix.model(0, 0, 0, 0, 0, 0, 1);

        light_position = new double[]10, 10, 10;
        camera_position = new double[]10, 0, 10;

        light_proj_matrix = Matrix.frustum(-1.0, 1.0, -1.0, 1.0, 1.0, 200.0);
        light_view_matrix = Matrix.lookAt(light_position, new double[]0, 0, 0, new double[]0, 1, 0);

        camera_proj_matrix = Matrix.perspective(60, aspect, 0.1f, 1000f);
        camera_view_matrix = Matrix.lookAt(camera_position, new double[]0, 0, 0, new double[]0, 1, 0);

        bias_matrix = new double[][]
                0.5, 0.0, 0.0, 0.0,
                0.0, 0.5, 0.0, 0.0,
                0.0, 0.0, 0.5, 0.0,
                0.5, 0.5, 0.5, 1.0;

        shadow_matrix = Matrix.mult(bias_matrix, light_proj_matrix, light_view_matrix);
    

    private void drawFromLight(GL4bc gl, int[] obj, double[][] model_matrix)
    
        gl.glUniformMatrix4fv(light_mvp_pointer, 1, false, Matrix.toArrayF(Matrix.mult(light_proj_matrix, light_view_matrix, model_matrix)), 0);
        BufferControllerDep.renderTrianglesVAO(gl, obj[0], obj[1], obj[2]);
    

    private void drawFromCamera(GL4bc gl, int[] obj, double[][] model_matrix)
    
        gl.glUniformMatrix4fv(camera_shadow_pointer, 1, false, Matrix.toArrayF(Matrix.mult(shadow_matrix, model_matrix)), 0);
        gl.glUniformMatrix4fv(camera_proj_pointer, 1, false, Matrix.toArrayF(camera_proj_matrix), 0);
        gl.glUniformMatrix4fv(camera_mv_pointer, 1, false, Matrix.toArrayF(Matrix.mult(camera_view_matrix, model_matrix)), 0);
        BufferControllerDep.renderTrianglesVAO(gl, obj[0], obj[1], obj[2]);
    

    @Override
    public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height)
    
        screen_width = width;
        screen_height = height;
        aspect = (1.0f * screen_width) / screen_height;
    

灯光着色器是非常基础的。 Camera Vertex shader如下:

#version 430 core

uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
uniform mat4 shadow_matrix;

layout (location = 0) in vec4 position;
layout (location = 1) in vec3 normal;

out vec4 shadow_coord;

uniform vec3 light_pos = vec3(10.0, 10.0, 10.0);
out vec3 N;
out vec3 L;

void main(void)

    N = normalize(mat3(mv_matrix) * normal);
    L = normalize(light_pos - (mv_matrix * position).xyz);

    shadow_coord = shadow_matrix * position;

    gl_Position = proj_matrix * mv_matrix * position;

和相机片段着色器:

#version 430 core

layout (location = 0) out vec4 color;

layout (binding = 0) uniform sampler2DShadow shadow_tex;

in vec4 shadow_coord;

uniform vec3 D = vec3(0.9, 0.8, 1.0);
in vec3 N;
in vec3 L;

void main(void)

    vec3 diffuse = dot(N, L) * D;

    float shadow = textureProj(shadow_tex, shadow_coord);

//    color = vec4(diffuse, 1.0);
    color = shadow * vec4(1);

仅渲染漫反射效果很好。投影、LookAt 和转换矩阵似乎可以工作。

但是,当尝试渲染阴影时,它会失败:

将光线移近物体会产生这样的效果:

我使用自己的 Matrix 和 Vector 类,如果需要可以提供源代码。

感谢任何见解。谢谢!

【问题讨论】:

乍一看似乎还不错。无论如何,在最后一次传递中绑定纹理之前,我总是会调用gl.glActiveTexture(gl.GL_TEXTURE0);。我不知道this sample 是否有帮助。但是,您需要找到一种调试方法,打开调试输出并退回到更简单的形状,例如要阴影的单个三角形 感谢您的建议。我将问题追溯到反转矩阵多顺序。另外,我想运行你的演示,但我遇到了一个问题:运行你直接引用的演示会打开一个黑色窗口,但没有进行渲染(永远不会到达render(GL gl) 中的断点)。我做错了什么? 控制台打印有问题吗?无论如何,为找到问题而喝彩:) 什么都没有。我直接从 Gl_400_fbo_shadow 类启动它。我看到它在测试类中通过assert render(gl);,但它没有到达 Gl_400_fbo_shadow 中render(GL gl) 中的任何断点。我对所有演示都得到了相同的结果... 你会为此打开一个issue 吗? 【参考方案1】:

问题是由 Java 代码中的逆矩阵乘法顺序引起的。这是更正的代码:

private void drawFromLight(GL4bc gl, int[] obj, double[][] model_matrix)

    gl.glUniformMatrix4fv(light_mvp_pointer, 1, false,
            Matrix.toArrayF(Matrix.mult(model_matrix, light_view_matrix, light_proj_matrix)), 0);

    BufferControllerDep.renderTrianglesVAO(gl, obj[0], obj[1], obj[2]);


private void drawFromCamera(GL4bc gl, int[] obj, double[][] model_matrix)

    gl.glUniformMatrix4fv(camera_shadow_pointer, 1, false,
            Matrix.toArrayF(Matrix.mult(model_matrix, shadow_matrix)), 0);

    gl.glUniformMatrix4fv(camera_mv_pointer, 1, false,
            Matrix.toArrayF(Matrix.mult(model_matrix, camera_view_matrix)), 0);

    gl.glUniformMatrix4fv(camera_mvp_pointer, 1, false,
            Matrix.toArrayF(Matrix.mult(model_matrix, camera_view_matrix, camera_proj_matrix)), 0);

    BufferControllerDep.renderTrianglesVAO(gl, obj[0], obj[1], obj[2]);

使用相同的着色器将产生以下结果:

同时添加

gl.glEnable(gl.GL_POLYGON_OFFSET_FILL);
gl.glPolygonOffset(2.0f, 0.0f);

在执行“light pass”之前消除失真:

混合阴影和漫反射会产生这样的效果:

还有改进的余地...

【讨论】:

以上是关于JOGL 阴影映射的主要内容,如果未能解决你的问题,请参考以下文章

如何在 WebGL 中实现阴影映射?

如何使用 QOpenGLFrameBufferObject 进行阴影映射?

屏幕空间的阴影映射技术个人理解

OpenGL阴影映射,具有多个glDrawElements的帧缓冲区

图形学31 Unity 的光源衰减和阴影

在将截锥体从相机空间转换为光空间以进行阴影映射时遇到重大问题