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);//创建并编译shaderShader 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)); // Zmodel = glm::rotate(model, glm::radians(angleroll), glm::vec3(1.0f, 0.0f, 0.0f)); // xmodel = 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);  // 将光源放置在 pointLightPositionglm::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

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

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【MySQL】SQL语言

【MySQL】SQL语言 文章目录 【MySQL】SQL语言前言一、SQL的通用语法二、SQL的分类三、SQLDDLDMLDQLDCL 总结 前言 本篇文章将讲到SQL语言&#xff0c;包括SQL的通用语法,SQL的分类,以及SQL语言的DDL,DML,DQL,DCL。 一、SQL的通用语法 在学习具体的SQL语句之前&#xff0c;先来…

.netcore + postgis 保存地图围栏数据

一、数据库字段 字段类型选择(Type) 设置对象类型为&#xff1a;geometry 二、前端传递的Json格式转换 前端传递围栏的各个坐标点数据如下&#xff1a; {"AreaRange": [{"lat": 30.123456,"lng": 120.123456},{"lat": 30.123456…

T265相机双目鱼眼+imu联合标定(全记录)

最近工作用到t265&#xff0c;记录一遍标定过程 1.安装驱动 首先安装realsense驱动&#xff0c;因为笔者之前使用过d435i&#xff0c;装的librealsense版本为2.55.1&#xff0c;直接使用t265会出现找不到设备的问题&#xff0c;经查阅发现是因为realsense在2.53.1后就不再支持…

【C语言指南】C语言内存管理 深度解析

&#x1f493; 博客主页&#xff1a;倔强的石头的CSDN主页 &#x1f4dd;Gitee主页&#xff1a;倔强的石头的gitee主页 ⏩ 文章专栏&#xff1a;《C语言指南》 期待您的关注 引言 C语言是一种强大而灵活的编程语言&#xff0c;为程序员提供了对内存的直接控制能力。这种对内存…

Python学习从0到1 day26 第三阶段 Spark ④ 数据输出

半山腰太挤了&#xff0c;你该去山顶看看 —— 24.11.10 一、输出为python对象 1.collect算子 功能: 将RDD各个分区内的数据&#xff0c;统一收集到Driver中&#xff0c;形成一个List对象 语法&#xff1a; rdd.collect() 返回值是一个list列表 示例&#xff1a; from …

【机器学习】机器学习中用到的高等数学知识-1.线性代数 (Linear Algebra)

向量(Vector)和矩阵(Matrix)&#xff1a;用于表示数据集&#xff08;Dataset&#xff09;和特征&#xff08;Feature&#xff09;。矩阵运算&#xff1a;加法、乘法和逆矩阵(Inverse Matrix)等&#xff0c;用于计算模型参数。特征值(Eigenvalues)和特征向量(Eigenvectors)&…

java项目-jenkins任务的创建和执行

参考内容: jenkins的安装部署以及全局配置 1.编译任务的general 2.源码管理 3.构建里编译打包然后copy复制jar包到运行服务器的路径 clean install -DskipTests -Pdev 中的-Pdev这个参数用于激活 Maven 项目中的特定构建配置&#xff08;Profile&#xff09; 在 pom.xml 文件…

【数据库取证】快速从服务器镜像文件中获取后台隐藏数据

文章关键词&#xff1a;电子数据取证、数据库取证、电子物证、云取证、手机取证、计算机取证、服务器取证 小编最近做了很多鉴定案件和参加相关电子数据取证比武赛&#xff0c;经常涉及到服务器数据库分析。现在分享一下技术方案&#xff0c;供各位在工作中和取证赛事中取得好成…

__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ is not explicitly defined

VUE_PROD_HYDRATION_MISMATCH_DETAILS 未明确定义。您正在运行 Vue 的 esm-bundler 构建&#xff0c;它期望这些编译时功能标志通过捆绑器配置全局注入&#xff0c;以便在生产捆绑包中获得更好的tree-shaking优化。 Vue.js应用程序正在使用ESM&#xff08;ECMAScript模块&#…

git撤销、回退某个commit的修改

