opengl 来画个三角形

本文介绍如何使用OpenGL通过顶点数组绘制一个简单的三角形,并详细解释了从初始化OpenGL环境到使用顶点缓冲区对象绘制的基本步骤。
opengl 来画个三角形
这次我们通过顶点数组的方式来画三角形。
#include "glew.h"
#include <glfw3.h>

int main(void)
{
	GLFWwindow* window;

	/* Initialize the library */
	if (!glfwInit())
		return -1;

	/* Create a windowed mode window and its OpenGL context */
	window = glfwCreateWindow(480, 320, "Hello World", NULL, NULL);
	if (!window)
	{
		glfwTerminate();
		return -1;
	}

	/* Make the window's context current */
	glfwMakeContextCurrent(window);

	// Needed in core profile
	if( glewInit() != GLEW_OK)
	{
		glfwTerminate();
		return -1;
	}

	// An array of 3 vectors which represents 3 vertices
	static const GLfloat g_vertex_buffer_data[] = {
		-1.0f,-1.0f,0.0f,
		1.0f,-1.0f,0.0f,
		0.0f,1.0f,0.0f,
	};

	
	//This will identify our vertex buffer
	GLuint vertexbuffer;

	
	//Generate 1 buffer,put the resulting identifier in vertexbuffer
	glGenBuffers(1,&vertexbuffer);		

	//The following commands will talk about our 'vertexbuffer' buffer
	glBindBuffer(GL_ARRAY_BUFFER,vertexbuffer);

	//Give our vertices to OpenGL.
	glBufferData(GL_ARRAY_BUFFER,sizeof(g_vertex_buffer_data),g_vertex_buffer_data,GL_STATIC_DRAW);

	/* Loop until the user closes the window */
	while (!glfwWindowShouldClose(window))
	{
		
		glEnableVertexAttribArray(0);		
		glVertexAttribPointer(
			0,			// attribute 0. No particular reason for 0, but must match the layout in the shader.
			3,			// size
			GL_FLOAT,	// type
			GL_FALSE,	// normalized?
			0,			// stride
			(void*)0	// array buffer offset
			);

		glDrawArrays(GL_TRIANGLES,0,3);// Starting from vertex 0; 3 vertices total -> 1 triangle
		glDisableVertexAttribArray(0);

		/* Swap front and back buffers */
		glfwSwapBuffers(window);

		/* Poll for and process events */
		glfwPollEvents();
	}

	glfwTerminate();
	return 0;
}

首先工程里添加glew32.lib,cpp里添加#include "glew.h",引入glew库。glewInit(),glew库初始化。
const GLfloat g_vertex_buffer_data[]  顶点数组。glGenBuffers创建buffer,glBindBuffer激活buffer(指定是哪种类型的buffer),glBufferData传入数据,glEnableVertexAttribArray开户顶点数组属性,glVertexAttribPointer设置顶点数组属性,glDrawArrays画数组,glDisableVertexAttribArray关闭顶点数组属性
OpenGL中,绘制一个三角形的基本步骤如下: 1. **设置渲染上下文**:首先,你需要创建一个OpenGL context并初始化它,通常会在窗口创建时完成。 2. **启用顶点数组对象**:使用`glEnableClientState(GL_VERTEX_ARRAY)`启用顶点数据数组。 3. **设置顶点坐标**:定义三角形的顶点位置。例如,如果你的三角形由三个点A(x1, y1), B(x2, y2), C(x3, y3)组成,你可以这样做: ```c++ GLfloat vertices[] = {x1, y1, 0.0f, x2, y2, 0.0f, x3, y3, 0.0f}; glVertexPointer(3, GL_FLOAT, 0, vertices); ``` 4. **指定颜色或其他属性**:可以使用`glColor3ub()`等函数为每个顶点设置颜色。 5. **绑定顶点数组**:`glBindVertexArray`用来绑定你想要操作的顶点数组对象。 6. **启用顶点数组**:再次调用`glEnableVertexAttribArray`启用顶点数据。 7. **绘制三角形**:最后,使用`glDrawArrays(GL_TRIANGLES, 0, 3);`命令实际绘制三角形,GL_TRIANGLES表示绘制的是三角形,参数0是起始索引,3是元素数量(因为每个顶点是一个三元组)。 ```c++ glDrawArrays(GL_TRIANGLES, 0, 3); ``` 8. **结束当前状态**:记得在完成绘制后关闭所有开启的状态,如顶点数组、着色器等。 以上是在OpenGL ES 2.x或更早版本中的做法,现代版本可能会有所不同,特别是当你使用了VBO或VAO的时候。记得在程序结束时调用`glDisableClientState()`和`glDeleteVertexArrays()`释放资源。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值