diff --git a/src/main.cpp b/src/main.cpp index 38751f0..cbbaf66 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,42 +6,69 @@ #include #include #include +#include + +void plotCircle(std::vector center, float radius, std::vector color, float depth) +{ + constexpr int iter = 300; + constexpr float pi = 3.1415926535897f; + glColor3f(color.at(0), color.at(1), color.at(2)); + for (int i = 0; i < iter; i++) { + float angle = i / (float)iter * pi * 2; + float angle_next = (i + 1) / (float)iter * pi * 2; + glVertex3f(center.at(0), center.at(1), depth); + glVertex3f(center.at(0) + radius * sinf(angle), center.at(1) + radius * cosf(angle), depth); + glVertex3f(center.at(0) + radius * sinf(angle_next), center.at(1) + radius * cosf(angle_next), depth); + } +} static void render() { glBegin(GL_TRIANGLES); - glColor3f(1.0f, 0.0f, 0.0f); - glVertex3f(0.0f, 0.5f, 0.0f); - glColor3f(0.0f, 1.0f, 0.0f); - glVertex3f(-0.5f, -0.5f, 0.0f); - glColor3f(0.0f, 0.0f, 1.0f); - glVertex3f(0.5f, -0.5f, 0.0f); + constexpr int n = 300; + constexpr float pi = 3.1415926535897f; + float radius = 0.3f; + float inner_radius = 0.15f; + static int x = 0; + std::vector green_color{.0f, 1.0f, .0f}; + std::vector red_color{1.0f, .0f, .0f}; + std::vector blue_color{ .0f, .0f, 1.0f }; + std::vector black_color{ .0f, .0f, 0.0f }; + std::vector green_circle_center{-0.4f, -0.4f, 1.0f}; + std::vector red_circle_center{0.0f, 0.4f, 1.0f}; + std::vector blue_circle_center{0.4f, -0.4f, 1.0f}; + //Circles + plotCircle(green_circle_center, radius, green_color, 0.9f); + plotCircle(red_circle_center, radius, red_color, 0.9f); + for (int i = 0; i < n; i++) { + float angle = i / (float)n * pi * 2; + float angle_next = (i + 1) / (float)n * pi * 2; + if (angle > pi / 6 && angle < pi * 11 / 6) + { + glColor3f(0.0f, 0.0f, 1.0f); + glVertex3f(blue_circle_center.at(0), blue_circle_center.at(1), 0.5f); + glVertex3f(blue_circle_center.at(0) + radius * sinf(angle), blue_circle_center.at(1) + radius * cosf(angle), 0.5f); + glVertex3f(blue_circle_center.at(0) + radius * sinf(angle_next), blue_circle_center.at(1) + radius * cosf(angle_next), 0.5f); + } + else + { + continue; + } + } + plotCircle(green_circle_center, inner_radius, black_color, -0.1f); + plotCircle(red_circle_center, inner_radius, black_color, -0.1f); + plotCircle(blue_circle_center, inner_radius, black_color, -0.1f); + //Black Triangle + glColor3f(0.0f, 0.0f, 0.0f); + glVertex3f(green_circle_center.at(0), green_circle_center.at(1), 0.6f); + glVertex3f(red_circle_center.at(0), red_circle_center.at(1), 0.6f); + glVertex3f(blue_circle_center.at(0), blue_circle_center.at(1), 0.6f); + CHECK_GL(glEnd()); - /* glBegin(GL_TRIANGLES); */ - /* constexpr int n = 100; */ - /* constexpr float pi = 3.1415926535897f; */ - /* float radius = 0.5f; */ - /* float inner_radius = 0.25f; */ - /* static int x = 0; */ - /* x++; */ - /* if (x > n) */ - /* x -= n; */ - /* for (int i = 0; i < x; i++) { */ - /* float angle = i / (float)n * pi * 2; */ - /* float angle_next = (i + 1) / (float)n * pi * 2; */ - /* glVertex3f(0.0f, 0.0f, 0.0f); */ - /* glVertex3f(radius * sinf(angle), radius * cosf(angle), 0.0f); */ - /* glVertex3f(radius * sinf(angle_next), radius * cosf(angle_next), 0.0f); */ - /* glVertex3f(inner_radius * sinf(angle), inner_radius * cosf(angle), 0.0f); */ - /* glVertex3f(inner_radius * sinf(angle_next), inner_radius * cosf(angle_next), 0.0f); */ - /* glVertex3f(inner_radius * sinf(angle), inner_radius * cosf(angle), 0.0f); */ - /* glVertex3f(radius * sinf(angle_next), radius * cosf(angle_next), 0.0f); */ - /* } */ - /* CHECK_GL(glEnd()); */ } int main() { if (!glfwInit()) { - const char *errmsg; + const char* errmsg; glfwGetError(&errmsg); if (!errmsg) errmsg = "(no error)"; std::cerr << "failed to initialize GLFW: " << errmsg << '\n'; @@ -61,18 +88,20 @@ int main() { } // Create window - GLFWwindow *window = glfwCreateWindow(640, 640, "Example", NULL, NULL); + GLFWwindow* window = glfwCreateWindow(640, 640, "Example", NULL, NULL); if (!window) { - const char *errmsg; + 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")) { + } + 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")) { + } + 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"; @@ -103,11 +132,15 @@ int main() { CHECK_GL(glEnable(GL_BLEND)); CHECK_GL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); CHECK_GL(glPointSize(64.0f)); + CHECK_GL(glEnable(GL_DEPTH_TEST)); + CHECK_GL(glDepthMask(GL_TRUE)); // start main game loop while (!glfwWindowShouldClose(window)) { // render graphics CHECK_GL(glClear(GL_COLOR_BUFFER_BIT)); + CHECK_GL(glClearDepth(1.0f)); + CHECK_GL(glClear(GL_DEPTH_BUFFER_BIT)); render(); // refresh screen glfwSwapBuffers(window);