2011-06-23 13 views
1

私はシェイダーを正しく動作させるために数日間立ち往生しました。 問題は、オブジェクトにテクスチャをつけていないときは、周囲を明るい色で乗算し、光がないときは暗いオブジェクトを取得し、光源がアクティブなときは適切に照明されるということです。OpenGLES 2.0 Phong shader strange resultは、テクスチャが有効になっているときにオブジェクトを透明にします!

テクスチャを貼り付けて周囲と明るい色で掛け合わせると、光源がアクティブになったときにだけ表示される透明なオブジェクトが得られ、照明中にオブジェクトを見ることさえできます。

私はインターネットからいくつかのコードスニペットを試してきましたが、私はいつも同じ結果を得ています。私はここで間違っていますか?私は必死です...

このアプリケーションは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; 
} 

そして、ここでは私のフラグメントシェーダです:事前に

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

感謝。

+0

この材料ではアルファブレンディングが有効になっていますか?フラグメントシェーダで結果として生じる色のアルファ値を計算しているため、一部のピクセルのアルファ値が1に等しくない可能性があります。 – JPD002

+0

アルファブレンディングをアクティブ化していません。愚かな質問のために私を許しなさい。しかし私がアルファを計算しているところはどこですか? – mrcomplicated

+0

申し訳ありませんが、私はこれを実現しました vec4 diffuseTerm = matDiffuse * max(ドット(N、L)、0.0); vec4 diffuseTerm = matDiffuse * max(ドット(N、L)、1.0); しかし、問題は私のオブジェクトに反応する光が見えなくなり、オブジェクト全体が明るい色で陰影付けられます。 – mrcomplicated

答えて

1

私はそれが、JPD002のおかげで、私は再びシェーダを改訂し、私はそれが

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

おかげJDP002なければならないことが判明、むしろコードに4目を持っていることは常に良いです見つけOK 2 = D

関連する問題