一个主 OpenGL 程序中的多个着色器

Multiple shaders inside one Main OpenGL program

我正在尝试将不同的着色器附加到我的 OpenGL 主程序中的不同程序,以渲染不同的对象。更具体地说,我有一个行星对象 (obj),我想将它渲染成两个不同的行星,一个具有凹凸贴图,另一个具有多个纹理。

我想出了以下框架,但没有用。

void setUp(){
   planet_1_programID = createProgramAndAttachShader(parameters...)
   planet_2_programID = createProgramAndAttachShader(parameters...)
}
void sendDataToOpenGL(){
    create VAO and bind the VAO
    load data into a VBO
    send the data to OpenGL
}

void paintGL(void){
    glm::mat4 lookatMatrix = ...
    glm::mat4 projectionMatrix = ...
    glm::mat4 lookatMatrix = ...        
    glm::mat4 transformationMatrix = ... 

    // planet 1
    glUseProgram(planet_1_programID)
    bind to the previous created VAO
    bind texture information
    some model transformation
    glDrawArrays();

    // planet 2
    glUseProgram(planet_2_programID)
    bind to the previous created VAO
    bind texture information
    some model transformation
    glDrawArrays();
}

渲染结果是最终场景中只出现一颗行星。我想知道我是否以错误的方式理解了 glUseProgram 函数。谁能给我一些提示?

[update] 是不是因为glUseProgram之后需要重新定义所有的变量?目前我在 glUseProgram 之前定义了大部分变量,因为这两个行星将具有基本相同的变量。

Is it because I need to define all the variables all over again after glUseProgram? Currently I define most of the variables before glUseProgram because these two planets will have basically the same variables.

如果您指的是 "uniforms",那么您的问题就在于此(!)。您会看到,统一值存储 每个程序 因此您必须为每个程序至少设置一次统一值。不必在每个 glUseProgram 之后都执行此操作,但您必须至少为每个“glUseProgram”执行一次。