0

I am importing .obj files into opengl and the mesh displays fine, but now I want to diffuse shade it. I have used the shader below, but it just makes my mesh's texture semi transparent. Also I didn't do anything special with the normals I just plugged em into opengl.

Vertex Shader

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

Fragment Shader

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

In the u_LightPos uniform I send in a light pos like this, GLES20.glUniform3f(uLightPosition, 0.0f, 0.0f, 1.0f);

Like I said it just makes the texture semi transparent instead of diffuse shading. Am I missing something?

genpfault
  • 51,148
  • 11
  • 85
  • 139
NJGUY
  • 2,045
  • 3
  • 23
  • 43
  • Where does this shader come from? Somebody asked the same question a couple of days ago, and the fragment shader code is identical: http://stackoverflow.com/questions/30466586/opengl-es-2-0-alpha/30471923#30471923. – Reto Koradi May 30 '15 at 05:32

1 Answers1

1

The last line is multiplying the w or alpha value of the gl_FragColor by diffuse+0.7 which in the case diffuse is close to 0 will effectively make your texture transparent.

To solve this issue first load the texture2D value into a vec3, then multiply by diffuse and after make a vec4 to store in gl_FragColor with the correct alpha value.

Luis Yanes
  • 347
  • 2
  • 13