2015-11-25 92 views
2

我正在尝试创建一个正方形,并在其上创建一个箭头。它就像是一个类似于2D的仪表。但我不知道如何在广场上创建箭头。OpenGL正方形和箭头

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 
+2

你不应该交换缓冲中介于'glBegin'和'glEnd'之间 – BeyelerStudios

+0

@Java_NewBie:我的回答对你有帮助吗? – Mykola

回答

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 
+0

@Java_NewBie:我的回答对你有帮助吗? – Mykola