2015-05-30 4 views
0

Я импортирую файлы .obj в opengl, а сетка отображается хорошо, но теперь я хочу рассеять оттенок. Я использовал шейдер ниже, но он просто делает мою сетчатую текстуру полупрозрачной. Кроме того, я не делал ничего особенного с нормалями, которые я только что подключил к 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;        
}           

Фрагмент 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));          
    }         

В u_LightPos форме я посылаю в светло-поз, как это, GLES20.glUniform3f(uLightPosition, 0.0f, 0.0f, 1.0f);

Как я сказал, что это просто делает текстура полупрозрачная, а не диффузная затенение. Я что-то упускаю?

+0

Откуда этот шейдер? Кто-то задал тот же вопрос пару дней назад, а код фрагмента шейдера идентичен: http://stackoverflow.com/questions/30466586/opengl-es-2-0-alpha/30471923#30471923. –

ответ

1

Последняя строка умножает w или альфа-значение gl_FragColor на диффузное + 0.7, которое в случае диффузии близко к 0 эффективно сделает вашу текстуру прозрачной.

Чтобы решить эту проблему, сначала загрузите значение texture2D в vec3, затем умножьте на diffuse и после создания vec4 для хранения в gl_FragColor с правильным значением альфа.

Смежные вопросы