2010-06-10 86 views
0

我用下面的代碼來繪製我的圈子:OpenGL的圓圈旋轉

double theta = 2 * 3.1415926/num_segments; 
double c = Math.Cos(theta);//precalculate the sine and cosine 
double s = Math.Sin(theta); 
double t; 
double x = r;//we start at angle = 0 
double y = 0; 

GL.glBegin(GL.GL_LINE_LOOP); 
for(int ii = 0; ii < num_segments; ii++) 
{ 
    float first = (float)(x * scaleX + cx)/xyFactor; 
    float second = (float)(y * scaleY + cy)/xyFactor; 

    GL.glVertex2f(first, second); // output Vertex 

    //apply the rotation matrix 
t = x; 
x = c * x - s * y; 
y = s * t + c * y; 
} 
GL.glEnd(); 

的問題是,當將scaleX從不同的scaleY圈,然後在除旋轉以正確的方式轉化。 在我的代碼序列如下:

circle.Scale(tmp_p.scaleX, tmp_p.scaleY); 
circle.Rotate(tmp_p.rotateAngle); 

我的問題是什麼其他的計算,我應該爲圓正常轉動時執行的scaleX和scaleY不相等?

alt text http://www.freeimagehosting.net/uploads/c0cfc89146.gif

被streched爲紅線的圓圈顯示,當acctually我希望它用綠線進行streched。

旋轉功能:

double cosFi = Math.Cos(angle*Math.PI/180.0); 
double sinFi = Math.Sin(angle * Math.PI/180.0); 
double x, y; 
double newX = 0, newY = 0; 
DVector center = objectSize.CenterPoint; 
y = ((MCircleCmd)cmdList[i]).m_Y; 
x = ((MCircleCmd)cmdList[i]).m_X; 
newX = (x - center.shiftX) * cosFi - (y - center.shiftY) * sinFi + center.shiftX; 
newY = (x - center.shiftX) * sinFi + (y - center.shiftY) * cosFi + center.shiftY; 
((MCircleCmd)cmdList[i]).m_X = newX; 
((MCircleCmd)cmdList[i]).m_Y = newY; 
UpdateSize(ref firstMove, newX, newY); 

量表功能:

public void Scale(double scale) // scale > 1 - increase; scale < 1 decrease 
{ 
    if (!isPrepared) return; 
    objectSize.x1 *= scale; 
    objectSize.x2 *= scale; 
    objectSize.y1 *= scale; 
    objectSize.y2 *= scale; 
    ((MCircleCmd)cmdList[i]).m_X *= scale; 
    ((MCircleCmd)cmdList[i]).m_Y *= scale; 
    ((MCircleCmd)cmdList[i]).m_R *= scale; 
    ((MCircleCmd)cmdList[i]).scaleX = scale; 
    ((MCircleCmd)cmdList[i]).scaleY = scale; 
} 
+3

您沒有向我們展示比例和旋轉功能。我猜Scale函數設置scaleX/Y,但是Rotate做什麼? – Skizz 2010-06-10 13:58:44

+0

這個問題並不清楚。 「除了旋轉之外,」圈子以正確的方式轉化「。那是什麼錯誤?該問題的繪製將是有用的。 – tafa 2010-06-10 14:00:21

+0

你能發表問題的圖片嗎? – 2010-06-10 19:24:03

回答

1

我想你接近這個錯誤的方式 - 你沒有更充分的利用了GPU(如果存在)。就個人而言,我想接近它是這樣的:

class Ellipse 
{ 
public: 
    Ellipse() 
    { 
    // build an array of precalculated vertices for a unit circle 
    // optionally, create a display list 
    } 

    void Draw() 
    { 
     glBegin (...); 
     // create and push a translation, rotation matrix from the m_* value 
     // setup all vertices from array (or use display list) 
     // pop matrix 
     glEnd(); 
    } 

    // setters to define position, rotation and scale 

private: 
    float m_rotation, m_scale_x, m_scale_y, m_x, m_y; 
    glVertex2f m_vertices [...]; 
}; 

這使得轉換和縮放到渲染管線(矩陣推)的工作,並讓硬件做艱苦的工作。