OpenGL正方形和箭头

OpenGL Squares and Arrows

本文关键字:正方形 OpenGL      更新时间:2023-10-16

我正在尝试创建一个正方形,并在其顶部创建一个箭头。它就像一个2D仪表,有点类似。但我不知道如何在正方形顶部创建箭头。

int count = 1;
for (float y = 1; y < 11; y++) {
    y1 = y1 - 0.51;
    y2 = y2 - 0.51;
    float x2 = -2.0;
    for (float x1 = -2.5; x1 < 2.5; x1 = x1 + 0.51) {
        glColor3f(windy[count], 1.0, 0.0);
        glVertex2f(x1, y1);
        glVertex2f(x1, y2);
        glVertex2f(x2, y2);
        glVertex2f(x2, y1);
        count = count + 1;
        x2 = x2 + 0.51;

    }
}
glutSwapBuffers();
glEnd(); //End the glBegin Function

这就是我想要的。

glBegin(GL_LINE_LOOP);//start drawing a line loop
        glVertex3f(-1.0f, 0.0f, 0.0f);//left of window
        glVertex3f(0.0f, -1.0f, 0.0f);//bottom of window
        glVertex3f(1.0f, 0.0f, 0.0f);//right of window
        glVertex3f(0.0f, 1.0f, 0.0f);//top of window
        glEnd();//end drawing of line loop

为什么在glEnd()调用之前交换缓冲区:

int count = 1;
for (float y = 1; y < 11; y++) {
    y1 = y1 - 0.51;
    y2 = y2 - 0.51;
    float x2 = -2.0;
    for (float x1 = -2.5; x1 < 2.5; x1 = x1 + 0.51) {
        glColor3f(windy[count], 1.0, 0.0);
        glVertex2f(x1, y1);
        glVertex2f(x1, y2);
        glVertex2f(x2, y2);
        glVertex2f(x2, y1);
        count = count + 1;
        x2 = x2 + 0.51;
    }
}
//glutSwapBuffers(); // wrong place
glEnd(); //End the glBegin Function
glutSwapBuffers(); // Must be after draw operation

要在形状的顶部绘制箭头,必须更多:

int count = 1;
for (float y = 1; y < 11; y++) {
    y1 = y1 - 0.51;
    y2 = y2 - 0.51;
    float x2 = -2.0;
    for (float x1 = -2.5; x1 < 2.5; x1 = x1 + 0.51) {
        glColor3f(windy[count], 1.0, 0.0);
        glVertex2f(x1, y1);
        glVertex2f(x1, y2);
        glVertex2f(x2, y2);
        glVertex2f(x2, y1);
        count = count + 1;
        x2 = x2 + 0.51;
    }
}
//glutSwapBuffers(); // wrong place
glEnd(); //End the glBegin Function
// draw arrow
glBegin(GL_LINE_LOOP);//start drawing a line loop
        glVertex3f(-1.0f, 0.0f, 0.0f);//left of window
        glVertex3f(0.0f, -1.0f, 0.0f);//bottom of window
        glVertex3f(1.0f, 0.0f, 0.0f);//right of window
        glVertex3f(0.0f, 1.0f, 0.0f);//top of window
        glEnd();//end drawing of line loo

// atlast we can swap our buffers
glutSwapBuffers(); // Must be after draw operation