码迷,mamicode.com
首页 > 其他好文 > 详细

OpenGL 绘制 2个不同颜色的三角形

时间:2020-02-04 12:28:55      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:you   res   fir   _for   cout   stat   图形   ever   源码   

#include <iostream>
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")

using namespace std;

// 窗口大小
const GLuint WIDTH = 800, HEIGHT = 600;

// 着色器 GLSL
const GLchar* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
"}\0";
const GLchar* fragmentShaderSource0 = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(0.5f, 0.2f, 1.0f, 1.0f);\n"
"}\n\0";
const GLchar* fragmentShaderSource1 = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(1.0f, 0.8f, 0.0f, 1.0f);\n"
"}\n\0";

int main(void) {

	// 初始化 GLFW
	glfwInit();
	// glfwWindowHint() // 配置 GLFW 函数(设置的选项,选项的值)
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
	// Mac OS 使用
	// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

	// 创建一个窗口
	GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "FirstTriangle", nullptr, nullptr);
	glfwMakeContextCurrent(window);

	// 初始化 GLEW
	glewExperimental = GL_TRUE;
	glewInit();

	// 设置窗口大小
	int width, height;
	glfwGetFramebufferSize(window, &width, &height);
	glViewport(0, 0, width, height);

	// 配置着色器
	// 创建一个着色器对象(GL_VERTEX_SHADER:顶点着色器)
	GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
	// 把着色器源码附加到着色器对象上,然后编译它
	glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
	glCompileShader(vertexShader);
	// 检测编译时错误,输出错误信息
	GLint success;
	GLchar infoLog[512];
	glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
	if (!success){
		glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
		std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
	}

	// 创建第1个着色器对象(GL_FRAGMENT_SHADER:片段着色器)
	GLuint fragmentShader0 = glCreateShader(GL_FRAGMENT_SHADER);
	// 着色器源码附加及编译
	glShaderSource(fragmentShader0, 1, &fragmentShaderSource0, NULL);
	glCompileShader(fragmentShader0);
	// 检测编译时错误,输出错误信息
	glGetShaderiv(fragmentShader0, GL_COMPILE_STATUS, &success);
	if (!success){
		glGetShaderInfoLog(fragmentShader0, 512, NULL, infoLog);
		std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
	}
	// 创建第2个着色器对象(GL_FRAGMENT_SHADER:片段着色器)
	GLuint fragmentShader1 = glCreateShader(GL_FRAGMENT_SHADER);
	// 着色器源码附加及编译
	glShaderSource(fragmentShader1, 1, &fragmentShaderSource1, NULL);
	glCompileShader(fragmentShader1);
	// 检测编译时错误,输出错误信息
	glGetShaderiv(fragmentShader1, GL_COMPILE_STATUS, &success);
	if (!success) {
		glGetShaderInfoLog(fragmentShader1, 512, NULL, infoLog);
		std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
	}

	// 创建 2个着色器程序对象
	GLuint shaderProgram0 = glCreateProgram();
	GLuint shaderProgram1 = glCreateProgram();
	// 链接着色器程序
	glAttachShader(shaderProgram0, vertexShader);
	glAttachShader(shaderProgram1, vertexShader);
	glAttachShader(shaderProgram0, fragmentShader0);
	glAttachShader(shaderProgram1, fragmentShader1);
	glLinkProgram(shaderProgram0);
	glLinkProgram(shaderProgram1);
	// 激活对象
	glUseProgram(shaderProgram0);
	// 检查错误,输出日志
	glGetProgramiv(shaderProgram0, GL_LINK_STATUS, &success);
	if (!success) {
		glGetProgramInfoLog(shaderProgram0, 512, NULL, infoLog);
		std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
	}
	glUseProgram(shaderProgram1);
	glGetProgramiv(shaderProgram1, GL_LINK_STATUS, &success);
	if (!success) {
		glGetProgramInfoLog(shaderProgram1, 512, NULL, infoLog);
		std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
	}
	// 激活后需要删除着色器对象
	glDeleteShader(vertexShader);
	glDeleteShader(fragmentShader0);
	glDeleteShader(fragmentShader1);

	// 输入顶点数据(这是一个平面的三角形,z 坐标表示深度)
	//GLfloat vertices[] = {
	//	-0.5f, -0.5f, 0.0f,
	//	 0.5f, -0.5f, 0.0f,
	//	 0.0f,  0.5f, 0.0f, };
	// 输入顶点数据(这是 2个平面的三角形) 
	GLfloat vertices0[] = {
	-0.5f, -0.5f, 0.0f,
	 0.0f,  0.0f, 0.0f,
	-0.3f,  0.3f, 0.0f, };
	GLfloat vertices1[] = {
	 0.5f, -0.5f, 0.0f,
	 0.0f,  0.0f, 0.0f,
	 0.3f,  0.3f, 0.0f, };

	// 生成 2个 VBO 对象
	GLuint VBOs[2];
	glGenBuffers(2, VBOs);
	// 创建 2个 VAO
	GLuint VAOs[2];
	glGenVertexArrays(2, VAOs);

	// 把新创建的缓冲绑定到 GL_ARRAY_BUFFER 目标上
	glBindVertexArray(VAOs[0]);
	glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
	// 把之前定义的顶点数据复制到缓冲的内存中(静态图形方式)
	// glBufferData() // 复制数据到当前绑定缓冲(缓冲类型,数据大小,数据,显存数据的管理方式)
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices0), vertices0, GL_STATIC_DRAW);
	// glVertexAttribPointer() // 解析顶点数据(1.顶点属性[对应前面的 layout (location = 0)],2.顶点属性的大小[3分量向量],
	//     3.数据类型[浮点数],4.是否标准化为(-1,1)范围内,5.单位步长,6.从起始位置的偏移量)
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
	glEnableVertexAttribArray(0);

	// 第2个三角形
	glBindVertexArray(VAOs[1]);
	glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
	glEnableVertexAttribArray(0);

	// 解绑
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);

	// 创建游戏循环
	while (!glfwWindowShouldClose(window)) {
		// 事件处理
		glfwPollEvents();
		// 指定背景色(深蓝色)
		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		// 使用着色器程序, glDrawArrays():绘制图元函数
		glUseProgram(shaderProgram0);
		glBindVertexArray(VAOs[0]);
		//glDrawArrays(GL_TRIANGLES, 0, 3);
		glDrawArrays(GL_TRIANGLES, 0, 3);
		// 第2个三角形
		glUseProgram(shaderProgram1);
		glBindVertexArray(VAOs[1]);
		glDrawArrays(GL_TRIANGLES, 0, 3);
		glBindVertexArray(0);
		// 交换缓冲
		glfwSwapBuffers(window);
	}

	// 删除 VAO 和 VBO,释放空间
	glDeleteVertexArrays(1, VAOs);
	glDeleteBuffers(1, VBOs);
	// 释放 GLFW 内存
	glfwTerminate();
	return 0;
}

 

OpenGL 绘制 2个不同颜色的三角形

标签:you   res   fir   _for   cout   stat   图形   ever   源码   

原文地址:https://www.cnblogs.com/darkmorn/p/12258911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!