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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/916860.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

传奇996_19——龙岭总结

功能&#xff1a; 切割 切割属性&#xff1a; 即人物属性&#xff0c;可以设置临时属性或者永久属性&#xff0c;龙岭使用的是临时属性&#xff0c;所谓临时就是存在有效期&#xff0c;龙岭设置的有效期是123456789秒&#xff0c;即1428.89802天。 龙岭写法&#xff08;倒叙…

亲测有效:Maven3.8.1使用Tomcat8插件启动项目

我本地maven的settings.xml文件中的配置&#xff1a; <mirror><id>aliyunmaven</id><mirrorOf>central</mirrorOf><name>阿里云公共仓库</name><url>https://maven.aliyun.com/repository/public</url> </mirror>…

关于 MSVCP110.dll 缺失的解决方案

背景&#xff1a;之前使用 PR&#xff08;Adobe Premiere&#xff09; 从来没有遇到过这样的问题。今天重装系统后&#xff08;window 10&#xff09;&#xff0c;想要重新安装以前的软件时&#xff0c;遇到了以下 DLL 文件缺失的错误。 解决方案&#xff1a; 可以到微软官网的…

贝叶斯网络——基于概率的图模型(详解)

贝叶斯网络&#xff08;Bayesian Network&#xff0c;简称BN&#xff09;是一种基于概率图模型的表示方法&#xff0c;用于表示变量之间的依赖关系&#xff0c;并通过条件概率推断变量间的关系。它通过有向无环图&#xff08;DAG&#xff09;来描述变量之间的依赖关系&#xff…

嵌入式硬件电子电路设计(五)MOS管详解(NMOS、PMOS、三极管跟mos管的区别)

引言&#xff1a;在我们的日常使用中&#xff0c;MOS就是个纯粹的电子开关&#xff0c;虽然MOS管也有放大作用&#xff0c;但是几乎用不到&#xff0c;只用它的开关作用&#xff0c;一般的电机驱动&#xff0c;开关电源&#xff0c;逆变器等大功率设备&#xff0c;全部使用MOS管…

cocoscreator-doc-TS-脚本开发-获取和设置资源

资源属性的声明 cc.Asset 的子类下面这些 cc.Texture2D、cc.SpriteFrame、cc.AnimationClip、cc.Prefab 等 加载场景&#xff0c;会自动加载场景关联的资源 &#xff0c;再加载关联的资源所关联的资源&#xff0c;直到全加载 在属性检查器中设置资源 property(cc.Label) la…

在Keil删除原有的组出现系统软件无响应的原因

取消掉core的勾选。 keil 添加文件夹&#xff0c;软件崩溃解决办法_keil5创建文件夹卡死-CSDN博客

超越传统:探索ONLYOFFICE的革命性办公新纪元

目录 &#x1f341;引言 &#x1f341;一、ONLYOFFICE产品简介 &#xff08;一&#xff09;、介绍 &#xff08;二&#xff09;、基本功能简介 &#x1f341;二、核心功能具体介绍 1、编辑操作 2、文本与段落&#xff1a; 3、样式与图形&#xff1a; 4、表格与图表&…

一文说清libc、glibc、glib的发展和关系

一 引言 在大家的技术生涯中&#xff0c;一定会遇到glib、glibc、libc这些个名词。 尤其像我这种对英文名脸盲的人&#xff0c;看着它们就头大&#xff0c;因为单从名字上看&#xff0c;也太像了&#xff0c;所以经常容易混淆。 即使翻翻网上的资料&#xff0c;看完还是有点懵…

OceanBase 升级过程研究(4.2.1.6-4.2.1.8)

