OpenGL广场和箭头

时间:2015-11-25 17:28:41

标签: c++ opengl

我正在尝试创建一个正方形,并在它上面创建一个箭头。它就像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

1 个答案:

答案 0 :(得分:0)

为什么在调用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