1

だから、うまくいけば簡単な質問があります:

私は単純なキューブを持っています。モデルビューMatrix.ScaleMをスケーリングし、キューブを圧縮するために使用しています(これには理由があります、私を信じてください)。

この作業、立方体が縮小します。ただし、私のフラグメント シェーダーは、拡散光源を立方体の上部と下部に適切に適用しなくなりました。シェードコードは次のとおりです。

precision mediump float;        
uniform vec3 u_LightPos;
uniform sampler2D u_Texture;
uniform sampler2D u_Texture2;

varying vec3 v_Position;
varying vec4 v_Color;
varying vec3 v_Normal;          // Interpolated normal for this fragment.
varying vec2 v_TexCoordinate;   // Interpolated texture coordinate per fragment.

// The entry point for our fragment shader.
void main()                         
{                              

        float distance = length(u_LightPos - v_Position);

// Get a lighting direction vector from the light to the vertex.
vec3 lightVector = normalize(u_LightPos - v_Position);                  

    // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are
// pointing in the same direction then it will get max illumination.
float diffuse = max(dot(v_Normal, lightVector), 0.0);                                                                                 
mediump float emptyness = 0.0;
mediump float half_emptyness = 0.1;
// Add attenuation. 
diffuse = diffuse * (1.0 / (1.0 + (0.10 * distance)));

// Add ambient lighting
diffuse = diffuse + 0.3;  
vec4 textColor1 = texture2D(u_Texture, v_TexCoordinate);
vec4 textColor2 = texture2D(u_Texture2, v_TexCoordinate);



// Multiply the color by the diffuse illumination level and texture value to get final output color.


if(textColor2.w == emptyness){
    diffuse = diffuse * (1.0 / (1.0 + (0.10 * distance)));
    gl_FragColor = ( diffuse * textColor1 );//v_Color *

    gl_FragColor.a = 1.0;
} else{
    diffuse = diffuse * (1.0 / (1.0 + (0.75 * distance)));
    gl_FragColor = ( diffuse * textColor1 );//v_Color *
    gl_FragColor.a = 0.0;
}



}                                                                       

それで、何かアイデアはありますか?

そして、色が少し...奇妙であることは知っています。それは全く別の理由です。

編集: 要求に応じて、頂点シェーダー:

uniform mat4 u_MVPMatrix;
uniform mat4 u_MVMatrix;
attribute vec4 a_Position;
attribute vec4 a_Color;
attribute vec3 a_Normal;
attribute vec2 a_TexCoordinate;

varying vec3 v_Position;        // This will be passed into the fragment shader.
varying vec4 v_Color;           // This will be passed into the fragment shader.
varying vec3 v_Normal;          // This will be passed into the fragment shader.
varying vec2 v_TexCoordinate;   // This will be passed into the fragment shader.

// The entry point for our vertex shader.
void main()
{
// Transform the vertex into eye space.
v_Position = vec3(u_MVMatrix * a_Position);

// Pass through the color.
v_Color = a_Color;

// Pass through the texture coordinate.
v_TexCoordinate = a_TexCoordinate;

// Transform the normal's orientation into eye space.
v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));
float halfer = 2.0;

// gl_Position is a special variable used to store the final position.
// Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
gl_Position = u_MVPMatrix * a_Position;

}
4

1 に答える 1

1

次のような逆転置行列が必要になります。

シェーダー:

uniform mat4 u_IT_MVMatrix;
...
v_Normal = vec3(u_IT_MVMatrix * vec4(a_Normal, 0.0));

Javaコードでは、次のように通常のMVマトリックスからマトリックスを作成します。

invertM(tempMatrix, 0, modelViewMatrix, 0);
transposeM(it_modelViewMatrix, 0, tempMatrix, 0);

次に、これをユニフォームとしてシェーダーに渡す必要があります。

于 2013-02-22T02:24:43.837 に答える