如何将固定功能的材质和光照传递给片段着色器? [英] How to pass-through fixed-function material and lighting to fragment shader?

查看:22
本文介绍了如何将固定功能的材质和光照传递给片段着色器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向我的 OpenGL 2.1/GLSL 1.2 应用程序添加一个顶点和片段着色器.

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

片段着色器:

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

因此,您必须自己在着色器中进行照明计算.本教程提供了执行此操作所需的所有代码.由于它针对每个像素而不是每个顶点进行光照计算,因此结果实际上看起来要好得多!

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!

这篇关于如何将固定功能的材质和光照传递给片段着色器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