-1

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'
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
SparkyNZ
  • 6,266
  • 7
  • 39
  • 80
  • 3
    It might not be the issue here, but always double's instead of ints, ie "0.0" and "1.0" instead of "0" and "1". Some GPU's are picky about that. – reden Apr 12 '16 at 09:53
  • 2
    The error looks to be self explanatory. At the moment of linking you are linking 2 objects, one of which failed to compile. Why don't you debug and error check the function "BuildShader"? – Maurizio Benedetti Apr 12 '16 at 13:04
  • 2
    `GLint compileSuccess;` You forgot to initialize this variable. – Nicol Bolas Apr 12 '16 at 15:22
  • @Nicol Bolas: Thank you. Must have been too tired to see the obvious. I updated my question with what was needed. Once I had the shader error, it was obvious. – SparkyNZ Apr 12 '16 at 19:08
  • @NicolBolas: I don't understand why this question is [On Hold]. The tutorial I was following was intended for OpenGL. It has a fragment shader that doesn't work with OpenGL ES2, and I have answered my own question with the reason why. Surely somebody else will attempt the same tutorial and may be stuck because of the same reason? – SparkyNZ Apr 14 '16 at 00:08
  • @SparkyNZ: Whether it's from a tutorial or not, it's still just a typo. – Nicol Bolas Apr 14 '16 at 01:24
  • @NicolBolas: You mean the tutorial itself is wrong and "precision mediump float" should have been included regardless of whether it was OpenGL or OpenGL ES? – SparkyNZ Apr 14 '16 at 01:27
  • @SparkyNZ: If it's an OpenGL ES tutorial, yes. If it's a desktop GL tutorial, no. But in either case, *you* not putting it in *your code* (whether you copy-and-pasted it or not) is still functionally a typo. – Nicol Bolas Apr 14 '16 at 01:29
  • @NicolBolas: OK. Lets just delete the question then. I got what I needed in the end with your help. – SparkyNZ Apr 14 '16 at 01:34

1 Answers1

0

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;
}
SparkyNZ
  • 6,266
  • 7
  • 39
  • 80