user3351750
user3351750

Reputation: 947

Drawing a line along z axis in opengl

I am trying to draw a line along the point 0.5,-0.5,0.0 to 0.5,-0.5,-0.5 using GL_LINES in the z direction .

Intialization of the window :

glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RGB);

Setup in the display function.

glClearColor(1.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);

However, the line is not displayed on the screen. Please help as to how to display a line going along the z direction.

Upvotes: 1

Views: 2641

Answers (1)

Elvithari
Elvithari

Reputation: 834

You should probably share the piece of code where you actually attempt to draw the line using GL_LINES. Without it I must assume that you don't know how to do it properly. The correct way to draw the line after the setup is:

glBegin(GL_LINES);
  glVertex3f(0.5f, -0.5f, 0.0f);
  glVertex3f(0.5f, -0.5f, -0.5f);
glEnd();

Have you tried it that way? Also, if you use double buffering, don't forget to swap buffers after rendering, using glutSwapBuffers() when using glut or SwapBuffers(hdc) when not using it.


Edit:

Additionally you need to setup your camera correctly and move it slightly to actually see the line that you draw (it is possible that it's outside of the view area)

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,1,0.1,100); //Example values but should work in your case
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

This piece of code should setup your projection correctly. Now OpenGL by default looks in the negative direction of Z axis so if you want to see your line you need to move tha camera towards the positive end of Z axis using the code (in fact the code moves the whole world, not just your camera, but it doesn't matter):

glTranslate(0.0f,0.0f,-1.0f);

Use this before glBegin and you should be good to go.

Upvotes: 1

Related Questions