Guest User

hpp

a guest
Jun 21st, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <string>
  4. #include <fstream>
  5. #include <iostream>
  6. #include <array>
  7. #include <vector>
  8. #include <memory>
  9. #include <algorithm>
  10.  
  11. #include "gl/glad.h"
  12. #include "gl/glfw3.h"
  13. #include "glm/glm.hpp"
  14. #include "glm/gtc/matrix_transform.hpp"
  15. #include "glm/gtc/type_ptr.hpp"
  16. #include "tinygltf/tinygltf.hpp"
  17. #include "tinygltf/stb_image.h"
  18.  
  19. namespace glWrap
  20. {
  21.     class Engine;
  22.  
  23.     struct Vertex{
  24.         glm::vec3 pos{};
  25.         glm::vec3 nor{};
  26.         glm::vec2 tex{};
  27.     };
  28.  
  29.     struct Transform{
  30.         glm::vec3 pos{};
  31.         glm::vec3 rot{};
  32.         glm::vec3 scl{};
  33.     };
  34.  
  35.     class Shader
  36.     {
  37.         public:
  38.         unsigned int m_ID;
  39.        
  40.         Shader(std::string vertexPath, std::string fragmentPath);
  41.         Shader(const char* vertexShader, const char* fragmentShader, bool isText);
  42.  
  43.         void Use();
  44.  
  45.         void SetBool(const std::string &name, bool value) const;
  46.         void SetInt(const std::string &name, int value) const;
  47.         void SetFloat(const std::string &name, float value) const;
  48.         void SetMatrix4(const std::string &name, glm::mat4 mat) const;
  49.     };
  50.  
  51.     class Texture2D
  52.     {
  53.         public:
  54.         unsigned int m_ID;
  55.  
  56.         /** @brief Texture2D Constructor
  57.          *@param[in] image String path to image location on disk
  58.          *@param[in] flip If image should be vertically flipped
  59.          *@param[in] filter Select pixel interpolation: GL_LINEAR or GL_NEAREST
  60.          *@param[in] desiredChannels Select texture channels: GL_RED, GL_RG, GL_RGB or GL_RGBA
  61.          */
  62.         Texture2D(std::string image, bool flip, GLenum filter, GLenum desiredChannels);
  63.  
  64.         /** @brief Description
  65.          *@param[in] unit GL Texture Unit
  66.          */
  67.         void SetActive(unsigned int unit);
  68.     };
  69.  
  70.     class WorldObject{
  71.     protected:
  72.         Transform   m_transform{};
  73.  
  74.     public:
  75.         Transform GetTransform();
  76.         glm::vec3 GetPosition();
  77.         glm::vec3 GetRotation();
  78.         glm::vec3 GetScale();
  79.         glm::vec3 GetForwardVector();
  80.         glm::vec3 GetUpwardVector();
  81.         glm::vec3 GetDirection();
  82.  
  83.         void SetTransform(Transform transform);
  84.         void SetPosition(glm::vec3 position);
  85.         void SetRotation(glm::vec3 rotation);
  86.         void SetScale(glm::vec3 scale);
  87.  
  88.         void AddPosition(glm::vec3 position);
  89.         void AddRotation(glm::vec3 rotation);
  90.         void AddScale(glm::vec3 scale);
  91.     };
  92.  
  93.     class Camera : public WorldObject{
  94.     private:
  95.         float       m_FOV{90};
  96.         glm::vec2   m_aspect{800, 600};
  97.         glm::vec2   m_clip{0.1f, 1000.f};
  98.         bool        m_perspective{true};
  99.  
  100.     public:
  101.         float GetFOV();
  102.         glm::mat4 GetView();
  103.         glm::mat4 GetProjection();
  104.        
  105.         void SetFOV(float FOV);
  106.     };
  107.  
  108.     class Primitive{
  109.         public:
  110.  
  111.         std::vector<Vertex>         m_vertices;
  112.         std::vector<unsigned short> m_indices;
  113.         unsigned int                m_material;
  114.  
  115.         GLuint                      m_VBO,
  116.                                     m_VAO,
  117.                                     m_EBO;
  118.  
  119.         Primitive() = default;
  120.         void Draw();
  121.     };
  122.  
  123.     class Mesh{
  124.         public:
  125.         std::vector<Primitive> m_primitives;
  126.  
  127.         Mesh() = default;
  128.     };
  129.  
  130.     class Instance : public WorldObject {
  131.     private:
  132.         Mesh*                   m_mesh;
  133.         std::vector<Shader*>    m_shaders;
  134.         bool                    m_visible{true};
  135.  
  136.     public:
  137.         void SetMesh(Mesh* mesh);
  138.         void SetShader(Shader* shader, int primitive);
  139.         void SetVisibility(bool visibility);
  140.  
  141.         Mesh* GetMesh();
  142.         Shader* GetShader(int primitive);
  143.         bool GetVisibility();
  144.     };
  145.  
  146.     class Window{
  147.     private:
  148.         // static void keyCall(GLFWwindow* window, int key, int scancode, int action, int mods);
  149.         // static void framebuffer_size_callback(GLFWwindow* win, int width, int height);
  150.         GLFWwindow*                         m_window;
  151.         std::string                         m_name;
  152.         static std::vector<unsigned int>    m_heldKeys;
  153.  
  154.     public:
  155.         glm::vec4                           m_color;
  156.         Camera*                             m_ActiveCamera;
  157.  
  158.         // Window() = default;
  159.         Window(std::string name, glm::ivec2 size, glm::vec4 color, Camera* camera, GLFWwindow* context);
  160.         void Swap();
  161.         void Draw(Instance& instance);
  162.         ~Window();
  163.  
  164.         // bool isKeyPressed(unsigned int key);
  165.         // bool isKeyReleased(unsigned int key);
  166.         bool isKeyHeld(unsigned int key);
  167.         // bool isKeyRepeat(unsigned int key);
  168.         bool WindowRequestedClose();
  169.     };
  170.  
  171.     class Engine{
  172.     private:
  173.         GLFWwindow*             m_context;
  174.         std::unique_ptr<Shader> m_defaultShader;
  175.         double                  m_lastFrameTime;
  176.         double                  m_deltaTime;
  177.         bool                    m_firstFrame{true};
  178.  
  179.     public:
  180.         Engine();
  181.         void Update();
  182.         float GetDeltaTime();
  183.         Shader* GetDefaultShader();
  184.         GLFWwindow* GetContext();
  185.         ~Engine();
  186.     };
  187.  
  188.     void LoadMesh(std::map<std::string, Mesh>& container, std::string file);
  189. }
Add Comment
Please, Sign In to add comment