0

I can't seem to wrap my head around these shadow map matrices. It works great when light angles are about a 45 degree lookat position to 0,0,0. For example, if the sun angle is high(y) vs x,z. The shadows don't line up with the models. A light position of (-7,-10,7) or (-9,-10,0) works fine. (-1,-10,1) is skewed. Here is my code example. -x is left, -y is up and -z is far.

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

draw objects to shadow map texture

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

rotate around y axis and draw shadow map to screen

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

Thanks for any help,

Norm

Normfly
  • 11
  • 3

1 Answers1

0

I changed,

float height = (globals.glScreenSize/2) * Math.abs(lightPos.y);

Shadows all line up with the models now. If you are looking for a simple shadow map and obj loader example, check out https://github.com/Normfly/myOpenglES20

Happy coding,

Norm

Normfly
  • 11
  • 3