c++ - 在我的 for 循环中,如何将索引更改为字符串?

标签 c++ opengl

我有这段不断重复的代码。

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/

相关文章:

c++ - Gtkglextmm 和 Gtkmm3

c++ - 将 std::string 转换为整数

c++ - 使用和不使用 SSE 的不同结果( float 组乘法)

c++ - 继承结构的类型转换给出 g++ 编译错误

c++ - 投影矩阵产生不正确的结果

react-native - React Native 360​​/全景查看器

c++ - 在 OpenCv 中乘以图像并在其上应用拉普拉斯滤波器

c++ - 调用栈溢出时如何打印调用栈?

opengl - 为什么 glutSolidTorus 会产生一些缺陷

c++ - OpenGL glCreateProgram() 总是返回 1 并删除之前的程序