Java JOGL中的阴影贴图(纹理矩阵问题)

Java JOGL中的阴影贴图(纹理矩阵问题),java,opengl,jogl,Java,Opengl,Jogl,我正在尝试使用用于OpenGL的JOGL包装器实现阴影映射技术。我遵循的教程来自,但我无法正确渲染阴影纹理。我认为我的问题在于错误地使用了纹理阴影矩阵,或者可能是矩阵的近似运算 这是我到目前为止得到的结果(正如您所看到的,阴影渲染非常混乱): 从灯光角度渲染的深度纹理应按如下方式渲染: 纹理矩阵应通过以下矩阵乘法创建: T是纹理矩阵,Pl是灯光的投影矩阵,Vl是灯光的视图矩阵,Vc是相机的视图矩阵 我使用JAMA工具箱进行矩阵乘法。我从gamedev stackexchange的线程中得到

我正在尝试使用用于OpenGL的JOGL包装器实现阴影映射技术。我遵循的教程来自,但我无法正确渲染阴影纹理。我认为我的问题在于错误地使用了纹理阴影矩阵,或者可能是矩阵的近似运算

这是我到目前为止得到的结果(正如您所看到的,阴影渲染非常混乱):

从灯光角度渲染的深度纹理应按如下方式渲染:

纹理矩阵应通过以下矩阵乘法创建:

T是纹理矩阵,Pl是灯光的投影矩阵,Vl是灯光的视图矩阵,Vc是相机的视图矩阵

我使用JAMA工具箱进行矩阵乘法。我从gamedev stackexchange的线程中得到了灵感

    //----- Atributes (Matrices) ----------
float biasmatrix[] = {
          0.5f, 0.0f, 0.0f, 0.0f,
          0.0f, 0.5f, 0.0f, 0.0f,
          0.0f, 0.0f, 0.5f, 0.0f,
          0.5f, 0.5f, 0.5f, 1.0f
        };

Matrix bi = new Matrix(4, 4);
Matrix lp = new Matrix(4, 4);
Matrix lv = new Matrix(4, 4);
Matrix cv = new Matrix(4, 4);
Matrix st = new Matrix(4, 4);

    private void initializeMatrices(GL2 gl) {
                lightProjectionMatrix = new float[16];
                lightViewMatrix= new float[16];
                cameraProjectionMatrix= new float[16];
                cameraViewMatrix= new float[16];
                textureShadowMatrix = new float[16];

                // ------------- Camera -------------
                //Camera projection matrix
                gl.glMatrixMode(GL2.GL_PROJECTION); // choose projection matrix
                    gl.glLoadIdentity(); // reset projection matrix
                    glu.gluPerspective(zoomFactor, (float)wwidth/wheight, 1.0f, 100.0f);

                //Camera view matrix
                gl.glMatrixMode(GL2.GL_MODELVIEW); // choose projection matrix
                gl.glLoadIdentity();
                    glu.gluLookAt(
                             cameraPosition[0],  cameraPosition[1], cameraPosition[2],//Camera position
                             0.0f, 1.0f, 0.0, // Where to look
                            0.f, 1.0f, 0.0f);  //Positive Z vector

                //Get matrices
                gl.glGetFloatv(GL2.GL_PROJECTION_MATRIX, cameraProjectionMatrix, 0);
                gl.glGetFloatv(GL2.GL_MODELVIEW_MATRIX, cameraViewMatrix, 0);




                // ------------- Light -------------            
                //Light projection matrix
                gl.glMatrixMode(GL2.GL_PROJECTION); // choose projection matrix
                    gl.glLoadIdentity();
                    glu.gluPerspective(45.0f, 1.0f, 2.0f, 10.0f);


                //Light view matrix
                gl.glMatrixMode(GL2.GL_MODELVIEW); // choose projection matrix
                gl.glLoadIdentity();
                  glu.gluLookAt(
                     lightPosition[0],  lightPosition[1], lightPosition[2],//Light position
                     0.0f, 0.0f, 0.0, // Where to look
                    0.f, 1.0f, 0.0f);  //Positive Z vector

                //Get matrices        
                gl.glGetFloatv(GL2.GL_PROJECTION_MATRIX, lightProjectionMatrix, 0); 
                gl.glGetFloatv(GL2.GL_MODELVIEW_MATRIX, lightViewMatrix, 0);

                //Store values into JAMA matrices

                bi = new Matrix(buildJamaMatrix(biasmatrix));
                lp = new Matrix(buildJamaMatrix(lightProjectionMatrix));
                lv = new Matrix(buildJamaMatrix(lightViewMatrix));
                cv = new Matrix(buildJamaMatrix(cameraViewMatrix));

            }

        private void calcTextureMatrix(GL2 gl) {

                //Do tranformations
                st = bi.times(lp).times(lv);

                //Transpose
                st = st.transpose();
        }

    //I wrote this function to be able to convert a float[] array, which represents
    //the matrix into 4x4 JamaMatrix format

