-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathmain.cpp
More file actions
286 lines (249 loc) · 9.51 KB
/
Copy pathmain.cpp
File metadata and controls
286 lines (249 loc) · 9.51 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <glm/glm.hpp>
#include <glm/ext.hpp>
#include "check_gl.hpp" // includes glad/glad.h
#include <GLFW/glfw3.h> // must be placed behind glad/glad.h
#include <stdexcept>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
constexpr float PI = 3.1415926535897f;
float DigToPI(float dig)
{
return dig / 360.f * 2 * PI;
}
struct Point
{
float x;
float y;
float z;
};
struct Color
{
float r;
float g;
float b;
};
struct Triangle
{
Point vertexes[3];
Color color;
};
static std::vector<Triangle> SpawnTaiji()
{
std::vector<Triangle> res;
// 底色
float radius = 0.75f;
for (int i = 0; i < 360; i++)
{
int ni = i + 1;
float angle = DigToPI(i);
float angleNext = DigToPI(ni);
if (i >= 0 && i < 90)
{
// 全白
Triangle t;
t.color = {1.0f, 1.0f, 1.0f};
t.vertexes[0] = { 0.0f, 0.0f, 0.0f };
t.vertexes[1] = { radius * sinf(angle), radius * cosf(angle), 0.0f };
t.vertexes[2] = { radius * sinf(angleNext), radius * cosf(angleNext), 0.0f };
res.push_back(t);
}
if (i >= 90 && i < 180)
{
float angleSemi = DigToPI(180.f - 2 * (180.f - i));
float angleSemiN = DigToPI(180.f - 2 * (180.f - ni));
float radiusSemi = radius / 2;
// 白底
{
Triangle t;
t.color = { 1.0f, 1.0f, 1.0f };
t.vertexes[0] = { 0.0f, 0.0f, 0.0f };
t.vertexes[1] = { radius * sinf(angle), radius * cosf(angle), 0.0f };
t.vertexes[2] = { radius * sinf(angleNext), radius * cosf(angleNext), 0.0f };
res.push_back(t);
}
// 红半圆
{
Triangle t;
t.color = { 1.0f, 0.0f, 0.0f };
t.vertexes[0] = { 0.0f, 0.0f, 0.0f };
t.vertexes[1] = { radiusSemi * sinf(angleSemi), radiusSemi * cosf(angleSemi) - radiusSemi, 0.0f };
t.vertexes[2] = { radiusSemi * sinf(angleSemiN), radiusSemi * cosf(angleSemiN) - radiusSemi, 0.0f };
res.push_back(t);
}
}
if (i >= 180 && i < 270)
{
// 全红
{
Triangle t;
t.color = { 1.0f, 0.0f, 0.0f };
t.vertexes[0] = { 0.0f, 0.0f, 0.0f };
t.vertexes[1] = { radius * sinf(angle), radius * cosf(angle), 0.0f };
t.vertexes[2] = { radius * sinf(angleNext), radius * cosf(angleNext), 0.0f };
res.push_back(t);
}
}
if (i >= 270 && i < 360)
{
float angleSemi = DigToPI(180.f - 2 * (360 - i) + 180.f);
float angleSemiN = DigToPI(180.f - 2 * (360 - ni) + 180.f);
float radiusSemi = radius / 2;
// 红底
{
Triangle t;
t.color = { 1.0f, 0.0f, 0.0f };
t.vertexes[0] = { 0.0f, 0.0f, 0.0f };
t.vertexes[1] = { radius * sinf(angle), radius * cosf(angle), 0.0f };
t.vertexes[2] = { radius * sinf(angleNext), radius * cosf(angleNext), 0.0f };
res.push_back(t);
}
// 白半圆
{
Triangle t;
t.color = { 1.0f, 1.0f, 1.0f };
t.vertexes[0] = { 0.0f, 0.0f, 0.0f };
t.vertexes[1] = { radiusSemi * sinf(angleSemi), radiusSemi * cosf(angleSemi) + radiusSemi, 0.0f };
t.vertexes[2] = { radiusSemi * sinf(angleSemiN), radiusSemi * cosf(angleSemiN) + radiusSemi, 0.0f };
res.push_back(t);
}
}
}
// 两个圆
float radiusSmall = radius / 4;
for (int i = 0; i < 360; i++)
{
int ni = i + 1;
float angle = DigToPI(i);
float angleNext = DigToPI(ni);
// 全红
{
Triangle t;
t.color = { 1.0f, 0.0f, 0.0f };
t.vertexes[0] = { 0.0f, radius / 2, 0.0f };
t.vertexes[1] = { radiusSmall * sinf(angle), radiusSmall * cosf(angle) + radius / 2, 0.0f };
t.vertexes[2] = { radiusSmall * sinf(angleNext), radiusSmall * cosf(angleNext) + radius / 2, 0.0f };
res.push_back(t);
}
// 全白
{
Triangle t;
t.color = { 1.0f, 1.0f, 1.0f };
t.vertexes[0] = { 0.0f, -radius / 2, 0.0f };
t.vertexes[1] = { radiusSmall * sinf(angle), radiusSmall * cosf(angle) - radius / 2, 0.0f };
t.vertexes[2] = { radiusSmall * sinf(angleNext), radiusSmall * cosf(angleNext) - radius / 2, 0.0f };
res.push_back(t);
}
}
return res;
}
static std::vector<Triangle> taiji = SpawnTaiji();
Point Rotate(const Point& in, Point o, float d)
{
float dPi = DigToPI(d);
Point r;
r.x = in.x * cosf(dPi) - in.y * sinf(dPi);
r.y = in.x * sinf(dPi) + in.y * cosf(dPi);
r.z = in.z;
return r;
}
Triangle Rotate(const Triangle& in, Point o, float d)
{
Triangle r;
r.color = in.color;
r.vertexes[0] = Rotate(in.vertexes[0], o, d);
r.vertexes[1] = Rotate(in.vertexes[1], o, d);
r.vertexes[2] = Rotate(in.vertexes[2], o, d);
return r;
}
int rot = 0;
static void render() {
glBegin(GL_TRIANGLES);
if (rot > 360)
{
rot = 0;
}
rot++;
for (const auto& triangle : taiji)
{
auto newTriangle = Rotate(triangle, Point{ 0, 0, 0 }, rot);
glColor3f(newTriangle.color.r, newTriangle.color.g, newTriangle.color.b);
glVertex3f(newTriangle.vertexes[0].x, newTriangle.vertexes[0].y, newTriangle.vertexes[0].z);
glVertex3f(newTriangle.vertexes[1].x, newTriangle.vertexes[1].y, newTriangle.vertexes[1].z);
glVertex3f(newTriangle.vertexes[2].x, newTriangle.vertexes[2].y, newTriangle.vertexes[2].z);
}
CHECK_GL(glEnd());
}
int main() {
if (!glfwInit()) {
const char *errmsg;
glfwGetError(&errmsg);
if (!errmsg) errmsg = "(no error)";
std::cerr << "failed to initialize GLFW: " << errmsg << '\n';
return -1;
}
// hint the version required: OpenGL 2.0
constexpr int version = 20;
glfwWindowHint(GLFW_OPENGL_API, GLFW_OPENGL_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, version / 10);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, version % 10);
if (version >= 33) {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
}
// Create window
GLFWwindow *window = glfwCreateWindow(640, 640, "Example", NULL, NULL);
if (!window) {
const char *errmsg;
glfwGetError(&errmsg);
if (!errmsg) errmsg = "(no error)";
std::cerr << "GLFW failed to create window: " << errmsg << '\n';
std::cerr << "==============================================\n";
if (!strcmp(errmsg, "X11: The DISPLAY environment variable is missing")) {
std::cerr << "You seems not running with graphic display\n";
} else if (!strcmp(errmsg, "WGL: The driver does not appear to support OpenGL")) {
std::cerr << "Please consider install an OpenGL driver, or use the mesa driver\n";
} else if (!strcmp(errmsg, "WGL: Failed to create OpenGL context")) {
std::cerr << "Your driver seems not supporting the required OpenGL version\n";
}
std::cerr << "- If you have a physical graphic card (e.g. NVIDIA), install it from your graphic card vendor official website: http://www.nvidia.com/Download/index.aspx\n";
std::cerr << "- If you are using Windows, download opengl32.dll from https://pan.baidu.com/s/1TZ6nVJC7DZIuUarZrGJYow?pwd=opgl and place it into the same directory as this executable file (alternatively you may download opengl32sw.dll from Internet and rename it to opengl32.dll to place into the same directory as this executable file)\n";
std::cerr << "- If you are using Linux or WSL1, install the mesa driver: https://ubuntuhandbook.org/index.php/2021/07/install-latest-mesa-ubuntu-20-04-21-04/";
std::cerr << "- If you use WSL2, install WSLg: https://learn.microsoft.com/zh-cn/windows/wsl/tutorials/gui-apps\n";
std::cerr << "- If you are using SSH remote server, try connect it using ssh -X <ip address>\n";
std::cerr << "- If you are using MacOS, you probably want to use Windows or Linux instead for better OpenGL support\n";
std::cerr << "- If you are using a Laptop with dual-cards, make sure you have switch to dedicated card (NVIDIA) instead of the integrated card (Intel)\n";
std::cerr << "==============================================\n";
#ifdef _WIN32
std::system("pause");
#endif
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Load glXXX function pointers
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
glfwTerminate();
std::cerr << "GLAD failed to load GL functions\n";
return -1;
}
std::cerr << "OpenGL version: " << glGetString(GL_VERSION) << '\n';
CHECK_GL(glEnable(GL_POINT_SMOOTH));
CHECK_GL(glEnable(GL_BLEND));
CHECK_GL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
CHECK_GL(glPointSize(64.0f));
// start main game loop
while (!glfwWindowShouldClose(window)) {
// render graphics
CHECK_GL(glClear(GL_COLOR_BUFFER_BIT));
render();
// refresh screen
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}