OpenGL使用不同的VAO和VBO创建两个三角形


OpenGL使用不同的VAO和VBO创建两个三角形

留个笔记以防以后找不到这点程序了,算是个OpenGL的启蒙。OpenGL3.3 ,使用不同的VAO、VBO和shader创建两个三角形,程序稍微有点乱可以直接运行,稍微改动一下就成了用EBO检索 vertices[]中的数据创建不同三角形的程序了。

OpenGL使用不同的VAO和VBO创建两个三角形_第1张图片这是效果图 pic_center =30x30)

以下为代码:


#include<GL/glew.h>
#include<GLFW/glfw3.h>
#include<iostream>
#include<GL/freeglut.h>
#include "Cons2.h"
using namespace std;
/*用同一个vertexshader创建了两个VAO,用不同的fragmentShader2创建了两个VBO用于渲染两个三角形,
两个三角形用不同 VAO和VBO 创建的三角形,一个VAO对应一个VBO*/
/*稍加改变即可用EBO创建多个三角形*/
void pro(GLFWwindow* window);
const char* vertexshaderSource =//vertexshader(顶点着色器)源代码
"#version 330 core                                   \n   "
"layout(location = 0) in vec3 aPos;                   \n  "// 位置变量的属性位置值为0
"void main() {                                       \n   "
"	gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);  \n"
"}  \n   ";//1.0的解释:纯量位置为1.0,向量位置为0.0

//vertexshader送出vex4的position,fragmentShader送出vec4的color

const char* fragmentShaderSource =//创建第二段片段着色器fragmentShader2源代码
"#version 330 core                          \n"
"out vec4 FragColor;                          \n"// 为片段着色器指定一个颜色输出
"void main() {                                  \n"
"	FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);;}      \n   ";
const char* fragmentShaderSource2 =//fragmentShader源代码
"#version 330 core                          \n"
"out vec4 FragColor;                          \n"// 为片段着色器指定一个颜色输出
"void main() {                                  \n"
"	FragColor = vec4(0.5f, 0.2f, 1.0f, 1.0f);;}      \n  

float firstTriangle[] = {
		-0.9f, -0.5f, 0.0f,  // left 
		-0.0f, -0.5f, 0.0f,  // right
		-0.45f, 0.5f, 0.0f,  // top 
};
float secondTriangle[] = {
	0.0f, -0.5f, 0.0f,  // left
	0.9f, -0.5f, 0.0f,  // right
	0.45f, 0.5f, 0.0f   // top 
};
unsigned int indices[] = { // 注意索引从0开始! 
	0, 1, 3, // 第一个三角形
	1, 3, 2,  // 第二个三角形
	0, 2, 4  // 第3个三角形
};

int main( int argc, char ** argv)
{
	glewInit();
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//告诉pc要用OpenGL的核心配置
	GLFWwindow* window = glfwCreateWindow(800, 600, "testwindow", NULL, NULL);//创建一个窗口
	if (window == NULL) {
		cout << "opengl window failed" << endl;
		glfwTerminate();//终止函数
		return EXIT_FAILURE;
	}
	glfwMakeContextCurrent(window);
	glewExperimental = true;
	if (glewInit()!=GLEW_OK){
		cout << "glew init failed" << endl;
		glfwTerminate();//终止glfw函数
		return EXIT_FAILURE;
	}
	glViewport(0, 0, 800, 600);
	unsigned int VBO[2];
	unsigned int VAO[2];

	glGenBuffers(2, VBO);
	glGenVertexArrays(2, VAO);

	glBindVertexArray(VAO[0]);
	glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(firstTriangle), firstTriangle, GL_STATIC_DRAW);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
	glEnableVertexAttribArray(0);

	glBindVertexArray(VAO[1]);
	glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(secondTriangle), secondTriangle, GL_STATIC_DRAW);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
	glEnableVertexAttribArray(0);

	unsigned int EBO;//创建EBO,用于挑选需要创建的三角形,
	glGenBuffers(1, &EBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);//只显示线
	//glCullFace(GL_FRONT_AND_BACK);//去除某一个面

	unsigned int vertexshader;
	vertexshader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertexshader, 1, &vertexshaderSource, NULL);
	glCompileShader(vertexshader);
	int success;
	char infolog[512];
	glGetShaderiv(vertexshader, GL_COMPILE_STATUS, &success);
	if (!success){
		glGetShaderInfoLog(vertexshader, 512, NULL, infolog);
		cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infolog << endl;
	}

	unsigned int fragmentshader;
	fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragmentshader, 1, &fragmentShaderSource, NULL);
	glCompileShader(fragmentshader);
	glGetShaderiv(fragmentshader, GL_COMPILE_STATUS, &success);
	if (!success){
		glGetShaderInfoLog(fragmentshader, 512, NULL, infolog);
		cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infolog << endl;
	}
	unsigned int shaderprogram;
	shaderprogram = glCreateProgram();
	glAttachShader(shaderprogram, vertexshader);
	glAttachShader(shaderprogram, fragmentshader);
	glLinkProgram(shaderprogram);
	glGetProgramiv(shaderprogram, GL_LINK_STATUS, &success);
	if (!success)
	{
		glGetProgramInfoLog(shaderprogram, 512, NULL, infolog);
	}

	fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragmentshader, 1, &fragmentShaderSource2, NULL);
	glCompileShader(fragmentshader);
	glGetShaderiv(fragmentshader, GL_COMPILE_STATUS, &success);
	if (!success) {
		glGetShaderInfoLog(fragmentshader, 512, NULL, infolog);
		cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infolog << endl;
	}
	unsigned int shaderprogram2;
	shaderprogram2 = glCreateProgram();
	glAttachShader(shaderprogram2, vertexshader);
	glAttachShader(shaderprogram2, fragmentshader);
	glLinkProgram(shaderprogram2);
	glGetProgramiv(shaderprogram2, GL_LINK_STATUS, &success);
	if (!success)
	{
		glGetProgramInfoLog(shaderprogram2, 512, NULL, infolog);
	}

	glDeleteShader(fragmentshader);
	glDeleteShader(vertexshader);


	while (!glfwWindowShouldClose(window))
	{
		glClearColor(0.5, 0.5, 0.5, 1.0);
		glClear(GL_COLOR_BUFFER_BIT);
		glBindVertexArray(VAO[0]);
		glUseProgram(shaderprogram2);
		glDrawArrays(GL_TRIANGLES, 0, 3);
		glBindVertexArray(VAO[1]);
		//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
		glUseProgram(shaderprogram);
		glDrawArrays(GL_TRIANGLES, 0, 3);
		//glDrawElements(GL_TRIANGLES, 9, GL_UNSIGNED_INT, (void*)0);//该这里,用EBO检索要创建的vertices[]中的三角形
		pro(window);//按ESC键退出
		glfwSwapBuffers(window);//将交换颜色缓冲区(包含GLFW窗口中每个像素的颜色值的大缓冲区),该缓冲区在此迭代期间用于绘制并将其显示为屏幕输出
		glfwPollEvents();//函数检查是否触发了任何事件(如键盘输入或鼠标移动事件),更新窗口状态,没有这个就动不了鼠标
	}
	glfwTerminate();//结束程序后进行清除资源
	return 0;
	glDeleteBuffers(2,VBO);
	glDeleteVertexArrays(2, VAO);
	glDeleteProgram(shaderprogram);
	glDeleteProgram(shaderprogram2);
}

void pro(GLFWwindow* window)
{
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
	{
		glfwSetWindowShouldClose(window, true);
	}
}

你可能感兴趣的:(OpenGL使用不同的VAO和VBO创建两个三角形)