ShaderProgramUniformVariable.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "ShaderProgramUniformVariable.h"
  2. #include "rsrc/ShaderProgram.h"
  3. #include "rsrc/Texture.h"
  4. #include "gl/GlStateMachine.h"
  5. //==============================================================================
  6. // doSanityChecks =
  7. //==============================================================================
  8. void ShaderProgramUniformVariable::doSanityChecks() const
  9. {
  10. ASSERT(getLocation() != -1);
  11. ASSERT(GlStateMachineSingleton::get().getCurrentProgramGlId() ==
  12. getFatherSProg().getGlId());
  13. ASSERT(glGetUniformLocation(getFatherSProg().getGlId(),
  14. getName().c_str()) == getLocation());
  15. }
  16. //==============================================================================
  17. // set uniforms =
  18. //==============================================================================
  19. void ShaderProgramUniformVariable::set(const float f) const
  20. {
  21. doSanityChecks();
  22. ASSERT(getGlDataType() == GL_FLOAT);
  23. glUniform1f(getLocation(), f);
  24. }
  25. void ShaderProgramUniformVariable::set(const float f[], uint size) const
  26. {
  27. doSanityChecks();
  28. ASSERT(getGlDataType() == GL_FLOAT);
  29. if(size == 1)
  30. {
  31. glUniform1f(getLocation(), f[0]);
  32. }
  33. else
  34. {
  35. glUniform1fv(getLocation(), size, f);
  36. }
  37. }
  38. void ShaderProgramUniformVariable::set(const Vec2 v2[], uint size) const
  39. {
  40. doSanityChecks();
  41. ASSERT(getGlDataType() == GL_FLOAT_VEC2);
  42. if(size == 1)
  43. {
  44. glUniform2f(getLocation(), v2[0].x(), v2[0].y());
  45. }
  46. else
  47. {
  48. glUniform2fv(getLocation(), size, &(const_cast<Vec2&>(v2[0]))[0]);
  49. }
  50. }
  51. void ShaderProgramUniformVariable::set(const Vec3 v3[], uint size) const
  52. {
  53. doSanityChecks();
  54. ASSERT(getGlDataType() == GL_FLOAT_VEC3);
  55. if(size == 1)
  56. {
  57. glUniform3f(getLocation(), v3[0].x(), v3[0].y(), v3[0].z());
  58. }
  59. else
  60. {
  61. glUniform3fv(getLocation(), size, &(const_cast<Vec3&>(v3[0]))[0]);
  62. }
  63. }
  64. void ShaderProgramUniformVariable::set(const Vec4 v4[], uint size) const
  65. {
  66. doSanityChecks();
  67. ASSERT(getGlDataType() == GL_FLOAT_VEC4);
  68. glUniform4fv(getLocation(), size, &(const_cast<Vec4&>(v4[0]))[0]);
  69. }
  70. void ShaderProgramUniformVariable::set(const Mat3 m3[], uint size) const
  71. {
  72. doSanityChecks();
  73. ASSERT(getGlDataType() == GL_FLOAT_MAT3);
  74. glUniformMatrix3fv(getLocation(), size, true, &(m3[0])[0]);
  75. }
  76. void ShaderProgramUniformVariable::set(const Mat4 m4[], uint size) const
  77. {
  78. doSanityChecks();
  79. ASSERT(getGlDataType() == GL_FLOAT_MAT4);
  80. glUniformMatrix4fv(getLocation(), size, true, &(m4[0])[0]);
  81. }
  82. void ShaderProgramUniformVariable::set(const Texture& tex, uint texUnit) const
  83. {
  84. doSanityChecks();
  85. ASSERT(getGlDataType() == GL_SAMPLER_2D ||
  86. getGlDataType() == GL_SAMPLER_2D_SHADOW);
  87. tex.bind(texUnit);
  88. glUniform1i(getLocation(), texUnit);
  89. }