c++ opengl绘制三角形

 首先有必要看一下opengl的主要渲染流程:

首先是顶点缓冲对象/顶点数组对象准备好数据---->经过顶点着色器(进行顶点的坐标转换涉及到模型矩阵-->视图矩阵--->投影矩阵的一系列转换)----> 图元装配(图元包括点,线,三角形图元,是为接下来的光栅化进行准备的,假如没有图元的准备怎么进行绘制,又绘制什么呢哈哈)---> 光栅化(将对应的目标图元转换为像素点进行显示这里对应的每一个像素都是没有颜色的为下一步的片段着色做好准备)--->片段着色器(对每个片段进行着色操作,下一步就是显示了,对每个片段进行操作)---> 逐片段操作(裁剪测试,模板测试,深度测试,混合,抖动等)---> 送往帧缓冲 --->交换屏幕缓冲区就可以显示了。

最近在看opengl,记录一下:

#include 
#include 
#include 
#include 
#include 
typedef unsigned int uint;
using namespace std;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
GLFWwindow* showWindowSomething();
// template
void renderTriangle(int shaderProgram, unsigned int VAO, unsigned int VBO, unsigned int EBO,uint verticesSizeof,uint indicesSizeof,float* elementVertices,uint* indices);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int (*sum)(int a, int b);
int sumA(int (&arr)[5]);
void renderTwoTriangle(unsigned int VAO, unsigned int VBO, float* vertices, uint verticesSizeof);
int main()
{
	/*int max;
	glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max);
	cout << "max is " << max << endl;*/
	int temp[] = { 2,3,4,5,5 };
	int len = sizeof(temp) / sizeof(int);
	int (&arr)[5] = temp;
	sumA(arr);
	GLFWwindow* window = showWindowSomething();
	if (window == NULL)
	{
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

	// glad: load all OpenGL function pointers
	// ---------------------------------------
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		std::cout << "Failed to initialize GLAD" << std::endl;
		return -1;
	}
	unsigned int VAO, EBO, VBO,VAO1,VBO1;
	int shaderProgram,yellowShaderProgram;
    // 原始的program
	shaderProgram = glCreateProgram();
	// 黄色的program
	yellowShaderProgram = glCreateProgram();
	glGenBuffers(1, &EBO);
	glGenBuffers(1, &VBO);
	glGenBuffers(1, &VBO1);
	glGenVertexArrays(1, &VAO);
	glGenVertexArrays(1, &VAO1);
	// 顶点着色器
	const char* vertSharderCode = "#version 330 core\n"
		"layout(location = 0) in vec3 aPos;\n"
		"out vec4 outputColor;\n"
		"void main()\n"
		"{\n"
		"	"
		"	gl_Position = vec4(aPos,1.0f);\n"
		"   outputColor = vec4(aPos,1.0);\n"
		"}";
	// 声明顶点着色器
	unsigned int vertexShader;
	vertexShader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertexShader, 1, &vertSharderCode, NULL);
	glCompileShader(vertexShader);
	int success;
	char infoLog[512];
	glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
	if (!success) {
		glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
		cout << "vertex compile error" << endl;
		cout << infoLog << endl;
	}
	// 片段着色器
	const char* fragmentShaderCode = "#version 330 core\n"
		"in vec4 outputColor;\n"
		"out vec4 fragColor;\n"
		"uniform vec4 outcolor;\n"
		"void main(){\n"
		"	fragColor = outcolor;\n"
		"}\n";
	const char* fragmentShaderCode2 = "#version 330 core\n"
		"out vec4 fragColor;\n"
		"in vec4 outputColor;\n"
		"uniform vec4 outColor;\n"
		"void main(){\n"
		"	fragColor = outColor;\n"
		"}\n";
	unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragmentShader, 1, &fragmentShaderCode, NULL);
	glCompileShader(fragmentShader);
	glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
	if (!success) {
		glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
		cout << "fragmentshader compile error" << endl;
		cout << infoLog << endl;
	}
	// 编译第二个着色器
	uint fragmentShader2 = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragmentShader2, 1, &fragmentShaderCode2, NULL);
	glCompileShader(fragmentShader2);
	// 附加到程序上
	glAttachShader(shaderProgram, vertexShader);
	glAttachShader(shaderProgram, fragmentShader);
	// 链接
	glLinkProgram(shaderProgram);
	glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
	if (!success) {
		glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
		cout << infoLog << endl;
		cout << "链接程序失败" << endl;
	}
	glAttachShader(yellowShaderProgram, vertexShader);
	glAttachShader(yellowShaderProgram, fragmentShader2);
	glLinkProgram(yellowShaderProgram);
	glDeleteShader(vertexShader);
	glDeleteShader(fragmentShader);
	glDeleteShader(fragmentShader2);
	
	// 定义三角形所需要的顶点数据
	/*float vertices[] = {
		0.5f,-0.5f,0.0f,
		0.0f,0.5f,0.0f,
		-0.5f,-0.5f,0.0f
	};*/
	float elementVertices[] = {
	0.5f, 0.5f, 0.0f,   // 右上角
	0.5f, -0.5f, 0.0f,  // 右下角
	-0.5f, -0.5f, 0.0f, // 左下角
	-0.5f, 0.5f, 0.0f   // 左上角
	};
	unsigned int indices[] = {
		0, 1, 3, // 第一个三角形
		1, 2, 3  // 第二个三角形
	};
	uint eLen = sizeof(elementVertices);
	uint iLen = sizeof(indices);
	cout << "索引数组的sizeof is " << sizeof(indices) << endl;
	
	// 绘制三角形
	// renderTriangle(shaderProgram,VAO,VBO,NULL,vertices,nullptr);
	// 绘制矩形
	// renderTriangle(shaderProgram, VAO, VBO, EBO,eLen,iLen,elementVertices,indices);
	// 绘制两个三角形
	// 第一个三角形的坐标值
	float firstTiangle[] = {
		0.0f,0.0f,0.0f,
		0.0f,1.0f,0.0f,
		1.0f,0.0f,0.0f
		
	};
	float secondTiangle[] = {
		0.0f,0.0f,0.0f,
		0.0f,1.0f,0.0f,
		-1.0f,0.0f,0.0f
		
	};
	
	renderTwoTriangle(VAO,VBO,firstTiangle,sizeof(firstTiangle));
	renderTwoTriangle(VAO1,VBO1,secondTiangle,sizeof(secondTiangle));

	cout << "送往显卡数据完成,准备渲染" << endl;
	// 设置线框模式
	// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	// render loop
	// -----------
	while (!glfwWindowShouldClose(window))
	{
		// input
		// -----
		processInput(window);

		// render a triangle
		float timeValue = glfwGetTime();
		// ------
		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		// 改变第一个shader的颜色值
		float fredValue = (cos(timeValue) / 5.0f) + 0.8f;
		float fgreenValue = (sin(timeValue) / 10.0f) + 0.9f;
		float fblueValue = (sin(timeValue) / 5.0f) + 0.8f;
		// 获取到shader中uniform变量的位置
		int locationf = glGetUniformLocation(shaderProgram, "outcolor");
		glUseProgram(shaderProgram);
		glUniform4f(locationf,fredValue,fgreenValue,fblueValue,1.0f);
		glBindVertexArray(VAO);
		glDrawArrays(GL_TRIANGLES,0,3);

		// 改变第二个shader的颜色
		// 红色的分量
		float redValue = (sin(timeValue) / 4.0f) + 0.75f;
		// 绿色的分量
		float greenValue = (sin(timeValue) / 2.0f) + 0.5f;
		// 获取到声明的uniform变量的位置
		int vertexColorLocation = glGetUniformLocation(yellowShaderProgram,"outColor");
		glUseProgram(yellowShaderProgram);
		glUniform4f(vertexColorLocation,redValue,greenValue,0.0f,1.0f);
		glBindVertexArray(VAO1);
		glDrawArrays(GL_TRIANGLES,0,3);
	

		// glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
		// glBindVertexArray(0);
		// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
		// -------------------------------------------------------------------------------
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	// 删除对应的缓冲对象
	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);
	glDeleteBuffers(1, &EBO);
	// glfw: terminate, clearing all previously allocated GLFW resources.
	// 回收占用的内存
	// ------------------------------------------------------------------
	glfwTerminate();
	return 0;
}
int sumA(int (&arr)[5]) {
	cout << "sizeof arr is " << sizeof(&arr) << endl;
	cout << arr << endl;
	return sizeof(&arr);
}
// 渲染具体的物体对象方法
/**
   绘制三角形对象
*/
void renderTriangle(int shaderProgram, unsigned int VAO, unsigned int VBO, unsigned int EBO, uint verticesSizeof, uint indicesSizeof, float* elementVertices, uint* indices)
{
	cout << "顶点数组的sizeof is " << verticesSizeof << endl;
	// 定义顶点缓冲对象 GL_ARRAY_BUFFER
	glBindVertexArray(VAO);
	// 绑定缓冲对象 修改opengl状态机从此以后任何的缓冲调用都会来配置当前绑定的缓冲
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, verticesSizeof, elementVertices, GL_STATIC_DRAW);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesSizeof, indices, GL_STATIC_DRAW);

	// 配置顶点属性指针
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
	glEnableVertexAttribArray(0);
	// 链接都对象后删除着色器对象我们不在需要了
	
	// 数据已经送往了GPU内存中了可以解绑了
	glBindBuffer(GL_ARRAY_BUFFER, 0);

	glBindVertexArray(0);
}
//渲染两个相连的三角形
void renderTwoTriangle(unsigned int VAO,unsigned int VBO,float* vertices,uint verticesSizeof) {
	
	glBindVertexArray(VAO);
	glBindBuffer(GL_ARRAY_BUFFER,VBO);
	glBufferData(GL_ARRAY_BUFFER,verticesSizeof,vertices,GL_STATIC_DRAW);
	// 配置顶点属性指针
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);

}
GLFWwindow* showWindowSomething() {
	// glfw: initialize and configure
	// ------------------------------
	glfwInit();
	// glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif

	// glfw window creation
	// --------------------
	GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "OpenGL_GAME", NULL, NULL);

	return window;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
		glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
	// make sure the viewport matches the new window dimensions; note that width and 
	// height will be significantly larger than specified on retina displays.
	cout << "width is " << width << "height is " << height << endl;
	glViewport(0, 0, width, height);
}

c++ opengl绘制三角形_第1张图片

你可能感兴趣的:(c++,opengl)