Мне нужно сделать так, чтобы для отрисовки нескольких треугольников я мог писать не так:
GLfloat vertices[] = {
// Position Color
-0.8f, -0.8f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, -0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.2f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
1.0f, -0.3f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f
};
А вот так:
GLfloat vertices[] = {
// Color of triangle
1.0f, 0.0f, 0.0f,
// Positions
-0.8f, -0.8f, 0.0f,
0.0f, -0.0f, 0.0f,
0.0f, 0.2f, 0.0f,
// Color of triangle
0.0f, 1.0f, 0.0f,
// Positions
0.5f, 0.5f, 0.0f,
1.0f, -0.3f, 0.0f,
0.0f, 0.5f, 0.0f,
};
Вершинный шейдер
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
out vec3 fcolor;
void main()
{
fcolor = color;
gl_Position = vec4(position, 1.0);
}
Фрагментный шейдер
#version 330 core
in vec3 fcolor;
void main()
{
gl_FragColor = vec4(fcolor, 1.0f);
}
VAO и VBO
GLuint VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);