OpenGL ES2.0:这些着色器出了什么问题?

时间:2016-04-12 09:30:29

标签: opengl-es opengl-es-2.0 fragment-shader vertex-shader

我试图关注FreeType2教程here,但我无法将着色器链接起来。

这些着色器有问题吗?

顶点着色器:

attribute vec4 coord;
varying vec2 texcoord;

void main()
{
  gl_Position = vec4( coord.xy, 0, 1 );
  texcoord = coord.zw;
}

片段着色器:

varying vec2 texcoord;
uniform sampler2D tex;
uniform vec4 color;

void main()
{
  gl_FragColor = vec4(1, 1, 1, texture2D( tex, texcoord ).r ) * color;
}

我已成功使用下面的函数调用我自己的着色器进行简单的实体填充和简单的纹理。我不知道为什么我不能使用上面的着色器。

这里是编码器并将着色器链接到程序中的代码:

GLuint BuildShader( char *pszSource, GLenum shaderType )
{
  GLuint hShader = glCreateShader( shaderType );
  glShaderSource(hShader, 1, &pszSource, 0 );
  glCompileShader( hShader );

  GLint compileSuccess = GL_FALSE;

  glGetShaderv( hShader, GL_COMPILE_STATUS, &compileSuccess );      

  if( compileSuccess == GL_FALSE )
  {
    GLchar message[ 256 ];
    glGetShaderInfoLog( hShader, sizeof( message ), 0, &message[ 0 ] );
    printf( "SHADER (%s) %s\n", pszSource, message );
    exit( 1 );
  }

  return hShader;
}

GLuint BuildProgram( char *pszVertexShaderSource, char *pszFragmentShaderSource )
{
  GLuint vShader = BuildShader( pszVertexShaderSource,   GL_VERTEX_SHADER );
  GLuint fShader = BuildShader( pszFragmentShaderSource, GL_FRAGMENT_SHADER );

  GLuint hProgram = glCreateProgram();
  glAttachShader( hProgram, vShader );
  glAttachShader( hProgram, fShader );
  glLinkProgram( hProgram );

  GLint linkSuccess;

  glGetProgramiv( hProgram, GL_LINK_STATUS, &linkSuccess );

  if( linkSuccess == GL_FALSE )
  {
    GLchar message[ 256 ];
    glGetProgramInfoLog( hProgram, sizeof( message ), 0, &message[ 0 ] );
    printf( "BUILD %s\n", message );
    exit( 1 );
  }

  return hProgram;
}

...
g_FontProgram = BuildProgram( vsFont, fsFont );
...

我在链接阶段得到以下输出:

BUILD ERROR: One or more attached shaders not successfully compiled

更新:我修复了着色器编译检查。现在我收到以下错误:

SHADER (varying vec2 texcoord; uniform sampler2D tex; uniform vec4 color; void main() { gl_FragColor = vec4(1, 1, 1, texture2D( tex, texcoord ).r ) * color; }) ERROR: 0:1: 'vec2' : declaration must include a precision qualifier for type
ERROR: 0:1: 'vec4' : declaration must include a precision qualifier for type
ERROR: 0:1: Use of undeclared identifier 'texcoord'
ERROR: 0:1: Use of undeclared identifier 'color'

1 个答案:

答案 0 :(得分:0)

着色器无法编译,因为需要以下行:

precision mediump float;

对于其他试图遵循相同教程的人,以下片段着色器将在OpenGL ES2.0下工作:

precision mediump float;
varying vec2 texcoord;
uniform sampler2D tex;
uniform vec4 color;

void main()
{
  gl_FragColor = vec4(1, 1, 1, texture2D( tex, texcoord ).r ) * color;
}