Sonicsmooth
Sonicsmooth

Reputation: 2777

OpenGL Lines coming out gray, with lighting enabled

I'm using Python OpenGL, version '3.3.0 - Build 8.15.10.2725' on Win7 x64.

I'm using GL_LINES to draw 2D grids and XYZ axes in 3D space. When I disable lighting the grid colors come out fine.

When I enable lighting and the light Z position is > 0, the grid colors come out fine.

However when I put the light Z position < 0, the grid colors are almost gray, even though the 3D portion of the model is rendering properly. Since lines are one dimensional, I don't think they have a surface; I'm not sure how OpenGL regards lines in the presence of lighting.

I can get colored lines with lighting Z position < 0 if I use a dumb fragment shader as follows:

# From http://bazaar.launchpad.net/~mcfletch/openglcontext/trunk/view/head:/tests/shader_2.py
self.color_fragmentshader = shaders.compileShader("""
    varying vec4 vertex_color;
    void main() {
        gl_FragColor = vertex_color;
    }
    """, GL.GL_FRAGMENT_SHADER)

But if I leave the default shader (by not calling glUseProgram), or use some other shader I found, then the 2D lines come out gray.

My grid and axes functions are as follows:

def drawGrid(self, size, squares):
    GL.glLineWidth(0.1)
    GL.glColor((trolltechGreen.light().red()/255,
                trolltechGreen.light().green()/255,
                trolltechGreen.light().blue()/255))
    GL.glBegin(GL.GL_LINES)
    for x in np.linspace(-size, size, squares+1, True):
        GL.glVertex2f(x, -size)
        GL.glVertex2f(x, size)
    for y in np.linspace(-size, size, squares+1, True):
        GL.glVertex2f(-size, y)
        GL.glVertex2f(size, y)
    GL.glVertex2f(-size,-size)
    GL.glVertex2f(size,-size)
    GL.glVertex2f(size,size)
    GL.glVertex2f(-size,size)
    GL.glEnd()

def drawAxes(self, size):
    GL.glLineWidth(5)
    GL.glBegin(GL.GL_LINES)
    GL.glColor(1,0,0,1)
    GL.glVertex2f(0,0)
    GL.glVertex2f(size,0)
    GL.glColor(0,1,0,1)
    GL.glVertex2f(0,0)
    GL.glVertex2f(0,size)
    GL.glColor(0,0,1,1)
    GL.glVertex2f(0,0)
    GL.glVertex3f(0,0,size)
    GL.glEnd()

I can also get the line color to come out fine if I change the shader each time I render the lines, then change again to render the 3D objects, but I don't think this is the right solution.

Here is what the problem looks like. The lighting is "behind" the teapot in -Z. How can I force the lines to ignore the lighting, without constantly changing the shader? Thanks!

enter image description here

Upvotes: 0

Views: 1252

Answers (1)

derhass
derhass

Reputation: 45372

I'm not sure how OpenGL regards lines in the presence of lighting.

When you enable lighting in the fixed function pipeline, it will affec all primitives you draw - points, lights, triangles.

Lines might not define a mathematical surface, but you still have normal vectors defined for them. Remember that OpenGL is a state machine, so if you don't specify them with your line geometry, you still have some "current" normal vector which will be used.

When you use shaders, you yourself control the color which is generated. Shaders apply to all primitive types as well.

If you want the lines to be unaffected by the lighting, disable lighting when you draw the grid in the fixed-function pipeline, or use a color-only shader if you use the programmable pipeline.

You can of course set some material properties so that your lines will appear just in the color you whish while lighting is enabled. In fixed-function GL, this can be achieved by setting the "emissive" material coeffients to the desired color, and all others to 0, for example. If you use shaders, it will depend on how the shader work, of course.

Upvotes: 4

Related Questions