From 2e92a24302d65e15616e2d76c3a516ead73c597d Mon Sep 17 00:00:00 2001 From: Yongning Xu <809257291@qq.com> Date: Thu, 24 Aug 2023 16:46:22 +0800 Subject: [PATCH 1/4] hw01 render OpenCV icon --- src/main.cpp | 98 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 70 insertions(+), 28 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 38751f0..854731d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,35 +8,77 @@ #include static void render() { + ////render triangle + //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); + //CHECK_GL(glEnd()); + + //// render concentric circle + //glBegin(GL_TRIANGLES); + //constexpr int n = 5000; + //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()); + + // render OpenCV icon 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); - 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()); */ + constexpr int n = 5000; + constexpr float pi = 3.1415926535897f; + float radius = 0.3f; + float inner_radius = 0.12f; + float x = 0, y = 0; + int clear_start = 0, clear_end = 0; + for (int count = 0; count < 3; count++) { + switch (count) { + case 0: x = 0.00f , y = 0.40f , clear_start = n * 150 / 360, clear_end = n * 210 / 360; break; + case 1: x = -0.40f * cosf(2 * pi * 30 / 360), y = -0.40f * sinf(2 * pi * 30 / 360), clear_start = n * 30 / 360, clear_end = n * 90 / 360; break; + case 2: x = 0.40f * cosf(2 * pi * 30 / 360), y = -0.40f * sinf(2 * pi * 30 / 360), clear_start = n * 330 / 360, clear_end = n * 30 / 360; break; + } + for (int i = 0; i < n; i++) { + float angle = i / (float)n * pi * 2; + float angle_next = (i + 1) / (float)n * pi * 2; + + switch (count) { + case 0: glColor3f(1.0f, 0.0f, 0.0f); break; + case 1: glColor3f(0.0f, 1.0f, 0.0f); break; + case 2: glColor3f(0.0f, 0.0f, 1.0f); break; + } + + if ((clear_start <= clear_end) ? (i > clear_start && i < clear_end) : (i > clear_start || i < clear_end)) + glColor3f(0.0f, 0.0f, 0.0f); + 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()); } int main() { From 2c28b2fa6520e93f7484bf23b4c8f55a83b2e59f Mon Sep 17 00:00:00 2001 From: Yongning Xu <809257291@qq.com> Date: Thu, 24 Aug 2023 16:58:00 +0800 Subject: [PATCH 2/4] hw01 modify const int to float --- src/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 854731d..fc8589f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -53,9 +53,9 @@ static void render() { int clear_start = 0, clear_end = 0; for (int count = 0; count < 3; count++) { switch (count) { - case 0: x = 0.00f , y = 0.40f , clear_start = n * 150 / 360, clear_end = n * 210 / 360; break; - case 1: x = -0.40f * cosf(2 * pi * 30 / 360), y = -0.40f * sinf(2 * pi * 30 / 360), clear_start = n * 30 / 360, clear_end = n * 90 / 360; break; - case 2: x = 0.40f * cosf(2 * pi * 30 / 360), y = -0.40f * sinf(2 * pi * 30 / 360), clear_start = n * 330 / 360, clear_end = n * 30 / 360; break; + case 0: x = 0.0f , y = 0.4f , clear_start = 150.0f / 360.0f * n, clear_end = 210.0f / 360.0f * n; break; + case 1: x = -0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), clear_start = 30.0f / 360.0f * n , clear_end = 90.0f / 360.0f * n; break; + case 2: x = 0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), clear_start = 330.0f / 360.0f * n, clear_end = 30.0f / 360.0f * n; break; } for (int i = 0; i < n; i++) { float angle = i / (float)n * pi * 2; From 49ff1279f43803283c776e7673fdda90ed276fa1 Mon Sep 17 00:00:00 2001 From: Yongning Xu <809257291@qq.com> Date: Thu, 24 Aug 2023 17:53:08 +0800 Subject: [PATCH 3/4] hw01 rotational OpenCV v1.0 --- src/main.cpp | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index fc8589f..4dba79b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,14 +50,35 @@ static void render() { float radius = 0.3f; float inner_radius = 0.12f; float x = 0, y = 0; - int clear_start = 0, clear_end = 0; + //int clear_start = 0, clear_end = 0; + //glm::vec2 start = glm::vec2(0, n / 2); + //glm::vec2 end = glm::vec2(n / 2, n); + //float start[2] = { 0, n / 2 }, end[2] = { n / 2, n }; + float start = 0, end = 0; + + static float bound = 0; + bound++; + if (bound > n) + bound -= n; + for (int count = 0; count < 3; count++) { - switch (count) { + /*switch (count) { case 0: x = 0.0f , y = 0.4f , clear_start = 150.0f / 360.0f * n, clear_end = 210.0f / 360.0f * n; break; case 1: x = -0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), clear_start = 30.0f / 360.0f * n , clear_end = 90.0f / 360.0f * n; break; case 2: x = 0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), clear_start = 330.0f / 360.0f * n, clear_end = 30.0f / 360.0f * n; break; + }*/ + /*switch (count) { + case 0: x = 0.0f , y = 0.4f , end.x = 150.0f / 360.0f * n, end.y = 360.0f / 360.0f * n, start.x = 0.0f / 360.0f * n , start.y = 210.0f / 360.0f * n; break; + case 1: x = -0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), end.x = 30.0f / 360.0f * n , end.y = 360.0f / 360.0f * n, start.x = 0.0f / 360.0f * n , start.y = 90.0f / 360.0f * n; break; + case 2: x = 0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), end.x = 180.0f / 360.0f * n, end.y = 330.0f / 360.0f * n, start.x = 30.0f / 360.0f * n, start.y = 180.0f / 360.0f * n; break; + }*/ + switch (count) { + case 0: x = 0.0f , y = 0.4f , end = 150.0f / 360.0f * n, start = 210.0f / 360.0f * n; break; + case 1: x = -0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), end = 30.0f / 360.0f * n, start = 90.0f / 360.0f * n; break; + case 2: x = 0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), end = 330.0f / 360.0f * n, start = 30.0f / 360.0f * n; break; } - for (int i = 0; i < n; i++) { + + for (float i = start; i < bound; i++) { float angle = i / (float)n * pi * 2; float angle_next = (i + 1) / (float)n * pi * 2; @@ -67,15 +88,18 @@ static void render() { case 2: glColor3f(0.0f, 0.0f, 1.0f); break; } - if ((clear_start <= clear_end) ? (i > clear_start && i < clear_end) : (i > clear_start || i < clear_end)) + /*if ((start <= end) ? (i > start && i < end) : (i > clear_start || i < clear_end)) + glColor3f(0.0f, 0.0f, 0.0f);*/ + if ((start <= end) ? (i < start || i > end) : (i < start && i > end)) glColor3f(0.0f, 0.0f, 0.0f); - 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); + 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()); From bdd910d9645bf79fd8c3a08961b5432241e6e044 Mon Sep 17 00:00:00 2001 From: Yongning Xu <809257291@qq.com> Date: Fri, 25 Aug 2023 14:49:32 +0800 Subject: [PATCH 4/4] hw01 rotated OpenCV icon v1.1 --- src/main.cpp | 87 +++++++++++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 49 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4dba79b..5cb6646 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,29 +19,29 @@ static void render() { //CHECK_GL(glEnd()); //// render concentric circle - //glBegin(GL_TRIANGLES); - //constexpr int n = 5000; - //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()); + // glBegin(GL_TRIANGLES); + // constexpr int n = 5000; + // 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()); // render OpenCV icon glBegin(GL_TRIANGLES); @@ -50,35 +50,25 @@ static void render() { float radius = 0.3f; float inner_radius = 0.12f; float x = 0, y = 0; - //int clear_start = 0, clear_end = 0; - //glm::vec2 start = glm::vec2(0, n / 2); - //glm::vec2 end = glm::vec2(n / 2, n); - //float start[2] = { 0, n / 2 }, end[2] = { n / 2, n }; float start = 0, end = 0; - - static float bound = 0; - bound++; - if (bound > n) - bound -= n; + static glm::vec3 bound = glm::vec3(0.0f, 0.0f, 0.0f); for (int count = 0; count < 3; count++) { - /*switch (count) { - case 0: x = 0.0f , y = 0.4f , clear_start = 150.0f / 360.0f * n, clear_end = 210.0f / 360.0f * n; break; - case 1: x = -0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), clear_start = 30.0f / 360.0f * n , clear_end = 90.0f / 360.0f * n; break; - case 2: x = 0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), clear_start = 330.0f / 360.0f * n, clear_end = 30.0f / 360.0f * n; break; - }*/ - /*switch (count) { - case 0: x = 0.0f , y = 0.4f , end.x = 150.0f / 360.0f * n, end.y = 360.0f / 360.0f * n, start.x = 0.0f / 360.0f * n , start.y = 210.0f / 360.0f * n; break; - case 1: x = -0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), end.x = 30.0f / 360.0f * n , end.y = 360.0f / 360.0f * n, start.x = 0.0f / 360.0f * n , start.y = 90.0f / 360.0f * n; break; - case 2: x = 0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), end.x = 180.0f / 360.0f * n, end.y = 330.0f / 360.0f * n, start.x = 30.0f / 360.0f * n, start.y = 180.0f / 360.0f * n; break; - }*/ switch (count) { - case 0: x = 0.0f , y = 0.4f , end = 150.0f / 360.0f * n, start = 210.0f / 360.0f * n; break; - case 1: x = -0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), end = 30.0f / 360.0f * n, start = 90.0f / 360.0f * n; break; - case 2: x = 0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), end = 330.0f / 360.0f * n, start = 30.0f / 360.0f * n; break; + case 0: x = 0.0f , y = 0.4f , end = 150.0f / 360.0f * n, start = 210.0f / 360.0f * n, bound.x += 1.0f; break; + case 1: x = -0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), end = 30.0f / 360.0f * n , start = 90.0f / 360.0f * n , bound.y += 1.0f; break; + case 2: x = 0.4f * cosf(30.0f / 360.0f * 2 * pi), y = -0.4f * sinf(30.0f / 360.0f * 2 * pi), end = 330.0f / 360.0f * n, start = 30.0f / 360.0f * n , bound.z += 1.0f; break; } - for (float i = start; i < bound; i++) { + // red/green + if ((start > end) && (bound[count] > n + end)) + bound[count] -= (n); + // blue + else if ((start <= end) && (bound[count] > end)) + bound[count] -= (n); + + + for (float i = start; i < bound[count]; i++) { float angle = i / (float)n * pi * 2; float angle_next = (i + 1) / (float)n * pi * 2; @@ -88,8 +78,7 @@ static void render() { case 2: glColor3f(0.0f, 0.0f, 1.0f); break; } - /*if ((start <= end) ? (i > start && i < end) : (i > clear_start || i < clear_end)) - glColor3f(0.0f, 0.0f, 0.0f);*/ + // blue circle or red/green one if ((start <= end) ? (i < start || i > end) : (i < start && i > end)) glColor3f(0.0f, 0.0f, 0.0f); @@ -127,7 +116,7 @@ int main() { } // Create window - GLFWwindow *window = glfwCreateWindow(640, 640, "Example", NULL, NULL); + GLFWwindow *window = glfwCreateWindow(640, 640, "draw rotated OpenCV icon", NULL, NULL); if (!window) { const char *errmsg; glfwGetError(&errmsg);