带旋转的3d字母不起作用

时间:2013-11-11 00:27:38

标签: opengl

我需要使用opengl绘制一个3d字母A和轴。为了绘制字母A,我使用A绘制了第2个字母z == 50,然后使用A绘制了字母z = 50 - offset

这里我的opengl和字母坐标初始化

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("PROJECT");
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, 0, HEIGHT, -100, 100);    
glMatrixMode(GL_MODELVIEW);

p1.x = 0; p1.y = 400 ; p1.z = 50;
p2.x = -300; p2.y = -500; p2.z = 50;
p3.x = -200; p3.y = -500; p3.z = 50;
p4.x = -100; p4.y = -200; p4.z = 50;
p5.x = 100; p5.y = -200; p5.z = 50;
p6.x = 200; p6.y = -500; p6.z = 50;
p7.x = 300; p7.y = -500; p7.z = 50;

p8.x = -130; p8.y = -100; p8.z = 50;
p9.x = 130; p9.y = -100; p9.z = 50;
p10.x = 0; p10.y = 100; p10.z = 50;

glutDisplayFunc(display);
glutMainLoop();

在功能显示中,我绘制轴和两个字母A

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    axes();
    drawLetter(0);
    drawLetter(100);

glFlush();
glutSwapBuffers();
}

这里是轴的初始值

float angle = 17;

float AXIS_X[3] = { 250, 0, 0 },
      AXIS_Y[3] = { 0, 250, 0 },
      AXIS_Z[3] = { 0, 0, 150 };

我画这样的轴

void axes() {

glPushMatrix();

glTranslatef(WIDTH/2, HEIGHT/2, 0);
glBegin(GL_LINES);
    glColor3f(1, 0, 0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3fv(AXIS_X);
glEnd();
glBegin(GL_LINES);
    glColor3f(0, 0, 1);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3fv(AXIS_Y);
glEnd();
glBegin(GL_LINES);
    glColor3f(0, 1, 0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3fv(AXIS_Z);
glEnd();

glPopMatrix();

}

这是字母的代码

void drawLetter(float offset) {

glPushMatrix();

glTranslatef(WIDTH/2, HEIGHT/2, 0);
glScalef(0.3, 0.3, 0.3);
glBegin(GL_POLYGON);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(p1.x, p1.y, p1.z - offset);
    glVertex3f(p2.x, p2.y, p2.z - offset);
    glVertex3f(p3.x, p3.y, p3.z - offset);
    glVertex3f(p10.x, p10.y, p10.z - offset);
glEnd();

glBegin(GL_POLYGON);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(p1.x, p1.y, p1.z - offset);
    glVertex3f(p10.x, p10.y, p10.z - offset);
    glVertex3f(p6.x, p6.y, p6.z - offset);
    glVertex3f(p7.x, p7.y, p7.z - offset);
glEnd();

glBegin(GL_POLYGON);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(p8.x, p8.y, p8.z - offset);
    glVertex3f(p4.x, p4.y, p4.z - offset);
    glVertex3f(p5.x, p5.y, p5.z - offset);
    glVertex3f(p9.x, p9.y, p9.z - offset);
glEnd();

glPopMatrix();

}

默认情况下,它看起来像这样

enter image description here

但是当我在glRotatef(angle, 0.0, 1.0, 0.0);之前添加axes()时,它看起来像这样(简单的绿线是)

enter image description here

有什么问题?

另外一个问题 - 如何在我的信件的不同平面(xy,yz,xz)上创建正交和透视投影?我需要使用glFrustrumglOrtho

1 个答案:

答案 0 :(得分:1)

通常您希望按此顺序执行操作:

  1. 缩放
  2. 旋转
  3. 翻译
  4. 由于OpenGL使用列主矩阵,您需要:

    1. 翻译
    2. 旋转
    3. 缩放
    4. 注意:从概念上讲,顺序是相同的,当你使用column-major时,你只需要以不同的顺序进行矩阵乘法。

      我首先要解决这个问题。

      另一个需要注意的重要事项是,如果您在此代码中旋转drawLetter (...)axes (...)之外,旋转将累积多个帧(因为您不加载单位矩阵)。您应该为每个帧加载一个用于模型视图的单位矩阵,或者在PushMatrix / PopMatrix块内部进行旋转(无论如何,您需要执行此操作以进行正确的旋转)。