GLMatrix.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  4. * Copyright (c) 2021, Jelle Raaijmakers <jelle@gmta.nl>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "AK/Array.h"
  9. #include "GL/gl.h"
  10. #include "GLContext.h"
  11. extern GL::GLContext* g_gl_context;
  12. void glMatrixMode(GLenum mode)
  13. {
  14. g_gl_context->gl_matrix_mode(mode);
  15. }
  16. /*
  17. * Push the current matrix (based on the current matrix mode)
  18. * to its' corresponding matrix stack in the current OpenGL
  19. * state context
  20. */
  21. void glPushMatrix()
  22. {
  23. g_gl_context->gl_push_matrix();
  24. }
  25. /*
  26. * Pop a matrix from the corresponding matrix stack into the
  27. * corresponding matrix in the state based on the current
  28. * matrix mode
  29. */
  30. void glPopMatrix()
  31. {
  32. g_gl_context->gl_pop_matrix();
  33. }
  34. /*
  35. * Transposes input matrices (column-major) to our Matrix (row-major).
  36. */
  37. template<typename I, typename O>
  38. static constexpr Matrix4x4<O> transpose_input_matrix(I const* matrix)
  39. {
  40. if constexpr (IsSame<I, O>) {
  41. // clang-format off
  42. return {
  43. matrix[0], matrix[4], matrix[8], matrix[12],
  44. matrix[1], matrix[5], matrix[9], matrix[13],
  45. matrix[2], matrix[6], matrix[10], matrix[14],
  46. matrix[3], matrix[7], matrix[11], matrix[15],
  47. };
  48. // clang-format on
  49. }
  50. Array<O, 16> elements;
  51. for (size_t i = 0; i < 16; ++i)
  52. elements[i] = static_cast<O>(matrix[i]);
  53. // clang-format off
  54. return {
  55. elements[0], elements[4], elements[8], elements[12],
  56. elements[1], elements[5], elements[9], elements[13],
  57. elements[2], elements[6], elements[10], elements[14],
  58. elements[3], elements[7], elements[11], elements[15],
  59. };
  60. // clang-format on
  61. }
  62. void glMultMatrixd(GLdouble const* matrix)
  63. {
  64. g_gl_context->gl_mult_matrix(transpose_input_matrix<double, float>(matrix));
  65. }
  66. void glMultMatrixf(GLfloat const* matrix)
  67. {
  68. g_gl_context->gl_mult_matrix(transpose_input_matrix<float, float>(matrix));
  69. }
  70. void glLoadMatrixd(GLdouble const* matrix)
  71. {
  72. g_gl_context->gl_load_matrix(transpose_input_matrix<double, float>(matrix));
  73. }
  74. void glLoadMatrixf(GLfloat const* matrix)
  75. {
  76. g_gl_context->gl_load_matrix(transpose_input_matrix<float, float>(matrix));
  77. }
  78. void glLoadIdentity()
  79. {
  80. g_gl_context->gl_load_identity();
  81. }
  82. /**
  83. * Create a viewing frustum (a.k.a a "Perspective Matrix") in the current matrix. This
  84. * is usually done to the projection matrix. The current matrix is then multiplied
  85. * by this viewing frustum matrix.
  86. *
  87. * https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml
  88. *
  89. *
  90. * FIXME: We need to check for some values that could result in a division by zero
  91. */
  92. void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal)
  93. {
  94. g_gl_context->gl_frustum(left, right, bottom, top, nearVal, farVal);
  95. }
  96. void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal)
  97. {
  98. g_gl_context->gl_ortho(left, right, bottom, top, nearVal, farVal);
  99. }
  100. void glRotated(GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
  101. {
  102. g_gl_context->gl_rotate(angle, x, y, z);
  103. }
  104. void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
  105. {
  106. g_gl_context->gl_rotate(angle, x, y, z);
  107. }
  108. void glScaled(GLdouble x, GLdouble y, GLdouble z)
  109. {
  110. g_gl_context->gl_scale(x, y, z);
  111. }
  112. void glScalef(GLfloat x, GLfloat y, GLfloat z)
  113. {
  114. g_gl_context->gl_scale(x, y, z);
  115. }
  116. void glTranslated(GLdouble x, GLdouble y, GLdouble z)
  117. {
  118. g_gl_context->gl_translate(x, y, z);
  119. }
  120. void glTranslatef(GLfloat x, GLfloat y, GLfloat z)
  121. {
  122. g_gl_context->gl_translate(x, y, z);
  123. }