• OpenGL官方文档中的入门教程源代码:在3维空间中自由移动


    项目总览:

    1.开发语言:C++语言
    2.IDE:VS2022
    3.开发用时:一周
    4.本项目是OpenGL官方文档https://learnopengl.com中《入门》栏目的案例,素材和函数功能都可在官方文档找到。
    5.源码已上传到我的GitHub,链接:https://github.com/2394799692/OpenGLpractice1 或点此跳转


    以下是本篇文章正文内容,欢迎朋友们进行指正,一起探讨,共同进步。——来自读研路上的lwj。QQ:2394799692(请标明来意)

    一、开发前的准备工作

    链接:https://pan.baidu.com/s/1dtXE22PZw14hif0ui3r2qA?pwd=z22g 提取码:z22g

    1.将以上链接中的三个文件分别放到自己硬盘的一个文件夹中:例如D盘/OpenGL/…

    在这里插入图片描述

    2.打开VS2022创建一个项目,右击窗体选择属性

    在这里插入图片描述

    3.配置这3个文件的一些环境到你的项目中,这里可以参考:这节视频,同时这也是一节非常好的OpenGL课程,感谢傅老师!

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    二、代码目录

    在这里插入图片描述

    1.头文件Camera.h

    定义源文件Camera.cpp中所使用到的函数和变量,这两个文件的作用是定义鼠标为摄像机的位置,从而实现在3维坐标中上下左右的移动

    #pragma once
    
    #include 
    #include 
    
    class Camera
    {
    public:
    	Camera(glm::vec3 position, glm::vec3 target, glm::vec3 worldup);
    	Camera(glm::vec3 position, float pitch, float yaw, glm::vec3 worldup);
    
    	glm::vec3 Position;
    	glm::vec3 Forward;
    	glm::vec3 Right;
    	glm::vec3 Up;
    	glm::vec3 WorldUp;
    	float Pitch;
    	float Yaw;
    	float SenseX = 0.001f;
    	float SenseY = 0.001f;
    	float speedZ = 0;
    	glm::mat4 GetViewMatrix();
    	void ProcessMouseMovement(float deltaX, float deltaY);
    	void UpdateCameraPos();
    private:
    	void UpdateCameraVectors();
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    2.源文件Camera.cpp
    #include "Camera.h"
    
    Camera::Camera(glm::vec3 position, glm::vec3 target, glm::vec3 worldup)
    {
    	Position = position;
    	WorldUp = worldup;
    	Forward = glm::normalize(target - position);//转为单位向量
    	Right = glm::normalize(glm::cross(Forward, WorldUp));//cross是叉乘的意思
    	Up = glm::normalize(glm::cross(Forward, Right));
    }
    Camera::Camera(glm::vec3 position, float pitch, float yaw, glm::vec3 worldup)
    {
    	Position = position;
    	WorldUp = worldup;
    	Pitch = pitch;
    	Yaw = yaw;
    	Forward.x = glm::cos(Pitch) * glm::sin(Yaw);
    	Forward.y = glm::sin(Pitch);		   
    	Forward.z = glm::cos(Pitch) * glm::cos(Yaw);
    	Right = glm::normalize(glm::cross(Forward, WorldUp));//cross是叉乘的意思
    	Up = glm::normalize(glm::cross(Forward, Right));
    }
    
    
    glm::mat4 Camera::GetViewMatrix()
    {
    	return glm::lookAt(Position, Position + Forward, WorldUp);
    }
    void Camera::UpdateCameraVectors()
    {
    	Forward.x = glm::cos(Pitch) * glm::sin(Yaw);
    	Forward.y = glm::sin(Pitch);
    	Forward.z = glm::cos(Pitch) * glm::cos(Yaw);
    	Right = glm::normalize(glm::cross(Forward, WorldUp));//cross是叉乘的意思
    	Up = glm::normalize(glm::cross(Forward, Right));
    }
    void Camera::ProcessMouseMovement(float deltaX, float deltaY)
    {
    	Pitch -= deltaY* SenseX;
    	Yaw -= deltaX* SenseY;
    	UpdateCameraVectors();
    }
    void Camera::UpdateCameraPos()
    {
    	Position += Forward*speedZ*0.01f;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    3.头文件Shader.h

    结合shader.cpp和着色器资源实现渲染/着色功能

    #pragma once
    #include
    using namespace std;
    class Shader
    {
    public:
    	Shader(const char* vertexPath,const char* fragmentPath);
    	string vertexString;
    	string fragmentString;
    	const char* vertexSource;
    	const char* fragmentSource;
    	unsigned int ID;//shader program id
    	void use();
    
    private:
    	void checkCompileErrors(unsigned int ID, std::string type);
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    4.源文件Shader.cpp
    #include "Shader.h"
    #include
    #include
    #include
    #define GLEW_STATIC
    #include
    #include
    using namespace std;
    //shader命名空间下的shader方法
    Shader::Shader(const char* vertexPath, const char* fragmentPath)
    {
    	ifstream vertexFile;
    	ifstream fragmentFile;
    	stringstream vertexSStream;
    	stringstream fragmentSStream;
    
    	vertexFile.open(vertexPath);
    	fragmentFile.open(fragmentPath);
    	vertexFile.exceptions(ifstream::failbit || ifstream::badbit);
    	fragmentFile.exceptions(ifstream::failbit || ifstream::badbit);
    	try
    	{
    		if (!vertexFile.is_open() || !fragmentFile.is_open())
    		{
    			throw exception("open file error");
    		}
    		vertexSStream << vertexFile.rdbuf();
    		fragmentSStream << fragmentFile.rdbuf();
    
    		vertexString = vertexSStream.str();
    		fragmentString = fragmentSStream.str();
    
    		vertexSource = vertexString.c_str();
    		fragmentSource = fragmentString.c_str();
    
    		unsigned int vertex, fragment;//承载gpu返还的shader
    		vertex = glCreateShader(GL_VERTEX_SHADER);
    		glShaderSource(vertex, 1, &vertexSource, NULL);
    		glCompileShader(vertex);
    		checkCompileErrors(vertex, "VERIEX");
    
    		fragment = glCreateShader(GL_FRAGMENT_SHADER);
    		glShaderSource(fragment, 1, &fragmentSource, NULL);
    		glCompileShader(fragment);
    		checkCompileErrors(fragment, "FRAGMENT");
    
    		ID = glCreateProgram();
    		glAttachShader(ID, vertex);
    		glAttachShader(ID, fragment);
    		glLinkProgram(ID);
    		checkCompileErrors(ID, "PROGRAM");
    
    		glDeleteShader(vertex);
    		glDeleteShader(fragment);
    	}
    	catch (const std::exception& ex)
    	{
    		printf(ex.what());
    	}
    }
    
    void Shader::use() {
    	glUseProgram(ID);
    }
    void Shader::checkCompileErrors(unsigned int ID, string type)
    {
    	int success;
    	char infoLog[512];
    	if (type != "PROGRAM")
    	{
    		glGetShaderiv(ID, GL_COMPILE_STATUS, &success);
    		if (!success)
    		{
    			glGetShaderInfoLog(ID, 512, NULL, infoLog);
    			cout << "shader compile error:" << infoLog << endl;
    		}
    	}
    	else
    	{
    		glGetProgramiv(ID, GL_LINK_STATUS, &success);
    		if (!success)
    		{
    			glGetProgramInfoLog(ID, 512, NULL, infoLog);
    			cout << "program link error:" << infoLog << endl;
    		}
    
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    5.源文件main.cpp
    #include
    #define GLEW_STATIC
    #include
    #include
    
    #include "Shader.h"
    
    #include 
    #include 
    #include 
    #include "Camera.h"
    #define STB_IMAGE_IMPLEMENTATION
    #include "stb_image.h"
    //float vertices[] = {
    //	//     ---- 位置 ----       ---- 颜色 ----     - 纹理坐标 -
    //		 0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // 右上
    //		 0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // 右下
    //		-0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // 左下
    //		-0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // 左上
    //};
    float vertices[] = {
    	-0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
    	 0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
    	 0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
    	 0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
    	-0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
    	-0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
    
    	-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
    	 0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
    	 0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
    	 0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
    	-0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
    	-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
    
    	-0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
    	-0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
    	-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
    	-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
    	-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
    	-0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
    
    	 0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
    	 0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
    	 0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
    	 0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
    	 0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
    	 0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
    
    	-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
    	 0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
    	 0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
    	 0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
    	-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
    	-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
    
    	-0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
    	 0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
    	 0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
    	 0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
    	-0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
    	-0.5f,  0.5f, -0.5f,  0.0f, 1.0f
    };
    unsigned int indices[] = {
    	0,1,2,
    	2,3,0
    }; 
    
    glm::vec3 cubePositions[] = {
      glm::vec3(0.0f,  0.0f,  0.0f),
      glm::vec3(2.0f,  5.0f, -15.0f),
      glm::vec3(-1.5f, -2.2f, -2.5f),
      glm::vec3(-3.8f, -2.0f, -12.3f),
      glm::vec3(2.4f, -0.4f, -3.5f),
      glm::vec3(-1.7f,  3.0f, -7.5f),
      glm::vec3(1.3f, -2.0f, -2.5f),
      glm::vec3(1.5f,  2.0f, -2.5f),
      glm::vec3(1.5f,  0.2f, -1.5f),
      glm::vec3(-1.3f,  1.0f, -1.5f)
    };
    
    float lastX;
    float lastY;
    bool firstMouse = true;
    //引入class类
    	//Camera camera(glm::vec3(0,0,3.0f),glm::vec3(0,0,0),glm::vec3(0,1.0f,0));
    Camera camera(glm::vec3(0, 0, 3.0f), glm::radians(15.0f), glm::radians(180.0f), glm::vec3(0, 1.0f, 0));
    void processInput(GLFWwindow* window)
    {
    	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
    	{
    		glfwSetWindowShouldClose(window, true);
    	}
    	if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
    	{
    		camera.speedZ=1.0f;
    	}
    	else if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
    	{
    		camera.speedZ = -1.0f;
    	}
    	else {
    		camera.speedZ = 0;
    	}
    
    }
    void mouse_callback(GLFWwindow* window, double xPos, double yPos)
    {
    	if (firstMouse == true)
    	{
    		lastX = xPos;
    		lastY = yPos;
    		firstMouse = false;
    	}
    	float deltaX, deltaY;
    	deltaX = xPos - lastX;
    	deltaY = yPos - lastY;
    
    	lastX = xPos;
    	lastY = yPos;
    	camera.ProcessMouseMovement(deltaX, deltaY);
    	/*printf("%f \n", deltaX);*/
    }
    int main()
    {
    	glfwInit();//初始化
    	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    	//打开一个窗体
    	GLFWwindow* window = glfwCreateWindow(800, 600, "My OpenGL Game", NULL, NULL);
    	if (window == NULL)
    	{
    		printf("open window failed.");
    		glfwTerminate();
    		return -1;
    	}
    	glfwMakeContextCurrent(window);
    	glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);//消失鼠标
    	glfwSetCursorPosCallback(window, mouse_callback);
    	//init glew
    	glewExperimental = true;
    	if (glewInit() != GLEW_OK)
    	{
    		printf("Init GLEW failed.");
    		glfwTerminate();
    		return -1;
    	}
    	
    	glViewport(0, 0, 800, 600);
    	glEnable(GL_CULL_FACE);
    	glCullFace(GL_BACK);//背面剔除,正面:GL_FRONT
    	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);//显示边框
    	glEnable(GL_DEPTH_TEST);//做深度缓冲
    
    	Shader* myShader = new Shader("vertexSource.txt", "fragmentSource.txt");
    
    	unsigned int VAO;
    	glGenVertexArrays(1, &VAO);
    	glBindVertexArray(VAO); 
    
    	unsigned int VBO;
    	glGenBuffers(1, &VBO);
    	glBindBuffer(GL_ARRAY_BUFFER, VBO);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    
    	unsigned int EBO;
    	glGenBuffers(1, &EBO);
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
    	glEnableVertexAttribArray(0);
    	/*glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
    	glEnableVertexAttribArray(1);*/
    	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    	glEnableVertexAttribArray(2);
    
    	unsigned int TexBufferA;
    	glGenTextures(1, &TexBufferA);
    	glActiveTexture(GL_TEXTURE0);
    	glBindTexture(GL_TEXTURE_2D, TexBufferA);
    
    	int width, height, nrChannel;
    	stbi_set_flip_vertically_on_load(true);//图像翻转
    	unsigned char* data = stbi_load("container.jpg", &width, &height, &nrChannel, 0);
    	if (data)
    	{
    		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    		glGenerateMipmap(GL_TEXTURE_2D);
    	}
    	else
    	{
    		printf("load image failed.");
    	}
    	stbi_image_free(data);
    
    	unsigned int TexBufferB;//载入笑脸
    	glGenTextures(1, &TexBufferB);
    	glActiveTexture(GL_TEXTURE3);
    	glBindTexture(GL_TEXTURE_2D, TexBufferB);
    	unsigned char* data2 = stbi_load("awesomeface.png", &width, &height, &nrChannel, 0);
    	if (data2)
    	{
    		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
    		glGenerateMipmap(GL_TEXTURE_2D);
    	}
    	else
    	{
    		printf("load image failed.");
    	}
    	stbi_image_free(data2);
    	
    
    	glm::mat4 trans;
    	//trans = glm::translate(trans, glm::vec3(-0.01f, 0, 0));//平移
    	//trans = glm::rotate(trans,glm::radians(45.0f), glm::vec3(0, 0, 1.0f));//旋转45度
    	//trans = glm::scale(trans, glm::vec3(2.0f, 2.0f, 2.0f));//缩放
    	glm::mat4 modelMat;
    	modelMat = glm::rotate(modelMat, glm::radians(-45.0f), glm::vec3(0, 1.0f, 1.0f));
    	glm::mat4 viewMat;
    	//viewMat = glm::translate(viewMat, glm::vec3(0, 0, -3.0f));
    	//viewMat = camera.GetViewMatrix();
    	glm::mat4 projMat;
    	projMat = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);
    	
    	
    	while (!glfwWindowShouldClose(window))
    	{
    		/*glm::mat4 trans;//定义在里面每次重置角度不会转太快
    		trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0));
    		trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0, 0, 1.0f));*/
    		processInput(window);
    		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    		/*glClear(GL_COLOR_BUFFER_BIT);*/
    		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//加了一个清除上一轮深度缓冲
    		glActiveTexture(GL_TEXTURE0);
    		glBindTexture(GL_TEXTURE_2D, TexBufferA);
    		glActiveTexture(GL_TEXTURE3);
    		glBindTexture(GL_TEXTURE_2D, TexBufferB);
    		glBindVertexArray(VAO);
    		//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    		viewMat = camera.GetViewMatrix();
    		for (int i = 0; i < 10; i++) {
    			glm::mat4 modelMat2;
    			modelMat2 = glm::translate(modelMat2, cubePositions[i]);
    			myShader->use();
    			glUniform1i(glGetUniformLocation(myShader->ID, "ourTexture"), 0);
    			glUniform1i(glGetUniformLocation(myShader->ID, "ourFace"), 3);
    			//glUniformMatrix4fv(glGetUniformLocation(myShader->ID, "transform"), 1, GL_FALSE, glm::value_ptr(trans));
    			glUniformMatrix4fv(glGetUniformLocation(myShader->ID, "modelMat"), 1, GL_FALSE, glm::value_ptr(modelMat2));
    			glUniformMatrix4fv(glGetUniformLocation(myShader->ID, "viewMat"), 1, GL_FALSE, glm::value_ptr(viewMat));
    			glUniformMatrix4fv(glGetUniformLocation(myShader->ID, "projMat"), 1, GL_FALSE, glm::value_ptr(projMat));
    
    			glDrawArrays(GL_TRIANGLES, 0, 36);
    			//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    		}
    		
    
    		glfwSwapBuffers(window);
    		glfwPollEvents();
    		camera.UpdateCameraPos();
    	}
    	glfwTerminate();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266

    三、着色器

    1.顶点着色器vertexSource.txt

    顶点着色器(Vertex Shader)是几个可编程着色器中的一个。如果我们打算做渲染的话,现代OpenGL需要我们至少设置一个顶点和一个片段着色器。

    #version 330 core                                
    layout (location=0) in vec3 aPos;                
    layout (location=1) in vec3 aColor;    
    layout (location=2) in vec2 aTexCoord;    
    
    //uniform mat4 transform;
    uniform mat4 modelMat;
    uniform mat4 viewMat;
    uniform mat4 projMat;
    
    out vec4 vertexColor;
    out vec2 TexCoord;
    void main(){                                     
        gl_Position=projMat*viewMat*modelMat*vec4(aPos.x,aPos.y,aPos.z,1.0f);  
        vertexColor=vec4(aColor.x,aColor.y,aColor.z,1.0f);
        TexCoord=aTexCoord;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    2.片段着色器fragmentSource.txt

    片段着色器(Fragment Shader)是第二个也是最后一个我们打算创建的用于渲染三角形的着色器。片段着色器所做的是计算像素最后的颜色输出。

    #version 330 core           
    in vec4 vertexColor;             
    in vec2 TexCoord;
    
    uniform sampler2D ourTexture;
    uniform sampler2D ourFace;
    
    
    out vec4 FragColor; 
    void main(){                
        FragColor = mix(texture(ourTexture,TexCoord),texture(ourFace,TexCoord), 0.2);
        //如果第三个值是0.0,它会返回第一个输入;如果是1.0,会返回第二个输入值。0.2会返回80%的第一个输入颜色和20%的第二个输入颜色,即返回两个纹理的混合色。
    } 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    四、效果展示:

    视频效果:

    OpenGL入门demo


    图片效果:
    在这里插入图片描述

  • 相关阅读:
    Mac M2芯片安装Anaconda的版本不兼容的问题
    FreeMarker快速入门详解
    libusb系列-003-Linux下libusb源码编译
    基于EasyCode定制Mybatisplus全自动单表实现:新增/批量新增/修改/批量删除/分页查询/ID查询
    Java日志框架log4j、logback、jul这么多?到底如何选择,他们之间有有什么关联?如何整合使用?
    Bash脚本debug攻略
    perl 用 XML::LibXML DOM 解析 Freeplane.mm文件,生成测试用例.csv文件
    GB/T 8924纤维增强塑料燃烧性能试验方法 氧指数法
    九洲
    Cadence Allegro PCB设计88问解析(十五) 之 Allegro中如何替换过孔类型
  • 原文地址:https://blog.csdn.net/lwj8819/article/details/128073549