Send two triangles to a vertex shader. OpenGL, C ++, GLSL

4

I'm learning in college graphics processing with OpenGL in C ++. We are using the GLFW and GLEW libraries. My program consists of creating two triangles in different parts of the window. I created an array with the coordinates of the triangle, and two VBO and a VAO for it. Then I want to apply a translation to this triangle to print it on another part of the screen, using the second VBO and the same VAO.

My problem here is this: I straightened the two triangles in the VAO, one at location 0 and one at 1.

//4- Cria os vértices e seu VBO e VAO. Define a cor a partir de um Uniform
void SceneManager::setupScene()
{
// Set up vertex data (and buffer(s)) and attribute pointers
    GLfloat vertices[] = {
        // Positions                    
        -0.25f,  0.0f, 0.0f,
        0.25f,  0.0f, 0.0f,
        0.0f, 0.5f, 0.0f,
    };



    GLuint VBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);


    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    // Position attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(0);



    glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)

                                                            GLint colorLoc = glGetUniformLocation(shader->Program, "color");
    assert(colorLoc > -1);
    glUseProgram(shader->Program);
    glUniform4f(colorLoc, 0.1f, 1.0f, 0.2f, 1.0f);

    triangulo2(*vertices);

}

Triangle 2:

 void SceneManager::triangulo2(GLfloat arrayVertices) {

        //Variável que via armazenar o VBO do triangulo2
       GLuint VBO2;
            glGenBuffers(1, &VBO2);

            //Vincula o VAO único
            glBindVertexArray(VAO);

            //Inicializa o VBO2 e gera seus atributos
            glBindBuffer(GL_ARRAY_BUFFER, VBO2);
            glBufferData(GL_ARRAY_BUFFER, sizeof(arrayVertices), &arrayVertices, GL_STATIC_DRAW);

            //Ponteiro de atributo de posição desse triângulo. VAO
            glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
            glEnableVertexAttribArray(1);


    glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)


    GLint colorLoc = glGetUniformLocation(shader->Program, "color");
    assert(colorLoc > -1);
    glUseProgram(shader->Program);
    glUniform4f(colorLoc, 0.1f, 1.0f, 0.2f, 1.0f);

}

Excerpt where I send VAO to vertex shader:

void SceneManager::render()
{
    // Clear the colorbuffer
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    posicionaTriangulo2();

    if (resized) //se houve redimensionamento na janela, redefine a projection matrix
    {
        setupCamera2D();
        resized = false;
    }
    //Busca o VAO e desenha na tela.
    // Draw container
    glBindVertexArray(VAO);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    //drawElements é quando estou usando o EBO.
    glBindVertexArray(0);
}





   Só que eu não sei como passar corretamente os dois locations para o meu programa vertex shader. Sei que o objeto gl_Position da GLSL pode ser usado no meu vertex shader para processar as posições dos vértices de um objeteo passado ao shader, mas não sei como fazer quando eu passo mais de um objeto à esse shader. 

I tried to use two gl_Position, but in doing so, instead of appearing only a triangle as before, nothing appeared on the screen.

Can anyone give a light? It does not have quality material about it, it does not have decent tutorials ...

This is my Vertex Shader file:

version 440 core

//Posições do primeiro triângulo
layout (location = 0) in vec3 position;

//Posições do segundo triângulo:
layout (location = 1) in vec3 posicao2;

uniform vec4 color;  

out vec4 ourColor;

uniform mat4 model1;
uniform mat4 projection;

void main()
{
    gl_Position = projection * model1 * vec4(position, 1.0f);

//Quando eu adicionei este gl_Position, nada mais aparece na tela.
    gl_Position = projection * vec4(posicao2, 1.0f);

    ourColor = color;
}

This is the "positionTriangle2 ()" function, where I look for the model matrix in vertex shader (this is the matrix where I put the transformations, in that case), and I put some transformations in it to be applied in Vertex Shader to my triangle set in the VAO.

void SceneManager::posicionaTriangulo2() {
    // Render scene
        shader->Use();

    // Create transformations 
    //Esse método cria uma matriz4, mas não tem parâmetros... ONDE ELES SÃO DEFINIDOS? É a glm que o define?
    //A MATRIZ É PASSADA PARA O VERTEX SHADER
    model = glm::mat4();
    //model = glm::rotate(model, (GLfloat)glfwGetTime()*5, glm::vec3(0.0f, 0.0f, 0.1f));
    //model = glm::scale(model, glm::vec3(0.5, 1.0, 2.0));
    model = glm::translate(model, glm::vec3(1.0f, 0.3f, 0.0f));


    //Aplicar no rotate para uma rotação específica de 90 graus: glm::radians(90.0f)


    //EXEMPLO DO OPENGL:
    //trans = glm::rotate(trans, glm::radians(90.0f), glm::vec3(0.0, 0.0, 1.0));
    //trans = glm::scale(trans, glm::vec3(0.5, 0.5, 0.5));

    // Get their uniform location
    GLint modelLoc = glGetUniformLocation(shader->Program, "model");

    // Pass them to the shaders
    glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));

}
    
asked by anonymous 06.05.2018 / 04:01

1 answer

4

There is a function there called positionTriangle2 (...), it does not have the implementation there but it seems to me that you still do not understand the concept of modern OpenGL.

The idea of OpenGL is to put all the information of what is going to be drawn in a linear array, vertices, colors, normal, etc. With this array in hand you upload that information to the GPU, on the side of the vertex shader you basically adjust the vertices and on the side of the fragment shader you adjust the colors of each pixel. Think CPU / GPU as client / server.

Once the information is uploaded to the GPU, you will use the client to ask the server to render the information, basically you say: draws starting at the X position of the array, each polygon will have Y spaces in the array and then you will skip N array positions and continue until the array is finished.

To draw 2 separate triangles you have a few different options:

  • save 2 separate arrays for each triangle and upload individually (it's the weakest in performance)
  • To use glDrawElements, in it you will upload the vertices where the order does not matter in the array, because when you request it to be drawn it is necessary to inform the order (the indexes) that will be drawn
  • Use a stop called "primitive restart", I do not recommend using it because it is something much newer and therefore not available in any version of OpenGL, being much more restrictive on the platform

If you say you do not have quality material, you do not know these two sites:

These two sites are simply fantastic to help anyone who wants to learn OpenGL, whether beginner or even advanced.

    
07.05.2018 / 14:38