Pedro
Pedro

Reputation: 21

OpenGL: lines dont get rendered

Using OpenGL I am trying to render a simple line between the points p1 and p2, but it doesnt work. However, I am able to render cubes at these positions.

        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);

        GLfloat   mat_ambient[]     = {0.0, 0.0, 1.0, 1.0};
        GLfloat   mat_flash[]       = {0.0, 0.0, 1.0, 1.0};
        GLfloat   mat_flash_shiny[] = {50.0};
        GLfloat   light_position[]  = {100.0,-200.0,200.0,0.0};
        GLfloat   ambi[]            = {0.1, 0.1, 0.1, 0.1};
        GLfloat   lightZeroColor[]  = {0.9, 0.9, 0.9, 0.1};


        glLightfv(GL_LIGHT0, GL_POSITION, light_position);
        glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
        glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
        glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);  
        glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
        glMatrixMode(GL_MODELVIEW);

        glLoadIdentity(); 

        glTranslatef(p1.x, p1.y, p1.z); 
        glutSolidCube(20); 

        glLoadIdentity(); 
        glTranslatef(p2.x, p2.y, p2.z); 
        glutSolidCube(20); 
        glDisable( GL_LIGHTING );
        glDisable( GL_DEPTH_TEST );

I am using the following code for rendering the line. But I cant find the problem why it doesnt get rendered:

        glDisable(GL_LIGHTING); 
        glEnable(GL_COLOR_MATERIAL); 

        glLoadIdentity(); 

        glColor3f(1,0,0); 
        glLineWidth(10.0); 

        glBegin(GL_LINES); //doesnt work with GL_LINE either

            glVertex3f(p1.x, p1.y, p1.z); 
            glVertex3f(p2.x, p2.y, p2.z);  
        glEnd();

Upvotes: 2

Views: 2354

Answers (2)

Lemme-Code-It
Lemme-Code-It

Reputation: 1

A couple of suggestions for you, w.r.t your second code snippet:

1) You probably don't need the 'glEnable(GL_COLOR_MATERIAL)', try commenting out that line.

2) You're passing int's to glColor3f(). Try using glColor3f(1.0f, 0.0f, 0.0f); instead.

Also, did you check the line drawing without the cube at the same position? (ie., is it being occluded or not)

Upvotes: 0

datenwolf
datenwolf

Reputation: 162164

I assume what happens is that the lines are actually rendered, but there's still a vertex normal set, pointing away from the light source, or being 0. Easy fix: Disable lighting when drawing lines.

While it is possible to "illuminate" lines, the whole operation makes no sense if one assumes the "regular" normal based illumination models. Lines don't have a normal direction, they have a normal plane (or in other words, a line it it's tangent itself).

So the proper way to illuminate a line is by tangent based shading. Fixed function OpenGL can't do it, but it is trivial to implement with a shader.

Upvotes: 1

Related Questions