1

I'm trying to rotate a polygon in place, but it keeps revolving instead.

To rotate, I calculate the center by finding the average location of each vertex. I call the rotation functions and then call a translation using the center to move it to the middle of the screen. It does end up centered, but it still rotates as if it's not. Any ideas as to what I could be doing wrong?

Here's my code:

void Polygon::DrawPolygon()
{
    glPushMatrix();
    glLoadMatrixf(matrix);
    glTranslatef(displace[0], displace[1], displace[2]);
    glRotatef(rotation[0], 1, 0, 0);
    glRotatef(rotation[1], 0, 1, 0);
    glRotatef(rotation[2], 0, 0, 1);
    glTranslatef(-displace[0], -displace[1], displace[2]);
    displace[0] = 0; displace[1] = 0; displace[2] = 0;
    glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
    DrawMaterial();
    DrawFaces();
    ConnectFaces();
    glPopMatrix();
}

Here's how I calculate the center:

void Polygon::FindCenter()
{
    float x = 0;
    float y = 0;
    float z = 0;

    for(int j = 0; j < 2; j++)
    {
        for(int i =  0; i < vertexCount; i++)
        {
            x += vertices[i][0];
            y += vertices[i][1];
            z += vertices[i][2] + extrusionDistance * j;
        }
    }
    x = x / (vertexCount * 2);
    y = y / (vertexCount * 2);
    z = z / (vertexCount * 2);

    displace[0] = x;
    displace[1] = y;
    displace[2] = z;
}

Because of the way my extrusion works I don't need to add the x and y for the vertices of both faces, but I did anyway to keep it consistent.

Here is how I draw the shape:

void Polygon::DrawFaces()
{
    for(int j = 0; j < 2; j++)
    {
        glBegin(GL_POLYGON);
        for(int i = 0; i < vertexCount; i++)
        {
             glVertex3f(vertices[i][0], vertices[i][1], j*extrusionDistance);
        }
        glEnd();
    }
}

void Polygon::ConnectFaces()
{
    for(int i = 0; i < vertexCount; i++)
    {
        glBegin(GL_POLYGON);
        glVertex3f(vertices[i][0], vertices[i][1], 0);
        glVertex3f(vertices[i][0], vertices[i][1], extrusionDistance);
        glVertex3f(vertices[(i+1)%vertexCount][0], vertices[(i+1)%vertexCount][1], extrusionDistance);
        glVertex3f(vertices[(i+1)%vertexCount][0], vertices[(i+1)%vertexCount][1], 0);
        glEnd();
    }
}
4

1 に答える 1

0

奇妙に思える点がいくつかあります。

1)と への呼び出しglLoadMatrixf(matrix)の前に呼び出していglTranslate()ますglRotate()。読み込んでいるマトリックスの内容に応じて、状況が変わります。

2)FindCenter()メソッドでは z の計算に を含めて中心を計算してvertex[i][2]いますが、実際に で面を描くときは、パーツをDrawFaces()含めず、vertex[i][2]パーツだけを含めますextrusion * j。したがって、中心を計算しているのと同じものを描いているわけではありません。

于 2013-11-05T06:02:20.680 に答える