gpt4 book ai didi

c++ - 绘制和旋转箭头

转载 作者:行者123 更新时间:2023-12-02 10:26:21 24 4
gpt4 key购买 nike

我正在尝试使用以下样式绘制箭头,但该箭头也应根据条件旋转(需要通过每种条件的度数)。我可以绘制矩形和三角形,但不能将三角形绘制为箭头。另外,如何将旋转度包含在代码中?有没有更简单的绘制箭头和旋转箭头的方法?

int triangleRect=4, triangleTri=3, lineWidth=3;
double twicePi = 2.0f * M_PI, angle_offsetR =1.5* M_PI/2, radius = 0.05,
xR=m_start.x(), y=m_start.y(), xT=m_start.x()+ m_rect_width;


glColor3f(0,1,0);
glLineWidth(lineWidth);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_MULTISAMPLE);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glBegin(GL_TRIANGLE_FAN);

//RECTANGLE
for(int i = 0; i <= triangleRect; i++) {
glVertex2f((xR + (radius * cos(i * twicePi / triangleRect + angle_offsetR)))* m_parent_width_function(),
(y + (radius * sin(i * twicePi / triangleRect + angle_offsetR)))* m_parent_height_function());
}
// TRIANGLE
for(int i = 0; i <= triangleTri; i++) {
glVertex2f((xT + (radius * cos(i * twicePi / triangleTri + angle_offsetR)))* m_parent_width_function(),
(y + (radius * sin(i * twicePi / triangleTri + angle_offsetR)))* m_parent_height_function());
}

glEnd();

最佳答案

您不小心将angle_offsetR添加到三角形 vector 的角度。此外,当您绘制新形状时,必须重新启动GL_TRIANGLE_FAN primitiv(请参阅Triangle primitives)。
如果要旋转模型,则通过 glRotatef 设置将绕z轴的旋转添加到模型 View 矩阵。
不要平移和缩放顶点坐标。使用 glScale glTranslate 。矩阵转换不是commutative,顺序很重要:

float angle_of_roation = 30.0; // 30°
glPushMatrix();
// scale
glScalef( m_parent_width_function(), m_parent_height_function(), 1.0f);
// move triangle and rectangle to the position in the world
glTranslatef(xR, y, 0.0f);
// roatate triangle and rectangle
glRotatef(angle_of_roation, 0, 0, 1);

//RECTANGLE
glBegin(GL_TRIANGLE_FAN);
for(int i = 0; i <= triangleRect; i++) {
float angle = i * twicePi / triangleRect + angle_offsetR;
glVertex2f(radius * cos(angle), radius * sin(angle));
}
glEnd();

glPushMatrix();
// translate triangle relative to rectangle
glTranslatef(xT-xR, 0.0f, 0.0f);

// TRIANGLE
glBegin(GL_TRIANGLE_FAN);
for(int i = 0; i <= triangleTri; i++) {
float angle = i * twicePi / triangleTri;
glVertex2f(radius * cos(angle), radius * sin(angle));
}
glEnd();

glPopMatrix();
glPopMatrix();

关于c++ - 绘制和旋转箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64337641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com