Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opengl 对两个不同的VAO使用两个不同的着色器_Opengl_Shader_Glfw_Glew_Vao - Fatal编程技术网

Opengl 对两个不同的VAO使用两个不同的着色器

Opengl 对两个不同的VAO使用两个不同的着色器,opengl,shader,glfw,glew,vao,Opengl,Shader,Glfw,Glew,Vao,我有两个独立的VAO对象,它们以while循环的方式进行渲染。每一个都需要使用不同的着色器程序绘制。我可以让它们单独渲染,但只有skybox一起渲染 do{ // Clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Access the active camera glm::mat4 ViewMatrix; if(worldCamMode) {

我有两个独立的VAO对象,它们以while循环的方式进行渲染。每一个都需要使用不同的着色器程序绘制。我可以让它们单独渲染,但只有skybox一起渲染

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 );
do{
//清除屏幕
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
//访问活动摄像机
glm::mat4视图矩阵;
if(世界模式)
{
worldCam->update();
ViewMatrix=worldCamParent->getViewMtx();
}
其他的
{
playerCam->update();
ViewMatrix=playerCamParent->getViewMtx();
}
//检索投影矩阵和模型矩阵
glm::mat4 ProjectionMatrix=getProjectionMatrix();
glm::mat4 ModelMatrix=glm::mat4(1.0f);
glm::mat4 MVP=ProjectionMatrix*ViewMatrix*ModelMatrix;
glm::mat4 MVPX=ProjectionMatrix*ViewMatrix;
//首先渲染skybox
glm::mat4视图=glm::mat4(glm::mat3(ViewMatrix));
cloudySkybox->render(立方体纹理、视图、投影矩阵、skyboxProgramID);
int viewHandle=glGetUniformLocation(lampID,“视图”);
如果(viewHandle==-1){
std::cout shape[i].mesh.material_id[i]];
glUniformMatrix4fv(modelniformhandle,1,false,glm::value_ptr(model));
glpaurements(GL_三角形,this->shape[i].mesh.index.size(),GL_UNSIGNED_INT,0);
glBindVertexArray(0);
glFlush();
}
}

通过调用
glUseProgram
对作为当前状态一部分的程序对象进行操作。在调用
glUniformMatrix4fv
之前,设置程序
glUseProgram(lampID)
,通过调用
glUseProgram
对作为当前状态一部分的程序对象进行操作。在调用
glUniformMatrix4fv
之前设置程序
glUseProgram(lampID)

天哪,非常感谢。这两个转换的语句能改变什么真是太神奇了,真是太感谢你了。两个切换语句可以改变的东西令人惊讶
void skybox::render(unsigned int cubemapTexture, glm::mat4 view, glm::mat4 projection, int shaderID)
{
    glUseProgram(shaderID);
    //glDepthMask(GL_FALSE);

    //int viewUniformHandle = glGetUniformLocation(shaderID, "view");
    //if (viewUniformHandle == -1)
    //  exit(1);
    //glUniformMatrix4fv( viewUniformHandle, 1, false, glm::value_ptr(view) );

    //int projectionUniformHandle = glGetUniformLocation(shaderID, "projection");
    //if (projectionUniformHandle == -1)
    //  exit(1);
    //glUniformMatrix4fv( projectionUniformHandle, 1, false, glm::value_ptr(projection) );

    //glBindVertexArray(skyboxVAO);
    //glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
    //glDrawArrays(GL_TRIANGLES, 0, 36);
    //glDepthMask(GL_TRUE);

    //glBindVertexArray(0);
    //glFlush();
}



void Model::render(int programID)
{
    glUseProgram(programID);

    int modelUniformHandle = glGetUniformLocation(programID, "model");
    if (modelUniformHandle == -1)
        exit(1);

    glm::mat4 model;

    for (int i = 0; i < VAOs.size(); i++) {

        glUseProgram(programID);
        glBindVertexArray(VAOs[i]);
        model = glm::mat4(1.0f);

        glBindTexture(GL_TEXTURE_2D, TexID[this->shape[i].mesh.material_ids[i]]);
        glUniformMatrix4fv(modelUniformHandle, 1, false, glm::value_ptr(model));
        glDrawElements(GL_TRIANGLES, this->shape[i].mesh.indices.size(), GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);
        glFlush();
    }
}