C++ 使用GLM在OpenGL中绘制2个立方体

C++ 使用GLM在OpenGL中绘制2个立方体,c++,opengl,glm-math,C++,Opengl,Glm Math,因此,我可以使用OpenGL3.2+绘制旋转立方体,并将其从0,0,0平移到左侧,但当我尝试绘制第二个立方体(向右)时,它不会渲染 这是我的显示功能: void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(myShader.handle()); GLuint matLocatio

因此,我可以使用OpenGL3.2+绘制旋转立方体,并将其从0,0,0平移到左侧,但当我尝试绘制第二个立方体(向右)时,它不会渲染

这是我的显示功能:

    void display()                                  
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glUseProgram(myShader.handle());

    GLuint matLocation = glGetUniformLocation(myShader.handle(), "ProjectionMatrix");
    glUniformMatrix4fv(matLocation, 1, GL_FALSE, &ProjectionMatrix[0][0]);

    spinY+=0.03;
    if(spinY>360) spinY = 0;

    glm::mat4 viewMatrix;
    viewMatrix = glm::translate(glm::mat4(1.0),glm::vec3(0,0,-100));        //viewing matrix
    ModelViewMatrix = glm::translate(viewMatrix,glm::vec3(-30,0,0));        //translate object from the origin
    ModelViewMatrix = glm::rotate(ModelViewMatrix,spinY, glm::vec3(0,1,0));                 //rotate object about y axis

    glUniformMatrix4fv(glGetUniformLocation(myShader.handle(), "ModelViewMatrix"), 1, GL_FALSE, &ModelViewMatrix[0][0]); //pass matrix to shader

    //Add the following line just before the line to draw the cube to 
    //check that the origin of the cube in eye space is (-30, 0, -100);
    result = glm::vec3(ModelViewMatrix * glm::vec4(0,0,0,1));
    std::cout<<glm::to_string(result)<<std::endl; //print matrix to get coordinates.

    myCube.render();
    glUseProgram(0);
    }
更新

着色器:

    uniform mat4 ModelViewMatrix;
    //uniform mat4 ModelViewMatrix_Two; //NOT NEEDED - USED SAME SHADER OBJECT
    uniform mat4 ProjectionMatrix;

    in  vec3 in_Position;  // Position coming in
    in  vec3 in_Color;     // colour coming in
    out vec3 ex_Color;     // colour leaving the vertex, this will be sent to the fragment shader

    void main(void)
    {
    gl_Position = ProjectionMatrix * ModelViewMatrix * vec4(in_Position, 1.0);
    //gl_Position = ProjectionMatrix * ModelViewMatrix_Two * vec4(in_Position, 1.0);
    ex_Color = in_Color;
    }

除非着色器具有名为ModelViewMatrix_-Two的统一属性,否则这将无法工作。我看不出为什么着色器需要另一个统一的模型视图,因为您没有在同一调用中绘制两个立方体。如果不是问题,你能发布你的着色器代码吗?

最后,我创建了第二个立方体对象,第二个查看矩阵,并将它们与着色器中已经建立的模型矩阵一起使用,似乎两个立方体都被单独调用/渲染

正确的代码是:

    glm::mat4 viewMatrix_Two, ModelViewMatrix_Two;
    viewMatrix_Two = glm::translate(glm::mat4(1.0),glm::vec3(0,0,-200));
    ModelViewMatrix = glm::translate(viewMatrix_Two,glm::vec3(30,0,0));
    ModelViewMatrix = glm::rotate(ModelViewMatrix,spinX, glm::vec3(1,0,0));

    glUniformMatrix4fv(glGetUniformLocation(myShader.handle(), "ModelViewMatrix"), 1, GL_FALSE, &ModelViewMatrix[0][0]); //pass matrix to shader

    myCube_Two.render();

哦,我想念你。。。你说得对,我的着色器中没有提供ModelViewMatrix\u 2。。。我会试试。修正了均匀性后,它现在只渲染首先传递给着色器的ModelViewMatrix…解决了。谢谢你的帮助。。。我只需要视图的第二个矩阵,并将其与着色器中的现有模型矩阵一起使用。。。现在一切正常。我将在下面发布正确的代码。谢谢
    uniform mat4 ModelViewMatrix;
    //uniform mat4 ModelViewMatrix_Two; //NOT NEEDED - USED SAME SHADER OBJECT
    uniform mat4 ProjectionMatrix;

    in  vec3 in_Position;  // Position coming in
    in  vec3 in_Color;     // colour coming in
    out vec3 ex_Color;     // colour leaving the vertex, this will be sent to the fragment shader

    void main(void)
    {
    gl_Position = ProjectionMatrix * ModelViewMatrix * vec4(in_Position, 1.0);
    //gl_Position = ProjectionMatrix * ModelViewMatrix_Two * vec4(in_Position, 1.0);
    ex_Color = in_Color;
    }
    glm::mat4 viewMatrix_Two, ModelViewMatrix_Two;
    viewMatrix_Two = glm::translate(glm::mat4(1.0),glm::vec3(0,0,-200));
    ModelViewMatrix = glm::translate(viewMatrix_Two,glm::vec3(30,0,0));
    ModelViewMatrix = glm::rotate(ModelViewMatrix,spinX, glm::vec3(1,0,0));

    glUniformMatrix4fv(glGetUniformLocation(myShader.handle(), "ModelViewMatrix"), 1, GL_FALSE, &ModelViewMatrix[0][0]); //pass matrix to shader

    myCube_Two.render();