From ab9a3cdd3fc7e3ce2cde6d64b59d25981bfa70d1 Mon Sep 17 00:00:00 2001 From: adonis747 Date: Wed, 13 Sep 2023 09:07:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=94=BB=E4=B8=89=E8=A7=92=E5=BD=A2?= =?UTF-8?q?=EF=BC=8COpenCV=E5=9B=BE=E6=A0=87=EF=BC=8C=E5=9C=86=E5=BD=A2?= =?UTF-8?q?=E5=9B=BE=E8=85=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.cpp | 95 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 29 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 38751f0..1fbe6a1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,8 +6,61 @@ #include #include #include +//using namespace std; -static void render() { +static void render(float r, float g, float b, float x, float y, float start, float end) { + glBegin(GL_TRIANGLES); + constexpr int n = 1000; + constexpr float pi = 3.1415926535897f; + float radius = 0.4f;//外半径 + float inner_radius = 0.15f;//内半径 + float limit = n * end; + for (float i = n * start; i < limit; i++) { + float angle = i / (float)n * pi * 2; + float angle_next = (i + 1) / (float)n * pi * 2; + glColor3f(r, g, b); + glVertex3f(radius * sinf(angle) + x, radius * cosf(angle) + y, 0.0f); + glVertex3f(radius * sinf(angle_next) + x, radius * cosf(angle_next) + y, 0.0f); + glVertex3f(inner_radius * sinf(angle) + x, inner_radius * cosf(angle) + y, 0.0f); + + glVertex3f(inner_radius * sinf(angle_next) + x, inner_radius * cosf(angle_next) + y, 0.0f); + glVertex3f(inner_radius * sinf(angle) + x, inner_radius * cosf(angle) + y, 0.0f); + glVertex3f(radius * sinf(angle_next) + x, radius * cosf(angle_next) + y, 0.0f); + } + CHECK_GL(glEnd()); +} + + //--圆图腾--花里胡哨风~画三角形点数量不是3n时,会接到下个循环;eg:123 412 341 234 ... +static void render_round(){ + glBegin(GL_TRIANGLES); + constexpr int n = 1000;//修改n获得不同图案 + 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; + glColor3f(1.0f, 1.0f, 1.0f);//白 + glVertex3f(0.0f, 0.0f, 0.0f); + glColor3f(1.0f, 0.0f, 1.0f);//紫 + glVertex3f(radius * sinf(angle), radius * cosf(angle), 0.0f); + glColor3f(1.0f, 1.0f, 0.0f);//黄 + glVertex3f(radius * sinf(angle_next), radius * cosf(angle_next), 0.0f); + glColor3f(0.0f, 1.0f, 1.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()); +} + //---彩色三角---- +static void render_triangle() +{ glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.5f, 0.0f); @@ -16,32 +69,11 @@ static void render() { glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(0.5f, -0.5f, 0.0f); 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; + if (!glfwInit()) {//初始化 + const char* errmsg; glfwGetError(&errmsg); if (!errmsg) errmsg = "(no error)"; std::cerr << "failed to initialize GLFW: " << errmsg << '\n'; @@ -61,18 +93,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"; @@ -108,7 +142,10 @@ int main() { while (!glfwWindowShouldClose(window)) { // render graphics CHECK_GL(glClear(GL_COLOR_BUFFER_BIT)); - render(); + //render_round();//画圆 + render(1.0f, 0.0f, 0.0f, 0.0f, 0.4f, (float)7 / 12, (float)17 / 12); + render(0.0f, 1.0f, 0.0f, -0.46f, -0.4f, 0.25f, (float)13 / 12); + render(0.0f, 0.0f, 1.0f, 0.46f, -0.4f, (float)1/12, (float)11/12); // refresh screen glfwSwapBuffers(window); glfwPollEvents(); From 99e25e5a3488180cc6d3256465fe68dba14599a5 Mon Sep 17 00:00:00 2001 From: adonis747 Date: Wed, 13 Sep 2023 09:22:21 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A1=A5=E4=BA=86=E4=B8=80=E4=B8=8B?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 1fbe6a1..d740c65 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include //using namespace std; +//参数:颜色,圆心坐标,起始,结束 static void render(float r, float g, float b, float x, float y, float start, float end) { glBegin(GL_TRIANGLES); constexpr int n = 1000; @@ -142,7 +143,7 @@ int main() { while (!glfwWindowShouldClose(window)) { // render graphics CHECK_GL(glClear(GL_COLOR_BUFFER_BIT)); - //render_round();//画圆 + render_round();//画圆 render(1.0f, 0.0f, 0.0f, 0.0f, 0.4f, (float)7 / 12, (float)17 / 12); render(0.0f, 1.0f, 0.0f, -0.46f, -0.4f, 0.25f, (float)13 / 12); render(0.0f, 0.0f, 1.0f, 0.46f, -0.4f, (float)1/12, (float)11/12);