BuildinMaterialVariable.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "BuildinMaterialVariable.h"
  2. #include "Util/Exception.h"
  3. #include "Util/Assert.h"
  4. #include <cstring>
  5. #include <boost/lexical_cast.hpp>
  6. #include <boost/assign/list_of.hpp>
  7. //==============================================================================
  8. // Statics =
  9. //==============================================================================
  10. ConstCharPtrHashMap<BuildinMaterialVariable::BuildinEnum>::Type
  11. BuildinMaterialVariable::buildinNameToEnum = boost::assign::map_list_of
  12. ("position", POSITION)
  13. ("tangent", TANGENT)
  14. ("normal", NORMAL)
  15. ("texCoords", TEX_COORDS)
  16. ("modelMat", MODEL_MAT)
  17. ("viewMat", VIEW_MAT)
  18. ("projectionMat", PROJECTION_MAT)
  19. ("modelViewMat", MODELVIEW_MAT)
  20. ("viewProjectionMat", VIEWPROJECTION_MAT)
  21. ("normalMat", NORMAL_MAT)
  22. ("modelViewProjectionMat", MODELVIEWPROJECTION_MAT)
  23. ("msNormalFai", MS_NORMAL_FAI)
  24. ("msDiffuseFai", MS_DIFFUSE_FAI)
  25. ("msSpecularFai", MS_SPECULAR_FAI)
  26. ("msDepthFai", MS_DEPTH_FAI)
  27. ("isFai", IS_FAI)
  28. ("ppsPrePassFai", PPS_PRE_PASS_FAI)
  29. ("ppsPostPassFai", PPS_POST_PASS_FAI)
  30. ("rendererSize", RENDERER_SIZE)
  31. ("sceneAmbientColor", SCENE_AMBIENT_COLOR)
  32. ("blurring", BLURRING);
  33. boost::unordered_map<BuildinMaterialVariable::BuildinEnum, GLenum>
  34. BuildinMaterialVariable::buildinToGlType = boost::assign::map_list_of
  35. (POSITION, GL_FLOAT_VEC3)
  36. (TANGENT, GL_FLOAT_VEC4)
  37. (NORMAL, GL_FLOAT_VEC3)
  38. (TEX_COORDS, GL_FLOAT_VEC2)
  39. (MODEL_MAT, GL_FLOAT_MAT4)
  40. (VIEW_MAT, GL_FLOAT_MAT4)
  41. (PROJECTION_MAT, GL_FLOAT_MAT4)
  42. (PROJECTION_MAT, GL_FLOAT_MAT4)
  43. (VIEWPROJECTION_MAT, GL_FLOAT_MAT4)
  44. (NORMAL_MAT, GL_FLOAT_MAT3)
  45. (MODELVIEWPROJECTION_MAT, GL_FLOAT_MAT4)
  46. (MS_NORMAL_FAI, GL_SAMPLER_2D)
  47. (MS_DIFFUSE_FAI, GL_SAMPLER_2D)
  48. (MS_SPECULAR_FAI, GL_SAMPLER_2D)
  49. (MS_DEPTH_FAI, GL_SAMPLER_2D)
  50. (IS_FAI, GL_SAMPLER_2D)
  51. (PPS_PRE_PASS_FAI, GL_SAMPLER_2D)
  52. (PPS_POST_PASS_FAI, GL_SAMPLER_2D)
  53. (RENDERER_SIZE, GL_FLOAT_VEC2)
  54. (SCENE_AMBIENT_COLOR, GL_FLOAT_VEC3)
  55. (BLURRING, GL_FLOAT);
  56. //==============================================================================
  57. // Constructor =
  58. //==============================================================================
  59. BuildinMaterialVariable::BuildinMaterialVariable(
  60. const char* shaderProgVarName,
  61. const ShaderPrograms& shaderProgsArr)
  62. : MaterialVariable(BUILDIN, shaderProgVarName, shaderProgsArr),
  63. bEnum(BUILDINS_NUM)
  64. {
  65. GLenum dataType;
  66. // Sanity checks
  67. if(!isBuildin(shaderProgVarName, &bEnum, &dataType))
  68. {
  69. throw EXCEPTION("The variable is not buildin: " + shaderProgVarName);
  70. }
  71. if(dataType != getGlDataType())
  72. {
  73. throw EXCEPTION("The buildin variable \"" + shaderProgVarName +
  74. "\" should be of type: " +
  75. boost::lexical_cast<std::string>(dataType));
  76. }
  77. }
  78. //==============================================================================
  79. // isBuildin =
  80. //==============================================================================
  81. bool BuildinMaterialVariable::isBuildin(const char* name,
  82. BuildinEnum* var, GLenum* dataType)
  83. {
  84. ConstCharPtrHashMap<BuildinEnum>::Type::const_iterator it =
  85. buildinNameToEnum.find(name);
  86. if(it == buildinNameToEnum.end())
  87. {
  88. return false;
  89. }
  90. if(var)
  91. {
  92. *var = it->second;
  93. }
  94. if(dataType)
  95. {
  96. boost::unordered_map<BuildinEnum, GLenum>::const_iterator it2 =
  97. buildinToGlType.find(it->second);
  98. ASSERT(it2 != buildinToGlType.end());
  99. *dataType = it2->second;
  100. }
  101. return true;
  102. }