2011-04-21 41 views
2

我正在繪製opengl中使用C++ 的球池陣列我正面臨的問題是數組以直線繪製。 當我使用gltranslate球仍然只沿線翻譯,當我編輯z和y軸 我想要做的是設置像一個三角形形狀像一個池匹配打破 如何使用數組代碼如此設置球? 任何幫助,將不勝感激opengl池球陣列

balls[7]; 
    for (int x = ball-start; x<ball-end;x++) 
    { 
     glTranslatef(0,0,0.5); 
     glColor3f(1,0,0); 
     ball[x].drawball(); 
    } 

回答

2

做這樣的事情:

// first of all, include the x,y position (assuming 2D, since pool) in the Ball object: 
class Ball 
{ 
    //... 

    private: 
     float xpos, ypos; 
    //... 
}; 

然後,當你構建球的陣列,而不是僅僅做8個球,你會想在堆上分配內存,使其將持續貫穿整個遊戲。所以這樣做:

Ball *ball= new Ball*[8]; 
ball[0] = new Ball(x0,y0); 
ball[1] = new Ball(x1,y1); 
ball[2] = new Ball(x2,y2); 
ball[3] = new Ball(x3,y3); 
// ... 

確保當你的遊戲結束後,你自己清理。

for (int i = 0; i < 8; i++) 
    delete ball[i]; 

delete [] ball; 

然後在你的球::平局()做這樣的事情:

Ball::draw() 
{ 
    glColor3f(/*yellow*/); // Set the color to yellow 
    glTranslatef(-xpos, -ypos, 0); // Move to the position of the ball 
    // Draw the ball 
    glTranslatef(xpos, ypos, 0); // Move back to the default position 
} 

所有你需要做的就是拿出正確的(X0,Y0),(X1,Y1) ,(x2,y2)...形成一個三角形!這是否有意義/回答你的問題?

+0

謝謝這是有益的:)我會給這個去看看它是如何變成! – DK10 2011-04-21 17:02:29

+1

請Upvote +接受,如果這解決了你的問題。 – 2011-04-21 21:10:19

3

假設:

struct Ball { 
    double x,y,z; 
    void drawball(void); 
    /* ... */ 
    } ball[7]; 

嘗試:

for(int i=0; i<7 ;i++) 
    { 
    glPushMatrix(); 
     glTranslated(ball[i].x,ball[i].y,ball[i].z); 
     glColor3f(1,0,0); 
     ball[i].drawball(); 
    glPopMatrix(); 
    } 

細節可能有所不同,但希望你的想法。

+0

謝謝!!我會試一試,看看我得到了什麼結果...我不認爲如果這樣做:) – DK10 2011-04-21 01:21:05