Encapsulating uniform variables of OpenGL Shader with template classes

Source: Internet
Author: User

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;
};

Encapsulating uniform variables of OpenGL Shader with template classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.