OpenGL之——光照贴图(纹理贴图)

 上一节中的那个材质系统是肯定不够的,它只是一个最简单的模型,所以我们需要拓展之前的系统,引入漫反射镜面光贴图(Map)。这允许我们对物体的漫反射分量(以及间接地对环境光分量,它们几乎总是一样的)和镜面光分量有着更精确的控制。

OpenGL之——光照贴图(纹理贴图)_第1张图片

 主要代码:


#include 
#include 
#include 
#define STB_IMAGE_IMPLEMENTATION
#include 
#include "Shader.h"
#include "Camera.h"

#include 
#include 
#include 

#pragma comment(lib,"glfw3.lib")

using namespace std;

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow* window);

const GLuint Width = 800, Height = 600;

//摄像机
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));//初始化摄像机位置
float lastX = Width / 2.0f;
float lastY = Height / 2.0f;
bool firstMouse = true;

//计时器
GLfloat deltaTime = 0.0f;
GLfloat lastFrame = 0.0f;


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(Width, Height, "LearnOpenGL", NULL, NULL);
	if (window == NULL) {
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);//绑定上下文到此窗口
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);//注册回调函数
	glfwSetCursorPosCallback(window, mouse_callback);//注册回调函数
	glfwSetScrollCallback(window, scroll_callback);//注册回调函数

	glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);//隐藏光标

	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
		std::cout << "Failed to initialize GLAD" << std::endl;
		return -1;
	}


	//开启深度测试
	glEnable(GL_DEPTH_TEST);
	
	Shader ShaderColor("shader.vs","shader.frag");//创建并编译着色器

	Shader LightColor("light.vs", "light.frag");
	
	//顶点数组
	GLfloat vertices[] = {
		// 第一个三角形     texture       normal
		0.5f, -0.5f, -0.5f,  1.0f, 1.0f, 0.0, -1.0, 0.0,  // 右上角
		0.5f, -0.5f,  0.5f, 1.0f, 0.0f, 0.0, -1.0, 0.0,  // 右下角
		-0.5f,-0.5f, -0.5f, 0.0f, 1.0f, 0.0, -1.0, 0.0,  // 左上角
		// 第二个三角形
		0.5f, -0.5f,  0.5f, 1.0f, 0.0f, 0.0, -1.0, 0.0,  // 右下角
		-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0, -1.0, 0.0, // 左下角
		-0.5f,-0.5f, -0.5f, 0.0f, 1.0f, 0.0, -1.0, 0.0,  // 左上角
		//第三个三角形
		-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0, 0.5, 0.7, // 左下角
		  0.0f,  0.2f,  0.0f, 0.5f, 1.0f, 0.0, 0.5, 0.7, // 顶点
		0.5f, -0.5f,  0.5f, 1.0f, 0.0f, 0.0, 0.5, 0.7,  // 右下角
		//第四个三角形
		0.5f, -0.5f,  0.5f, 0.0f, 0.0f, 0.7, 0.5, 0.0,  // 右下角
		0.0f,  0.2f,  0.0f, 0.5f, 1.0f, 0.7, 0.5, 0.0, // 顶点
		0.5f, -0.5f, -0.5f,  1.0f, 0.0f, 0.7, 0.5, 0.0,  // 右上角
		//第五个三角形
		0.5f, -0.5f, -0.5f,  0.0f, 0.0f, 0.0, 0.5, -0.7,  // 右上角
		0.0f,  0.2f,  0.0f, 0.5f, 1.0f, 0.0, 0.5, -0.7, // 顶点
		-0.5f,-0.5f, -0.5f, 1.0f, 0.0f, 0.0, 0.5, -0.7, // 左上角
		//第六个三角形
		-0.5f,-0.5f, -0.5f, 0.0f, 0.0f, -0.7, 0.5, 0.0,  // 左上角
		0.0f,  0.2f,  0.0f, 0.5f, 1.0f, -0.7, 0.5, 0.0, // 顶点
		-0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - 0.7, 0.5, 0.0 // 左下角
	}; 
	//模型位置数组	
	glm::vec3 cubePositions[] = {
		  glm::vec3(0.0f,  0.0f,  0.0f),
		  glm::vec3(1.0f,  1.0f, 0.0f),
		  glm::vec3(-1.5f, -2.2f, -2.5f),
		  glm::vec3(-3.8f, -2.0f, -3.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)
	};
	
	GLuint VAO, VBO;
	glGenVertexArrays(1, &VAO);//创建顶点数组对象
	glGenBuffers(1, &VBO);//创建顶点缓冲对象


	glBindBuffer(GL_ARRAY_BUFFER, VBO);//将顶点缓冲绑定到顶点缓冲对象
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);//缓冲区三角形顶点数据

	glBindVertexArray(VAO);//将顶点数组绑定到顶点数组对象
	//设置顶点属性指针
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
	glEnableVertexAttribArray(0);//顶点坐标
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 );
	glEnableVertexAttribArray(1);//纹理坐标
	glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(5 * sizeof(GLfloat)));
	glEnableVertexAttribArray(2);//法向量
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);

	//创建光源顶点数组
	GLuint lightVAO;
	glGenVertexArrays(1, &lightVAO);
	glBindVertexArray(lightVAO);
	//只需要绑定VBO不用再次设置VBO数据,因为金字塔的VBO数据已经包含了正确的金字塔数据
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	//设置灯金字塔的顶点属性(对我们的灯来说仅仅只有位置数据)
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)0);
	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);

	//纹理
	GLuint diffuseMap, specularMap;
	glGenTextures(1, &diffuseMap);
	glGenTextures(1, &specularMap);
	//加载并生成纹理0
	int width, height, nrChannels;
	stbi_set_flip_vertically_on_load(true);//在图像加载时帮助我们翻转y轴,只需要在加载任何图像前加入以下语句即可
	unsigned char *image;
	image = stbi_load("box2.png", &width, &height, &nrChannels, 0);
	glBindTexture(GL_TEXTURE_2D, diffuseMap);
	if (image) {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	else {
		std::cout << "Failed to load texture1" << std::endl;
	}
	stbi_image_free(image);
	//为当前绑定的对象设置环绕,过滤方式
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	//加载并生成纹理1
	image = stbi_load("box_specular.png", &width, &height, &nrChannels, 0);
	glBindTexture(GL_TEXTURE_2D, specularMap);
	if (image) {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	else {
		std::cout << "Failed to load texture2" << std::endl;
	}
	stbi_image_free(image);
	//为当前绑定的对象设置环绕,过滤方式
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glBindTexture(GL_TEXTURE_2D, 0);
	
	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	ShaderColor.use();
	ShaderColor.setInt("material.diffuse", 0);//设置采样器material.diffuse属于0号纹理单元
	ShaderColor.setInt("material.specular", 1);//设置采样器material.specular属于1号纹理单元

	//设置光源
	glm::vec3 lightPos(0.0f, -1.0f, -2.0f);//设置光源位置
	glm::mat4 Lmodel = glm::mat4(1.0f);
	Lmodel = glm::translate(Lmodel, lightPos);
	Lmodel = glm::scale(Lmodel, glm::vec3(0.2f));//将光源移到那个位置并缩小一点
	//物体材质属性
	//ShaderColor.setVec3("material.specular", glm::vec3(0.0f, 0.0f, 0.0f));//木头材质不反光
	ShaderColor.setFloat("material.shininess", 64.0f);
	//光分量
	ShaderColor.setVec3("light.ambient", glm::vec3(0.2f, 0.2f, 0.2f));
	ShaderColor.setVec3("light.diffuse", glm::vec3(0.5f, 0.5f, 0.5f));
	ShaderColor.setVec3("light.specular", glm::vec3(1.0f, 1.0f, 1.0f));

	ShaderColor.setVec3("light.position", glm::vec3(0.0f, -1.0f, -2.0f));

	
	while (!glfwWindowShouldClose(window)) {

		GLfloat currentFrame = glfwGetTime();
		deltaTime = currentFrame - lastFrame;//当前帧与上一帧的时间差
		lastFrame = currentFrame;//上一帧的时间

		processInput(window);

		glClearColor(0.01f, 0.01f, 0.01f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		ShaderColor.use();

		//变换矩阵
		glm::mat4 model = glm::mat4(1.0f);
		glm::mat4 view = glm::mat4(1.0f);
		glm::mat4 projection = glm::mat4(1.0f);
		//GLuint modelLoc = glGetUniformLocation(ShaderColor.ID, "model");
		//GLuint viewLoc = glGetUniformLocation(ShaderColor.ID, "view");
		GLuint projectionLoc = glGetUniformLocation(ShaderColor.ID, "projection");
		//model = glm::rotate(model, (float)-glfwGetTime()*60.0f, glm::vec3(1.0f, 0.0f, 0.0f));
		//view = glm::translate(view, glm::vec3(0.0f, 0.0f, -1.5f));
		projection = glm::perspective(glm::radians(camera.Zoom), (GLfloat)Width / Height, 0.1f, 100.0f);

		view = glm::lookAt(camera.Position, camera.Position + camera.Front, camera.Up);

		//glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
		ShaderColor.setMat4("view", view);
		//glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
		ShaderColor.setMat4("projection", projection);
		ShaderColor.setVec3("viewPos", camera.Position);
		

		for (GLuint i = 0; i < 10; i++) {
			model = glm::translate(model, cubePositions[i]);
			model = glm::rotate(model, (float)i*10.0f, glm::vec3(0.0, 1.0, 0.0));//旋转
			//glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
			ShaderColor.setMat4("model", model);


			glActiveTexture(GL_TEXTURE0);//激活0号纹理
			glBindTexture(GL_TEXTURE_2D, diffuseMap);//绑定0号纹理为diffuseMap

			glActiveTexture(GL_TEXTURE1);//激活1号纹理
			glBindTexture(GL_TEXTURE_2D, specularMap);//绑定1号纹理为specularMap


			glBindVertexArray(VAO);//绑定顶点数组
			glDrawArrays(GL_TRIANGLES, 0, 18);
		}


		//画光源,位置,观察,透视矩阵赋值
		LightColor.use();//必须在这个着色器下设置相应的uniform值
		LightColor.setMat4("Lmodel", Lmodel);
		LightColor.setMat4("view", view);
		LightColor.setMat4("projection", projection);
		glBindVertexArray(lightVAO);
		glDrawArrays(GL_TRIANGLES, 0, 18);
		glBindVertexArray(0);//解绑顶点数组

		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	glDeleteBuffers(1, &VBO);
	glDeleteVertexArrays(1, &VAO);
	glDeleteVertexArrays(1, &lightVAO);
	glfwTerminate();
	return 0;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
	glViewport(0, 0, width, height);
}

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


	if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
		camera.ProcessKeyboard(FORWARD, deltaTime);
	if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
		camera.ProcessKeyboard(BACKWARD, deltaTime);
	if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
		camera.ProcessKeyboard(LEFT, deltaTime);
	if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
		camera.ProcessKeyboard(RIGHT, deltaTime);
}

void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
	if (firstMouse)
	{
		lastX = xpos;
		lastY = ypos;
		firstMouse = false;
	}

	float xoffset = xpos - lastX;
	float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
	lastX = xpos;
	lastY = ypos;

	camera.ProcessMouseMovement(xoffset, yoffset);
}

void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
	camera.ProcessMouseScroll(yoffset);
}

