2016-12-25 68 views
-2

如何在中心的按鈕上繪製正方形(如停止按鈕)?如何使用OpenGL在中心按鈕上繪製正方形?

在這段代碼中,我嘗試像按鈕的矩形,全:

void ButtonDraw(Button *b) 
    { 
     if(b) 
     { 
      /* 
      * We will indicate that the mouse cursor is over the button by changing its 
      * colour. 
      */ 
      if (b->highlighted) 
       glColor3f(0.7f,0.7f,0.8f); 
      else 
       glColor3f(0.6f,0.6f,0.6f); 
      /* 
      * draw background for the button. 
      */ 
      glBegin(GL_QUADS); 
       glVertex2i(b->x  , b->y  ); 
       glVertex2i(b->x  , b->y+b->h); 
       glVertex2i(b->x+b->w, b->y+b->h); 
       glVertex2i(b->x+b->w, b->y  ); 
      glEnd(); 

      /*draw red square on the button*/ 
      glBegin(GL_QUADS); 
      glColor3f(1.0f, 0.0f, 0.0f); 
      glVertex2i(b->x, b->y); 
      glVertex2i(b->x+b->w, b->y); 
      glVertex2i(b->x+b->w, b->y + b->h); 
      glVertex2i(b->x, b->y + b->h); 
      glEnd(); 

      /* 
      * Draw an outline around the button with width 3 
      */ 
      glLineWidth(3); 


     } 
    } 
+1

請將代碼作爲代碼發佈,而不是截圖。 –

+0

謝謝你的建議唐老鴨。 – Melo234

回答

1

最明顯的方法是不畫一個紅色矩形大小相同的按鈕,而是畫一個正方形。這是最基本的方法:

/*draw red square on the button*/ 
glBegin(GL_QUADS); 
glColor3f(1.0f, 0.0f, 0.0f); 
const int SQUARE_SIDE = 6; // Or however long you want a side 
// Calculate the centre of the button rectangle 
const int xMid = b->x + b->w/2; 
const int yMid = b->y + b->h/2; 
// Trace a square around the centre 
glVertex2i(b->xMid - SQUARE_SIDE/2, b->yMid - SQUARE_SIDE/2); 
glVertex2i(b->xMid - SQUARE_SIDE/2, b->yMid + SQUARE_SIDE/2); 
glVertex2i(b->xMid + SQUARE_SIDE/2, b->yMid + SQUARE_SIDE/2); 
glVertex2i(b->xMid + SQUARE_SIDE/2, b->yMid - SQUARE_SIDE/2); 
glEnd(); 

上面的代碼計算按鈕的中間,然後繪製一個正方形。對於產品代碼,你當然會檢查你沒有重疊按鈕的邊緣,並使用浮動來處理舍入問題等。

+0

非常感謝您的回答Ken Y-N。 – Melo234

+0

@ Melo234:如果它解決了您的問題,請不要忘記[接受答案](http://meta.stackexchange.com/a/5235/222560)。 –

+0

我已經接受你的答案。再次感謝提醒我,因爲這是我第一次使用StackOverflow。 – Melo234