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