片段着色器:

#version 330 core

in vec2 TexCoods;
in vec3 Normal;
in vec3 FragPos;

struct Material{
	sampler2D diffuse;
	sampler2D specular;
	float shininess;
};
uniform Material material;

struct Light{
	vec3 position;
	
	vec3 ambient;
	vec3 diffuse;
	vec3 specular;
};
uniform Light light;

uniform vec3 viewPos;

out vec4 FragColor;

void main()
{	
	//环境光
	vec3 ambient = light.ambient * texture(material.diffuse, TexCoods).rgb;
	
	//漫反射
	vec3 normal = normalize(Normal);
	vec3 lightDir = normalize(light.position - FragPos);
	float diff = max(dot(normal,lightDir),0.0);//漫反射分量
	vec3 diffuse = light.diffuse * diff * texture(material.diffuse, TexCoods).rgb;
	
	
	//镜面光
	vec3 viewDir = normalize(viewPos - FragPos);
	vec3 reflectDir = reflect(-lightDir, normal);
	float spec = pow(max(dot(viewDir, reflectDir), 0.0),material.shininess);
	vec3 specular = light.specular * spec * texture(material.specular, TexCoods).rgb;//只有镜面贴图不透明的地方才有镜面高光
	
	FragColor = vec4(ambient + diffuse + specular, 1.0);
}

 

OpenGL之——光照贴图(纹理贴图)_第2张图片

  

 

你可能感兴趣的:(OpenGL)