GLSL中的纹理投影

GLSL中的纹理投影,glsl,textures,projection,Glsl,Textures,Projection,我正在尝试直接基于这个示例实现纹理投影。我的环境是Android 2.2上的OpenGL ES 2.0 矩阵数学从来不是我的强项,我怀疑我的问题出在纹理投影矩阵设置(texGenMatrix)的某个地方。我已经尝试了一切可以想象的方法来让它工作,但是投影没有正确出来。如果能多看几眼我的代码,我将不胜感激。谢谢 此处未显示的是我的几何体(穿过原点的水平面),以及其他一些类似管道的着色器统一/属性绑定 摄像机设置: // Set the camera projection matrix Matri

我正在尝试直接基于这个示例实现纹理投影。我的环境是Android 2.2上的OpenGL ES 2.0

矩阵数学从来不是我的强项,我怀疑我的问题出在纹理投影矩阵设置(texGenMatrix)的某个地方。我已经尝试了一切可以想象的方法来让它工作,但是投影没有正确出来。如果能多看几眼我的代码,我将不胜感激。谢谢

此处未显示的是我的几何体(穿过原点的水平面),以及其他一些类似管道的着色器统一/属性绑定

摄像机设置:

// Set the camera projection matrix 
Matrix.frustumM(cameraProjMatrix, 0, 
    -nearPlaneSize.x, nearPlaneSize.x,  // near plane left/right
    -nearPlaneSize.y, nearPlaneSize.y,  // near plane bottom/top
    nearPlaneDistance, farPlaneDistance // near/far plane distance
);

// Set the camera view matrix
Matrix.setLookAtM(cameraViewMatrix, 0, 
    0, 100, 100,  // eye
    0, 0, 0,  // center
    0, 1, 0 // up-vector
);

// Translate the model as needed
Matrix.translateM(modelMatrix, 0, x, y, z);

// Calculate the model * view matrix
Matrix.multiplyMM(modelViewMatrix, 0, cameraViewMatrix, 0, modelMatrix, 0);

// Calculate the inverse camera view matrix (needed in the vert shader)
Matrix.invertM(inverseCameraViewMatrix, 0, cameraViewMatrix, 0);
纹理投影设置:

// Set a texture projection matrix
Matrix.frustumM(texProjMatrix, 0, 
    -nearPlaneSize.x, nearPlaneSize.x,  // near plane left/right
    -nearPlaneSize.y, nearPlaneSize.y,  // near plane bottom/top
    nearPlaneDistance, farPlaneDistance // near/far plane distance
);


// Set the texture projection to point down at the origin
Matrix.setLookAtM(texViewMatrix, 0, 
    0, 100, 10,  // eye
    0, 0, 0,  // center
    0, 1, 0 // up-vector
);

 // scale bias
float[] scaleBiasMatrix = new float[] { 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
};

// calculate the TexGenMatrix 
Matrix.multiplyMM(texGenMatrix, 0, scaleBiasMatrix, 0, texProjMatrix, 0);
Matrix.multiplyMM(texGenMatrix, 0, texGenMatrix, 0, texViewMatrix, 0);
顶点着色器:

attribute vec3 aPosition; // vertex x y z 

uniform mat4 texGenMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 cameraProjMatrix;

varying vec4 vProjCoord;

void main() {
    gl_Position = cameraProjMatrix * modelViewMatrix * aPosition;
    vec4 posEye = modelViewMatrix * aPosition;
    vec4 posWorld = inverseCameraViewMatrix * posEye;
    vProjCoord = texGenMatrix * posWorld;
}
uniform sampler2D projectionMap;     
varying vec4 vProjCoord;

void main() {
    vec3 finalColor = vec3( 0.5, 0.5, 0.5);
    if (vProjCoord.q > 0.0) {
        vec4 ProjMapColor = texture2DProj(projectionMap, vProjCoord);
        finalColor += ProjMapColor.rgb;
    }
    gl_FragColor = vec4(finalColor, 1.0);
}
片段着色器:

attribute vec3 aPosition; // vertex x y z 

uniform mat4 texGenMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 cameraProjMatrix;

varying vec4 vProjCoord;

void main() {
    gl_Position = cameraProjMatrix * modelViewMatrix * aPosition;
    vec4 posEye = modelViewMatrix * aPosition;
    vec4 posWorld = inverseCameraViewMatrix * posEye;
    vProjCoord = texGenMatrix * posWorld;
}
uniform sampler2D projectionMap;     
varying vec4 vProjCoord;

void main() {
    vec3 finalColor = vec3( 0.5, 0.5, 0.5);
    if (vProjCoord.q > 0.0) {
        vec4 ProjMapColor = texture2DProj(projectionMap, vProjCoord);
        finalColor += ProjMapColor.rgb;
    }
    gl_FragColor = vec4(finalColor, 1.0);
}

棘手的事情,不是吗?你设法让它工作了吗?我正在尝试在OpenGL C++中实现一些东西,如果你有工作样例代码,我会喜欢的!如果你能发布一个屏幕截图,展示的内容会更简单。(我知道这是一个有8年历史的帖子)。多亏了你的代码,我才能够实现纹理投影。