Opengl光照测试

代码

#include "Model.h"
#include "shader_m.h"
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
//以上是放在同目录的头文件

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>  
#include <fstream>  
#include <sstream>  
#include <vector>  
#include <array>  


void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);//相机位置控制函数
void keyCallbackrollpitchyaw(GLFWwindow* window, int key, int scancode, int action, int mods);//物体的平移和旋转函数

const unsigned int SCR_WIDTH = 700;
const unsigned int SCR_HEIGHT = 452;


glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, 50.0f);//相机位置
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);



float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;

float deltaTime = 0.0f;
float lastFrame = 0.0f;

//旋转平移控制变量
float angleroll = 0.0f;
float anglepitch = 0.0f;
float angleyaw = 0.0f;
float translateX = 0.0f;
float translateY = 0.0f;
float translateZ = 0.0f;


glm::vec3 pointLightPosition = glm::vec3(0.0f, 8.0f, 0.0f);//点光源
glm::vec3 pointLightPosition1 = glm::vec3(0.0f, 6.0f, 0.0f); 
glm::vec3 pointLightPosition2 = glm::vec3(3.0f, 3.0f, 0.0f); 


glm::vec3 dirLightDirection = glm::vec3(0.0f, 0.0f, 0.0f);//平行光
glm::vec3 dirLightDirection1 = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 dirLightDirection2 = glm::vec3(0.0f, 0.0f, 0.0f);


// 聚光灯的相关参数
glm::vec3 spotLightPosition = glm::vec3(0.0f, 0.0f, 0.0f);  // 聚光灯位置
glm::vec3 spotLightDirection = glm::normalize(glm::vec3(-1.0f, -1.0f, -1.0f));  // 聚光灯方向(指向原点或其他目标)
//glm::vec3 spotLightDirection = glm::normalize(glm::vec3(spotLightPosition.x, spotLightPosition.y, spotLightPosition.z));  // 聚光灯方向(指向原点或其他目标)
float spotLightCutOff = glm::cos(glm::radians(12.5f));  // 聚光灯的内角度(单位:弧度)
float spotLightOuterCutOff = glm::cos(glm::radians(17.5f));  // 聚光灯的外角度(单位:弧度)

glm::vec3 spotLightPosition1 = glm::vec3(0.0f, 0.0f, 0.0f);  // 聚光灯位置
glm::vec3 spotLightDirection1 = glm::normalize(glm::vec3(-1.0f, -1.0f, -1.0f));  // 聚光灯方向(指向原点或其他目标)
//glm::vec3 spotLightDirection = glm::normalize(glm::vec3(spotLightPosition.x, spotLightPosition.y, spotLightPosition.z));  // 聚光灯方向(指向原点或其他目标)
float spotLightCutOff1 = glm::cos(glm::radians(12.5f));  // 聚光灯的内角度(单位:弧度)
float spotLightOuterCutOff1 = glm::cos(glm::radians(17.5f));  // 聚光灯的外角度(单位:弧度)

glm::vec3 spotLightPosition2 = glm::vec3(0.0f, 0.0f, 0.0f);  // 聚光灯位置
glm::vec3 spotLightDirection2 = glm::normalize(glm::vec3(-1.0f, -1.0f, -1.0f));  // 聚光灯方向(指向原点或其他目标)
//glm::vec3 spotLightDirection = glm::normalize(glm::vec3(spotLightPosition.x, spotLightPosition.y, spotLightPosition.z));  // 聚光灯方向(指向原点或其他目标)
float spotLightCutOff2 = glm::cos(glm::radians(12.5f));  // 聚光灯的内角度(单位:弧度)
float spotLightOuterCutOff2 = glm::cos(glm::radians(17.5f));  // 聚光灯的外角度(单位:弧度)


//物体颜色强度和光照强度
float ambientStrength = 0.0f;
float diffuseStrength = 0.7f;
float specularStrength = 0.3f;
float modelStrength = 1.0f;






