OpenGLESv2 着色器 glGetAttribLocation 返回 -1
嘿,我尝试编写 OpenGL ES 2 引擎,但在着色器创建时,函数 glGetAttribLocation 返回 -1,这意味着
指定的属性变量不是 指定的活动属性 程序对象或名称开头 保留的前缀“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。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有指定不使用哪个属性,但我假设它是“texcoord_vs”,只是因为您没有使用它,因此它被着色器编译器删除。
我想你的意思是 texcoord_fs = 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
?