输入不匹配的顶点属性

时间:2016-09-22 11:16:23

标签: opengl glsl opengl-4

我为教育目的写了glsl包装器,但我停下来因为我有一些误解。当我想将insert变量放到特定位置时,我有不匹配的警告。因为location是GLint,但glVertexAttrib位置必须是GLuint。

这是我的代码示例

bool Material::AddAttrib(GLchar *variable, std::vector<GLdouble> values) {
GLint location = glGetAttribLocation(program,variable);
GLenum error = glGetError();
bool isNor = PrintError(error);
if(!isNor) return isNor;
switch (values.size()) {
    case 1:
        glVertexAttrib1d(location, values.at(0));
        break;
    case 2:
        glVertexAttrib2d(location, values.at(0), values.at(1));
        break;
    case 3:
        glVertexAttrib3d(location, values.at(0), values.at(1), values.at(2));
        break;
    case 4:
        glVertexAttrib4d(location, values.at(0), values.at(1), values.at(2), values.at(3));
        break;
    default:
        PrintErrorSize();
        return false;
}
error = glGetError();
isNor = PrintError(error);
return isNor;
}

1 个答案:

答案 0 :(得分:1)

如果出现错误,

glGetAttribLocation()可能会返回负数索引。当然,如果用于glVertexAttrib...(),则负指数无效。这就是类型不匹配的原因。您可以通过简单的演员解决此问题:

GLint retrievedLocation = glGetAttribLocation(program,variable);
if(retrievedLocation < 0)
    return ...; //there is no variable with this name
GLuint location = (GLuint) retrievedLocation;