//Imgui
void renderImGui() {
	ImGui::Begin("Light Control");
	ImGui::SliderFloat("Model Strength", &modelStrength, 0.0f, 1.0f);
	ImGui::SliderFloat("Ambient Strength", &ambientStrength, 0.0f, 1.0f);
	ImGui::SliderFloat("Diffuse Strength", &diffuseStrength, 0.0f, 1.0f);
	ImGui::SliderFloat("Specular Strength", &specularStrength, 0.0f, 1.0f);
	ImGui::End();
}





int main()
{
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "ModelDemo", NULL, NULL);
	if (window == NULL)
	{
		std::cout << "创建GLFW窗体失败" << std::endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
	//glfwSetScrollCallback(window, scroll_callback);



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


	//创建并编译shader
	Shader ImShader("LightTest.vs", "LightTest.fs");

	// 加载模型
	Model BallModel("sphere1.ply");//物体模型
	Model lightModel("spotlight3.ply"); // 用于表示点光源的模型
	Model lightModel1("spotlight3.ply"); // 用于表示点光源的模型
	Model lightModel2("spotlight3.ply"); // 用于表示点光源的模型

	Model lightModel3("spotlight3.ply"); // 用于表示聚光灯光源的模型
	Model lightModel4("spotlight3.ply"); // 用于表示聚光灯光源的模型
	Model lightModel5("spotlight3.ply"); // 用于表示聚光灯光源的模型

	//给物体赋予颜色的颜色图谱
	unsigned char ucColormap[256 * 3];
	FILE* file0 = fopen("colormap1.bin", "rb");
	std::memset(ucColormap, 0, 256 * 3);
	std::fread(ucColormap, 1, 256 * 3, file0);
	std::fclose(file0);
	int numColors = 256; 
	GLubyte* colormapData = (GLubyte*)malloc(numColors * 3 * sizeof(GLubyte));
	for (int i = 0; i < numColors * 3; i++) {
		colormapData[i] = ucColormap[i]; 
		 
	}
	for (int i = 0; i < 2; i++) {
		std::printf("testColorMap #%d: R=%u, G=%u, B=%u\n", i, colormapData[i * 3], colormapData[i * 3 + 1], colormapData[i * 3 + 2]);
	}

	GLuint colormapTexture;
	glGenTextures(1, &colormapTexture);
	glBindTexture(GL_TEXTURE_1D, colormapTexture);
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, numColors, 0, GL_RGB, GL_UNSIGNED_BYTE, colormapData);
	std::free(colormapData);
	glActiveTexture(GL_TEXTURE0);
	glUniform1i(glGetUniformLocation(ImShader.ID, "colormap"), 0);
	//颜色图谱结束


	glfwSetKeyCallback(window, keyCallbackrollpitchyaw);//控制模型旋转平移


	float left = -(float)SCR_WIDTH / 2.0f;//投影矩阵设置
	float right = (float)SCR_WIDTH / 2.0f;
	float bottom = -(float)SCR_HEIGHT / 2.0f;
	float top = (float)SCR_HEIGHT / 2.0f;
	float zNear = -1.0f;
	float zFar = 300.0f;
	

	IMGUI_CHECKVERSION();//IMGUI设置
	ImGui::CreateContext();
	ImGuiIO& io = ImGui::GetIO(); (void)io;
	ImGui::StyleColorsDark();
	ImGui_ImplGlfw_InitForOpenGL(window, true);
	ImGui_ImplOpenGL3_Init("#version 410");



	float modelStrength = 0.0f;//物体和光照强度初始化
	float ambientStrength = 0.7f;
	float diffStrength = 0.3f;
	float specularStrength = 1.0f;
	


	bool pointLightEnabled0 = false;
	bool pointLightEnabled1 = false;
	bool pointLightEnabled2 = false;
	bool spotLightEnabled = false;
	bool spotLightEnabled1 = false;
	bool spotLightEnabled2 = false;

	while (!glfwWindowShouldClose(window))
	{		
		float currentFrame = glfwGetTime();
		deltaTime = currentFrame - lastFrame;
		lastFrame = currentFrame;

		ImGui_ImplOpenGL3_NewFrame();
		ImGui_ImplGlfw_NewFrame();
		ImGui::NewFrame();		
		ImGui::Begin("Lighting Controls");
		ImGui::SliderFloat("Model Strength", &modelStrength, 0.0f, 1.0f);//调节范围
		ImGui::SliderFloat("Ambient Light Strength", &ambientStrength, 0.0f, 1.0f);
		ImGui::SliderFloat("Diffuse Light Strength", &diffStrength, 0.0f, 1.0f);
		ImGui::SliderFloat("Specular Light Strength", &specularStrength, 0.0f, 1.0f);

		ImGui::SliderFloat3("Camera Position", &cameraPosition[0], -10.0f, 10.0f);
		ImGui::SliderFloat3("Point Light Position", &pointLightPosition[0], -100.0f, 100.0f);
		ImGui::SliderFloat3("Point Light Position1", &pointLightPosition1[0], -100.0f, 100.0f);
		ImGui::SliderFloat3("Point Light Position2", &pointLightPosition2[0], -100.0f, 100.0f);

		ImGui::SliderFloat3("Spot Light Position", &spotLightPosition[0], -100.0f, 100.0f);//聚光灯
		//ImGui::SliderFloat3("Spot Light Direction", &spotLightDirection[0], -100.0f, 100.0f);
		ImGui::SliderFloat("Spot Light CutOff", &spotLightCutOff, cos(glm::radians(5.0f)), cos(glm::radians(45.0f)));  // 角度范围从 5 到 45 度
		ImGui::SliderFloat("Spot Light Outer CutOff", &spotLightOuterCutOff, cos(glm::radians(10.0f)), cos(glm::radians(45.0f)));  // 角度范围从 10 到 45 度

		ImGui::SliderFloat3("Spot Light Position1", &spotLightPosition1[0], -100.0f, 100.0f);//聚光灯
		//ImGui::SliderFloat3("Spot Light Direction", &spotLightDirection1[0], -100.0f, 100.0f);
		ImGui::SliderFloat("Spot Light CutOff1", &spotLightCutOff1, cos(glm::radians(5.0f)), cos(glm::radians(45.0f)));  // 角度范围从 5 到 45 度
		ImGui::SliderFloat("Spot Light Outer CutOff1", &spotLightOuterCutOff1, cos(glm::radians(10.0f)), cos(glm::radians(45.0f)));  // 角度范围从 10 到 45 度

		ImGui::SliderFloat3("Spot Light Position2", &spotLightPosition2[0], -100.0f, 100.0f);//聚光灯
		//ImGui::SliderFloat3("Spot Light Direction", &spotLightDirection[0], -100.0f, 100.0f);
		ImGui::SliderFloat("Spot Light CutOff2", &spotLightCutOff2, cos(glm::radians(5.0f)), cos(glm::radians(45.0f)));  // 角度范围从 5 到 45 度
		ImGui::SliderFloat("Spot Light Outer CutOff2", &spotLightOuterCutOff2, cos(glm::radians(10.0f)), cos(glm::radians(45.0f)));  // 角度范围从 10 到 45 度


		ImGui::SliderFloat3("Directional Light Direction", &dirLightDirection[0], -1.0f, 1.0f);
		ImGui::SliderFloat3("Directional Light Direction1", &dirLightDirection1[0], -1.0f, 1.0f);
		ImGui::SliderFloat3("Directional Light Direction2", &dirLightDirection2[0], -1.0f, 1.0f);
		ImGui::End();

		


		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		ImShader.use();
 
		
		glUniform1f(glGetUniformLocation(ImShader.ID, "modelStrength"), modelStrength);
		glUniform1f(glGetUniformLocation(ImShader.ID, "ambientStrength"), ambientStrength);
		glUniform1f(glGetUniformLocation(ImShader.ID, "diffStrength"), diffStrength);
		glUniform1f(glGetUniformLocation(ImShader.ID, "specularStrength"), specularStrength);


		glUniform3f(glGetUniformLocation(ImShader.ID, "pointLight.position"), pointLightPosition.x, pointLightPosition.y, pointLightPosition.z);
		glUniform3f(glGetUniformLocation(ImShader.ID, "pointLight1.position"),pointLightPosition1.x, pointLightPosition1.y, pointLightPosition1.z);
		glUniform3f(glGetUniformLocation(ImShader.ID, "pointLight2.position"),pointLightPosition2.x, pointLightPosition2.y, pointLightPosition2.z);


		
		glUniform3f(glGetUniformLocation(ImShader.ID, "spotLightPosition"), spotLightPosition.x, spotLightPosition.y, spotLightPosition.z);
		glUniform3f(glGetUniformLocation(ImShader.ID, "spotLightDirection"), spotLightDirection.x, spotLightDirection.y, spotLightDirection.z);
		glUniform1f(glGetUniformLocation(ImShader.ID, "spotLightCutOff"), spotLightCutOff);
		glUniform1f(glGetUniformLocation(ImShader.ID, "spotLightOuterCutOff"), spotLightOuterCutOff);

		glUniform3f(glGetUniformLocation(ImShader.ID, "spotLightPosition1"), spotLightPosition1.x, spotLightPosition1.y, spotLightPosition1.z);
		glUniform3f(glGetUniformLocation(ImShader.ID, "spotLightDirection1"), spotLightDirection1.x, spotLightDirection1.y, spotLightDirection1.z);
		glUniform1f(glGetUniformLocation(ImShader.ID, "spotLightCutOff1"), spotLightCutOff1);
		glUniform1f(glGetUniformLocation(ImShader.ID, "spotLightOuterCutOff1"), spotLightOuterCutOff1);

		glUniform3f(glGetUniformLocation(ImShader.ID, "spotLightPosition2"), spotLightPosition2.x, spotLightPosition2.y, spotLightPosition2.z);
		glUniform3f(glGetUniformLocation(ImShader.ID, "spotLightDirection2"), spotLightDirection2.x, spotLightDirection2.y, spotLightDirection2.z);
		glUniform1f(glGetUniformLocation(ImShader.ID, "spotLightCutOff2"), spotLightCutOff2);
		glUniform1f(glGetUniformLocation(ImShader.ID, "spotLightOuterCutOff2"), spotLightOuterCutOff2);

		glUniform3f(glGetUniformLocation(ImShader.ID, "directionalLight.position"), dirLightDirection.x, dirLightDirection.y, dirLightDirection.z);
		glUniform3f(glGetUniformLocation(ImShader.ID, "directionalLight1.position"), dirLightDirection1.x, dirLightDirection1.y, dirLightDirection1.z);
		glUniform3f(glGetUniformLocation(ImShader.ID, "directionalLight2.position"), dirLightDirection2.x, dirLightDirection2.y, dirLightDirection2.z);
		
		
		
		glUniform3f(glGetUniformLocation(ImShader.ID, "lightColor"), 1.0f, 1.0f, 1.0f);
		int viewPosLocation = glGetUniformLocation(ImShader.ID, "viewPos");
		glUniform3fv(viewPosLocation, 1, &cameraPosition[0]);

		processInput(window);//调整摄像机位置


		glm::mat4 projectiono = glm::ortho(left, right, bottom, top, zNear, zFar);
		glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 1000.0f);

		glm::mat4 view = glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);
		//设置着色器中用到的矩阵
		ImShader.setMat4("projection", projection);
		ImShader.setMat4("view", view);


		if (ImGui::Begin("Light Controls")) {
			if (ImGui::Button("Toggle Point Light 0")) {
				pointLightEnabled0 = !pointLightEnabled0;
			}
			if (ImGui::Button("Toggle Point Light 1")) {
				pointLightEnabled1 = !pointLightEnabled1;
			}
			if (ImGui::Button("Toggle Point Light 2")) {
				pointLightEnabled2 = !pointLightEnabled2;
			}
			if (ImGui::Button("Toggle Spot Light")) {
				spotLightEnabled = !spotLightEnabled;
			}
			if (ImGui::Button("Toggle Spot Light1")) {
				spotLightEnabled1 = !spotLightEnabled1;
			}
			if (ImGui::Button("Toggle Spot Light2")) {
				spotLightEnabled2 = !spotLightEnabled2;
			}
			ImGui::End();
		}

		// 模型变换矩阵的初始化
		glm::mat4 model = glm::mat4(1.0f);
		model = glm::scale(model, glm::vec3(6.0f, 6.0f, 6.0f));//放大尺寸
		//model = glm::rotate(model, glm::radians(270.0f), glm::vec3(0.0f, 0.0f, 1.0f));//旋转
		//model = glm::translate(model, glm::vec3(10.0f, -10.0f, -50.0f));//物体模型平移

		model = glm::rotate(model, glm::radians(anglepitch), glm::vec3(0.0f, 1.0f, 0.0f)); // y轴旋转
		model = glm::rotate(model, glm::radians(angleyaw), glm::vec3(0.0f, 0.0f, 1.0f)); // Z
		model = glm::rotate(model, glm::radians(angleroll), glm::vec3(1.0f, 0.0f, 0.0f)); // x

		model = glm::translate(model, glm::vec3(translateX, 0.0f, 0.0f));//平移
		model = glm::translate(model, glm::vec3(0.0f, translateY, 0.0f));
		model = glm::translate(model, glm::vec3(0.0f, 0.0f, translateZ));

		ImShader.setMat4("model", model);//设置模型矩阵model的函数,
	
		// 绘制模型物体
		BallModel.Draw(ImShader);


		// 计算从光源位置指向原点的方向
		glm::vec3 target = glm::vec3(0.0f, 0.0f, 0.0f);  // 坐标原点
		// 设置点光源0的模型变换矩阵
		if (pointLightEnabled0) 
		{
			// 设置点光源的模型变换矩阵
			glm::mat4 lightModelMatrix = glm::mat4(1.0f);
			lightModelMatrix = glm::translate(lightModelMatrix, pointLightPosition);  // 将光源放置在 pointLightPosition

			glm::vec3 direction = glm::normalize(target - pointLightPosition);
			// 使用 glm::lookAt 函数创建一个朝向矩阵
			glm::mat4 orientationMatrix = -glm::lookAt(pointLightPosition, target, glm::vec3(0.0f, 1.0f, 0.0f));
			// 将朝向矩阵乘到模型矩阵上,确保它指向原点
			lightModelMatrix *= lightModelMatrix * glm::mat4(glm::mat3(orientationMatrix));  // 去除位移分量,只保留旋转部分
			lightModelMatrix = glm::scale(lightModelMatrix, glm::vec3(0.3f)); // 将光源球体缩小至合适大小
			// 设置着色器并传递点光源的位置
			ImShader.use();
			ImShader.setMat4("model", lightModelMatrix);  // 应用光源变换
			// 绘制点光源
			lightModel.Draw(ImShader);
		}

		// 设置点光源1的模型变换矩阵
		if (pointLightEnabled1) 
		{
			glm::mat4 lightModelMatrix1 = glm::mat4(1.0f);
			lightModelMatrix1 = glm::translate(lightModelMatrix1, pointLightPosition1);  // 将光源放置在 pointLightPosition
			// 计算从光源位置指向原点的方向
			glm::vec3 direction1 = glm::normalize(target - pointLightPosition1);
			// 使用 glm::lookAt 函数创建一个朝向矩阵
			glm::mat4 orientationMatrix1 = -glm::lookAt(pointLightPosition1, target, glm::vec3(0.0f, 1.0f, 0.0f));
			// 将朝向矩阵乘到模型矩阵上,确保它指向原点
			lightModelMatrix1 *= lightModelMatrix1 * glm::mat4(glm::mat3(orientationMatrix1));  // 去除位移分量,只保留旋转部分

			lightModelMatrix1 = glm::scale(lightModelMatrix1, glm::vec3(0.3f)); // 将光源球体缩小至合适大小
			// 设置着色器并传递点光源的位置
			ImShader.use();
			ImShader.setMat4("model", lightModelMatrix1);  // 应用光源变换
			// 绘制点光源
			lightModel1.Draw(ImShader);
		}



		// 设置点光源2的模型变换矩阵
		if (pointLightEnabled2) 
		{
			glm::mat4 lightModelMatrix2 = glm::mat4(1.0f);
			lightModelMatrix2 = glm::translate(lightModelMatrix2, pointLightPosition2);  // 将光源放置在 pointLightPosition

			// 计算从光源位置指向原点的方向
			glm::vec3 direction2 = glm::normalize(target - pointLightPosition2);
			// 使用 glm::lookAt 函数创建一个朝向矩阵
			glm::mat4 orientationMatrix2 = -glm::lookAt(pointLightPosition2, target, glm::vec3(0.0f, 1.0f, 0.0f));
			// 将朝向矩阵乘到模型矩阵上,确保它指向原点
			lightModelMatrix2 *= lightModelMatrix2 * glm::mat4(glm::mat3(orientationMatrix2));  // 去除位移分量,只保留旋转部分
			lightModelMatrix2 = glm::scale(lightModelMatrix2, glm::vec3(0.3f)); // 将光源球体缩小至合适大小
			// 设置着色器并传递点光源的位置
			ImShader.use();
			ImShader.setMat4("model", lightModelMatrix2);  // 应用光源变换
			// 绘制点光源
			lightModel2.Draw(ImShader);
		}

		// 计算从聚光灯光源位置指向原点的方向
		if (spotLightEnabled) 
		{
			glm::mat4 lightModelMatrix3 = glm::mat4(1.0f);
			lightModelMatrix3 = glm::translate(lightModelMatrix3, spotLightPosition);  // 将光源放置在 pointLightPosition
			// 计算从光源位置指向原点的方向
			glm::vec3 direction3 = glm::normalize(spotLightPosition - target);
			// 使用 glm::lookAt 函数创建一个朝向矩阵
			glm::mat4 orientationMatrix3 = glm::lookAt(spotLightPosition, target, glm::vec3(0.0f, 1.0f, 0.0f));
			// 将朝向矩阵乘到模型矩阵上,确保它指向原点
			lightModelMatrix3 *= lightModelMatrix3 * glm::mat4(glm::mat3(orientationMatrix3));  // 去除位移分量,只保留旋转部分
			lightModelMatrix3 = glm::scale(lightModelMatrix3, glm::vec3(0.3f)); // 将光源球体缩小至合适大小
			// 设置着色器并传递点光源的位置
			ImShader.use();
			ImShader.setMat4("model", lightModelMatrix3);  // 应用光源变换
			// 绘制聚光灯光源
			lightModel3.Draw(ImShader);
		}

		// 计算从聚光灯光源位置指向原点的方向
		if (spotLightEnabled1)
		{
			glm::mat4 lightModelMatrix4 = glm::mat4(1.0f);
			lightModelMatrix4 = glm::translate(lightModelMatrix4, spotLightPosition1);  // 将光源放置在 pointLightPosition
			// 计算从光源位置指向原点的方向
			glm::vec3 direction4 = glm::normalize(spotLightPosition1 - target);
			// 使用 glm::lookAt 函数创建一个朝向矩阵
			glm::mat4 orientationMatrix4 = glm::lookAt(spotLightPosition1, target, glm::vec3(0.0f, 1.0f, 0.0f));
			// 将朝向矩阵乘到模型矩阵上,确保它指向原点
			lightModelMatrix4 *= lightModelMatrix4 * glm::mat4(glm::mat3(orientationMatrix4));  // 去除位移分量,只保留旋转部分
			lightModelMatrix4 = glm::scale(lightModelMatrix4, glm::vec3(0.3f)); // 将光源球体缩小至合适大小
			// 设置着色器并传递点光源的位置
			ImShader.use();
			ImShader.setMat4("model", lightModelMatrix4);  // 应用光源变换
			// 绘制聚光灯光源
			lightModel4.Draw(ImShader);
		}


		// 计算从聚光灯光源位置指向原点的方向
		if (spotLightEnabled2)
		{
			glm::mat4 lightModelMatrix5 = glm::mat4(1.0f);
			lightModelMatrix5 = glm::translate(lightModelMatrix5, spotLightPosition2);  // 将光源放置在 pointLightPosition
			// 计算从光源位置指向原点的方向
			glm::vec3 direction5 = glm::normalize(spotLightPosition2 - target);
			// 使用 glm::lookAt 函数创建一个朝向矩阵
			glm::mat4 orientationMatrix5 = glm::lookAt(spotLightPosition2, target, glm::vec3(0.0f, 1.0f, 0.0f));
			// 将朝向矩阵乘到模型矩阵上,确保它指向原点
			lightModelMatrix5 *= lightModelMatrix5 * glm::mat4(glm::mat3(orientationMatrix5));  // 去除位移分量,只保留旋转部分
			lightModelMatrix5 = glm::scale(lightModelMatrix5, glm::vec3(0.3f)); // 将光源球体缩小至合适大小
			// 设置着色器并传递点光源的位置
			ImShader.use();
			ImShader.setMat4("model", lightModelMatrix5);  // 应用光源变换
			// 绘制聚光灯光源
			lightModel5.Draw(ImShader);
		}

		ImGui::Render();
		ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
		glfwSwapBuffers(window);
		glfwPollEvents();
	}


	glfwTerminate();
	return 0;
}

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

	float cameraSpeed = static_cast<float>(2.5 * deltaTime);
	if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
		cameraPosition += cameraSpeed * cameraFront;
	if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
		cameraPosition -= cameraSpeed * cameraFront;
	if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
		cameraPosition -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
	if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
		cameraPosition += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}
