OpenGL之——多光源

 竖直向上的平行光,四个点光源,还有与摄像头方向一致的手电筒光源

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号纹理单元

	
	//物体材质属性
	//ShaderColor.setVec3("material.specular", glm::vec3(0.0f, 0.0f, 0.0f));//木头材质不反光
	ShaderColor.setFloat("material.shininess", 64.0f);

	ShaderColor.setVec3("dirLight.direction", glm::vec3(0.0f, 0.1f, 0.0f));
	//光分量
	ShaderColor.setVec3("dirLight.ambient", glm::vec3(0.01f, 0.01f, 0.01f));
	ShaderColor.setVec3("dirLight.diffuse", glm::vec3(0.2f, 0.2f, 0.2f));
	ShaderColor.setVec3("dirLight.specular", glm::vec3(1.0f, 1.0f, 1.0f));
	//点光源位置数组
	glm::vec3 pointLightPositions[] = {
		glm::vec3(0.7f,  0.2f,  2.0f),
		glm::vec3(2.3f, -3.3f, -4.0f),
		glm::vec3(-4.0f,  2.0f, 6.0f),
		glm::vec3(0.0f,  0.0f, -3.0f)
	};
	ShaderColor.setVec3("pointLights[0].ambient", glm::vec3(0.2f, 0.2f, 0.2f));
	ShaderColor.setVec3("pointLights[0].diffuse", glm::vec3(1.0f, 1.0f, 1.0f));
	ShaderColor.setVec3("pointLights[0].specular", glm::vec3(1.0f, 1.0f, 1.0f));
	ShaderColor.setVec3("pointLights[0].position", glm::vec3(0.7f, 0.2f, 2.0f));
	ShaderColor.setVec3("pointLights[1].ambient", glm::vec3(0.1f, 0.1f, 0.1f));
	ShaderColor.setVec3("pointLights[1].diffuse", glm::vec3(0.3f, 0.3f, 0.3f));
	ShaderColor.setVec3("pointLights[1].specular", glm::vec3(0.6f, 0.6f, 0.6f));
	ShaderColor.setVec3("pointLights[1].position", glm::vec3(2.3f, -3.3f, -4.0f));
	ShaderColor.setVec3("pointLights[2].ambient", glm::vec3(0.1f, 0.1f, 0.1f));
	ShaderColor.setVec3("pointLights[2].diffuse", glm::vec3(0.1f, 0.1f, 0.1f));
	ShaderColor.setVec3("pointLights[2].specular", glm::vec3(0.6f, 0.6f, 0.6f));
	ShaderColor.setVec3("pointLights[2].position", glm::vec3(-4.0f, 2.0f, 6.0f));
	ShaderColor.setVec3("pointLights[3].ambient", glm::vec3(0.1f, 0.1f, 0.1f));
	ShaderColor.setVec3("pointLights[3].diffuse", glm::vec3(0.1f, 0.1f, 0.1f));
	ShaderColor.setVec3("pointLights[3].specular", glm::vec3(0.6f, 0.6f, 0.6f));
	ShaderColor.setVec3("pointLights[3].position", glm::vec3(0.0f, 0.0f, -3.0f));

	ShaderColor.setVec3("spotLight.ambient", glm::vec3(0.2f, 0.2f, 0.2f));
	ShaderColor.setVec3("spotLight.diffuse", glm::vec3(0.8f, 0.8f, 0.8f));
	ShaderColor.setVec3("spotLight.specular", glm::vec3(1.0f, 1.0f, 1.0f));

	
	//点光源的衰减系数设置
	ShaderColor.setFloat("pointLights[0].constant", 1.0f);
	ShaderColor.setFloat("pointLights[0].linear", 0.09f);
	ShaderColor.setFloat("pointLights[0].quadratic", 0.032f);
	ShaderColor.setFloat("pointLights[1].constant", 1.0f);
	ShaderColor.setFloat("pointLights[1].linear", 0.09f);
	ShaderColor.setFloat("pointLights[1].quadratic", 0.032f);
	ShaderColor.setFloat("pointLights[2].constant", 1.0f);
	ShaderColor.setFloat("pointLights[2].linear", 0.09f);
	ShaderColor.setFloat("pointLights[2].quadratic", 0.032f);
	ShaderColor.setFloat("pointLights[3].constant", 1.0f);
	ShaderColor.setFloat("pointLights[3].linear", 0.09f);
	ShaderColor.setFloat("pointLights[3].quadratic", 0.032f);

	ShaderColor.setFloat("spotLight.constant", 1.0f);
	ShaderColor.setFloat("spotLight.linear", 0.09f);
	ShaderColor.setFloat("spotLight.quadratic", 0.032f);
	
	while (!glfwWindowShouldClose(window)) {

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

		processInput(window);

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

		ShaderColor.use();//设置手电筒光源属性
		ShaderColor.setVec3("spotLight.position", camera.Position);//注意这个光源的位置与摄像头一致
		ShaderColor.setVec3("spotLight.direction", camera.Front);//注意这个光源的方向与摄像头一致
		ShaderColor.setFloat("spotLight.cutOff", glm::cos(glm::radians(12.5)));//设置为12.5°的弧度的余弦值来表示内光切角度
		ShaderColor.setFloat("spotLight.outerCutOff", glm::cos(glm::radians(17.5)));
		
		//变换矩阵
		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);
		}

		for (GLuint j = 0; j < 4; j++) {
			glm::mat4 Lmodel = glm::mat4(1.0f);
			Lmodel = glm::translate(Lmodel, pointLightPositions[j]);
			Lmodel = glm::scale(Lmodel, glm::vec3(0.2f));//将光源移到那个位置并缩小一点

			//画光源,位置,观察,透视矩阵赋值
			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);
}

shader.frag

#version 330 core

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

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

struct DirLight{
	vec3 direction;
	
	vec3 ambient;
	vec3 diffuse;
	vec3 specular;
};
uniform DirLight dirLight;
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);

struct PointLight{
	vec3 position;

	
	float constant;
	float linear;
	float quadratic;
	
	vec3 ambient;
	vec3 diffuse;
	vec3 specular;
};
#define NR_POINT_LIGHTS 4
uniform PointLight pointLights[NR_POINT_LIGHTS];
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);

struct SpotLight{
	vec3 position;
	vec3 direction;
	float cutOff;
	float outerCutOff;
	
	vec3 ambient;
	vec3 diffuse;
	vec3 specular;

	float constant;
	float linear;
	float quadratic;
};
uniform SpotLight spotLight;
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 FragPos, vec3 viewDir);

uniform vec3 viewPos;

out vec4 FragColor;

void main()
{	

	//属性
	vec3 normal = normalize(Normal);
	vec3 viewDir = normalize(viewPos - FragPos);
	
	//第一阶段:定向光照
	vec3 result = CalcDirLight(dirLight, normal, viewDir);
	//第二阶段:点光源
	for(int i=0;i

OpenGL之——多光源_第2张图片

你可能感兴趣的:(OpenGL)