Search code examples
opengl-esopengl-es-2.0fragment-shadervertex-shader

OpenGL ES2.0: What's wrong with these shaders?


I'm trying to follow a FreeType2 tutorial here but I cannot get the shaders to link.

Is there something wrong with these shaders?

Vertex shader:

attribute vec4 coord;
varying vec2 texcoord;

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

Fragment shader:

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

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

I have successfully uses the below function calls with my own shaders for doing simple solid filling and simple textures. I have no idea why I can't use the above shaders.

Here's the code which compilers and links the shaders into the program:

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

I get the below output at the linking phase:

BUILD ERROR: One or more attached shaders not successfully compiled

UPDATE: I have fixed the shader compilation check. Now I get the following error:

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'

Solution

  • The shaders failed to compile because the following line was needed:

    precision mediump float;
    

    For anyone else trying to follow the same tutorial, the following fragment shader will work under 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;
    }