ShaderMatrix.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "client/rendering/ShaderMatrix.h"
  2. ShaderMatrix::ShaderMatrix(Shader* shader, MatrixStack<16>& stack, Matrix* view)
  3. : shader(shader), stack(stack), view(view) {
  4. }
  5. ShaderMatrix& ShaderMatrix::operator=(ShaderMatrix&& other) {
  6. shader = other.shader;
  7. stack = other.stack;
  8. view = other.view;
  9. return *this;
  10. }
  11. void ShaderMatrix::pop() {
  12. stack.pop();
  13. }
  14. void ShaderMatrix::push() {
  15. stack.push();
  16. }
  17. ShaderMatrix& ShaderMatrix::update() {
  18. shader->setMatrix("model", stack.peek().getValues());
  19. return *this;
  20. }
  21. ShaderMatrix& ShaderMatrix::update(const Vector3& pos,
  22. const Quaternion& rotation) {
  23. Vector3 right = rotation * Vector3(1.0f, 0.0f, 0.0f);
  24. Vector3 up = rotation * Vector3(0.0f, 1.0f, 0.0f);
  25. Vector3 back = rotation * Vector3(0.0f, 0.0f, -1.0f);
  26. view->set(0, Vector4(right[0], right[1], right[2], right.dot(-pos)));
  27. view->set(1, Vector4(up[0], up[1], up[2], up.dot(-pos)));
  28. view->set(2, Vector4(back[0], back[1], back[2], back.dot(-pos)));
  29. view->set(3, Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  30. shader->setMatrix("view", view->getValues());
  31. return *this;
  32. }
  33. ShaderMatrix& ShaderMatrix::scale(float sx, float sy, float sz) {
  34. stack.peek().scale(Vector3(sx, sy, sz));
  35. return *this;
  36. }
  37. ShaderMatrix& ShaderMatrix::scale(float s) {
  38. stack.peek().scale(s);
  39. return *this;
  40. }
  41. ShaderMatrix& ShaderMatrix::translate(float tx, float ty, float tz) {
  42. stack.peek().translate(Vector3(tx, ty, tz));
  43. return *this;
  44. }
  45. ShaderMatrix& ShaderMatrix::translateX(float tx) {
  46. stack.peek().translateX(tx);
  47. return *this;
  48. }
  49. ShaderMatrix& ShaderMatrix::translateY(float ty) {
  50. stack.peek().translateY(ty);
  51. return *this;
  52. }
  53. ShaderMatrix& ShaderMatrix::translateZ(float tz) {
  54. stack.peek().translateZ(tz);
  55. return *this;
  56. }
  57. ShaderMatrix& ShaderMatrix::translateTo(float tx, float ty, float tz) {
  58. stack.peek().translateTo(Vector3(tx, ty, tz));
  59. return *this;
  60. }
  61. ShaderMatrix& ShaderMatrix::rotateX(float degrees) {
  62. stack.peek().rotateX(degrees);
  63. return *this;
  64. }
  65. ShaderMatrix& ShaderMatrix::rotateY(float degrees) {
  66. stack.peek().rotateY(degrees);
  67. return *this;
  68. }
  69. ShaderMatrix& ShaderMatrix::rotateZ(float degrees) {
  70. stack.peek().rotateZ(degrees);
  71. return *this;
  72. }
  73. ShaderMatrix& ShaderMatrix::identity() {
  74. stack.peek().translateTo(Vector3());
  75. return *this;
  76. }