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 回答