1 /*
2  *  Copyright (C) 2005-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include <stack>
12 
13 #include "system_gl.h"
14 
15 class TransformMatrix;
16 
17 class CMatrixGL
18 {
19 public:
20   CMatrixGL() = default;
21 
CMatrixGL(GLfloat x0,GLfloat x1,GLfloat x2,GLfloat x3,GLfloat x4,GLfloat x5,GLfloat x6,GLfloat x7,GLfloat x8,GLfloat x9,GLfloat x10,GLfloat x11,GLfloat x12,GLfloat x13,GLfloat x14,GLfloat x15)22   constexpr CMatrixGL(GLfloat x0, GLfloat x1, GLfloat x2, GLfloat x3,
23                       GLfloat x4, GLfloat x5, GLfloat x6, GLfloat x7,
24                       GLfloat x8, GLfloat x9, GLfloat x10, GLfloat x11,
25                       GLfloat x12, GLfloat x13, GLfloat x14, GLfloat x15)
26     :m_pMatrix{x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15} {}
27 
28   CMatrixGL(const TransformMatrix &src) noexcept;
29 
30   operator const float*() const                { return m_pMatrix; }
31 
32   void LoadIdentity();
33   void Ortho(GLfloat l, GLfloat r, GLfloat b, GLfloat t, GLfloat n, GLfloat f);
34   void Ortho2D(GLfloat l, GLfloat r, GLfloat b, GLfloat t);
35   void Frustum(GLfloat l, GLfloat r, GLfloat b, GLfloat t, GLfloat n, GLfloat f);
36   void Translatef(GLfloat x, GLfloat y, GLfloat z);
37   void Scalef(GLfloat x, GLfloat y, GLfloat z);
38   void Rotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
39   void MultMatrixf(const CMatrixGL &matrix) noexcept;
40   void LookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez, GLfloat centerx, GLfloat centery, GLfloat centerz, GLfloat upx, GLfloat upy, GLfloat upz);
41 
42   static bool Project(GLfloat objx, GLfloat objy, GLfloat objz, const GLfloat modelMatrix[16], const GLfloat projMatrix[16], const GLint viewport[4], GLfloat* winx, GLfloat* winy, GLfloat* winz);
43 
44 private:
45   /* alignas(16) allows better SIMD optimizations (e.g. SSE2 benefits
46      a lot from this) */
47   alignas(16) GLfloat m_pMatrix[16];
48 };
49 
50 class CMatrixGLStack
51 {
52 public:
Push()53   void Push()
54   {
55     m_stack.push(m_current);
56   }
57 
Clear()58   void Clear()
59   {
60     m_stack = std::stack<CMatrixGL>();
61   }
62 
Pop()63   void Pop()
64   {
65     if(!m_stack.empty())
66     {
67       m_current = m_stack.top();
68       m_stack.pop();
69     }
70   }
71 
72   void Load();
PopLoad()73   void PopLoad() { Pop(); Load(); }
74 
Get()75   CMatrixGL& Get()        { return m_current; }
76   CMatrixGL* operator->() { return &m_current; }
77 
78 private:
79   std::stack<CMatrixGL> m_stack;
80   CMatrixGL             m_current;
81 };
82 
83 extern CMatrixGLStack glMatrixModview;
84 extern CMatrixGLStack glMatrixProject;
85 extern CMatrixGLStack glMatrixTexture;
86