Android 缩放模型矩阵与漫反射照明混乱

Android 缩放模型矩阵与漫反射照明混乱,android,opengl-es,opengl-es-2.0,Android,Opengl Es,Opengl Es 2.0,所以,我有一个希望很简单的问题: 我有一个简单的多维数据集,我使用Matrix.ScaleM缩放模型视图并压缩多维数据集(相信我,这是有原因的) 这项工作,立方体收缩。但是,我的片段着色器不再正确地将漫反射光源应用于立方体的顶部和底部。阴影代码如下所示 precision mediump float; uniform vec3 u_LightPos; uniform sampler2D u_Texture; uniform sampler2D u_Texture2; varyi

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

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

}

您将需要这样一个倒置的转置矩阵:

invertM(tempMatrix, 0, modelViewMatrix, 0);
transposeM(it_modelViewMatrix, 0, tempMatrix, 0);
着色器:

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

然后,您只需将其作为一个统一体传递到着色器中。

您将需要一个倒置的转置矩阵,如下所示:

invertM(tempMatrix, 0, modelViewMatrix, 0);
transposeM(it_modelViewMatrix, 0, tempMatrix, 0);
着色器:

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

然后,您只需要将其作为一个统一的对象传递到着色器中。

您也可以发布您的顶点着色器吗?您的法线应该与模型视图的反向转置相乘,而不是与模型视图相乘。对法线进行非规范化会产生非常奇怪的效果。这里是您的问题:v_Normal=vec3(u_MVMatrix*vec4(a_Normal,0.0));如安德烈亚斯所说,将倒数转置并使用它。你的法线也在被缩放。好吧,我能理解你的意思,但我没有办法把它转换成代码。我该怎么做?谈到GLSL,我并不是最好的。@rPaskiewicz:你可以在着色器外部计算逆,并将其作为一个整体发送。你也可以发布你的顶点着色器吗?你的法线应该与模型视图的逆转置相乘,而不是与模型视图相乘。对法线进行非规范化会产生非常奇怪的效果。这里是您的问题:v_Normal=vec3(u_MVMatrix*vec4(a_Normal,0.0));如安德烈亚斯所说,将倒数转置并使用它。你的法线也在被缩放。好吧,我能理解你的意思,但我没有办法把它转换成代码。我该怎么做?在GLSL方面,我并不是最好的。@rPaskiewicz:你可以在着色器外部计算逆,并将其作为统一的格式发送