opengl 第八版的第一个示例

#include 

using namespace std;

#include	"vgl.h"
#include	"LoadShaders.h"

enum VAO_IDs
{
	Triangles,
	NumVAOs
};
enum Buffer_IDs
{
	ArrayBuffer,
	NumBuffers
};
enum Atrrib_IDs
{
	vPosition = 0
};

GLuint VAOs[NumVAOs];//顶点数组
GLuint Buffers[NumBuffers];//缓存

const GLuint NumVertices = 6;//顶点数量

void init(void)
{	
	//申请顶点数组
	glGenVertexArrays(NumVAOs,VAOs);
	glBindVertexArray(VAOs[Triangles]);

	GLfloat vertices[NumVertices][2] = {
		{-0.90, -0.90},
		{ 0.85 -0.90 },
		{ -0.90, 0.85 },

		{ 0.90, -0.85 },
		{ 0.90, 0.90 },
		{ -0.85, 0.90 },
	};

	//申请缓存
	glGenBuffers(NumBuffers, Buffers);
	glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	ShaderInfo shaders[] = {
		{GL_VERTEX_SHADER,"triangles.vert"},
		{GL_FRAGMENT_SHADER,"triangles.frag"},
		{GL_NONE,NULL}
	};

	GLuint program = LoadShaders(shaders);
	glUseProgram(program);

	glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
	glEnableVertexAttribArray(vPosition);
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glBindVertexArray(VAOs[Triangles]);

	glDrawArrays(GL_TRIANGLES, 0, NumVertices);

	glFlush();
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA);
	glutInitWindowSize(512, 512),
	glutInitContextVersion(4, 3);
	glutInitContextProfile(GLUT_CORE_PROFILE);
	glutCreateWindow(argv[0]);

	//如果在申请顶点数组时glGenVertexArrays宕机
	//需要在glewInit开启glew对扩展的支持
	glewExperimental = GL_TRUE;

	if (glewInit())
	{
		cerr << "Unable to initialize glew ... exiting" << endl;
		exit(EXIT_FAILURE);
	}

	init();

	glutDisplayFunc(display);
	glutMainLoop();

}

 

转载于:https://my.oschina.net/u/157383/blog/863942

你可能感兴趣的:(opengl 第八版的第一个示例)