I have a C++ problem here which I simply cannot understand.
I have 2 slightly different functions. Both of them should do the very same thing. But only one works properly.
Method 1: input of the method is '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(...);
}
}
Method 2:
void setUniformSampler(Gluint program, GLuint sampler) {
GLint uniformLocation = glGetUniformLocation(program, "test"); // returns 0
if(uniformLocation >= 0) {
glUniform1i(uniformLocation, sampler);
} else {
throw exception(...);
}
}
As you can see, glGetUniformLocation returns 2 different values. The correct return value would be "0", not "-1". So I wonder, what exactly is the difference between the two calls?
quote: "c_str() generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters". And that is precisely what the method glGetUniformLocation(...) needs as its second parameter. So, why does only Method 2 above succeed? Is it a compiler problem?
I'm working with MS Visual Studio 2008 on Win7.
I've been searching for this bug for almost 2 days now. I really want to clarify this. It drove me crazy...
Thanks Walter
EDIT:
This doesn't work either.
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(...);
}
}