文章目录 撤销某个特定的commit方法 1&#xff1a;使用 git revert方法 2&#xff1a;使用 git rebase -i方法 3&#xff1a;使用 git reset 撤销某个特定的commit 如果你要撤销某个很早之前的 commit&#xff0c;比如 7461f745cfd58496554bd672d52efa8b1ccf0b42&#xff0c;可…

Flume和kafka的整合

1、Kafka作为Source 【数据进入到kafka中&#xff0c;抽取出来】 在flume的conf文件夹下&#xff0c;有一个flumeconf 文件夹&#xff1a;这个文件夹是自己创建的 创建一个flume脚本文件&#xff1a; kafka-memory-logger.conf Flume 1.9用户手册中文版 — 可能是目前翻译最完…

JavaSE常用API-日期(计算两个日期时间差-高考倒计时)

计算两个日期时间差&#xff08;高考倒计时&#xff09; JDK8之前日期、时间 Date SimpleDateFormat Calender JDK8开始日期、时间 LocalDate/LocalTime/LocalDateTime ZoneId/ZoneDateTIme Instant-时间毫秒值 DateTimeFormatter Duration/Period

支持向量机SVM——基于分类问题的监督学习算法

支持向量机&#xff08;SVM&#xff0c;Support Vector Machine&#xff09;是一种常用于分类问题的监督学习算法&#xff0c;其核心思想是通过寻找一个最佳的超平面来将不同类别的数据点分开&#xff0c;从而实现分类。支持向量机广泛应用于模式识别、文本分类、图像识别等任务…

node对接ChatGpt的流式输出的配置

node对接ChatGpt的流式输出的配置 首先看一下效果 将数据用流的方式返回给客户端,这种技术需求在传统的管理项目中不多见,但是在媒体或者有实时消息等功能上就会用到,这个知识点对于前端还是很重要的。 即时你不写服务端,但是服务端如果给你这样的接口,你也得知道怎么去使用联…

SobarQube实现PDF报告导出

文章目录 前言一、插件配置二、使用步骤1.新生成一个Token2.将拷贝的Token加到上文中执行的命令中3.查看报告 三、友情提示总结 前言 这篇博文是承接此文 .Net项目在Windows中使用sonarqube进行代码质量扫描的详细操作配置 描述如何导出PDF报告 众所周知&#xff0c;导出PDF功…

[Codesys]常用功能块应用分享-BMOV功能块功能介绍及其使用实例说明

官方说明 功能说明 参数 类型 功能 pbyDataSrcPOINTER TO BYTE指向源数组指针uiSizeUINT要移动数据的BYTE数pbyDataDesPOINTER TO BYTE指向目标数组指针 实例应用-ST IF SYSTEM_CLOCK.AlwaysTrue THENCASE iAutoState OF0: //读写完成信号在下次读写信号的上升沿或复位信号…

sql注入之二次注入(sqlilabs-less24)

二阶注入&#xff08;Second-Order Injection&#xff09;是一种特殊的 SQL 注入攻击&#xff0c;通常发生在用户输入的数据首先被存储在数据库中&#xff0c;然后在后续的操作中被使用时&#xff0c;触发了注入漏洞。与传统的 SQL 注入&#xff08;直接注入&#xff09;不同&a…

springboot实现简单的数据查询接口(无实体类)

目录 前言&#xff1a;springboot整体架构 1、ZjGxbMapper.xml 2、ZjGxbMapper.java 3、ZjGxbService.java 4、ZjGxbController.java 5、调用接口测试数据是否正确 6、打包放到服务器即可 前言&#xff1a;springboot整体架构 文件架构&#xff0c;主要编写框选的这几类…

我的第一个PyQt5程序

PyQt5的开发环境配置完成之后&#xff0c;开始编写第一个PyQt5的程序。 方法一&#xff1a;使用将.ui转换成.py文件的方法 import sys from FirstPyQt import Ui_MainWindow from PyQt5.QtWidgets import *#QtCore,QtGui,QtWidgets # from QtTest import Ui_MainWindow#导入Q…

C++ | Leetcode C++题解之第560题和为K的子数组

题目&#xff1a; 题解&#xff1a; class Solution { public:int subarraySum(vector<int>& nums, int k) {unordered_map<int, int> mp;mp[0] 1;int count 0, pre 0;for (auto& x:nums) {pre x;if (mp.find(pre - k) ! mp.end()) {count mp[pre - …