前言

这篇博客针对《c++ OpenGL GLFW三维skybox场景obj模型AABB碰撞检测动画界面》编写代码,代码整洁,规则,易读。 学习与应用推荐首选。


运行结果

c++ OpenGL GLFW三维skybox场景obj模型AABB碰撞检测动画界面-LMLPHP


文章目录

一、所需工具软件
二、使用步骤
       1. 主要代码
       2. 运行结果
三、在线协助

一、所需工具软件

       1. VS2019, Qt
       2. C++

二、使用步骤

代码如下(示例):

void initOpenGLWindow() {
    myWindow.Create(1920, 1080, "Proiect OpenGL");
    glfwSetWindowPos(myWindow.getWindow(), 100, 100);
    glfwGetFramebufferSize(myWindow.getWindow(), &retina_width, &retina_height);
}

void setWindowCallbacks() {
    glfwSetWindowSizeCallback(myWindow.getWindow(), windowResizeCallback);
    glfwSetKeyCallback(myWindow.getWindow(), keyboardCallback);
    glfwSetCursorPosCallback(myWindow.getWindow(), mouseCallback);
}

void initOpenGLState() {
    glClearColor(0.7f, 0.7f, 0.7f, 1.0f);
    glViewport(0, 0, myWindow.getWindowDimensions().width, myWindow.getWindowDimensions().height);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);
    glfwSetInputMode(myWindow.getWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}

void initModels() {
    terrain.LoadModel("objects\\terrain\\terrain.obj", "objects\\terrain\\");
    terrain_trees.LoadModel("objects\\terrain_trees\\terrain_trees.obj", "objects\\terrain_trees\\");
    eliceMoara.LoadModel("objects\\elice\\elice_moara.obj", "objects\\elice\\");
    bmwBody.LoadModel("objects\\bmw\\bmw_body.obj", "objects\\bmw\\");
    bmwTire.LoadModel("objects\\bmw\\bmw_tire.obj", "objects\\bmw\\");
    tree1.LoadModel("objects\\tree1\\Tree.obj", "objects\\tree1\\");
    tree2.LoadModel("objects\\tree2\\Tree.obj", "objects\\tree2\\");

    faces.push_back("textures/skybox/posx.jpg");
    faces.push_back("textures/skybox/negx.jpg");
    faces.push_back("textures/skybox/posy.jpg");
    faces.push_back("textures/skybox/negy.jpg");
    faces.push_back("textures/skybox/posz.jpg");
    faces.push_back("textures/skybox/negz.jpg");
    mySkyBox.Load(faces);
}

void initShaders() {
    basicShader.loadShader("shaders/basicShader.vert", "shaders/basicShader.frag");
    fragmentDiscardingShader.loadShader("shaders/fragmentDiscardingShader.vert", "shaders/fragmentDiscardingShader.frag");
    skyboxShader.loadShader("shaders/skyboxShader.vert", "shaders/skyboxShader.frag");
    depthMapShader.loadShader("shaders/depthMap.vert", "shaders/depthMap.frag");
    screenQuadShader.loadShader("shaders/screenQuad.vert", "shaders/screenQuad.frag");
    screenQuad.LoadModel("objects/quad/quad.obj");
}

void initUniforms() {
    basicShader.useShaderProgram();
    view = myCamera.getViewMatrix();
    viewLoc = glGetUniformLocation(basicShader.shaderProgram, "view");
    glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
    model = glm::mat4(1.0f);
    modelLoc = glGetUniformLocation(basicShader.shaderProgram, "model");
    glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
    normalMatrix = glm::mat3(glm::inverseTranspose(view * model));
    normalMatrixLoc = glGetUniformLocation(basicShader.shaderProgram, "normalMatrix");
    glUniformMatrix3fv(normalMatrixLoc, 1, GL_FALSE, glm::value_ptr(normalMatrix));
    projection = glm::perspective(glm::radians(45.0f), (float)retina_width / (float)retina_height, 0.1f, 1000.0f);
    projectionLoc = glGetUniformLocation(basicShader.shaderProgram, "projection");
    glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
    lightDir = glm::vec3(-80.0f, 100.0f, 0.0f);
    lightRotation = glm::rotate(glm::mat4(1.0f), glm::radians(lightAngle), glm::vec3(0.0f, 1.0f, 0.0f));
    lightDirLoc = glGetUniformLocation(basicShader.shaderProgram, "lightDir");
    glUniform3fv(lightDirLoc, 1, glm::value_ptr(glm::inverseTranspose(glm::mat3(view * lightRotation)) * lightDir));
    lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
    lightColorLoc = glGetUniformLocation(basicShader.shaderProgram, "lightColor");
    glUniform3fv(lightColorLoc, 1, glm::value_ptr(lightColor));

    fragmentDiscardingShader.useShaderProgram();
    view = myCamera.getViewMatrix();
    viewLocFragmentDiscarding = glGetUniformLocation(fragmentDiscardingShader.shaderProgram, "view");
    glUniformMatrix4fv(viewLocFragmentDiscarding, 1, GL_FALSE, glm::value_ptr(view));
    model = glm::mat4(1.0f);
    modelLocFragmentDiscarding = glGetUniformLocation(fragmentDiscardingShader.shaderProgram, "model");
    glUniformMatrix4fv(modelLocFragmentDiscarding, 1, GL_FALSE, glm::value_ptr(model));
    projection = glm::perspective(glm::radians(45.0f), (float)retina_width / (float)retina_height, 0.1f, 1000.0f);
    projectionLocFragmentDiscarding = glGetUniformLocation(fragmentDiscardingShader.shaderProgram, "projection");
    glUniformMatrix4fv(projectionLocFragmentDiscarding, 1, GL_FALSE, glm::value_ptr(projection));
}

void initFBO() {
    glGenFramebuffers(1, &shadowMapFBO);
    glGenTextures(1, &depthMapTexture);
    glBindTexture(GL_TEXTURE_2D, depthMapTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    float borderColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    glBindFramebuffer(GL_FRAMEBUFFER, shadowMapFBO);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMapTexture, 0);
    glDrawBuffer(GL_NONE);
    glReadBuffer(GL_NONE);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

glm::mat4 computeLightSpaceTrMatrix() {
    glm::mat4 lightView = glm::lookAt(lightDir, glm::vec3(0.0f), glm::vec

运行结果

c++ OpenGL GLFW三维skybox场景obj模型AABB碰撞检测动画界面-LMLPHP

三、在线协助:

如需安装运行环境或远程调试,见文章底部个人 QQ 名片,由专业技术人员远程协助!

当前文章连接:https://blog.csdn.net/alicema1111/article/details/132666851
个人博客主页https://blog.csdn.net/alicema1111?type=blog
博主所有文章点这里:https://blog.csdn.net/alicema1111?type=blog

博主推荐:
Python人脸识别考勤打卡系统:
https://blog.csdn.net/alicema1111/article/details/133434445
Python果树水果识别https://blog.csdn.net/alicema1111/article/details/130862842
Python+Yolov8+Deepsort入口人流量统计:https://blog.csdn.net/alicema1111/article/details/130454430
Python+Qt人脸识别门禁管理系统:https://blog.csdn.net/alicema1111/article/details/130353433
Python+Qt指纹录入识别考勤系统:https://blog.csdn.net/alicema1111/article/details/129338432
Python Yolov5火焰烟雾识别源码分享:https://blog.csdn.net/alicema1111/article/details/128420453
Python+Yolov8路面桥梁墙体裂缝识别:https://blog.csdn.net/alicema1111/article/details/133434445

01-30 18:12