forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglcompat.cpp
More file actions
199 lines (175 loc) · 4.76 KB
/
glcompat.cpp
File metadata and controls
199 lines (175 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// glcompat.h
//
// Copyright (C) 2020, the Celestia Development Team
//
// OpenGL 2.1 compatibility layer for OpenGL ES 2.0
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <cassert>
#include <cstring> // for memcpy
#include "glcompat.h"
constexpr const int MODELVIEW_STACK_DEPTH = 8;
constexpr const int PROJECTION_STACK_DEPTH = 2;
static float g_modelViewStack[MODELVIEW_STACK_DEPTH][16];
static float g_projectionStack[PROJECTION_STACK_DEPTH][16];
static size_t g_modelViewPosition = 0;
static size_t g_projectionPosition = 0;
static int g_matrixMode = GL_MODELVIEW;
void
initGLCompat() noexcept
{
g_matrixMode = GL_PROJECTION;
for (size_t i = 0; i < PROJECTION_STACK_DEPTH; i++)
{
g_projectionPosition = i;
glesLoadIdentity();
}
g_projectionPosition = 0;
g_matrixMode = GL_MODELVIEW;
for (size_t i = 0; i < MODELVIEW_STACK_DEPTH; i++)
{
g_modelViewPosition = i;
glesLoadIdentity();
}
g_modelViewPosition = 0;
}
void
glesMatrixMode(int _g_matrixMode) noexcept
{
g_matrixMode = _g_matrixMode;
}
void
glesPushMatrix() noexcept
{
switch (g_matrixMode)
{
case GL_MODELVIEW:
if (g_modelViewPosition < MODELVIEW_STACK_DEPTH - 1)
g_modelViewPosition++;
else
assert(0 && "Matrix stack overflow");
break;
case GL_PROJECTION:
if (g_projectionPosition < PROJECTION_STACK_DEPTH - 1)
g_projectionPosition++;
else
assert(0 && "Matrix stack overflow");
break;
default:
assert(0 && "Incorrect matrix g_matrixMode");
}
}
void
glesPopMatrix() noexcept
{
switch (g_matrixMode)
{
case GL_MODELVIEW:
if (g_modelViewPosition > 0)
g_modelViewPosition--;
else
assert(0 && "Matrix stack underflow");
break;
case GL_PROJECTION:
if (g_projectionPosition > 0)
g_projectionPosition--;
else
assert(0 && "Matrix stack underflow");
break;
default:
assert(0 && "Incorrect matrix g_matrixMode");
}
}
void
glesLoadMatrix(const float *data) noexcept
{
glesLoadMatrix(g_matrixMode, data);
}
void
glesLoadMatrix(int mode, const float *data) noexcept
{
switch (mode)
{
case GL_MODELVIEW:
memcpy(g_modelViewStack[g_modelViewPosition], data, sizeof(float)*16);
break;
case GL_PROJECTION:
memcpy(g_projectionStack[g_projectionPosition], data, sizeof(float)*16);
break;
default:
assert("Incorrect matrix g_matrixMode");
}
}
float*
glesGetMatrix(int mode) noexcept
{
switch (mode)
{
case GL_MODELVIEW:
assert(g_modelViewPosition < MODELVIEW_STACK_DEPTH);
if (g_modelViewPosition < MODELVIEW_STACK_DEPTH)
return g_modelViewStack[g_modelViewPosition];
break;
case GL_PROJECTION:
assert(g_projectionPosition < PROJECTION_STACK_DEPTH);
if (g_projectionPosition < PROJECTION_STACK_DEPTH)
return g_projectionStack[g_projectionPosition];
break;
default:
assert(0 && "Incorrect matrix mode");
}
return nullptr;
}
void
glesGetMatrix(float *data) noexcept
{
glesGetMatrix(g_matrixMode, data);
}
void
glesGetMatrix(int mode, float *data) noexcept
{
switch (mode)
{
case GL_MODELVIEW:
assert(g_modelViewPosition < MODELVIEW_STACK_DEPTH);
if (g_modelViewPosition < MODELVIEW_STACK_DEPTH)
memcpy(data, g_modelViewStack[g_modelViewPosition], sizeof(float)*16);
break;
case GL_PROJECTION:
assert(g_projectionPosition < PROJECTION_STACK_DEPTH);
if (g_projectionPosition < PROJECTION_STACK_DEPTH)
memcpy(data, g_projectionStack[g_projectionPosition], sizeof(float)*16);
break;
default:
assert(0 && "Incorrect matrix mode");
}
}
void
glesLoadIdentity() noexcept
{
float *p = nullptr;
switch (g_matrixMode)
{
case GL_MODELVIEW:
assert(g_modelViewPosition < MODELVIEW_STACK_DEPTH);
if (g_modelViewPosition < MODELVIEW_STACK_DEPTH)
p = g_modelViewStack[g_modelViewPosition];
break;
case GL_PROJECTION:
assert(g_projectionPosition < PROJECTION_STACK_DEPTH);
if (g_projectionPosition < PROJECTION_STACK_DEPTH)
p = g_projectionStack[g_projectionPosition];
break;
default:
assert(0 && "Incorrect matrix mode");
}
if (p != nullptr)
{
for (size_t i = 0; i < 4; i++)
for (size_t j = 0; j < 4; j++)
p[i * 4 + j] = i == j ? 1.0f : 0.0f;
}
}