当前位置: 首页 > news >正文

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

相关文章:

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.…...

OpenSIP2.4.11 向 FreeSWITCH 注册

应朋友要求做了个简单的测试&#xff0c;花费时间不过半小时&#xff0c;记录如下&#xff1a; OpenSIPS IP 地址&#xff1a;192.168.31.213 FreeSWITCH IP 地址&#xff1a;192.168.31.166 加载 uac_registrant 模块&#xff08;这个模块依赖 uac_auth 模块&#xff0c;得…...

【C++】深入理解 C++ 优先级队列、容器适配器与 deque:实现与应用解析

个人主页: 起名字真南的CSDN博客 个人专栏: 【数据结构初阶】 &#x1f4d8; 基础数据结构【C语言】 &#x1f4bb; C语言编程技巧【C】 &#x1f680; 进阶C【OJ题解】 &#x1f4dd; 题解精讲 目录 前言&#x1f4cc; 1. 优先级队列、容器适配器和 deque 概述✨1.1 什么是优…...

Android 开发与救砖工具介绍

Android 开发与救砖工具介绍 在 Android 开发和设备维护中&#xff0c;fastboot、adb 和 9008 模式是三个非常重要的工具和模式。它们各自有不同的用途和操作方式&#xff0c;对于开发者和技术支持人员来说&#xff0c;了解它们的功能和使用方法是必不可少的。 1. Fastboot …...

vue2和vue3:diff算法的区别?

Vue 2 和 Vue 3 在 diff 算法方面的主要区别是&#xff1a; Vue 2 使用普通的 diff 算法&#xff0c;它会遍历所有的节点进行比对。 Vue 3 引入了 patch flag 的概念&#xff0c;并且对 diff 算法进行了优化&#xff0c;比如在相同层级的节点间不会去递归比对已经被移除的节点…...

后端返回大数问题

这个问题并不难,但是在开发的时候没有注意到 后端返回了一个列表数据,包含id,这个id是一个大数,列表进入详情,需要将id传入到详情页面详情页面内部通过id获取数据一直404,id不正确找问题,从路由传参到请求数据发现id没有问题,然后和后端进行联调,发现后端返回的id和我获取的id…...

vue3: ref, reactive, readonly, shallowReactive

vue3: ref, reactive, readonly, shallowReactive 原文地址:https://mp.weixin.qq.com/s/S3jPZKEMBP8nQQObF5d2VA <template><!-- <ul><li v-for"item in list.arr">{{item}}</li></ul><button click.prevent"add"…...

5G与4G互通的桥梁:N26接口

5G的商用部署进程将是一个基于4G系统进行的长期的替换、升级、迭代的过程&#xff0c;4G系统是在过渡到5G全覆盖过程中&#xff0c;作为保障用户业务连续性体验这一目的的最好补充。 因此4G/5G融合组网&#xff0c;以及互操作技术将是各大运营商在网络演进中需要重点考虑的问题…...

29-Elasticsearch 集群监控

Elasticsearch Stats 相关的 API ● Elasticsearch 提供了多个监控相关的 API ○ Node Stats&#xff1a; _nodes/stats ○ Cluster Stats: _cluster/stats ○ Index Stats: index_name/_stats Elasticsearch Task API ● 查看 Task 相关的 API ○ Pending Cluster Tasks…...

利用Excel批量生成含二维码的设备管理标签卡片

在日常办公中&#xff0c;批量制作标签是常见且繁琐的任务&#xff0c;尤其当这些标签需要包含大量数据并附带二维码以便快速扫描识别时&#xff0c;难度更是成倍增加。尽管传统的Word邮件合并功能在数据插入方面表现出色&#xff0c;但在二维码生成上却显得有些捉襟见肘。 为…...

小米运动健康与华为运动健康在苹手机ios系统中无法识别蓝牙状态 (如何在ios系统中开启 蓝牙 相册 定位 通知 相机等功能权限,保你有用)

小米运动健康与华为运动健康在苹手机ios系统中无法识别蓝牙状态 &#xff08;解决方案在最下面&#xff0c;参考蓝牙权限设置方式举一反三开启其它模块的权限&#xff09; 最近买了一台小米手表s4&#xff0c;但是苹手机ios系统中的 “小米运动健康” app 始终无法识别我手机…...

高亮变色显示文本中的关键字

效果 第一步&#xff1a;按如下所示代码创建一个用来高亮显示文本的工具类&#xff1a; public class KeywordUtil {/*** 单个关键字高亮变色* param color 变化的色值* param text 文字* param keyword 文字中的关键字* return*/public static SpannableString highLigh…...

Javascript垃圾回收机制-运行机制(大厂内部培训版本)

前言 计算机基本组成&#xff1a; 我们编写的软件首先读取到内存&#xff0c;用于提供给 CPU 进行运算处理。 内存的读取和释放&#xff0c;决定了程序性能。 冯诺依曼结构 解释和编译 这两个概念怎么理解呢。 编译相当于事先已经完成了可以直接用。好比去饭店吃饭点完上…...

【jvm】一个空Object对象的占多大空间

目录 1. 说明2. 结论 1. 说明 1.在Java中&#xff0c;一个空Object对象所占用的内存空间大小会受到JVM&#xff08;Java虚拟机&#xff09;实现和配置的影响&#xff0c;具体数值可能因不同JVM版本和配置而有所不同。2.但一般来说&#xff0c;可以基于一些通用的原则来估算这个…...

241114.学习日志——[CSDIY] [CS]数据结构与算法 [00]

