[OpenGL] 使用现代的OpenGL3.x画一个红色的三角形(详细注释)

需要配置一下glew和glfw库,这个三角形被正确显示出来的时候,我望着屏幕傻笑了很久,从在github上下载源码到编译glew,glfw,到配环境再到一步步理解每行代码的含义,到最终画出这个三角形,一个下午没了。

#include 
#include 
#include 

std::string vertexShader =
"#version 330 core\n"
"\n"
"layout(location = 0) in vec4 position;\n"
"\n"
"void main()\n"
"{\n"
"    gl_Position = position;\n"
"}\n";

// 这里的location=0和上面的glVertexAttriPointer属性的index=0(第一个参数)是一样的 
// 表示要使用的是顶点的坐标

std::string fragmentShader =
"#version 330 core\n"
"layout(location = 0) out vec4 color;\n"
"\n"
"void main()\n"
"{\n"
"    color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";


static unsigned int CompileShader(unsigned int type, const std::string& source)
{ 
	unsigned int id = glCreateShader(type);
	const char* src = source.c_str();
	glShaderSource(id, 1, &src, nullptr);
	glCompileShader(id);
	
	// TODO: Error handling
	int result;
	glGetShaderiv(id, GL_COMPILE_STATUS, &result);
	if (result == GL_FALSE) {  // Shader is not compiled successfully
		int length;
		glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
		char* message = (char*)alloca(length * sizeof(char));
		glGetShaderInfoLog(id, length, &length, message);
		std::cout << "Failed to compile the " << 
			(type == GL_VERTEX_SHADER ? "vertex" : "fragment") <<
			"shader" << std::endl;
		std::cout << message << std::endl;  // 编译Shader失败
		glDeleteShader(id); // 删除编译失败的shader
		return 0;  // 失败时返回0
	}
	return id;
}

static unsigned int CreateShader(const std::string & vertexShader, const std::string& fragmentShader) 
{
	unsigned int program = glCreateProgram();
	unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
	unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
	
	glAttachShader(program, vs);
	glAttachShader(program, fs);

	glLinkProgram(program);
	glValidateProgram(program);

	glDeleteShader(vs);
	glDeleteShader(fs);

	return program;
}


int main(void)
{
	GLFWwindow* window;

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



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

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

	if (glewInit() != GLEW_OK) {
		std::cout << "Error!" << std::endl;
	}

	std::cout << glGetString(GL_VERSION) << std::endl;

	float positions[6] = {
		-0.5f, -0.5f,
		 0.0f,  0.5f,
		 0.5f, -0.5f
	};

	unsigned int buffer;
	glGenBuffers(1, &buffer);  //  创建一块 buffer,并将获得的id存在变量buffer中
	glBindBuffer(GL_ARRAY_BUFFER, buffer);  // 说明buffer的用途:将这样的一块 Buffer 用来存变量
											// 由于opengl是一个状态机模型,这里glBindBuffer就是当前绑定的绘制对象
	glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6, positions, GL_STATIC_DRAW);

	// glVertexAttribPointer();  // 指明vertex的layout,包括顶点的坐标,纹理,法线等等
	// 在进行编程的时候,可以使用glVertexAttribPointer指定Vertex内部的Attribute的分布

	// 使用 glVertexAttribPointer 选择一个Vertex中的一个指定的属性
	// 例如一个Vertex本身有属性 顶点,纹理,法线,分别编号为0,1,2
	// 那么再使用glVertexAttribPointer的时候就可以分别传入0,1,2代表此时指向的属性
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, (const void*)(0));

	// glBindBuffer(GL_ARRAY_BUFFER, 0);  /*取消绑定, 什么都不准备绘制*/
	unsigned int shader = CreateShader(vertexShader, fragmentShader);
	glUseProgram(shader);

	/* Loop until the user closes the window */
	while (!glfwWindowShouldClose(window))
	{
		/* Render here */
		glClear(GL_COLOR_BUFFER_BIT);

		glDrawArrays(GL_TRIANGLES, 0, 3);  // 画三角形,从第0个顶点开始画,连续3个点

		// glDrawElements(GL_TRIANGLES, 3, unsigned int, position);  /*这种绘制方式是使用顶点缓存(index buffer)的绘制方式*/
		/* Swap front and back buffers */
		glfwSwapBuffers(window);

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

	glDeleteProgram(shader);

	glfwTerminate();
	return 0;
}

效果:

[OpenGL] 使用现代的OpenGL3.x画一个红色的三角形(详细注释)_第1张图片

你可能感兴趣的:(opengl)