Scaled ModelMatrix使用漫反射照明

时间:2012-12-13 22:06:27

标签: android opengl-es opengl-es-2.0

所以,我有一个很有希望的简单问题:

我有一个简单的多维数据集,我正在使用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;

}

1 个答案:

答案 0 :(得分: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);

然后你只需要将它作为制服传递到着色器中。