【问题标题】:OpenGL - Drawing PatternsOpenGL - 绘图模式
【发布时间】:2019-10-02 03:23:02
【问题描述】:

附加图像是我想要做的:

但是,我的代码给了我以下模式:

谁能指引我正确的方向?

void drawPattern(float xPos, float yPos, float length){
    glColor3f(0.0, 1.0, 0.0);
        // Drawing Square 
        glBegin(GL_POLYGON);
        glVertex2f(xPos + length, yPos);
        glVertex2f(xPos, yPos);
        glVertex2f(xPos , yPos + length);
        glVertex2f(xPos + length , yPos + length);
    glEnd();

    glColor3f(0,0,1);
    float halfPi = 0.5 * PI;
    //Drawing Bottom Left Circle
    glBegin(GL_LINE_LOOP);
    for (float angle = 0.0; angle < 90 * 0.01745329; angle += 0.01745329){
        glVertex2f( xPos + (length/2)*cos(angle), yPos + (length/2)*sin(angle));
    }
    glEnd();

    //Drawing Top Right Circle 
    glBegin(GL_LINE_LOOP);
    for (float angle = 0.0; angle < 90 * 0.01745329; angle += 0.01745329){
        glVertex2f( xPos + length/2 + (length/2)*cos(angle), yPos + length/2 + (length/2)*sin(angle));
    }
    glEnd();
}

【问题讨论】:

    标签: c++ opengl


    【解决方案1】:

    除了你必须使用line primitive typeGL_LINE_STRIP而不是GL_LINE_LOOP,答案中已经提到,右上角的弧线方向错误。

    这是因为圆弧的中心点必须是左上角(xPos + lengthyPos + length),而不是四边形的中心。此外,必须在左下角绘制圆弧。这是通过减去正弦和余弦项来实现的:

    glBegin(GL_LINE_STRIP);
    for (float angle = 0.0; angle < 90 * 0.01745329; angle += 0.01745329) {
        glVertex2f(
            xPos + length - (length/2)*cos(angle),
            yPos + length - (length/2)*sin(angle));
    }
    glEnd();
    

    【讨论】:

      【解决方案2】:

      使用 GL_LINE_STRIP 而不是 GL_LINE_LOOP 可能是个不错的主意。与前者不同,后者将始终将终点连接到起点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-24
        • 1970-01-01
        • 1970-01-01
        • 2011-06-19
        • 1970-01-01
        • 1970-01-01
        • 2014-06-20
        • 1970-01-01
        相关资源
        最近更新 更多