我在这里有一个 C++ 问题,我根本无法理解。
我有 2 个略有不同的功能。他们俩都应该做同样的事情。但只有一个可以正常工作。
方法一:方法的输入是'const string samplerName = "test"'
void setUniformSampler(Gluint program, const string samplerName, GLuint sampler) {
GLint uniformLocation = glGetUniformLocation(program, samplerName.c_str()); // returns -1
if(uniformLocation >= 0) {
glUniform1i(uniformLocation, sampler);
} else {
throw exception(...);
}
}
方法二:
void setUniformSampler(Gluint program, GLuint sampler) {
GLint uniformLocation = glGetUniformLocation(program, "test"); // returns 0
if(uniformLocation >= 0) {
glUniform1i(uniformLocation, sampler);
} else {
throw exception(...);
}
}
如您所见,glGetUniformLocation 返回 2 个不同的值。正确的返回值是“0”,而不是“-1”。所以我想知道,这两个电话之间到底有什么区别?
引用:“c_str() 生成一个以空字符结尾的字符序列(c-string),其内容与字符串对象相同,并将其作为指向字符数组的指针返回”。这正是 glGetUniformLocation(...) 方法作为其第二个参数所需要的。那么,为什么只有上面的方法 2 成功了呢?是编译器的问题吗?
我在 Win7 上使用 MS Visual Studio 2008。
我一直在寻找这个错误近 2 天了。我真的很想澄清这一点。它把我逼疯了...
谢谢沃尔特
编辑:
这也不起作用。
void setUniformSampler(Gluint program, const string samplerName, GLuint sampler) {
const GLchar* name = samplerName.c_str();
GLint uniformLocation = glGetUniformLocation(program, name); // still returns -1
if(uniformLocation >= 0) {
glUniform1i(uniformLocation, sampler);
} else {
throw exception(...);
}
}