OpenGLESv2 着色器 glGetAttribLocation 返回 -1

发布于 2024-11-04 04:03:29 字数 1576 浏览 0 评论 0原文

嘿,我尝试编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年已逝 2024-11-11 04:03:29

您没有指定不使用哪个属性,但我假设它是“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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文