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

Posted darkmorn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenGL 绘制 2个不同颜色的三角形相关的知识,希望对你有一定的参考价值。

#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
"
"layout (location = 0) in vec3 position;
"
"void main()
"
"{
"
"gl_Position = vec4(position.x, position.y, position.z, 1.0);
"
"}";
const GLchar* fragmentShaderSource0 = "#version 330 core
"
"out vec4 color;
"
"void main()
"
"{
"
"color = vec4(0.5f, 0.2f, 1.0f, 1.0f);
"
"}
";
const GLchar* fragmentShaderSource1 = "#version 330 core
"
"out vec4 color;
"
"void main()
"
"{
"
"color = vec4(1.0f, 0.8f, 0.0f, 1.0f);
"
"}
";

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
" << 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
" << 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
" << 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
" << 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
" << 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个不同颜色的三角形的主要内容,如果未能解决你的问题,请参考以下文章

带有片段着色器的OpenGL 3.3不同颜色

基于顶点的openGL中具有不同颜色的三角形的平面着色

绘制三角形的OpenGL程序给出了一个黄色的屏幕

OpenGL

OpenGL

OpenGL没有将最近的片段绘制到相机