OpenGL循环旋转

时间:2010-06-10 13:43:25

标签: c# 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

当实际上我希望它被绿线拉伸时,圆圈会以红线显示拉伸。

旋转功能:

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;
}

1 个答案:

答案 0 :(得分:1)

我认为你正在以错误的方式接近它 - 如果存在GPU,你就不会充分利用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 [...];
 };

这将翻译和缩放的工作放在渲染管道(矩阵推送)中,让硬件完成艰苦的工作。