CSDIY&#xff1a;这是一个非科班学生的努力之路&#xff0c;从今天开始这个系列会长期更新&#xff0c;&#xff08;最好做到日更&#xff09;&#xff0c;我会慢慢把自己目前对CS的努力逐一上传&#xff0c;帮助那些和我一样有着梦想的玩家取得胜利&#xff01;&#xff01;&…...

The Planets: Earth -- 练习

环境搭建 该靶场环境来自Vulnhub -------- Difficulty: Easy 靶机与Kali的IP地址只需要在同一局域网即可&#xff08;同一个网段,即两虚拟机处于同一网络模式&#xff09;&#xff0c;所以需要调整KALI和靶场的网络模式&#xff0c;为了方便测试本地采用NAT模式。 注意&…...

linux逻辑卷练习

目录 知识点&#xff1a; 常用命令 题目&#xff1a; 解题&#xff1a; 1&#xff09;分区 2&#xff09;创建物理卷 3&#xff09;创建卷组 4&#xff09;生成逻辑卷 "要带参数 -n" 5&#xff09;扩容 6&#xff09;格式化(添加文件系统) 7&#xff09;挂…...

openai 论文Scaling Laws for Neural Language Models学习

2001.08361 (arxiv.org) 论文研究语言模型在交叉熵损失下的性能经验缩放定律&#xff1a;模型损失&#xff08;性能&#xff09;随模型大小、数据集大小和用于训练的计算量呈现缩放为幂律的关系&#xff0c;有些趋势跨越超过 7 个数量级。其他模型架构细节 &#xff08;如网络…...

__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模块&#…...

基于PHP技术的校园站的设计与实现

毕业论文&#xff08;基于PHP技术的校园站的设计与实现&#xff09; 基于PHP技术的校园网站的设计与实现校园网作为教育、教学、科研、管理等工作的平台和基础设施&#xff0c;它的建立有助于加强师生之间的交流&#xff0c;改变传统的教学模式和教育管理方式&#xff0c;对促…...

JVM回收机制与算法

jvm基本结构 JVM&#xff08;Java虚拟机&#xff09;是Java程序可以跨平台运行的关键。它负责将Java字节码转换为特定平台的机器码&#xff0c;使Java程序能够在不同的硬件和操作系统上运行而无需重新编译。JVM的基本结构主要包括以下几个核心部分&#xff1a; ‌类加载器&…...

24/11/14 算法笔记 GMM高斯混合模型

高斯混合模型&#xff08;Gaussian Mixture Model&#xff0c;简称 GMM&#xff09;是一种概率模型&#xff0c;用于表示具有多个子群体的数据集&#xff0c;其中每个子群体的数据分布可以用高斯分布&#xff08;正态分布&#xff09;来描述。GMM 是一种软聚类方法&#xff0c;…...

Linux下编译安装Nginx

以下是在Linux下编译安装Nginx的详细步骤&#xff1a; 一、安装依赖库 安装基本编译工具和库 在Debian/Ubuntu系统中&#xff0c;使用以下命令安装&#xff1a;sudo apt -y update sudo apt -y install build - essential libpcre3 - dev zlib1g - dev libssl - dev在CentOS/…...

算力100问☞第4问:算力的构成元素有哪些?

算力的构成元素是一个多维度且相互交织的体系&#xff0c;它融合了硬件基础设施、软件优化策略、数据处理效能以及分布式计算技术等多个层面&#xff0c;共同塑造了强大的计算能力。具体如下&#xff1a; 1、硬件基础设施 中央处理器&#xff08;CPU&#xff09;&#xff1a;…...

安装paddle

网址&#xff1a;飞桨PaddlePaddle-源于产业实践的开源深度学习平台 或者找对应python和cuda版本的paddle下载后安装&#xff1a; https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html 你想要安装paddlepaddle - gpu2.6.1.post112版本。在你提供的文件列表中&am…...

飞凌嵌入式RK3576核心板已适配Android 14系统

在今年3月举办的RKDC2024大会上&#xff0c;飞凌嵌入式FET3576-C核心板作为瑞芯微RK3576处理器的行业首秀方案重磅亮相&#xff0c;并于今年6月率先量产发货&#xff0c;为客户持续稳定地供应&#xff0c;得到了众多合作伙伴的认可。 FET3576-C核心板此前已提供了Linux 6.1.57…...

SpringBoot+MyBatis+MySQL的Point实现范围查找

前言 最近做了一个功能&#xff0c;需要通过用户当前位置点获取指定范围内的数据。由于后端存储用的是 MySQL&#xff0c;故选择使用 MySQL 中的 Point 实现范围查找功能。ORM 框架用的是 MyBatis&#xff0c;MyBatis 原生并不支持 Point 字段与 POJO 的映射&#xff0c;需要自…...

【Apache Paimon】-- 1 -- Apache Paimon 是什么?

目录 1、简介 2、概览 3、哪些场景可以使用 Paimon 4、周边生态 5、小结 6、参考 1、简介 我们听说过数据仓库、数据湖、数据湖仓,那你听说过流式数据仓库(Stream warehouse,简称:Streamhouse)吗?那我们今天就来解锁看看他们之中的新秀: Apache paimon 到底是什么…...

解决VsCode无法跳转问题

在settings.json中加入以下代码 { "files.associations": { "*.c":"c", "*.h":"c", "*.s":"masm" }, "includePath":[ "${workspaceFold…...

优化C++设计模式:用模板代替虚函数与多态机制

文章目录 0. 引言1. 模板编程替换虚函数和多态的必要性1.1. MISRA C对类型转换和虚函数的规定1.2. 虚函数与多态问题的影响及如何适应MISRA C要求1.3. 模板编程的优势&#xff1a;替代虚函数和多态机制 2. 设计模式改进2.1. 单例模式的改进与静态局部变量的对比(第二种实现) 2.…...