所有

时间:2016-08-19 07:15:43

标签: c++ glsl opengl-es-2.0 fragment-shader vertex-shader

我为每个顶点分配一组唯一的坐标,我希望片段着色器中的片段具有该变量变量的插值。问题是,片段着色器值始终保持为0.0。我该如何解决这个问题。我正在使用带有glsl 1.2版的opengles 2.0。

顶点着色器:

attribute vec4 Position;
attribute vec4 SourceColor;
attribute vec3 BaryCentricCoord;
varying vec4 DestinationColor;
uniform mat4 Projection;
uniform mat4 Modelview;
attribute vec2 TexCoordIn;
varying vec2 TexCoordOut;
varying vec3 vBC;
void main() {
  vBC = BaryCentricCoord;
  DestinationColor = SourceColor;
  gl_Position = Projection * Modelview * Position; 
  TexCoordOut = TexCoordIn; 
};

片段着色器:

varying vec4 DestinationColor;
varying vec2 TexCoordOut;
varying vec3 vBC;
uniform sampler2D Texture;
uniform int TexEnabled;
float edgeFactor();
void main() {
     if (TexEnabled == 1) {
           gl_FragColor = texture2D(Texture, TexCoordOut) * DestinationColor;
     } else{
            gl_FragColor = vec4(DestinationColor.xyz, edgeFactor());
     }
}
float edgeFactor() {
    vec3 d = fwidth(vBC);
    vec3 a3 = smoothstep(vec3(0.0), d*1.5, vBC);
    return min(min(a3.x, a3.y), a3.z);
};

顶点数据:

float vertices[] {
        p1.x - deltaX, p1.y + deltaY, 0.0f, //top-left
        p2.x - deltaX, p2.y + deltaY, 0.0f, //top-right
        p2.x + deltaX, p2.y - deltaY, 0.0f, //bottom-right
        p2.x + deltaX, p2.y - deltaY, 0.0f, //bottom-right
        p1.x + deltaX, p1.y - deltaY, 0.0f, //bottom-left
        p1.x - deltaX, p1.y + deltaY, 0.0f //top-left
    };

我传递的重心坐标(每组3对应一个顶点):

// R 
    data[0] = 1.0; 
    data[1] = 0.0;
    data[2] = 0.0; 

    // G
    data[3] = 0.0;
    data[4] = 1.0;
    data[5] = 0.0;

    // B
    data[6] = 0.0; 
    data[7] = 0.0;
    data[8] = 1.0; 

    // B
    data[9] = 0.0; 
    data[10] = 0.0;
    data[11] = 1.0; 

    // G
    data[12] = 0.0;
    data[13] = 1.0;
    data[14] = 0.0;

    // R 
    data[15] = 1.0; 
    data[16] = 0.0;
    data[17] = 0.0; 

已启用:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable( GL_BLEND );

将数据传递为:glDrawArrays(GL_TRIANGLES, 0, 6);

0 个答案:

没有答案