Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

作业01 #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,51 @@
#include <cstring>
#include <cstdlib>

#include <array>

static void draw_logo_element(float pi, float radius, float inner_radius, float angle_correction, std::array<float, 3> center, std::array<float, 3> color) {
constexpr int n = 100;
glBegin(GL_TRIANGLES);
for (int i = 0; i < n; ++i) {
float angle = i / (float)n * pi * 2;
float next_angle = (i + 1) / (float)n * pi * 2;
if (angle > 5.0f / 6 * pi && angle < 7.0f / 6 * pi)
continue;
angle += angle_correction;
next_angle += angle_correction;

glColor3f(color[0], color[1], color[2]);
glVertex3f(center[0] + inner_radius * sinf(angle), center[1] + inner_radius * cosf(angle), 0.0f);
glVertex3f(center[0] + radius * sinf(angle), center[1] + radius * cosf(angle), 0.0f);
glVertex3f(center[0] + radius * sinf(next_angle), center[1] + radius * cosf(next_angle), 0.0f);

glVertex3f(center[0] + inner_radius * sinf(angle), center[1] + inner_radius * cosf(angle), 0.0f);
glVertex3f(center[0] + inner_radius * sinf(next_angle), center[1] + inner_radius * cosf(next_angle), 0.0f);
glVertex3f(center[0] + radius * sinf(next_angle), center[1] + radius * cosf(next_angle), 0.0f);
}
CHECK_GL(glEnd());
}

static void draw_logo() {
constexpr float pi = 3.1415926535897f;
float radius = 0.25f;
float inner_radius = 0.125f;
// edge length of the regular triangle with the three circle centers as vertices
float edge = 2 * radius + 0.065f;

float g_angle_corr = 8.0f / 6 * pi;
float g_y = -edge* sinf(pi / 3) * 1.0f / 3;
float g_x = edge / -2.0f;
float r_y = 2.0f * -g_y;
float b_x = -g_x;

draw_logo_element(pi, radius, inner_radius, 0.0f, { 0.0f, r_y, 0.0f }, { 1.0f, 0.0f, 0.0f });
draw_logo_element(pi, radius, inner_radius, g_angle_corr, { g_x, g_y, 0.0f }, { 0.0f, 1.0f, 0.0f });
draw_logo_element(pi, radius, inner_radius, pi, { b_x, g_y, 0.0f }, { 0.0f, 0.0f, 1.0f });
}

static void render() {
#if 0
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
Expand All @@ -16,6 +60,7 @@ static void render() {
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
CHECK_GL(glEnd());
#endif
/* glBegin(GL_TRIANGLES); */
/* constexpr int n = 100; */
/* constexpr float pi = 3.1415926535897f; */
Expand All @@ -37,6 +82,8 @@ static void render() {
/* glVertex3f(radius * sinf(angle_next), radius * cosf(angle_next), 0.0f); */
/* } */
/* CHECK_GL(glEnd()); */

draw_logo();
}

int main() {
Expand Down