Debian Codegraph @

utils/fgpanel/GL_utils.cxx

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2016 Gaetan Allaert
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include <iostream>
5 #include <math.h>
6 #include <string.h>
7
8 using std::cerr;
9 using std::cout;
10 using std::endl;
11
12 #include "GL_utils.hxx"
13
14 const float PI (3.1415926535897932384626433832795);
15
16 GL_utils::GL_utils () {
17 Matrix Tmp;
18 memset (&Tmp, 0x0, sizeof (Matrix));
19 int i;
20 for (i = GL_UTILS_MODELVIEW; i < GL_UTILS_LAST; ++i) {
21 m_Current_Matrix_Mode = (GLenum_Mode) i;
22 m_Matrix[m_Current_Matrix_Mode].push (Tmp);
23 glLoadIdentity ();
24 }
25 m_Current_Matrix_Mode = GL_utils::GL_UTILS_UNSET;
26 }
27
28 GL_utils::~GL_utils () {}
29
30 GL_utils&
31 GL_utils::instance () {
32 static GL_utils* const The_Instance (new GL_utils);
33 return *The_Instance;
34 }
35
36 //
37 ///
38 /// \brief Load a shader, check for compile errors, print error messages to std error
39 /// \param type Type of shader (GL_VERTEX_SHADER or GL_FRAGMENT_SHADER)
40 /// \param shader_src Shader source string
41 /// \return A new shader object on success, 0 on failure
42 //
43 GLuint
44 GL_utils::load_shader (GLenum type, const char *shader_src) {
45 GLint compiled;
46
47 // Create the shader object
48 GLuint shader (glCreateShader (type));
49
50 if (shader == 0) {
51 cerr << "Error creating shader" << endl;
52 return 0;
53 }
54
55 // Load the shader source
56 glShaderSource (shader, 1, &shader_src, NULL);
57
58 // Compile the shader
59 glCompileShader (shader);
60
61 // Check the compile status
62 glGetShaderiv (shader, GL_COMPILE_STATUS, &compiled);
63
64 if (!compiled) {
65 GLint info_len (0);
66
67 glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &info_len);
68
69 if (info_len > 1) {
70 char* info_log ((char *) malloc (sizeof (char) * info_len));
71
72 glGetShaderInfoLog (shader, info_len, NULL, info_log);
73 cerr << "Error compiling shader:" << endl << info_log << endl;
74
75 free (info_log);
76 }
77
78 glDeleteShader (shader);
79 return 0;
80 }
81
82 return shader;
83 }
84
85 //
86 ///
87 /// \brief Load a vertex and fragment shader, create a program object, link program.
88 // Errors output to std error.
89 /// \param vertShaderSrc Vertex shader source code
90 /// \param fragShaderSrc Fragment shader source code
91 /// \return A new program object linked with the vertex/fragment shader pair, 0 on failure
92 //
93 GLuint
94 GL_utils::load_program (const char *vert_shader_src, const char *frag_shader_src) {
95 GLint linked;
96
97 // Load the vertex/fragment shaders
98 GLuint vertex_shader (load_shader (GL_VERTEX_SHADER, vert_shader_src));
99 if (vertex_shader == 0) {
100 cerr << "Error loading vertex shader" << endl;
101 return 0;
102 }
103
104 GLuint fragment_shader (load_shader (GL_FRAGMENT_SHADER, frag_shader_src));
105 if (fragment_shader == 0) {
106 cerr << "Error loading fragment shader" << endl;
107 glDeleteShader (vertex_shader);
108 return 0;
109 }
110
111 // Create the program object
112 GLuint program_object (glCreateProgram ());
113
114 if (program_object == 0) {
115 cerr << "Error creating program" << endl;
116 return 0;
117 }
118
119 glAttachShader (program_object, vertex_shader);
120 glAttachShader (program_object, fragment_shader);
121
122 // Link the program
123 glLinkProgram (program_object);
124
125 // Check the link status
126 glGetProgramiv (program_object, GL_LINK_STATUS, &linked);
127
128 if (!linked) {
129 GLint info_len (0);
130
131 glGetProgramiv (program_object, GL_INFO_LOG_LENGTH, &info_len);
132
133 if (info_len > 1) {
134 char* info_log ((char *) malloc (sizeof (char) * info_len));
135
136 glGetProgramInfoLog (program_object, info_len, NULL, info_log);
137 cerr << "Error linking program:" << endl << info_log << endl;
138
139 free (info_log);
140 }
141
142 glDeleteProgram (program_object);
143 return 0;
144 }
145
146 // Free up no longer needed shader resources
147 glDeleteShader (vertex_shader);
148 glDeleteShader (fragment_shader);
149
150 return program_object;
151 }
152
153 void
154 GL_utils::glMatrixMode (const GLenum_Mode mode) {
155 if (mode < GL_UTILS_LAST) {
156 m_Current_Matrix_Mode = mode;
157 }
158 }
159
160 void
161 GL_utils::glLoadIdentity () {
162 if (m_Current_Matrix_Mode < GL_UTILS_LAST) {
163 memset (&(m_Matrix[m_Current_Matrix_Mode].top ()), 0x0, sizeof (Matrix));
164 m_Matrix[m_Current_Matrix_Mode].top ().m[0][0] = 1.0f;
165 m_Matrix[m_Current_Matrix_Mode].top ().m[1][1] = 1.0f;
166 m_Matrix[m_Current_Matrix_Mode].top ().m[2][2] = 1.0f;
167 m_Matrix[m_Current_Matrix_Mode].top ().m[3][3] = 1.0f;
168 }
169 }
170
171 void
172 GL_utils::gluOrtho2D (const GLfloat left,
173 const GLfloat right,
174 const GLfloat bottom,
175 const GLfloat top) {
176 GL_utils::glOrtho (left, right, bottom, top, -1.0f, 1.0f);
177 }
178
179 void
180 GL_utils::glOrtho (const GLfloat left,
181 const GLfloat right,
182 const GLfloat bottom,
183 const GLfloat top,
184 const GLfloat nearVal,
185 const GLfloat farVal) {
186 const GLfloat Delta_X (right - left);
187 const GLfloat Delta_Y (top - bottom);
188 const GLfloat Delta_Z (farVal - nearVal);
189 Matrix Ortho;
190
191 if ((Delta_X == 0.0f) || (Delta_Y == 0.0f) || (Delta_Z == 0.0f) || (m_Current_Matrix_Mode < GL_UTILS_LAST)) {
192 return;
193 }
194
195 memset (&Ortho, 0x0, sizeof (Matrix));
196 Ortho.m[0][0] = 2.0f / Delta_X;
197 Ortho.m[3][0] = -(right + left) / Delta_X;
198 Ortho.m[1][1] = 2.0f / Delta_Y;
199 Ortho.m[3][1] = -(top + bottom) / Delta_Y;
200 Ortho.m[2][2] = -2.0f / Delta_Z;
201 Ortho.m[3][2] = -(nearVal + farVal) / Delta_Z;
202 Ortho.m[3][3] = 1.0f;
203
204 GL_utils::glMultMatrixf (Ortho.m);
205 }
206
207 void
208 GL_utils::glTranslatef (const GLfloat x, const GLfloat y, const GLfloat z) {
209 if (m_Current_Matrix_Mode < GL_UTILS_LAST) {
210 m_Matrix[m_Current_Matrix_Mode].top ().m[3][0] += (m_Matrix[m_Current_Matrix_Mode].top ().m[0][0] * x +
211 m_Matrix[m_Current_Matrix_Mode].top ().m[1][0] * y +
212 m_Matrix[m_Current_Matrix_Mode].top ().m[2][0] * z);
213
214 m_Matrix[m_Current_Matrix_Mode].top ().m[3][1] += (m_Matrix[m_Current_Matrix_Mode].top ().m[0][1] * x +
215 m_Matrix[m_Current_Matrix_Mode].top ().m[1][1] * y +
216 m_Matrix[m_Current_Matrix_Mode].top ().m[2][1] * z);
217
218 m_Matrix[m_Current_Matrix_Mode].top ().m[3][2] += (m_Matrix[m_Current_Matrix_Mode].top ().m[0][2] * x +
219 m_Matrix[m_Current_Matrix_Mode].top ().m[1][2] * y +
220 m_Matrix[m_Current_Matrix_Mode].top ().m[2][2] * z);
221
222 m_Matrix[m_Current_Matrix_Mode].top ().m[3][3] += (m_Matrix[m_Current_Matrix_Mode].top ().m[0][3] * x +
223 m_Matrix[m_Current_Matrix_Mode].top ().m[1][3] * y +
224 m_Matrix[m_Current_Matrix_Mode].top ().m[2][3] * z);
225 }
226 }
227
228 void
229 GL_utils::glRotatef (const GLfloat angle, const GLfloat x, const GLfloat y, const GLfloat z) {
230 const GLfloat xx (x * x);
231 const GLfloat yy (y * y);
232 const GLfloat zz (z * z);
233 const GLfloat Mag (sqrtf (xx + yy + zz));
234
235 if ((Mag > 0.0f) && (m_Current_Matrix_Mode < GL_UTILS_LAST)) {
236 const GLfloat Sin_Angle (sinf (-angle * PI / 180.0f));
237 const GLfloat Cos_Angle (cosf (angle * PI / 180.0f));
238 const GLfloat xy (x * y);
239 const GLfloat yz (y * z);
240 const GLfloat zx (z * x);
241 const GLfloat xs (x * Sin_Angle);
242 const GLfloat ys (y * Sin_Angle);
243 const GLfloat zs (z * Sin_Angle);
244 const GLfloat One_Minus_Cos (1.0f - Cos_Angle);
245 Matrix Rot;
246
247 // x /= mag;
248 // y /= mag;
249 // z /= mag;
250
251 Rot.m[0][0] = (One_Minus_Cos * xx) + Cos_Angle;
252 Rot.m[0][1] = (One_Minus_Cos * xy) - zs;
253 Rot.m[0][2] = (One_Minus_Cos * zx) + ys;
254 Rot.m[0][3] = 0.0f;
255
256 Rot.m[1][0] = (One_Minus_Cos * xy) + zs;
257 Rot.m[1][1] = (One_Minus_Cos * yy) + Cos_Angle;
258 Rot.m[1][2] = (One_Minus_Cos * yz) - xs;
259 Rot.m[1][3] = 0.0f;
260
261 Rot.m[2][0] = (One_Minus_Cos * zx) - ys;
262 Rot.m[2][1] = (One_Minus_Cos * yz) + xs;
263 Rot.m[2][2] = (One_Minus_Cos * zz) + Cos_Angle;
264 Rot.m[2][3] = 0.0f;
265
266 Rot.m[3][0] = 0.0f;
267 Rot.m[3][1] = 0.0f;
268 Rot.m[3][2] = 0.0f;
269 Rot.m[3][3] = 1.0f;
270
271 GL_utils::glMultMatrixf (Rot.m);
272 }
273 }
274
275 void
276 GL_utils::glScalef (GLfloat x, GLfloat y, GLfloat z) {
277 if (m_Current_Matrix_Mode < GL_UTILS_LAST) {
278 Matrix Scale;
279 Scale.m[0][0] = x;
280 Scale.m[0][1] = 0.0f;
281 Scale.m[0][2] = 0.0f;
282 Scale.m[0][3] = 0.0f;
283
284 Scale.m[1][0] = 0.0f;
285 Scale.m[1][1] = y;
286 Scale.m[1][2] = 0.0f;
287 Scale.m[1][3] = 0.0f;
288
289 Scale.m[2][0] = 0.0f;
290 Scale.m[2][1] = 0.0f;
291 Scale.m[2][2] = z;
292 Scale.m[2][3] = 0.0f;
293
294 Scale.m[3][0] = 0.0f;
295 Scale.m[3][1] = 0.0f;
296 Scale.m[3][2] = 0.0f;
297 Scale.m[3][3] = 1.0f;
298
299 GL_utils::glMultMatrixf (Scale.m);
300 }
301 }
302
303 void
304 GL_utils::glMultMatrixf (const GLfloat m[4][4]) {
305 Matrix Tmp;
306
307 for (int i = 0; i < 4; ++i) {
308 Tmp.m[i][0] = (m[i][0] * m_Matrix[m_Current_Matrix_Mode].top ().m[0][0] +
309 m[i][1] * m_Matrix[m_Current_Matrix_Mode].top ().m[1][0] +
310 m[i][2] * m_Matrix[m_Current_Matrix_Mode].top ().m[2][0] +
311 m[i][3] * m_Matrix[m_Current_Matrix_Mode].top ().m[3][0]);
312
313 Tmp.m[i][1] = (m[i][0] * m_Matrix[m_Current_Matrix_Mode].top ().m[0][1] +
314 m[i][1] * m_Matrix[m_Current_Matrix_Mode].top ().m[1][1] +
315 m[i][2] * m_Matrix[m_Current_Matrix_Mode].top ().m[2][1] +
316 m[i][3] * m_Matrix[m_Current_Matrix_Mode].top ().m[3][1]);
317
318 Tmp.m[i][2] = (m[i][0] * m_Matrix[m_Current_Matrix_Mode].top ().m[0][2] +
319 m[i][1] * m_Matrix[m_Current_Matrix_Mode].top ().m[1][2] +
320 m[i][2] * m_Matrix[m_Current_Matrix_Mode].top ().m[2][2] +
321 m[i][3] * m_Matrix[m_Current_Matrix_Mode].top ().m[3][2]);
322
323 Tmp.m[i][3] = (m[i][0] * m_Matrix[m_Current_Matrix_Mode].top ().m[0][3] +
324 m[i][1] * m_Matrix[m_Current_Matrix_Mode].top ().m[1][3] +
325 m[i][2] * m_Matrix[m_Current_Matrix_Mode].top ().m[2][3] +
326 m[i][3] * m_Matrix[m_Current_Matrix_Mode].top ().m[3][3]);
327 }
328 memcpy (&(m_Matrix[m_Current_Matrix_Mode].top ()), &Tmp, sizeof (Matrix));
329 }
330
331 void
332 GL_utils::glPushMatrix () {
333 if (m_Current_Matrix_Mode < GL_UTILS_LAST) {
334 m_Matrix[m_Current_Matrix_Mode].push (m_Matrix[m_Current_Matrix_Mode].top ());
335 }
336 }
337
338 void
339 GL_utils::glPopMatrix () {
340 if (m_Current_Matrix_Mode < GL_UTILS_LAST) {
341 m_Matrix[m_Current_Matrix_Mode].pop ();
342 }
343 }
344
345 GLfloat*
346 GL_utils::get_top_matrix (const GL_utils::GLenum_Mode mode) {
347 if (mode < GL_UTILS_LAST) {
348 return &(m_Matrix[mode].top ().m[0][0]);
349 } else {
350 return NULL;
351 }
352 }
353
354 void
355 GL_utils::Debug (const GL_utils::GLenum_Mode mode) const {
356 if (mode < GL_UTILS_LAST) {
357 for (int l = 0; l < 4; ++l) {
358 cout << " ";
359 for (int c = 0; c < 4; ++c) {
360 cout << m_Matrix[mode].top ().m[c][l] << " ";
361 }
362 cout << endl;
363 }
364 }
365 }