阴影贴图矩阵变换



我似乎无法理解这些阴影映射矩阵。当灯光角度为0,0,0的45度左右时,效果非常好。例如,如果太阳角度相对于x,z为高(y(。阴影与模型不一致。(-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

在此处输入图像描述

感谢您的帮助,

标准

我改变了,

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

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

快乐编码,

标准

最新更新