我有这段不断重复的代码。
g_materialAmbientIndex[0] = glGetUniformLocation(g_shaderProgramID, "uMaterialProperties[0].ambient");
g_materialDiffuseIndex[0] = glGetUniformLocation(g_shaderProgramID, "uMaterialProperties[0].diffuse");
g_materialSpecularIndex[0] = glGetUniformLocation(g_shaderProgramID, "uMaterialProperties[0].specular");
g_materialAmbientIndex[1] = glGetUniformLocation(g_shaderProgramID, "uMaterialProperties[1].ambient");
g_materialDiffuseIndex[1] = glGetUniformLocation(g_shaderProgramID, "uMaterialProperties[1].diffuse");
g_materialSpecularIndex[1] = glGetUniformLocation(g_shaderProgramID, "uMaterialProperties[1].specular");
g_materialAmbientIndex[2] = glGetUniformLocation(g_shaderProgramID, "uMaterialProperties[2].ambient");
g_materialDiffuseIndex[2] = glGetUniformLocation(g_shaderProgramID, "uMaterialProperties[2].diffuse");
g_materialSpecularIndex[2] = glGetUniformLocation(g_shaderProgramID, "uMaterialProperties[2].specular");
我想将它放入一个 for 循环中,但我遇到了字符串参数的问题。下面是我的功能。我不断收到一条错误消息:
no suitable conversion func from "std::basic_string....." to "const GLchar*" exists
for (int i = 0; i < MAX_MATERIALS; i++)
{
stringstream ss;
ss << i;
string str = ss.str();
g_materialAmbientIndex[i] = glGetUniformLocation(g_shaderProgramID, "uMaterialProperties[" + str + "].ambient");
g_materialDiffuseIndex[i] = glGetUniformLocation(g_shaderProgramID, "uMaterialProperties[" + str + "].diffuse");
g_materialSpecularIndex[i] = glGetUniformLocation(g_shaderProgramID, "uMaterialProperties[" + str + "].specular");
}
最佳答案
错误消息会告诉您需要知道的一切:
不存在从“std::basic_string.....”到“const GLchar*”的合适转换函数
因此该方法不知道如何处理 std::string
- 它需要一个 GLchar*
。您应该尝试的第一件事是通过 str.c_str()
的常规 char*
。
关于c++ - 在我的 for 循环中,如何将索引更改为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45558976/