Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# OpenGL圆旋转_C#_Opengl - Fatal编程技术网

C# OpenGL圆旋转

C# OpenGL圆旋转,c#,opengl,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

我正在使用以下代码绘制圆:

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不相等时,我应该执行哪些其他计算才能使圆正确旋转

当我想要用绿线画圆圈时,圆圈被画成红线

旋转功能:

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;
}
比例函数:

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-增加;比例<1减少
{
如果(!isPrepared)返回;
objectSize.x1*=比例;
objectSize.x2*=比例;
objectSize.y1*=比例;
objectSize.y2*=比例;
((MCircleCmd)cmdList[i]).m_X*=量表;
((MCircleCmd)cmdList[i]).m_Y*=量表;
((MCircleCmd)cmdList[i]).m_R*=量表;
((MCircleCmd)cmdList[i])。scaleX=比例;
((MCircleCmd)cmdList[i])。scaleY=scale;
}

我认为你的做法是错误的——如果有GPU,你就没有充分利用它。就我个人而言,我会这样处理:

circle.Scale(tmp_p.scaleX, tmp_p.scaleY);
circle.Rotate(tmp_p.rotateAngle);
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 [...];
 };

这将转换和缩放的工作放到渲染管道(矩阵推送)中,并让硬件完成繁重的工作。

您还没有向我们展示缩放和旋转功能。我猜Scale函数设置了scaleX/Y,但是Rotate做什么呢?这个问题我不清楚。你说:“除了旋转外,圆是以正确的方式变换的。”。什么是错的?绘制问题图会很有用。你能发布问题的图片吗?我添加了其他信息来帮助理解问题;)