OpenGLESv2 Shader glGetAttribLocation returns -1
Hey, I try to code a OpenGL ES 2 engine but at shader creation the function glGetAttribLocation returns开发者_如何学运维 -1 which means
The named attribute variable is not an active attribute in the specified program object or the name starts with the reserved prefix "gl_".
But it IS defined in the shader and doesnt start with gl_. Whats wrong with my code?
C++ Code:
glprogram = glCreateProgram();
glvertshader = glCreateShader(GL_VERTEX_SHADER);
glfragshader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(glvertshader, 1, vertsrc, NULL);
glShaderSource(glfragshader, 1, fragsrc, NULL);
GLint ret;
glCompileShader(glvertshader);
glGetShaderiv(glvertshader, GL_COMPILE_STATUS, &ret);
if(!ret)
{
///error handling - stripped for the internet
}
glCompileShader(glfragshader);
glGetShaderiv(glfragshader, GL_COMPILE_STATUS, &ret);
if(!ret)
{
///error handling - stripped for the internet
}
glAttachShader(glprogram, glvertshader);
glAttachShader(glprogram, glfragshader);
glLinkProgram(glprogram);
glGetProgramiv(glprogram, GL_LINK_STATUS, &ret);
if(!ret)
{
///error handling - stripped for the internet
}
glattributes[0] = glGetAttribLocation(glprogram, "vertex");
glattributes[1] = glGetAttribLocation(glprogram, "texcoord_vs");
std::cout << "texcoord " << glGetAttribLocation(glprogram, "texcoord_vs";
Vert shader:
// Vertex Shader for OpenGL ES
uniform mat4 projmat;
uniform mat4 modviewmat;
attribute vec2 vertex;
attribute vec2 texcoord_vs;
varying vec2 texcoord_fs;
void main()
{
texcoord_fs = texcoord_vs;
gl_Position = projmat*modviewmat*vec4(vertex, 0.0, 1.0);
}
edit: correct shader code and the error is with texcoord_vs.
You didn't specify which attribute is not used, but I assume it's "texcoord_vs", simply because you're not using it, and as such it's being stripped by the shader compiler.
I suppose you meant to say texcoord_fs = texcoord_vs
?
精彩评论