为两个不同的VAOS使用两个不同的着色器

时间:2018-06-12 15:59:15

标签: opengl shader glfw glew vao

我有两个单独的VAO对象,这些对象在while循环中呈现。每个都需要使用不同的着色器程序绘制。我可以单独渲染它们,但只有天空盒渲染。

var Entity = Bookshelf.Model.extend({
  tableName: 'entities',
  hasTimestamps: true,
  administrators: function() {
    return this.belongsToMany('Did','entities_dids', 'entity_id', 'did_id');
  },
  classes: function() {
    return this.hasMany('Class');
  }
});

var Did = Bookshelf.Model.extend({
  tableName: 'dids',
  hasTimestamps: true,
  usermeta: function() {
    return this.hasOne('Usermeta');
  },
  entities: function() {
    return this.belongsToMany('Entity','entities_dids', 'did_id', 'entity_id');
  },
  classes: function() {
    return this.belongsToMany('Class','classes_dids', 'did_id', 'class_id');
  }
});

要渲染的第一个对象是cloudySkybox。除非我注释掉cloudySkybox行,否则这是渲染和testObj不会出现的唯一内容。我缩小了它,看看是什么打破了testObj。 testObj呈现正常,直到我使用glUseProgram(shaderID)行;然后它停止渲染。有没有人知道为什么会这样? testObj.render()函数与glUseProgram具有相同的格式(differentShader)

do{
    // Clear the screen
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Access the active camera
    glm::mat4 ViewMatrix;
    if(worldCamMode)
    {
        worldCam->update();
        ViewMatrix = worldCamParent->getViewMtx();
    }
    else
    {
        playerCam->update();
        ViewMatrix = playerCamParent->getViewMtx();
    }

    // Retrieve the projection and model matrices
    glm::mat4 ProjectionMatrix = getProjectionMatrix();
    glm::mat4 ModelMatrix = glm::mat4(1.0f);
    glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
    glm::mat4 MVPX = ProjectionMatrix * ViewMatrix;

    // Render the skybox first
    glm::mat4 view = glm::mat4(glm::mat3(ViewMatrix));
    cloudySkybox->render(cubemapTexture, view, ProjectionMatrix, skyboxProgramID);

    int viewHandle = glGetUniformLocation(lampID, "view");
    if (viewHandle == -1) {
        std::cout << "Uniform: view is not an active uniform label\n";
    }
    glUniformMatrix4fv( viewHandle, 1, false, &MVPX[0][0]);
    glUseProgram(lampID);
    testObj.render(lampID);

    // Swap buffers
    glfwSwapBuffers(window);
    glfwPollEvents();

} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
       glfwWindowShouldClose(window) == 0 );

1 个答案:

答案 0 :(得分:2)

glUniform通过调用glUseProgram对作为当前状态一部分的程序对象进行操作。在致电glUseProgram(lampID)

之前设置程序glUniformMatrix4fv