GLSL 2.0着色器在不同的设备中提供不同的光

时间:2016-06-11 15:31:12

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

我正在尝试为Android中的opengles 2.0视图编写着色器。 我的着色器是:

顶点着色器:

 uniform mat4 u_MVPMatrix;      // A constant representing the combined model/view/projection matrix.
uniform mat4 u_MVMatrix;        // A constant representing the combined model/view matrix.              

attribute vec4 a_Position;      // Per-vertex position information we will pass in.                             
attribute vec3 a_Normal;        // Per-vertex normal information we will pass in.      
attribute vec2 a_TexCoordinate; // Per-vertex texture coordinate information we will pass in.       

varying vec3 v_Position;        // 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 texture coordinate.
    v_TexCoordinate = a_TexCoordinate;                                      

    // Transform the normal's orientation into eye space.
    v_Normal = normalize(vec3(u_MVMatrix * vec4(a_Normal, 0.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);
} 

Fragment Shader:

precision highp float;          // Set the default precision to medium. We don't need as high of a 
                                // precision in the fragment shader.
uniform vec3 u_LightPos1;           // The position of the light in eye space.
uniform vec3 u_LightDir1;           // The position of the light in eye space.
float l_spotCutOff=45.0;
uniform sampler2D u_Texture;    // The input texture.

varying vec3 v_Position;        // Interpolated position for this fragment.
varying vec3 v_Normal;          // Interpolated normal for this fragment.
varying vec2 v_TexCoordinate;   // Interpolated texture coordinate per fragment.
float cutoff = 0.1;
// The entry point for our fragment shader.
void main()                         
{                              

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

        // Will be used for attenuation.
        float distance1 = length(u_LightPos1 - v_Position);

    float diffuse=0.0;

        // 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 diffuse1 = max(dot(v_Normal, lightVector1), 0.1);
        // Add attenuation. 
    diffuse1 = diffuse1 * (1.0 / (1.0+(0.25*distance1)));

    // Add ambient lighting
    diffuse = diffuse1+0.2;
    // Multiply the color by the diffuse illumination level and texture value to get final output color.
    vec4 color = (texture2D(u_Texture, v_TexCoordinate));
    color.rgb *= (diffuse);
    if( color.a < cutoff)
        discard;

    gl_FragColor = color;


  }                                                                         

现在着色器工作正常,但在不同的设备中表现不同:

设备1 :( moto x play)

1

设备2:(Samsung S7) 2

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您使用的纹理格式/类型可能存在问题。所有设备都不支持所有纹理格式。 例如:如果您的输出颜色可能具有负值且设备的纹理格式不支持它们,则它将被钳制为0并可能给出不同的结果。 最好使用

检查两个设备的功能
   GLES20.glGetString(GLES20.GL_EXTENSIONS));