如何直通固定功能材料和照明片段着色器?片段、功能、材料、着色器

2023-09-07 23:40:04 作者:余墨白

我加入一个顶点和片段着色器,以我的OpenGL 2.1 / 1.2 GLSL应用程序。

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

顶点着色器:

#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;
    }
}

问题是,如果条件不满足, gl_FragColor 刚刚被设置为以 gl.glColor3f无论最后调用()是我的固定功能的方法。

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);

有没有一种方法来指定这个值 gl_FragColor ?或者我需要从头开始实现在片段着色器的照明?

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.)

推荐答案

不幸的是没有办法做你想做的。使用固定管线和顶点/像素着色器是互斥的。每一个原始的渲染必须使用一个或另一个。

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.

因此​​,你必须做的光照计算自己的着色器。 本教程提供的所有code你需要这样做。由于它的灯光计算每一个像素,而不是每一个顶点,结果居然看远越好!

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!