2017-04-25 65 views
0

我想旋转一个对象,而它的移动,但我不能使它沿着我希望它移动的点移动和旋转。无法旋转c中的移动对象使用opengl

struct point { 
    int x; 
    int y; 
}; 

// A line between two points. 

struct line { 
    struct point start; 
    struct point end; 
}; 

float j = 100.0; 
float rotationAngle = 0.0f; // The angle of rotation for our object 

void rotateObjectWithMovement(void functionToDrawObject(), struct point pivot, float rotationAngle) { 
    glPushMatrix(); 
    glTranslatef(pivot.x, pivot.y, 0.0f); 
    glRotatef(rotationAngle, 0.0f, 0.0f, 1.0f); 
    functionToDrawObject(); 
    glPopMatrix(); 
} 

void rotateObjectWithoutMovement(void functionToDrawObject(), struct point pivot, float rotationAngle) { 
    glPushMatrix(); 
    glTranslatef(pivot.x, pivot.y, 0.0f); 
    glRotatef(rotationAngle, 0.0f, 0.0f, 1.0f); 
    glTranslatef(-pivot.x, -pivot.y, 0.0f); 
    functionToDrawObject(); 
    glPopMatrix(); 
} 

void rocket() { 

    struct point allPoints[2] = { 
     { 100, 100}, 
     { 150, 150} 
    };  
    struct line line1 = {allPoints[0], allPoints[1]}; 
    glColor3f(1.0, 1.0, 0.0); 
    drawLines(&line1, 1); 
} 

void display() { 
    glClear(GL_COLOR_BUFFER_BIT); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    struct point pivot={j,100}; 
    rotateObjectWithoutMovement(rocket,pivot,rotationAngle); 
    //rotateObjectWithMovement(rocket,pivot,rotationAngle); 

    //reference line 
    glBegin(GL_LINES); 
    glVertex2f(0, 100); 
    glVertex2d(1000, 100); 
    glEnd(); 

    j += 1; 

    rotationAngle += 0.5f; // Increment our rotation value 
    if (rotationAngle > 360.0f) // If we have rotated beyond 360 degrees (a full rotation) 
     rotationAngle -= 360.0f; // Subtract 360 degrees off of our rotation 
    glutSwapBuffers(); // Swap our buffers 
} 

void myinit() { 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(0.0, 988.0, 0.0, 999.0); 
    glClearColor(0.5f, 0.5f, 1, 1); 
    glMatrixMode(GL_MODELVIEW); 
} 

int main(int argc, char *argv[]) { 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE); 
    glutInitWindowSize(700, 500); 
    glutInitWindowPosition(100, 100); 
    glutCreateWindow("OpenGL Hierarchical Modeling 2D Example"); 

    myinit(); 

    glutDisplayFunc(display); 
    glutIdleFunc(display); 

    glutMainLoop(); 
    return 0; 
} 

rotateWithMoment()函数接受一个对象,静止坐标和角度,所述对象具有被旋转到并与统计的共ordinats作为枢轴转动它。

rotateWithoutMovement()函数需要一个对象,改变对象必须旋转到的坐标和角度,并且以变化的坐标作为枢轴旋转它(例如:旋转线条看起来像轮子旋转)发送变化的co这个函数使这条线以某种方式旋转。

我一直在努力让它工作2天,不知道我哪里出错了。

我想让我发送的对象移动到(x,y)点并以指定的角度旋转。要做到这一点,我试图建立这样的功能,通过接受函数来绘制要旋转和移动的对象。

rotateWithoutMovement很好,如果我发送一个不断的坐标。

+0

定义“不工作”。 –

+0

你的'main(...)'函数在哪里? – ryyker

+0

我不明白C++标记。你的标题说“C”语言。他们是不同的语言。例如,C++具有'std :: vector'和'std :: string',C不具有。在C中,我可以无错地命名变量“class”。 –

回答

2

您的功能似乎要么作出你没有披露的假设,要么具有比他们所需要的更少的参数。特别是,未指定相对于对象坐标旋转的轴所在的轴

