Question

I am adding a vertex and fragment shader to my OpenGL 2.1/GLSL 1.2 application.

Vertex shader:

#version 120

void main(void)  
{    
    gl_Position = ftransform();
    gl_FrontColor = gl_Color;
}

Fragment shader:

#version 120

void main(void) 
{   
    if (/* test some condition */) {
        discard;
    } else {
        gl_FragColor = gl_Color;
    }
}

The problem is that if the condition fails, gl_FragColor just gets set to whatever the last call to gl.glColor3f() was in my fixed-function method.

Instead, I want to pass through the normal color, derived from the material and lighting parameters. For example, this:

gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_AMBIENT, lightingAmbient, 0);
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_DIFFUSE, lightingDiffuse, 0);
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_SPECULAR, lightingSpecular, 0);
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_POSITION, directionalLightFront, 0);

gl.glMaterialfv(GL.GL_FRONT, GLLightingFunc.GL_AMBIENT, materialAmbient, 0);
gl.glMaterialfv(GL.GL_FRONT, GLLightingFunc.GL_DIFFUSE, materialDiffuse, 0);
gl.glMaterialfv(GL.GL_FRONT, GLLightingFunc.GL_SPECULAR, materialSpecular, 0);
gl.glMaterialfv(GL.GL_FRONT, GLLightingFunc.GL_EMISSION, materialEmissive, 0);
gl.glMaterialf(GL.GL_FRONT, GLLightingFunc.GL_SHININESS, shininess);

Is there a way to assign this value to gl_FragColor? Or do I need to implement the lighting from scratch in the fragment shader?

(Note, I'm not trying to do any kind of advanced lighting techniques. I'm using the shaders for clipping purposes and want to just use standard lighting methods.)

Was it helpful?

Solution

Unfortunately there is no way to do what you want. Using the fixed function pipeline and vertex/pixel shaders are mutually exclusive. Each primitive you render must use one or the other.

Thus, you must do the lighting computations yourself in the shader. This tutorial provides all the code you would need to do so. Since it does the lighting calculation for every pixel instead of every vertex, the results actually look far better!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top