float vertices[] = { 0.5f, 0.5f, 0.0f, // top right 0.5f, -0.5f, 0.0f, // bottom right -0.5f, -0.5f, 0.0f, // bottom left -0.5f, 0.5f, 0.0f// top left };
unsignedint indices[] = { // note that we start from 0! 数组索引从0开始 0, 1, 3, // first Triangle 1, 2, 3// second Triangle };
代码
四边形
根据图像设计坐标顶点
1 2 3 4 5 6
float vertices[] = { 0.5f, 0.5f, 0.0f, // top right 0.5f, -0.5f, 0.0f, // bottom right -0.5f, -0.5f, 0.0f, // bottom left -0.5f, 0.5f, 0.0f// top left };
用四个顶点画两个三角形。索引顺序为
1 2 3 4
unsignedint indices[] = { // note that we start from 0! 数组索引从0开始 0, 1, 3, // first Triangle 1, 2, 3// second Triangle };
// draw our first triangle 画出两个三角形 glUseProgram(shaderProgram); //使用着色器程序 glBindVertexArray(VAO); // 绑定VAO seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized //glDrawArrays(GL_TRIANGLES, 0, 6); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); //渲染元素 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); //线框模式GL_LINE / 填充模式GL_FILL // glBindVertexArray(0); // no need to unbind it every time