triangles.vert
#version 330layout(location = 0)in vec4 position;layout(location = 1)in vec4 color;out vec4 vs_fs_color;void main(void){ vs_fs_color = color; gl_Position = position;}triangles.frag
#version 330in vec4 vs_fs_color;layout(location = 0)out vec4 color;void main(void){ color = vs_fs_color;}实现程序:
#include "stdafx.h"#include<iostream>using namespace std;#include "vgl.h"#include "LoadShaders.h"#include "vmath.h"#define USE_PRIMITIVE_RESTART 1//设置立方体的8个角点,边长为2,中心为原点static const GLfloat cube_positions[] ={ -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,};//每个顶点的颜色static const GLfloat cube_colors[] ={ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,};//三角形条带的索引static const GLushort cube_indices[] ={ 0, 1, 2, 3, 6, 7, 4, 5, //第一组条带 0xFFFF, //<<--这是重启动的索引 2, 6, 0, 4, 1, 5, 3, 7 //第二组条带};enum{ Arrayebo, Numebo };enum{ Arrayvao, Numvao };enum{ Arrayvbo, Numvbo };GLuint ebo[Numebo];GLuint vao[Numvao];GLuint vbo[Numvbo];void init(void){ ShaderInfo shaders[] = { { GL_VERTEX_SHADER, "triangles.vert" }, { GL_FRAGMENT_SHADER, "triangles.frag" }, { GL_NONE, NULL } }; GLuint program = LoadShaders(shaders); glUseProgram(program); //设置元素数组缓存 glGenBuffers(Numebo, ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[Arrayebo]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_indices), cube_indices, GL_STATIC_DRAW); //设置顶点属性 glGenVertexArrays(Numvao, vao); glBindVertexArray(vao[Arrayvao]); glGenBuffers(Numvbo,vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo[Arrayvbo]); glBufferData(GL_ARRAY_BUFFER, sizeof(cube_positions) + sizeof(cube_colors), NULL, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(cube_positions), cube_positions); glBufferSubData(GL_ARRAY_BUFFER, sizeof(cube_positions), sizeof(cube_colors), cube_colors); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)sizeof(cube_positions)); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1);}void display(void){ glClear(GL_COLOR_BUFFER_BIT); //设置使用glDrawElements glBindVertexArray(vao[Arrayvao]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[Arrayebo]);#if USE_PRIMITIVE_RESTART //如果开启了图元重启动,那么只需要调用一次绘制命令 glEnable(GL_PRIMITIVE_RESTART); //void glPrimitiveRestartIndex(GLuint index); //设置一个顶点数组元素的索引值,用来指定渲染过程中,从什么地方启动新的图元绘制。如果在处理定点数组元素索引的过程中 //遇到了一个符合该索引的数值,那么系统不会处理它对应的顶点数据,而是终止当前的图元绘制,并且从下一个顶点重新开始渲染 //同一类型的图元集合。 glPrimitiveRestartIndex(0xFFFF); glDrawElements(GL_TRIANGLE_STRip, 17,GL_UNSIGNED_SHORT, NULL);#else glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT, NULL); glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT, (const GLvoid*)(9*sizeof(GLushort)));#endif}int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA); glutInitWindowSize(512, 512); glutInitContextVersion(3, 3); glutCreateWindow(argv[0]); glewExperimental = GL_TRUE; if (glewInit()) { cerr << "Unable to initialize GLEW ... exiting" << endl; exit(EXIT_FAILURE); } init(); glutDisplayFunc(display); glutMainLoop();}新闻热点
疑难解答