A
A
Amigo20192020-10-11 13:31:29
OpenGL
Amigo2019, 2020-10-11 13:31:29

Why doesn't the z-axis draw?

Good afternoon!
I can not understand why the z-axis does not draw. The x and y axes draw.

int main(int argc, char* argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
  glutInitWindowSize(1200, 800);
  glutCreateWindow("OpenGL lesson 5");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutKeyboardFunc(processNormalKeys);
  glutSpecialFunc(processSpecialKeys);
  glutMainLoop();
  return 0;
}
void reshape(int w, int h)
{
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-20, 20, -20, 20,-20, 20);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glClearColor(1, 1, 1, 0);
}
void display()
{
  float left = -15;
  float right = 15;
  float bottom = -15;
  float top = 15;
  float znear =15;
  float zfar = -15;
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3f(1.0, 0.0, 0.0);
  glPushMatrix();

  glBegin(GL_LINES);
  glColor3f(0, 0, 1);//x
  glVertex3f(left, 0, 0);
  glVertex3f(right, 0, 0);
  glColor3f(0, 1, 0);//y
  glVertex3f(0, bottom,0);
  glVertex3f(0, top,0);
  glColor3f(0, 1, 1);//z
  glVertex3f(0, 0,znear);
  glVertex3f(0, 0,zfar);
  glEnd();

  glColor3f(1.0, 0.0, 0.0);
  glutWireTeapot(5);
  glPopMatrix();
  glFlush();
  glutSwapBuffers();

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Shatunov, 2020-10-11
@Amigo2019

To begin with, it is worth considering the general system for identifying axes in 3D space. The axes X, Y, Z are labeled with color channels R, G, Bin a one-to-one correspondence. The axis Xis always and everywhere red. The axis Yis green. And the axis Zis blue.

glVertex3f(0, 0,znear);
glVertex3f(0, 0,zfar);

This code says to draw a line collinear with the view. The GPU will not waste resources on the entire line and will draw only one point, which with a high degree of probability will be overwritten when other primitives are rasterized. After all, all three lines are drawn from zero coordinates.
Shift the initial coordinates of the axes Xand Yfrom zero (at least by 1pt), change the color of the axis Z, then it will become a little better visible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question