GLSL 中的纹理投影

发布于 2024-10-20 13:08:34 字数 2828 浏览 1 评论 0原文

我正在尝试直接基于 oZone3d 中的示例来实现纹理投影。我的环境是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);
}

I'm trying to implement texture projection based directly on this example at oZone3d. My environment is OpenGL ES 2.0 on Android 2.2.

Matrix math has never been my strong point and I suspect that my problem lies somewhere in the texture projection matrix setup (texGenMatrix). I have tried everything imaginable to get this working but the projection doesn't come out correctly. I'd greatly appreciate some extra eyes on my code. Thanks.

Not shown here is my geometry (a horizontal plane through the origin), and some other plumbing like shader uniform/attribute binds.

Camera Setup :

// 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);

Texture Projection setup :

// 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);

The vertex shader :

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;
}

Fragment shader :

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文