【问题标题】:This opengl diffuse shader makes my texture transparent这个 opengl 漫反射着色器使我的纹理透明
【发布时间】:2015-05-30 02:30:03
【问题描述】:

我正在将 .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);

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

【问题讨论】:

标签: android ios opengl-es


【解决方案1】:

最后一行是将 gl_FragColor 的w 或 alpha 值乘以diffuse+0.7,在这种情况下,diffuse 接近0 将有效地使您的纹理透明。

要解决这个问题,首先将 texture2D 值加载到 vec3 中,然后乘以漫反射,然后生成 vec4 以使用正确的 alpha 值存储在 gl_FragColor 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 2012-09-08
    • 2014-05-30
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多