绘制和旋转箭头

时间:2020-10-13 14:54:03

标签: c++ qt opengl opengl-compat

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

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();

1 个答案:

答案 0 :(得分:0)

您不小心将angle_offsetR添加到三角形向量的角度。此外,当您绘制新形状时,必须重新启动GL_TRIANGLE_FAN基本体(请参见Triangle primitives)。
如果要旋转模型,请通过glRotatef设置将绕z轴的旋转添加到模型视图矩阵。
不要平移和缩放顶点坐标。使用glScaleglTranslate。矩阵转换不是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();