java - 添加灯光后,环境光变得更亮(OpenGL、法线贴图)

标签 java opengl libgdx shader blending

在过去的几天里,我在 Java (Libgdx) 中玩弄了闪电网络。我是 OpenGL 或着色器的新手,我偶然发现了一个很好的教程,如何使用法线贴图实现光照( https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson6 )。到目前为止,我成功地用一盏灯做到了这一点,现在我尝试用多盏灯做到同样的效果。我尝试使用附加混合对每个灯光执行一次绘制调用。阴影绘制正确,但每次添加灯光时,环境颜色都会变得更亮。我尝试了一些方法,但没有任何效果,我陷入了困境。

我的渲染方法:

@Override
public void render () {
    renderToFbo(Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY());
    renderToScreen(Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY());

    renderToFbo(200, 200);
    batch.setBlendFunction(GL_ONE,GL_ONE_MINUS_SRC_COLOR);
    renderToScreen(200,200);

    renderToFbo(500, 500);
    batch.setBlendFunction(GL_ONE, GL_ONE_MINUS_SRC_COLOR);
    renderToScreen(500,500);
}


private void renderToFbo(float posX, float posY){
    fbo.begin();
    batch.setBlendFunction(GL_ONE, GL_ZERO);
    batch.setShader(defaultShader);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(lightMap, posX - lightSize / 2, posY - lightSize  / 2, lightSize,lightSize);
    batch.end();
    fbo.end();
}

private void renderToScreen(float posX, float posY){
    batch.setShader(lightningShader);
    batch.begin();

    float x = posX / (float) Gdx.graphics.getWidth();
    float y = posY / (float) Gdx.graphics.getHeight();
    LIGHT_POS.x = x;
    LIGHT_POS.y = y;

    lightningShader.setUniformf("lightPos", LIGHT_POS.x, LIGHT_POS.y, LIGHT_POS.z);

    fbo.getColorBufferTexture().bind(2);
    normalMap.bind(1);
    texture.bind(0);

    batch.draw(texture, 0,0);
    batch.end();
}

这是我的片段着色器:

varying vec4 vColor;
varying vec2 vTexCoord;
uniform sampler2D u_texture;   //diffuse map
uniform sampler2D u_normals;   //normal map
uniform sampler2D u_light;     //light map

uniform vec2 resolution;      //resolution of screen
uniform vec3 lightPos;        //light position, normalized
uniform vec4 lightColor;      //light RGBA -- alpha is intensity
uniform vec4 ambientColor;    //ambient RGBA -- alpha is intensity


void main() {
//RGBA of our diffuse color
vec4 diffuseColor = texture2D(u_texture, vTexCoord);

//RGB of our normal map
vec3 normalMap = texture2D(u_normals, vTexCoord).rgb;
//NormalMap.g = 1.0 - NormalMap.g;

//The delta position of light
vec3 lightDir = vec3(lightPos.xy - (gl_FragCoord.xy / resolution.xy), lightPos.z);

lightDir.x *= resolution.x / resolution.y;


//normalize our vectors
vec3 N = normalize(normalMap * 2.0 - 1.0);
vec3 L = normalize(lightDir);

//Pre-multiply light color with intensity
//Then perform "N dot L" to determine our diffuse term
vec3 diffuse = (lightColor.rgb * lightColor.a) * max(dot(N, L), 0.0);

//pre-multiply ambient color with intensity
vec3 ambient = ambientColor.rgb * ambientColor.a;

//calculate attenuation from lightmap
vec2 lighCoord = (gl_FragCoord.xy / resolution.xy);
vec3 attenuation = texture2D(u_light, lighCoord).rgb;

//the calculation which brings it all together
vec3 intensity = ambient + diffuse * attenuation;
vec3 finalColor = diffuseColor.rgb * intensity;
gl_FragColor = vColor * vec4(finalColor, diffuseColor.a);
}

最佳答案

