阴影图矩阵变换

时间:2018-11-07 01:20:58

标签: dictionary matrix shadow

我似乎无法将这些阴影贴图矩阵包住头。当光线角度约为0,0,0的45度仰角位置时,它的效果很好。例如,如果太阳角度高(y)与x,z。阴影与模型不一致。轻度位置(-7,-10,7)或(-9,-10,0)可以正常工作。 (-1,-10,1)倾斜。这是我的代码示例。 -x左,-y向上,-z远。

public Vector3f cameraPosition = new Vector3f(0f, -10f, 7f);
public float[] lightPosition = new float[]{-7f, -10.0f, 7f}

将对象绘制为阴影贴图纹理

Matrix.setLookAtM(sunMatrix, 0, -lightPosition[0], lightPosition[1], lightPosition[2],
            0, 0, 0,
            0f, 1f, 0f);

    //projection matrix plus sun
    float width = (globals.glScreenSize/2);
    float height = (globals.glScreenSize/2);
    Matrix.orthoM(projectionMatrix, 0, -width, width,
            -height, height,
            -1f, 100f);
    tempMatrix = new float[16];
    Matrix.multiplyMM(tempMatrix, 0, projectionMatrix, 0, sunMatrix, 0);//add camera matrix to perspective
    projectionMatrix = tempMatrix.clone();

    //translate
    Matrix.setIdentityM(modelMatrix, 0);//set to 0

    //translate
    Matrix.translateM(modelMatrix, 0, modelPos.location.x,
            -(modelPos.location.y),
            modelPos.location.z);//move

    //rotate
    Matrix.rotateM(modelMatrix, 0, modelPos.angles.x, 1f, 0f, 0f);
    Matrix.rotateM(modelMatrix, 0, -modelPos.angles.y, 0f, 1f, 0f);
    Matrix.rotateM(modelMatrix, 0, -modelPos.angles.z, 0f, 0f, 1f);

    //scale
    Matrix.scaleM(modelMatrix, 0, modelPos.scales.x,
            modelPos.scales.y,
            modelPos.scales.z);//scale


    Matrix.multiplyMM(viewProjMatrix, 0, projectionMatrix, 0, modelMatrix, 0);
    //Matrix.multiplyMM(projectionMatrix, 0, globals.viewProjMatrix, 0, modelMatrix, 0);//perspective/model/view projection matrix
    finalMatrix = viewProjMatrix.clone();//final matrix created

绕y轴旋转并在屏幕上绘制阴影贴图

Matrix.setLookAtM(modelMatrix, 0, 0f, 0f, 0f, 
    globals.lightPosition[0], 0f, -globals.lightPosition[2],
                              0f, 1f, 0f);




    //scale
    Matrix.scaleM(modelMatrix, 0, scale.x, scale.y, scale.y);//scale

    Matrix.multiplyMM(projectionMatrix, 0, globals.viewProjMatrix, 0, modelMatrix, 0);//projection matrix

    finalMatrix = projectionMatrix.clone();//final matrix created

enter image description here

感谢您的帮助,

范本

1 个答案:

答案 0 :(得分:0)

我改变了

浮动高度=(globals.glScreenSize / 2)* Math.abs(lightPos.y);

所有阴影现在与模型对齐。如果您正在寻找一个简单的阴影贴图和obj loader示例,请查看https://github.com/Normfly/myOpenglES20

快乐的编码,

范本