public double[][] buildJamaMatrix(float[] matrix){
    double[][] niceMatrix = new double[4][4];

    //For every item of a matrix
    int i = 0;

    //Build row
    for (int j = 0; j < 4; j++) {
        //Build item
        for (int k = 0; k < 4; k++) {
            niceMatrix[j][k] = matrix[i];
            i++;
        }
    }
    return niceMatrix;
}

public float[] unpackRowFromJamaMatrix(Matrix jamaMatrix, int row){
    float[] rowContents = new float[4];

    for (int i = 0; i < 4; i++) {
        rowContents[i] = (float) jamaMatrix.get(i, row);
    }

    return rowContents;
}
我不知道这是否有助于排除故障,但初始化后的矩阵如下所示:

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

Light projection matrix -------------
2.4142134189605713 , 0.0 , 0.0 , 0.0
0.0 , 2.4142134189605713 , 0.0 , 0.0
0.0 , 0.0 , -1.5 , -1.0
0.0 , 0.0 , -5.0 , 0.0

Light view matrix -------------
-1.0 , 0.0 , 0.0 , 0.0
0.0 , 0.1961161345243454 , 0.9805806875228882 , 0.0
0.0 , 0.9805806875228882 , -0.1961161345243454 , 0.0
0.0 , 6.463912427534524E-8 , -6.118823528289795 , 1.0

Camera view matrix -------------
1.0 , 0.0 , 0.0 , 0.0
0.0 , 0.7808688282966614 , 0.6246950626373291 , 0.0
0.0 , -0.6246950626373291 , 0.7808688282966614 , 0.0
0.0 , -0.7808688282966614 , -7.027819633483887 , 1.0

当我做阴影贴图时,我遇到了一个非常相似的问题,这归结为我在着色器中计算平行光,而我的所有矩阵都来自点光源。通过一个简单的/w解决了这个问题。这对你没有多大帮助,但也许其他人会认为这是朝着正确的方向推进。祝你好运。我没有时间详细跟踪你的代码,但请仔细查看你是否无意中在某处转置了一个矩阵。德国劳埃德船级社需要一个带有连接列的一维16元素矩阵。您可能想在这里抛出Jama,并为GL格式的矩阵编写自己的矩阵乘法。在向Jama格式进行不必要的复制/从Jama格式复制期间,您当前的代码将产生大量垃圾。感谢各位的建议,我将尝试更仔细地查看它。此外,您应该避免使用传统的OpenGL,即现代的Hello三角形
Bias matrix -------------
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

Light projection matrix -------------
2.4142134189605713 , 0.0 , 0.0 , 0.0
0.0 , 2.4142134189605713 , 0.0 , 0.0
0.0 , 0.0 , -1.5 , -1.0
0.0 , 0.0 , -5.0 , 0.0

Light view matrix -------------
-1.0 , 0.0 , 0.0 , 0.0
0.0 , 0.1961161345243454 , 0.9805806875228882 , 0.0
0.0 , 0.9805806875228882 , -0.1961161345243454 , 0.0
0.0 , 6.463912427534524E-8 , -6.118823528289795 , 1.0

Camera view matrix -------------
1.0 , 0.0 , 0.0 , 0.0
0.0 , 0.7808688282966614 , 0.6246950626373291 , 0.0
0.0 , -0.6246950626373291 , 0.7808688282966614 , 0.0
0.0 , -0.7808688282966614 , -7.027819633483887 , 1.0