Skip to content

完成了回家作业: 绘制OpenCV Logo并且带动画 #55

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

Open
wants to merge 1 commit into
base: hw01
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions glm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
cmake_policy(VERSION 3.2)
# cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
# cmake_policy(VERSION 3.2)


file(READ "glm/detail/setup.hpp" GLM_SETUP_FILE)
Expand Down
100 changes: 72 additions & 28 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,85 @@
#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>

constexpr int Fineness = 120;
constexpr float pi = 3.1415926535897f;

/**
* @brief 通过度数范围计算为对应百分比
* @param du [0, 360]
* @return constexpr int
*/
constexpr int getShowRoundPercentage(float du) {
return static_cast<int>(Fineness * (du / 360.f));
}

/**
* @brief 画一个圆, 位于 (x, y), 半径是 r, 环半径是 r - dr
* @tparam IsClockwise 是否是顺时针
* @param x [-1, 1]
* @param y [-1, 1]
* @param r [-1, 1]
* @param dr [-1, 1]
* @param step 渲染步数
* @param begin 渲染起始位置
* @param end 渲染结束位置 [0, Fineness]
*/
template <bool IsClockwise>
void showRound(
float x, float y,
float r, float dr,
int step,
int begin, int end
) {
constexpr auto mod = Fineness + 1;
for (int i = begin; step && i != end; i = (i + (2 * IsClockwise - 1) + mod) % mod, --step) {
float angle = static_cast<float>(i) / Fineness * pi * 2;
float angleNext = static_cast<float>(i + 1) / Fineness * pi * 2;

glVertex3f(dr * std::sin(angle) + x, dr * std::cos(angle) + y, 0.f);
glVertex3f(r * std::sin(angle) + x, r * std::cos(angle) + y, 0.f);
glVertex3f(r * std::sin(angleNext) + x, r * std::cos(angleNext) + y, 0.f);

glVertex3f(dr * std::sin(angle) + x, dr * std::cos(angle) + y, 0.f);
glVertex3f(dr * std::sin(angleNext) + x, dr * std::cos(angleNext) + y, 0.f);
glVertex3f(r * std::sin(angleNext) + x, r * std::cos(angleNext) + y, 0.f);
}
}

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 float r = 0.3f, dr = 0.15f;
float xDown = std::sqrt(3.f) * 0.25f;

static int cntR = 0, cntG = 0, cntB = 0, tmp = 0; // 控制动画和间隔时间

glColor3f(1.f, 0.f, 0.f);
showRound<false>(0.f, 0.5f, r, dr, cntR,
getShowRoundPercentage(150), getShowRoundPercentage(210));

glColor3f(0.f, 1.f, 0.f);
showRound<false>(-xDown, -0.25f, r, dr, cntG,
getShowRoundPercentage(30), getShowRoundPercentage(90));

glColor3f(0.f, 0.f, 1.f);
showRound<false>(xDown, -0.25f, r, dr, cntB,
getShowRoundPercentage(330), getShowRoundPercentage(30));

constexpr int MaxStep = static_cast<int>(Fineness / 360.f * (360 - 60));

if (++cntR > MaxStep
&& ++cntG > MaxStep
&& ++cntB > MaxStep
&& ++tmp > MaxStep
) {
cntR = cntG = cntB = tmp = 0;
}

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() {
Expand Down