我需要这样做,以便为了绘制多个三角形,我可以用不同的方式编写:
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);