OpenGL project refactoring and obj file loader writing

I recently completed the design, rearranged the code, abstracted the original OpenGL framework, refactored it, and summarized it here. At the same time, the loader of an obj file is rewritten following assimp.

obj file loader writing

objFor the file format analysis, see the previous blog, pcd, obj, mtl file format analysis . objMainly include objfiles and material files mtl. The structure of the model is divided into groups, each group has its own material, and most models do not share vertices in different groups [guess~]. Therefore, the model is processed into groups, and each group has its own vertices [including vertices, textures, normals], and material information [diffuse, specular, maps, etc.]. Mainly contains two classes, one is model, this is a class for a model, and the other is mesh, a class for each group.
The following describes the data structure:

// 材质,暂时没有写贴图
struct Matrial
    {
        glm::vec3 Ka;
        glm::vec3 Kd;
        glm::vec3 Ks;
        int illum;
        std::string mat_name;
    };
// 顶点
struct Vertex
    {
        glm::vec3 pos;
        glm::vec3 nor;
        glm::vec2 tex;
    };  
// mesh类
class Mesh
    {           
    public:
        std::string meshName;
        std::string matrialName;
        // 该组的顶点信息
        std::vector<glm::vec3> points;
        std::vector<glm::vec3> normal;
        std::vector<glm::vec2> textCoords;
        // 存储网格信息,将顶点重新组织,每个顶点含有位置,纹理,法线        
        std::vector<Vertex> vertices;
        Matrial mat;
        void clear();
        void setUp();// 传输数据到GPU
        void draw(Shader *);// 绘制
    private:
        GLuint VAO;
        GLuint VBO;     
    };  
// model类
class Model
    {
    public:     
        Model(){};
        ~Model(){};
        std::string path;
        std::string matrial_path;
        bool loadModel(std::string path);// 读取模型
        void setUp();
        void draw(Shader *);
    private:
    // 包含所有的组
        std::vector<Mesh> meshes;
    // 记录所有的材质
        std::map<std::string, Matrial> matrials;
    };

For the specific implementation, see the last resource link.

Refactoring the OpenGL project

OpenGL is natively supported, but auxiliary function libraries are required to build a complete OpenGL project, mainly including window tool classes glfw, libraries for obtaining OpenGL function addresses, glewor glad. Natively writing OpenGL will make the code ugly and difficult to abstract. The code on the reference learning OpenGLis organized into the following format.

  • callBackclass Base class for
    registering callback functions
  • glfwinit.h
    Pass in the base class pointer and initialize the OpenGL environment
  • camera.h
    camera
  • shader.h
    shader class
  • EntranceClass Inherited from
    the callbackbase class, responsible for the main process control of rendering, the pointer is passed in toglfwinit.h

Part of the code is as follows:

// callback.h
class ICallbacks
{
public :    
    GLfloat deltaTime = 0.0f;
    virtual void RenderSceneCB() {};// 绘制场景接口
    // 回调函数接口
    virtual void MouseCB(MOUSE Button, KEY_STATE State, int x, int y){};    
    virtual void PassiveMouseCB(GLfloat x, GLfloat y) {};
    virtual void KeyboardCB(GLFWwindow* window, int Key, int KeyState){};
};

// glfwinit.h
void InitGlfw(ICallbacks * s,int width, int height, std::string title);// 初始化OpenGL环境
// 注册回调函数
void keyfunc(GLFWwindow* window, int k, int scancode, int action, int mode);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
// 绘制场景
void RunShader();

// Entrance.h
class Entrance :public ICallbacks
{
private:
    GLfloat lastX = 400, lastY = 300;
    GLfloat lastFrame = 0.0f;
    bool firstMouse = true;
public:
    Camera camera;
    std::vector<int> key;
    OBJ::Model * o_model;
    Shader * shader;
    Entrance(int width, int height, std::string title);
    void Run();// 绘制函数
    // 继承自callback基类的接口
    virtual void RenderSceneCB() override;
    virtual void KeyboardCB(GLFWwindow* window, int k, int action = GLFW_RELEASE) override;
    virtual void PassiveMouseCB(GLfloat xpos, GLfloat ypos) override;
    void Do_Movement();// 处理移动
};

write picture description here
The effect is as shown: only the simplest shader is applied, and no texture is used.
Complete code github address
If there is any error, please correct me~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325775039&siteId=291194637