GLMat.cpp 2.5 KB

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