0

I was trying to draw a line on the window but it is not displaying with glFrustum(). I have used the same code with glOrtho() it does display the line. Is there anything else I need to set up for my line to show up on set window?

void init() {
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum( -2.0,2.0,-2.0,2.0, 1.0, 20.0 );
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

 void display() {
  glBegin(GL_LINES);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(0.0f, 0.0f, 2.0f);
    glVertex3f(1.0f, 1.0f, 5.0f);
  glEnd();
 }
genpfault
  • 51,148
  • 11
  • 85
  • 139
LilRazi
  • 690
  • 12
  • 33

1 Answers1

3

In OpenGL the camera looks along the negative Z axis. Therefore, your line is behind the camera, and thus it's getting clipped.

Instead draw it at the front:

glVertex3f(0.0f, 0.0f, -2.0f);
glVertex3f(1.0f, 1.0f, -5.0f);
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • my view volume is glFrustum( -2.0,2.0,-2.0,2.0, 1.0, 20.0 ) which means z should be on 1 and 20. How come -2 and -5 is in thin range ?. – LilRazi Jul 03 '17 at 11:53
  • 1
    1 and 20 are distances to the near and far planes, not their absolute coordinates. The planes are located at the negative z-coordinates at those distances from origin. See [`glFrustum` documentation](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml): "*(left, bottom, -nearVal)* and *(right, top, -nearVal)* specify the points on the near clipping plane [...]. *-farVal* specifies the location of the far clipping plane." – Yakov Galka Jul 03 '17 at 11:58