2013-10-15 34 views
0

这样我就可以使用OpenGL3.2 +绘制一个旋转的立方体,它从0,0,0和向左走翻译抽2个立方体在OpenGL,但是当我尝试并绘制第二个(朝右),它并没有使...使用GLM

这是我的显示功能:

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

我希望能够使用相同的立方体类/大小等,而只是再次渲染它(我假设这是最有效/最好的方式)。

我想这

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

回答

1

最后,我创建了第二个立方体对象,第二观察矩阵,并用它们在我的着色器已经建立的模型矩阵似乎都立方体被称为/单独渲染。

正确的代码是:

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

除非你的着色器具有均匀称为ModelViewMatrix_Two,这是行不通的。我不明白了一个道理,为什么你的shader会需要另一个统一的模型视图,因为你没有在同一呼叫绘制两个立方体。如果这不是问题,你可以发布你的着色器代码吗?

+0

哎呀,我想了解你...你说得对,我还没有在我的着色器给予ModelViewMatrix_Two ......我想试试。 – Reanimation

+0

已经纠正了统一,现在只呈现则传递到shader第一... – Reanimation

+1

它解决了ModelViewMatrix。感谢的您的帮助......我只需要第二个矩阵视图和我着色器使用与现有型号矩阵...现在工作正常。我会在下面发布正确的代码。谢谢。 – Reanimation