簡體   English   中英

使用GLM在OpenGL中繪制2個立方體

[英]Draw 2 cubes in OpenGL using GLM

因此,我可以使用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);
    }

我希望能夠使用相同的Cube類/大小等,但是只需再次渲染即可(我認為這是最有效/最佳的方法)。

我試過了

    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();

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

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

    myCube.render();

    glUseProgram(0);
    }

顯然,我實現錯了……如何在屏幕的任一側獲得一個立方體? 謝謝。

UPDATE

我意識到,我還沒有創建第二個多維數據集對象,但是現在實現了,它仍然不起作用...我是否混淆了視圖/模型矩陣的交互方式? 我為每個對象創建了一個新對象。

新代碼:

myCube.render();

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

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

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

myCube_Two.render();

UPDATE

着色器:

    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;
    }

最后,我創建了第二個Cube對象,第二個查看矩陣,並將它們與我的着色器中已經建立的模型矩陣一起使用,似乎兩個多維數據集都是單獨調用/渲染的。

正確的代碼是:

    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_Two的制服,否則它將無法工作。 我沒有看到為什么您的着色器需要另一個統一的模型視圖的原因,因為您沒有在同一調用上繪制兩個立方體。 如果不是問題,可以發布着色器代碼嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM