Display rotation of 3D triangle

157 views Asked by At

I am trying to show a triangle which is rotated with respect to y axis. But I can not show it properly. How do I solve it? What is wrong with my code

init:

void init() {
     glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);
    glClearColor(0.7f, 0.9f, 1.0f, 1.0f);
    glEnable(GL_CULL_FACE);
  glCullFace(GL_BACK);


    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
  glFrustum(-30, 30, -30, 30, 30, 90);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

display:

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

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glRotatef(0.0, 0.0f, 1.0f, 0.0f);


    glBegin(GL_TRIANGLES);


    glVertex3f(20.0f, 10.0f, -10.0f);
    glVertex3f(50.0f, 15.0f, -20.0f);
    glVertex3f(32.0f, 30.0f, -10.0f);

    glEnd();

    glutSwapBuffers();
}
1

There are 1 answers

0
Rabbid76 On BEST ANSWER

The model rotates around (0, 0, 0). Don't translate the vertices along the negativ z axis, but arrange the vertices around (0, 0, 0) and translate the model by glTranslate after rotating the model. For instance:

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

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glTranslatef(0.0f, 0.0f, -60.0f);
    glRotatef(0.0, 0.0f, 1.0f, 0.0f);


    glBegin(GL_TRIANGLES);

    glVertex3f(-15.0f, -10.0f, 5.0f);
    glVertex3f(15.0f, -5.0f, -5.0f);
    glVertex3f(-3.0f, 10.0f, 5.0f);

    glEnd();

    glutSwapBuffers();
}

Note you have to ensure that the model is in between the near and far plane of the perspective projection. In your case the near plane is 30.0 and the the far plane in 90.0:

glFrustum(-30, 30, -30, 30, 30, 90);

All the geometry which is not in between the near and far plane is clipped.

Since your geometry is not a closed volume, I recommend to disable face culling:

glEnable(GL_CULL_FACE);