void keyCallbackrollpitchyaw(GLFWwindow* window, int key, int scancode, int action, int mods) {
	if (key == GLFW_KEY_Q && action == GLFW_PRESS) { // 按键减少旋转角度
		anglepitch -= 2.0f;
	}
	else if (key == GLFW_KEY_W && action == GLFW_PRESS) { // 按键增加旋转角度
		anglepitch += 2.0f;
	}
	if (key == GLFW_KEY_A && action == GLFW_PRESS) { // 按键减少旋转角度
		angleroll -= 2.0f;
	}
	else if (key == GLFW_KEY_S && action == GLFW_PRESS) { // 按键增加旋转角度
		angleroll += 2.0f;
	}
	if (key == GLFW_KEY_Z && action == GLFW_PRESS) { // 按键减少旋转角度
		angleyaw -= 2.0f;
	}
	else if (key == GLFW_KEY_X && action == GLFW_PRESS) { // 按键增加旋转角度
		angleyaw += 2.0f;
	}
	//平移
	if (key == GLFW_KEY_U && action == GLFW_PRESS) {
		translateX -= 5.0f;
	}
	else if (key == GLFW_KEY_I && action == GLFW_PRESS) { 
		translateX += 5.0f;
	}
	if (key == GLFW_KEY_J && action == GLFW_PRESS) { 
		translateY -= 5.0f;
	}
	else if (key == GLFW_KEY_K && action == GLFW_PRESS) { 
		translateY += 5.0f;
	}
	if (key == GLFW_KEY_N && action == GLFW_PRESS) { 
		translateZ -= 5.0f;
	}
	else if (key == GLFW_KEY_M && action == GLFW_PRESS) { 
		translateZ += 5.0f;
	}
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
	glViewport(0, 0, width, height);
}

