美文网首页
12. opengl模型加载-assimp编译(mac)

12. opengl模型加载-assimp编译(mac)

作者: 天叔 | 来源:发表于2020-06-19 16:58 被阅读0次
    1. Assimp介绍,参考:opengl-Assimp,Assimp的作用已经说的非常清晰了
    2. 编译参考:Assimp编译安装

    图省事,mac上开发直接用brew install assimp,iOS/Android平台需要编译对应的库,需要自己编译

    源码编译有两处坑:
    2.1 Ver 3.3.1源码有笔误,编译报错,4.1+版本无此问题,亲测

    code/D3MFImporter.cpp:230:29: error: invalid operands to binary expression ('float (*)(const char *, const char *)' and 'nullptr_t')
        vertex.z = ai_strtof>(xmlReader->getAttributeValue(D3MF::XmlTag::z.c_str()), nullptr);
    
    错误代码

    去掉多余的 '>'即可

    2.2 ld: library not found for -lminizip
    github上也有人提问,参考:https://github.com/assimp/assimp/issues/2553

    1)brew install minizip

    2)手动添加"LINK_DIRECTORIES(/usr/local/lib)"到源码的CMakeLists.txt
    3. 实现效果 3d模型效果

    注意:模型比较大,相机z坐标调大到20

    Camera camera(glm::vec3(0.0f, 0.0f, 20.0f));
    
    4. 完整代码
    4.1 工程目录 工程目录

    4.2 主程序代码,model mess代码可以参考learnOpenGL教程

    #include <glad/glad.h>
    #include <GLFW/glfw3.h>
    #define STB_IMAGE_IMPLEMENTATION
    #include "stb_image.h"
    
    #include <glm/glm.hpp>
    #include <glm/gtc/matrix_transform.hpp>
    #include <glm/gtc/type_ptr.hpp>
    
    #include "Shader.h"
    #include "camera.h"
    #include "model.h"
    
    #include <iostream>
    
    void framebuffer_size_callback(GLFWwindow* window, int width, int height);
    void processInput(GLFWwindow *window);
    void mouse_callback(GLFWwindow* window, double xpos, double ypos);
    void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
    unsigned int loadTexture(char const * path);
    
    // settings
    const unsigned int SCR_WIDTH = 800;
    const unsigned int SCR_HEIGHT = 600;
    
    // camera
    Camera camera(glm::vec3(0.0f, 0.0f, 20.0f));
    float lastX = SCR_WIDTH / 2.0f;
    float lastY = SCR_HEIGHT / 2.0f;
    bool firstMouse = true;
    
    // timeing
    float deltaTime = 0.0f;
    float lastFrame = 0.0f;
    
    glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
    
    int main()
    {
        glfwInit();
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        
    #ifdef __APPLE__
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    #endif
        
        GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
        if(window == NULL)
        {
            std::cout << "Failed to create GLFW window" << std::endl;
            glfwTerminate();
            return -1;
        }
        
        glfwMakeContextCurrent(window);
        glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
        glfwSetCursorPosCallback(window, mouse_callback);
        glfwSetScrollCallback(window, scroll_callback);
        
        // tell GLFW to capture our mouse
        // 这一行,不注释也是ok的,隐藏鼠标
    //    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
        
        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
        {
            std::cout << "Failed to initialize GLAD" << std::endl;
            return -1;
        }
        
        stbi_set_flip_vertically_on_load(true);
        glEnable(GL_DEPTH_TEST);
        
        Shader lightingShader("1.colors.vs", "1.colors.fs");
        Shader lightCubeShader("1.light_cube.vs", "1.light_cube.fs");
        
        Model ourModel("pack/backpack.obj");
        
        while (!glfwWindowShouldClose(window)) {
            float currentFrame = glfwGetTime();
            deltaTime = currentFrame - lastFrame;
            lastFrame = currentFrame;
            
            //input
            processInput(window);
            
            //render
            glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            
            lightingShader.use();
            
            //view/projection transformations
            glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH/(float)SCR_HEIGHT, 0.1f, 100.0f);
            glm::mat4 view = camera.GetViewMatrix();
            lightingShader.setMat4("projection", projection);
            lightingShader.setMat4("view", view);
            
            // 世界地图转换
            glm::mat4 model = glm::mat4(1.0f);
            model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f));
            model = glm::rotate(model, (float)glfwGetTime(), glm::vec3(0.0f, 1.0f, 0.0f));
    
    //        model = glm::rotate(model, glm::radians(55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
    //        model = glm::rotate(model, glm::radians(55), glm::vec3(1.0f, 0.0f, 0.0f));
            model = glm::scale(model, glm::vec3(1.0f, 1.0f, 1.0f));
            lightingShader.setMat4("model", model);
            ourModel.Draw(lightingShader);
    
            
            glfwSwapBuffers(window);
            glfwPollEvents();
        }
        
        glfwTerminate();
        return 0;
    }
    
    bool startRecord = false;
    void processInput(GLFWwindow *window)
    {
        if (glfwGetKey(window, GLFW_KEY_Y))
        {
            std::cout << "Y" << std::endl;
            startRecord = true;
        }
        
        if (glfwGetKey(window, GLFW_KEY_N))
        {
            std::cout << "N" << std::endl;
    
            startRecord = false;
        }
        
        if (startRecord) {
            return;
        }
        
        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        {
            glfwSetWindowShouldClose(window, true);
        }
        if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
        {
            std::cout << "W" << std::endl;
    
            camera.ProcessKeyboard(FORWARD, deltaTime);
        }
        if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
        {
            camera.ProcessKeyboard(BACKWARD, deltaTime);
        }
        if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
        {
            camera.ProcessKeyboard(LEFT, deltaTime);
        }
        if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
        {
            camera.ProcessKeyboard(RIGHT, deltaTime);
        }
    
    }
    
    void framebuffer_size_callback(GLFWwindow* window, int width, int height)
    {
        glViewport(0, 0, width, height);
    }
    
    void mouse_callback(GLFWwindow* window, double xpos, double ypos)
    {
        if (startRecord) {
            return;
        }
        
        if (firstMouse)
        {
            lastX = xpos;
            lastY = ypos;
            firstMouse = false;
        }
        
        float xoffset = xpos - lastX;
        float yoffset = lastY - ypos;
        
        lastX = xpos;
        lastY = ypos;
        
        camera.ProcessMouseMovement(xoffset, yoffset);
    }
    
    void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
    {
        camera.ProcessMouseScroll(yoffset);
    }
    
    
    // 通用纹理加载方法
    unsigned int loadTexture(char const * path)
    {
        unsigned int textureID;
        glGenTextures(1, &textureID);
        
        int width, height, nrComponents;
        unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);
        
        if(data)
        {
            GLenum format;
            if (nrComponents == 1)
            {
                format = GL_RED;
            }
            else if (nrComponents == 3)
            {
                format = GL_RGB;
            }
            else if (nrComponents == 4)
            {
                format = GL_RGBA;
            }
            
            glBindTexture(GL_TEXTURE_2D, textureID);
            glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
            glGenerateMipmap(GL_TEXTURE_2D);
                    
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            
            stbi_image_free(data);
        }
        else
        {
            std::cout << "Texture failed to laod at path: " << path << std::endl;
            stbi_image_free(data);
        }
        
        return textureID;
    }
    
    

    相关文章

      网友评论

          本文标题:12. opengl模型加载-assimp编译(mac)

          本文链接:https://www.haomeiwen.com/subject/xkqbxktx.html