2010-06-10 5 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가 동일하지 않을 때 내가 제대로 회전 원을 위해 수행해야하는 어떤 다른 계산인가? 내가이 녹색 선으로 늘어나는 것과해야 할 때 acctually

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

크기 조정 및 회전 기능을 표시하지 않았습니다. Scale 함수는 scaleX/Y를 설정하지만 Rotate는 무엇을할까요? – Skizz

+0

질문이 나에게 명확하지 않습니다. "원은 회전을 제외하고 올바른 방향으로 변형됩니다"라고 말합니다. 뭐가 잘못 됐어? 문제의 그림이 유용 할 것입니다. – tafa

+0

문제의 이미지를 게시 할 수 있습니까? –

답변

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 [...]; 
}; 

.