嘿,我尝试编写 OpenGL ES 2 引擎代码,但在创建着色器时,函数 glGetAttribLocation 返回 -1,这意味着
The named attribute variable is not an active attribute in the specified program object or the name starts with the reserved prefix "gl_".
但它是在着色器中定义的并且不是以 gl_ 开头的。 我的代码有什么问题?
C++代码:
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";
垂直着色器:
// 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);
}
编辑:更正着色器代码,错误与 texcoord_vs 有关。
最佳答案
您没有指定未使用哪个属性,但我假设它是“texcoord_vs”,只是因为您没有使用它,因此它被着色器编译器剥离。
我想你的意思是 texcoord_fs = texcoord_vs
?
关于c++ - OpenGLESv2 着色器 glGetAttribLocation 返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5826037/