模拟业务 使用benchmark加载10仓数据模拟业务场景 升级方法 使用滚动升级方式来进行OB升级。该方法前提是OB集群必须满足官方规定的高可用架构(如果 Zone 个数小于 3&#xff0c;滚动升级时则无法构成多数派), 滚动升级的原理就是轮流完成每个ZONE的升级工作&#xff0c;由于…

使用 Ant Design Vue 自定渲染函数customRender实现单元格合并功能rowSpan

使用 Ant Design Vue 自定渲染函数customRender实现单元格合并功能rowSpan 背景 在使用Ant Design Vue 开发数据表格时&#xff0c;我们常常会遇到需要合并单元格的需求。 比如&#xff0c;某些字段的值可能会在多行中重复出现&#xff0c;而我们希望将这些重复的单元格合并为…

Godot的开发框架应当是什么样子的?

目录 前言 全局协程还是实例协程&#xff1f; 存档&#xff01; 全局管理类&#xff1f; UI框架&#xff1f; Godot中的异步&#xff08;多线程&#xff09;加载 Godot中的ScriptableObject 游戏流程思考 结语 前言 这是一篇杂谈&#xff0c;主要内容是对我…

ssm118亿互游在线平台设计与开发+vue(论文+源码)_kaic

毕业设计(论文) 亿互游在线平台的设计与开发 学生姓名 XXX 学 号 XXXXXXXX 分院名称 XXXXXXXX 专业班级 XXXXX 指导教师 XXXX 填写日期 XXXX年…

MySQL技巧之跨服务器数据查询:基础篇-A数据库与B数据库查询合并

MySQL技巧之跨服务器数据查询&#xff1a;基础篇-A数据库与B数据库查询合并 上一篇已经描述&#xff1a;借用微软的SQL Server ODBC 即可实现MySQL跨服务器间的数据查询。 而且还介绍了如何获得一个在MS SQL Server 可以连接指定实例的MySQL数据库的链接名: MY_ODBC_MYSQL 以…

LLaMA-Factory全流程训练模型

&#x1f917;本文主要讲述在docker下使用LLaMA-Factory训练推理模型。 &#x1fae1;拉取镜像 首先需要启动docker&#xff0c;然后在终端中输入&#xff1a; docker run -tid --gpus all -p 8000:8000 --name LLM -e NVIDIA_DRIVER_CAPABILITIEScompute,utility -e NVIDIA…

英语每日一句

目录 每日一句今日单词知识加餐 每日一句 We will fail when we fail to try. 当我们不努力的时候&#xff0c;我们就会失败。 —— 罗莎帕克斯 今日单词 fail /feɪl/ v. 不及格&#xff1b;失败&#xff1b;未能&#xff08;做到&#xff09;&#xff1b;失灵&#xff1b…

若点集A=B则A必能恒等变换地变为B=A这一几何常识推翻直线(平面)公理

黄小宁 关键词&#xff1a;“更无理”复数 复平面z各点z的对应点z1的全体是z1面。z面平移变为z1面就使x轴⊂z面沿本身平移变为ux1轴。R可几何化为R轴&#xff0c;R轴可沿本身平移变为R′轴&#xff0c;R′轴可沿本身平移变为R″轴&#xff0c;...。直线公理和平面公理使几百年…

SpringMVC学习笔记(二)

五、Rest风格编程 &#xff08;一&#xff09;Rest风格URL规范介绍 1、什么是restful RESTful架构&#xff0c;就是目前最流行的一种互联网软件架构风格。它结构清晰、符合标准、易于理解、扩展方便&#xff0c;所以正得到越来越多网站的采用。REST这个词&#xff0c;是Roy T…

PyTorch深度学习与企业级项目实战-预训练语言模型GPT

【图书推荐】《PyTorch深度学习与企业级项目实战》-CSDN博客 13个PyTorch深度学习案例简介-CSDN博客 《PyTorch深度学习与企业级项目实战&#xff08;人工智能技术丛书&#xff09;》(宋立桓&#xff0c;宋立林)【摘要 书评 试读】- 京东图书 (jd.com) PyTorch深度学习算法与…

自存 关于RestController请求传参数 前端和后端相关

1.Get请求 Get请求传递参数一般是 1.通过PathVariable来映射 URL 绑定的占位符 后端 GetMapping("test/{id}")public R test(PathVariable Integer id){System.out.println(id);return R.success(id);}前端 export function test(id:any){return request({url:&q…