android - OpenGLES 2.0 Phong 着色器奇怪的结果,在启用纹理时使我的对象透明!

标签 android opengl-es-2.0 shader

我已经被困了好几天了,试图让我的着色器正常工作。 问题是,当我没有在我的对象上附加纹理时,我将环境光颜色乘以光的颜色,并且在没有光的情况下得到一个深色对象,并在激活光源时正确地照亮。

问题是,当我附加一个纹理并将其乘以环境光和光颜色时,我得到一个透明物体,它只在光源被激活时才会出现,你甚至可以在物体被照亮时看到它!

我一直在尝试来自互联网的几个代码 fragment ,但我总是得到相同的结果。我在这里做错了什么?我很绝望……

该应用程序是在 Android 上开发的。

这是我的顶点着色器:

uniform mat4 uMVPMatrix;
uniform mat4 normalMatrix;

// eye pos
uniform vec3 eyePos;

// position and normal of the vertices
attribute vec4 aPosition;
attribute vec3 aNormal; 

// texture variables
uniform float hasTexture;
varying float tex;
attribute vec2 textureCoord;
varying vec2 tCoord;

// lighting
uniform vec4 lightPos;
uniform vec4 lightColor;

// material
uniform vec4 matAmbient;
uniform vec4 matDiffuse;
uniform vec4 matSpecular;
uniform float matShininess;

// normals to pass on
varying vec3 vNormal;
varying vec3 EyespaceNormal;

varying vec3 lightDir, eyeVec;

void main() {
    // pass on texture variables
    tex = hasTexture;
    tCoord = textureCoord;

    // normal
    EyespaceNormal = vec3(normalMatrix * vec4(aNormal, 1.0));

    // the vertex position
    vec4 position = uMVPMatrix * aPosition; 

    // light dir
    lightDir = lightPos.xyz - position.xyz;
    eyeVec = -position.xyz;

    gl_Position = uMVPMatrix * aPosition; 
}

这是我的 fragment 着色器:

precision mediump float;

// texture variables
uniform sampler2D texture1; // color texture

varying float tex;
varying vec2 tCoord;

varying vec3 vNormal;
varying vec3 EyespaceNormal;

// light
uniform vec4 lightPos;
uniform vec4 lightColor;

// material
uniform vec4 matAmbient;
uniform vec4 matDiffuse;
uniform vec4 matSpecular;
uniform float matShininess;

// eye pos
uniform vec3 eyePos;

// from vertex s
varying vec3 lightDir, eyeVec;

void main() {

    vec4 b = lightColor;
    vec4 c = matAmbient;
    vec4 d = matDiffuse;
    vec4 e = matSpecular;
    vec3 g = eyePos;
    float f = matShininess;

    vec3 N = normalize(EyespaceNormal);
    vec3 E = normalize(eyeVec); 

    vec3 L = normalize(lightDir);

    // Reflect the vector. Use this or reflect(incidentV, N);
    vec3 reflectV = reflect(-L, N);

    // Get lighting terms
    vec4 ambientTerm;
    if (tex >= 1.0) {
        ambientTerm = texture2D(texture1, tCoord);
    }
    else
        ambientTerm = matAmbient * lightColor;

    vec4 diffuseTerm = matDiffuse * max(dot(N, L), 0.0);
    vec4 specularTerm = matSpecular * pow(max(dot(reflectV, E), 0.0), matShininess);

    gl_FragColor =  ambientTerm * diffuseTerm + specularTerm;
}

提前致谢。

最佳答案

OK 找到了,感谢JPD002,又在修改shader,发现一定是

vec4 diffuseTerm = matDiffuse * max(dot(N, L), 0.0);
vec4 specularTerm = matSpecular * pow(max(dot(reflectV, E), 1.0), matShininess);

感谢 JDP002,用 4 只眼睛而不是 2 只眼睛看代码总是好的 =D

关于android - OpenGLES 2.0 Phong 着色器奇怪的结果,在启用纹理时使我的对象透明!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6448468/

相关文章:

c++ - 将倒置场景渲染到帧缓冲区

android - 我想为所有 Activity 保留一个操作栏

java - 如何在 ListView 中显示firebase数据?

android - 在设备上安装 apk 时出现“权限被拒绝”错误

java - glCreateShader 和 glCreateProgram 在 android 上失败

c++ - OpenGL 缓冲区到属性

opengl - Shader - 简单的 SSS 光照问题

android - 更新 Android Studio 2.1 后插件错误

android - 如何从不那么大的 View 滑动到与屏幕一样大的 fragment ?

ios - OpenGL ES 2.0 多个着色器程序 - 纹理渲染不再工作