然后是配置一下glfw+glad环境

_USE_MATH_DEFINES;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)

assimp.lib

OpenMeshCored.lib

OpenMeshToolsd.lib

OpenMeshCore.lib

OpenMeshTools.lib

legacy_stdio_definitions.lib

glfw3.lib

opengl32.lib

相关推荐
tinker在coding22 分钟前
Coding Caprice - Linked-List 1
算法·leetcode
唐诺5 小时前
几种广泛使用的 C++ 编译器
c++·编译器
XH华5 小时前
初识C语言之二维数组(下)
c语言·算法
南宫生5 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
不想当程序猿_5 小时前
【蓝桥杯每日一题】求和——前缀和
算法·前缀和·蓝桥杯
落魄君子5 小时前
GA-BP分类-遗传算法(Genetic Algorithm)和反向传播算法(Backpropagation)
算法·分类·数据挖掘
冷眼看人间恩怨6 小时前
【Qt笔记】QDockWidget控件详解
c++·笔记·qt·qdockwidget
菜鸡中的奋斗鸡→挣扎鸡6 小时前
滑动窗口 + 算法复习
数据结构·算法
红龙创客6 小时前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
Lenyiin6 小时前
第146场双周赛:统计符合条件长度为3的子数组数目、统计异或值为给定值的路径数目、判断网格图能否被切割成块、唯一中间众数子序列 Ⅰ
c++·算法·leetcode·周赛·lenyiin