现在的位置: 首页 > 综合 > 正文

用模板类封装OpenGL Shader Uniform 变量2

2018年04月08日 ⁄ 综合 ⁄ 共 3822字 ⁄ 字号 评论关闭

  enum E_UniformType
  {
        UT_INT           =   0x1404,
        UT_INT_VEC2      =   0x8B53,
        UT_INT_VEC3      =   0x8B54,
        UT_INT_VEC4      =   0x8B55,

        UT_FLOAT           = 0x1406,
        UT_FLOAT_VEC2    =   0x8B50,
        UT_FLOAT_VEC3    =   0x8B51,
        UT_FLOAT_VEC4    =   0x8B52,

        UT_BOOL          =   0x8B56,
        UT_BOOL_VEC2     =   0x8B57,
        UT_BOOL_VEC3     =   0x8B58,
        UT_BOOL_VEC4     =   0x8B59,

        UT_FLOAT_MAT2    =   0x8B5A,
        UT_FLOAT_MAT3    =   0x8B5B,
        UT_FLOAT_MAT4    =   0x8B5C,

        UT_SAMPLER_2D    =   0x8B5E,
        UT_SAMPLER_CUBE  =   0x8B60
  };

  class ActiveUniform
  {
  public:
      std::string _name;
      unsigned int _location;
      unsigned int _type;
      unsigned int _length;
      unsigned int _size;
  public:
      ActiveUniform()
      {
          this->_name = "";
          this->_location = 0;
          this->_type = 0;
          this->_length = 0;
      }
      ~ActiveUniform(){}
  };

  enum E_UniformClassType {UCT_BASE=0, UCT_COMMON, UCT_ARRY, UCT_AUTO};
  class UniformBase
  {
  public:
      UniformBase()
      {
          this->_name = "";
          this->_datatype = UT_FLOAT;
          this->_textureUnitIndex = 0;
          this->_size = 0;
          this->_uniformClassType = UCT_BASE;
      }
      ~UniformBase(){}
  public:
      std::string _name;
      E_UniformType _datatype;
      unsigned int _textureUnitIndex;
      unsigned int _size;
      E_UniformClassType  _uniformClassType;
  protected:
      ActiveUniform _activeUniform;
  };
  typedef std::map<std::string, UniformBase*> UniformMap;

  template<class T>
  class Uniform: public UniformBase
  {
  public:
      typedef T (* GetValueFunc)(void*);
  public:
      Uniform()
      {
          this->_getValueFunc = NULL;
          this->_funcOnwer = NULL;
          this->_uniformClassType = UCT_COMMON;
      }

      Uniform(const char *pname,  E_UniformType type, GetValueFunc funcAddr,  void* funcOwner)
      {
          this->_name = std::string(pname);
          this->_datatype = type;
          this->_size = 1;
          this->_location = 0;
          this->_getValueFunc = funcAddr;
          this->_funcOnwer = funcOwner;
          this->_uniformClassType = UCT_COMMON;
      }

      Uniform(const char *pname,  E_UniformType type,  int loc, int size)
      {
          this->_name = std::string(pname);
          this->_datatype = type;
          this->_size = size;
          this->_location = loc;
          this->_getValueFunc = NULL;
          this->_funcOnwer = NULL;
          this->_uniformClassType = UCT_COMMON;
      }
      Uniform(const ActiveUniform& activeUniform, const std::string& uniformName, unsigned location, T value)
      {
          this->_activeUniform = activeUniform;
          this->_name = uniformName;
          this->_location = location;
          this->_datatype = E_UniformType(activeUniform._type);
          this->_value = value;
          this->_getValueFunc = NULL;
          this->_funcOnwer = NULL;
          this->_uniformClassType = UCT_COMMON;
      }
      ~Uniform(){}
  public:
     T _value;
     unsigned int _location;

     GetValueFunc _getValueFunc;
     void* _funcOnwer;
  };

  template<class T>
  class UniformArray: public UniformBase
  {
  public:
      UniformArray()
      {
             this->_uniformClassType = UCT_ARRY;
      }

      UniformArray(const std::string& uniformName, unsigned int type,
                   const std::vector<unsigned int>& locations)
      {
          this->_name = uniformName;
          this->_locations = locations;
          this->_value.clear();
          this->_datatype = E_UniformType(type);
          this->_uniformClassType = UCT_ARRY;
      }

      UniformArray(const ActiveUniform& activeUniform, const std::string& uniformName,
                   const std::vector<unsigned int>& locations, const std::vector<T>& values)
      {
          this->_activeUniform = activeUniform;
          this->_name = uniformName;
          this->_locations = locations;
          this->_value = values;
          this->_datatype = E_UniformType(activeUniform._type);
          this->_uniformClassType = UCT_ARRY;
      }
  public:
      std::vector<T> _value;
      std::vector<unsigned int> _locations;
  };

抱歉!评论已关闭.