• OpenGL笔记九之彩色三角形与重心插值算法


    OpenGL笔记九之彩色三角形与重心插值算法

    —— 2024-07-07 晚上

    bilibili赵新政老师的教程看后笔记

    code review!

    1.运行

    在这里插入图片描述

    3.main.cpp

    在这里插入图片描述

    代码

    #include 
    
    #define DEBUG
    
    //注意:glad头文件必须在glfw引用之前引用
    #include 
    #include 
    #include 
    #include //断言
    #include "wrapper/checkError.h"
    #include "application/Application.h"
    
    /*
    *┌────────────────────────────────────────────────┐
    *│ 目	   标: 学习彩色三角形绘制
    *│ 讲    师: 赵新政(Carma Zhao)
    *│ 拆分目标:
    *│			vs:
    *│ 		1 在vs当中获取layout为1的属性-color
    *			2 在vs当中声明out类型变量,向后传递颜色数据
    *			3 给out类型变量赋值
    *			fs:
    *			1 在fs当中声明in类型变量,接收前方阶段传递的颜色数据
    *			2 将获取的in类型颜色数据,赋值给fragColor
    *└────────────────────────────────────────────────┘
    */
    
    GLuint vao, program;
    
    
    void OnResize(int width, int height) {
    	GL_CALL(glViewport(0, 0, width, height));
    	std::cout << "OnResize" << std::endl;
    }
    
    void OnKey(int key, int action, int mods) {
    	std::cout << key << std::endl;
    }
    
    
    
    void prepareVAO() {
    	//1 准备positions colors
    	float positions[] = {
    		-0.5f, -0.5f, 0.0f,
    		0.5f, -0.5f, 0.0f,
    		0.0f,  0.5f, 0.0f,
    	};
    
    	float colors[] = {
    		1.0f, 0.0f,0.0f,
    		0.0f, 1.0f,0.0f,
    		0.0f, 0.0f,1.0f
    	};
    
    	unsigned int indices[] = {
    		0, 1, 2
    	};
    
    	//2 VBO创建
    	GLuint posVbo, colorVbo;
    	glGenBuffers(1, &posVbo);
    	glBindBuffer(GL_ARRAY_BUFFER, posVbo);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
    
    	glGenBuffers(1, &colorVbo);
    	glBindBuffer(GL_ARRAY_BUFFER, colorVbo);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
    
    	//3 EBO创建
    	GLuint ebo;
    	glGenBuffers(1, &ebo);
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    
    	//4 VAO创建
    	glGenVertexArrays(1, &vao);
    	glBindVertexArray(vao);
    
    	//5 绑定vbo ebo 加入属性描述信息
    	//5.1 加入位置属性描述信息
    	glBindBuffer(GL_ARRAY_BUFFER, posVbo);
    	glEnableVertexAttribArray(0);
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0);
    
    	//5.2 加入颜色属性描述数据
    	glBindBuffer(GL_ARRAY_BUFFER, colorVbo);
    	glEnableVertexAttribArray(1);
    	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0);
    
    	//5.3 加入ebo到当前的vao
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    
    	glBindVertexArray(0);
    }
    
    void prepareShader() {
    	//1 完成vs与fs的源代码,并且装入字符串
    	const char* vertexShaderSource =
    		"#version 330 core\n"
    		"layout (location = 0) in vec3 aPos;\n"
    		"layout (location = 1) in vec3 aColor;\n"
    		"out vec3 color;\n"
    		"void main()\n"
    		"{\n"
    		"   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
    		"   color = aColor;\n"
    		"}\0";
    	const char* fragmentShaderSource =
    		"#version 330 core\n"
    		"out vec4 FragColor;\n"
    		"in vec3 color;\n"
    		"void main()\n"
    		"{\n"
    		"   FragColor = vec4(color, 1.0f);\n"
    		"}\n\0";
    
    
    	//2 创建Shader程序(vs、fs)
    	GLuint vertex, fragment;
    	vertex = glCreateShader(GL_VERTEX_SHADER);
    	fragment = glCreateShader(GL_FRAGMENT_SHADER);
    
    
    	//3 为shader程序输入shader代码
    	glShaderSource(vertex, 1, &vertexShaderSource, NULL);
    	glShaderSource(fragment, 1, &fragmentShaderSource, NULL);
    
    	int success = 0;
    	char infoLog[1024];
    	//4 执行shader代码编译 
    	glCompileShader(vertex);
    	//检查vertex编译结果
    	glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
    	if (!success) {
    		glGetShaderInfoLog(vertex, 1024, NULL, infoLog);
    		std::cout << "Error: SHADER COMPILE ERROR --VERTEX" << "\n" << infoLog << std::endl;
    	}
    
    	glCompileShader(fragment);
    	//检查fragment编译结果
    	glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
    	if (!success) {
    		glGetShaderInfoLog(fragment, 1024, NULL, infoLog);
    		std::cout << "Error: SHADER COMPILE ERROR --FRAGMENT" << "\n" << infoLog << std::endl;
    	}
    
    	//5 创建一个Program壳子
    	program = glCreateProgram();
    
    	//6 将vs与fs编译好的结果放到program这个壳子里
    	glAttachShader(program, vertex);
    	glAttachShader(program, fragment);
    
    	//7 执行program的链接操作,形成最终可执行shader程序
    	glLinkProgram(program);
    
    	//检查链接错误
    	glGetProgramiv(program, GL_LINK_STATUS, &success);
    	if (!success) {
    		glGetProgramInfoLog(program, 1024, NULL, infoLog);
    		std::cout << "Error: SHADER LINK ERROR " << "\n" << infoLog << std::endl;
    	}
    
    	//清理
    	glDeleteShader(vertex);
    	glDeleteShader(fragment);
    }
    
    void render() {
    	//执行opengl画布清理操作
    	GL_CALL(glClear(GL_COLOR_BUFFER_BIT));
    
    	//1 绑定当前的program
    	GL_CALL(glUseProgram(program));
    
    	//2 绑定当前的vao
    	GL_CALL(glBindVertexArray(vao));
    	//3 发出绘制指令
    //	glDrawArrays(GL_LINE_STRIP, 0, 6);
    	glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
    	glBindVertexArray(0);
    }
    
    
    int main() {
    	if (!app->init(800, 600)) {
    		return -1;
    	}
    
    	app->setResizeCallback(OnResize);
    	app->setKeyBoardCallback(OnKey);
    
    	//设置opengl视口以及清理颜色
    	GL_CALL(glViewport(0, 0, 800, 600));
    	GL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f));
    
    	prepareShader();
    	prepareVAO();
    	while (app->update()) {
    		render();
    	}
    
    	app->destroy();
    
    	return 0;
    }
    
  • 相关阅读:
    【MySQL数据库】 七
    JavaScript常用知识点
    嵌入式以太网硬件构成与MAC、PHY芯片功能介绍
    HDMI线连接显示器后色彩灰暗问题解析与解决方案
    unity内存优化之AB包篇(微信小游戏)
    python下用cartopy绘制地形晕染(shading)图
    数学公式大全--极限、微分、积分
    药品研发---崩解时限检查法检验原始记录模板
    图解LSTM VS GRU
    【计算机图形学入门】笔记2:向量与线性代数(图形学中用到的线性代数)
  • 原文地址:https://blog.csdn.net/weixin_43297891/article/details/140365462