要通过单个渲染调用来完成此操作,您的片段着色器必须接受要处理的灯光位置数组。着色器必须在编译时知道数组大小,因此您应该将数组设置得足够大,以容纳所需数量的灯光(当您需要更少的灯光时,可以将剩余的灯光设置为黑色)。

我在下面调整了您的着色器,只是假设它在您的代码中正常工作。我不知道你在用光照贴图做什么,所以我用更传统的东西替换了你的衰减计算。

varying vec4 vColor;
varying vec2 vTexCoord;
uniform sampler2D u_texture;   //diffuse map
uniform sampler2D u_normals;   //normal map

const int LIGHT_COUNT = 4;

uniform vec2 resolution;      //resolution of screen
uniform vec3[LIGHT_COUNT] lightPos;      //light position, normalized
uniform vec4[LIGHT_COUNT] lightColor;      //light RGBA -- alpha is intensity
uniform vec4 ambientColor;    //ambient RGBA -- alpha is intensity

void main() {

    vec4 diffuseColor = texture2D(u_texture, vTexCoord);
    vec3 normalMap = texture2D(u_normals, vTexCoord).rgb;
    vec3 N = normalize(normalMap * 2.0 - 1.0);

    float resolutionFactor = resolution.x / resolution.y;

    vec3 diffuse = new vec3(0.0);
    for (int i=0; i<LIGHT_COUNT; i++){
        vec3 lightDir = vec3(lightPos[i].xy - (gl_FragCoord.xy / resolution.xy), lightPos[i].z);
        lightDir.x *= resolutionFactor;
        vec3 L = normalize(lightDir);
        float distance = length(lightDir);
        vec3 attenuation = 1.0 / ( 0.4 + 3.0*distance + (20.0*distance*distance ) );
        diffuse += attenuation * (lightColor[i].rgb * lightColor[i].a) * max(dot(N, L), 0.0);
    }

    //pre-multiply ambient color with intensity
    vec3 ambient = ambientColor.rgb * ambientColor.a;


    //the calculation which brings it all together
    vec3 intensity = min(vec3(1.0), ambient + diffuse); // don't remember if min is critical, but I think it might be to avoid shifting the hue when multiple lights add up to something very bright.
    vec3 finalColor = diffuseColor.rgb * intensity;
    gl_FragColor = vColor * vec4(finalColor, diffuseColor.a);

}

将灯光参数传递给着色器:

static final int LIGHT_COUNT = 4;
final float[] tmpLightPositions = new float[3 * LIGHT_COUNT];
final float[] tmpLightColors = new float[4 * LIGHT_COUNT];

//...

int i = 0;
for (Vector3 pos : myLightPositions) {// should be LIGHT_COUNT of them
    tmpLightPositions[i++] = pos.x;
    tmpLightPositions[i++] = pos.y;
    tmpLightPositions[i++] = pos.z;
}
i = 0;
for (Color col : myLightColors) {
    tmpLightColors[i++] = color.r;
    tmpLightColors[i++] = color.g;
    tmpLightColors[i++] = color.b;
    tmpLightColors[i++] = color.a;
}
shader.setUniform3fv("lightPos", tmpLightPositions, 0, tmpLightPositions.length);
shader.setUniform4fv("lightColor", tmpLightColors, 0, tmpLightColors.length);

关于java - 添加灯光后,环境光变得更亮(OpenGL、法线贴图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45591791/

相关文章:

java - Elasticsearch,嵌套 "ANDS"和 "ORS"

java - JSF commandLink 操作在更新模型值之前触发

c# - Nhibernate HQL SQL Server 2005/2008 包含稳定

c++ - 如何将 new 和 delete 与 OpenGL 的缓冲区对象一起使用?

python - pyOpenGL 三角形没有被绘制

java - 打造情侣 Actor 同台表演

android - Android 上的 Libgdx 并正确处理

java - 如何调整 libGDX 中的文本大小

java - 基本的java打印

c++ - GLSL 着色器无法编译,有人可以帮我找出原因吗?