OpenGL ES2.0:这些着色器有什么问题?
OpenGL ES2.0: What's wrong with these shaders?
我正在尝试遵循 FreeType2 教程 here,但我无法将着色器设置为 link。
这些着色器有问题吗?
顶点着色器:
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;
}
我已经成功地使用下面的函数调用和我自己的着色器来完成简单的实体填充和简单的纹理。我不知道为什么我不能使用上面的着色器。
这是将着色器编译并 link 到程序中的代码:
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 );
...
我在 linking 阶段得到以下输出:
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'
着色器编译失败,因为需要以下行:
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;
}
我正在尝试遵循 FreeType2 教程 here,但我无法将着色器设置为 link。
这些着色器有问题吗?
顶点着色器:
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;
}
我已经成功地使用下面的函数调用和我自己的着色器来完成简单的实体填充和简单的纹理。我不知道为什么我不能使用上面的着色器。
这是将着色器编译并 link 到程序中的代码:
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 );
...
我在 linking 阶段得到以下输出:
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'
着色器编译失败,因为需要以下行:
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;
}