c++ - OpenGLESv2 着色器 glGetAttribLocation 返回 -1

标签 c++ opengl-es

嘿,我尝试编写 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/

相关文章:

ios - 图 block 大小,视网膜与非视网膜,iPad 与 iPhone

android - 如何在 Android 中将 GLSurfaceView 覆盖在 MapView 上?

c++ - 手动对象所有权与智能指针

c++ - 在 C++ 模板中将类作为非类型参数传递

c++ - 在调整大小时 move 对话框上的按钮

ios - SceneKit自定义几何纹理错误

iphone - setStatusBarOrientation 在 iOS8 中导致问题

c++ - 在 C++ 中搜索一个字符串以查找所有出现的子字符串

c++ - Qt 程序无法打开我的 MySql 数据库

android - PolygonRegion 创建 anr