码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-04 21:29:21      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

   struct UniformTypeName
   {
       std::string name;
       E_UniformType type;
   };

   static UniformTypeName  _datatypeToGlsl[17] = {
        {"float", UT_FLOAT}, {"vec2", UT_FLOAT_VEC2}, {"vec3", UT_FLOAT_VEC3}, {"vec4", UT_FLOAT_VEC4},
        {"int", UT_INT},     {"ivec2", UT_INT_VEC2},  {"ivec3",UT_INT_VEC3},   {"ivec4", UT_INT_VEC4},
        {"bool",UT_BOOL},    {"bvec2", UT_BOOL_VEC2}, {"bvec3",UT_BOOL_VEC3},  {"bvec4", UT_BOOL_VEC4},
        {"mat2", UT_FLOAT_MAT2},  {"mat3", UT_FLOAT_MAT3}, {"mat4", UT_FLOAT_MAT4},
        {"sampler2D", UT_SAMPLER_2D},{"samplerCube", UT_SAMPLER_CUBE}};


   class AutomaticUniformBase: public UniformBase
   {
   public:
       AutomaticUniformBase(){}
       ~AutomaticUniformBase(){}
       std::string getDeclaration(const std::string& name)
       {
           std::string declaration = "uniform " + getDataTypeName(this->_datatype) + " " + name;

           std::stringstream sizestr;
           sizestr<<_size;
           if (_size == 1) {
               declaration += ";";
           } else {
               declaration += "[" + sizestr.str() + "];";
           }

           return declaration;
       }

       std::string getDataTypeName(E_UniformType typeEnum)
       {
           std::string result("");
           for(int i = 0; i< sizeof(_datatypeToGlsl); i++)
           {
               if(typeEnum == _datatypeToGlsl[i].type)
               {
                   result = _datatypeToGlsl[i].name;
                   break;
               }
           }

           return result;
       }
   };



   template<class T>

   class AutomaticUniform: public AutomaticUniformBase
   {
   public:
       AutomaticUniform()
       {
           this->_uniformClassType = UCT_AUTO;
       }
       typedef T (* GetValueFunc)(UniformState*);
       AutomaticUniform( unsigned int size, E_UniformType type, GetValueFunc getValue)
       {
           this->_size = size;
           this->_datatype = type;
           this->_getValue = getValue;
           this->_uniformClassType = UCT_AUTO;
       }

       struct Option
       {
           unsigned int _size;
           E_UniformType _datatype;
           GetValueFunc _getValue;
       };

       AutomaticUniform(const Option& option)
       {
           this->_size = option._size;
           this->_datatype = option._datatype;
           this->_getValue = option._getValue;
           this->_uniformClassType = UCT_AUTO;
       }

   public:
       GetValueFunc _getValue;
   };

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

标签:

原文地址:http://blog.csdn.net/zangle260/article/details/42396745

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!