OpenGL circle rotation
I'm using following code to draw my circles:
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();
The problem is that when scaleX is different from scaleY then circles are transformed in the right way except for the rotation. In my code sequence looks like this:
circle.Scale(tmp_p.scaleX, tmp_p.scaleY);
circle.Rotate(tmp_p.rotateAngle);
My question is what other calculations should i perform for circle to r开发者_运维百科otate properly when scaleX and scaleY are not equal?
alt text http://www.freeimagehosting.net/uploads/c0cfc89146.gif
The circle is streched as red line shows when acctually i want it to be streched by green line.
Rotate function:
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);
Scale function:
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;
}
I think you are approaching this the wrong way - you're not taking full use of the GPU if one is present. Personally, I'd approach it like this:
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 [...];
};
This puts the job of translating and scaling into the rendering pipeline (the matrix push) and lets the hardware do the hard work.
精彩评论