这个opengl漫反射着色器使我的纹理透明

时间:2015-05-30 02:30:03

标签: android ios opengl-es

我正在将.obj文件导入opengl并且网格显示正常,但现在我想漫反射它。我使用了下面的着色器,但它只是使我的网格纹理半透明。此外,我没有做任何特殊的法线,我只是将它插入opengl。

顶点着色器

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 = 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;                               
}                                          

片段着色器

precision mediump float;        // Set the default precision to medium. We don't need as high of a 
                                // precision in the fragment shader.
uniform vec3 u_LightPos;        // The position of the light in eye space.
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.

// The entry point for our fragment shader.
void main()                         
{                              
    // Will be used for attenuation.
    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);                                                                                 

    // Add attenuation. 
    diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance)));

    // Add ambient lighting
    diffuse = diffuse + 0.7;  

    // Multiply the color by the diffuse illumination level and texture value to get final output color.
    gl_FragColor = (diffuse * texture2D(u_Texture, v_TexCoordinate));                                       
  }                                   

在u_LightPos制服中,我发送了一个像这样的灯光GLES20.glUniform3f(uLightPosition, 0.0f, 0.0f, 1.0f);

就像我说的那样,它只是使纹理半透明而不是漫反射着色。我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

最后一行是将gl_FragColor的http://www.example.dev/wp-json/posts?type[]=post&_jsonp=? http://www.example.dev/wp-json/posts?type[]=post&filter[status]=private&_jsonp=? http://www.example.dev/wp-json/posts?filter[status]=private 或alpha值乘以diffuse + 0.7,在此情况下,diffuse接近0将有效地使纹理透明。

要解决此问题,首先将texture2D值加载到vec3中,然后乘以diffuse,然后使用vec4以gl_FragColor存储正确的alpha值。