只有通过物体中心的轴的旋转才是物体的净移动 - 围绕顶点质心的旋转保持质心;那些关于(虚拟)质量中心的东西保存质量中心。经由glRotatef()施加的OpenGL旋转总是关于通过原点坐标轴,所以一般,则需要执行该逻辑系列操作:

  1. 选择驻留在期望的旋转轴上的点P0(例如,顶点心)。

  2. 应用将P0移动到原点的翻译。

  3. 应用所需的旋转。

  4. 应用将原点返回到P0的翻译。

您可以将它与其他翻译相结合以实现对象移动。任一

  • 执行(1),在(1)/(2),或
  • 之后,或在与结合执行它占它之前的翻译(4)。

如果我们假设您functionToDrawObject()定义坐标系上的可绘制对象,使得期望的旋转轴穿过原点已经 - 因为它看来你确实是假设 - 那么(1)和(2)不需要,因为所需的翻译为零。否则,您需要在对象的坐标系中提供所需的枢轴点,与世界坐标系中对象的所需位置分开。让我们做出这个假设。

之前我们写的功能,我们需要知道一两件事:在OpenGL当前矩阵定义为一个在左边乘以坐标:Matrix * X = X',当你申请一个新的变换(旋转,平移,或比例),其矩阵与右侧上的当前矩阵组合,如Current * M = New。实际上,这意味着您想要应用矩阵的程序顺序与您希望将它们应用于图形坐标的逻辑顺序相反,或者等价地说,程序的结构反映了矩阵方程的结构它编码。因此

你想要的功能将有这种形式:

void rotateObjectWithoutMovement(void functionToDrawObject(), struct point pivot, 
     float rotationAngle) { 
    glPushMatrix(); 
    // No translation needed, because we're rotating around the object's 
    // natural origin (thus pivot is unused) ... 
    glRotatef(rotationAngle, 0.0f, 0.0f, 1.0f); 
    functionToDrawObject(); 
    glPopMatrix(); 
} 

void rotateObjectWithMovement(void functionToDrawObject(), struct point pivot, 
     float rotationAngle) { 
    glPushMatrix(); 

    // Translate to the desired position after rotating to orient the object 
    // as desired. No additional translation is needed because we did not 
    // apply any before the rotation. 
    glTranslatef(pivot.x, pivot.y, 0.0f); 
    glRotatef(rotationAngle, 0.0f, 0.0f, 1.0f); 
    // No _initial_ translation needed, because we're rotating around the object's 
    // natural origin ... 
    functionToDrawObject(); 
    glPopMatrix(); 
} 

还要注意的是没有旋转前应用于翻译影响绘制的对象的最终取向 - 这样的翻译在场景中仅影响对象的位置(当然,之后应用的翻译也是如此)。

然而,我怀疑我们之前的假设并不是你想要做的。在这种情况下,你真正想要的可能是更像这样的东西:

void rotateObjectWithMovement(void functionToDrawObject(), struct point pivot, 
     float rotationAngle, struct point translation) { 
    glPushMatrix(); 
    glTranslatef(pivot.x + translation.x, pivot.y + translation.y, 0.0f); 
    glRotatef(rotationAngle, 0.0f, 0.0f, 1.0f); 
    glTranslatef(-pivot.x, -pivot.y, 0.0f); 
    functionToDrawObject(); 
    glPopMatrix(); 
} 

void rotateObjectWithoutMovement(void functionToDrawObject(), struct point pivot, 
     float rotationAngle) { 
    static const struct point zero = { 0, 0 }; 
    rotateObjectWithMovement(functionToDrawObject, pivot, rotationAngle, zero); 
} 
+0

我试过这种方法。这是行不通的。我得到相同的输出。对象围绕整个屏幕旋转。 –

+0

这两种变化都适用于我,@NaseeruddinVN,给出适当的输入。请注意,第一个变体是在您提供完整示例之前准备的;您对问题所做的后续编辑似乎表明,正如我所怀疑的那样,您确实需要第二种变体。一定要通过正确的论点。 –