Question

So I can draw a spinning cube using OpenGL3.2+ and translate it away from the 0,0,0 and to the left, but when I try and draw a second one (towards the right), it doesn't render...

This is my display function:

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

I want to be able to use the same Cube class / size etc, but just render it again (I assume that's the most efficient / best way to do it).

I tried this

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

Obviously, I've implemented it wrong... How can I get a cube either side of the screen? Thanks.

UPDATE

I realised, I hadn't created a second cube object, but with that now implemented, it still doesn't work... Am I confusing how the view/model matrices interact? I've created a new one for each object....

New Code:

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

Shader:

    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;
    }
Was it helpful?

Solution

In the end, I created a second Cube object, second viewing matrix and used them with the already established model matrix in my shader seems both cubes are called/rendered individually.

The correct code is:

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

OTHER TIPS

Unless your shader has a uniform called ModelViewMatrix_Two, this won't work. I don't see a reason why your shader would need another uniform for the model view since you are not drawing both cube on the same call. If it's not the problem, can you post your shader code?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top