【问题标题】:libgdx point light not working on generated meshlibgdx 点光源不适用于生成的网格
【发布时间】:2014-01-19 13:26:20
【问题描述】:

我一直在尝试照亮以下生成的平面网格:

private Model createPlane(float w, float h, Texture texture) {
   Mesh mesh = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 3, "a_position"),
           new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord0"),
           new VertexAttribute(Usage.Normal, 3, "a_normal")); 

   float w2 = w * this.CELL_SIZE;
   float h2 = h * this.CELL_SIZE;

    mesh.setVertices(new float[]
            { w2, 0f, h2, 0, 0, 0, 1, 0,
            w2, 0f, -h2, 0, h, 0, 1, 0,
            -w2, 0f, h2, w, 0, 0, 1, 0,
            -w2, 0f, -h2 , w,h, 0, 1, 0
            });
    mesh.setIndices(new short[] { 0, 1, 2, 1, 3, 2});
    Model model = ModelBuilder.createFromMesh(mesh, GL10.GL_TRIANGLES, new Material(TextureAttribute.createDiffuse(texture)));
    return model;
}

并使用以下方式呈现:

//the environment setup
env = new Environment();
        env.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
        env.add(new PointLight().set(Color.ORANGE, 5f, 1f, 5f, 10f));
        env.add(new DirectionalLight().set(Color.WHITE, -1f, -0.8f, -0.2f));
...
//the render method
batch.begin();
batch.render(inst, env);//inst is a ModelInstance created using the Model generated from createPlane(...)
batch.end();

网格正确显示(UV、纹理)并且似乎受到定向和环境光照的适当影响。

当我尝试添加点光源时(参见上面的环境),从 createPlane(...) 生成的平面都不会受到影响。我尝试使用 ModelBuilder 类的 createBox(...) 创建另一个几何图形,它似乎可以正确响应点光源。因此,我假设我没有正确生成平面,但它显然受到定向/环境光影响的事实让我有点失望。

值得注意的是,生成的平面的大小各不相同,我不是特别确定点光源是否会对 4 个顶点产生很大影响,但我期望多于没有。移动点光源(靠近某些顶点)也没有任何作用。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

  • 我会尝试将您的灯直接放在网格的一个角上,然后将其强度放大很多,看看其中一个是否是您的问题。除非您使用具有逐像素照明的着色器,否则四顶点网格不会很好地响应范围不大于顶点之间距离的两倍的点光源。

标签: android opengl-es 3d libgdx


【解决方案1】:

很高兴知道您正在使用哪个着色器。默认的?我不确定他们是否已经修复了这个问题,他们前一阵子有一个错误,点光源只在某些设备上工作(这与制造商实施 opengles 有关。我个人使用我自己的着色器修复了这个问题。

编辑:This seems to be fixed

我检查了我正在使用的代码。问题是在着色器中确定正确的光阵列。 最后是这样的吗:

    // on some devices this was working
    int u_lightPosition = program.getUniformLocation("u_lightPosition[0]");
    int u_lightColors = program.getUniformLocation("u_lightColor[0]");
    if(u_lightPosition < 0 && u_lightColors < 0) {
        // on others this was working
        u_lightPosition = program.getUniformLocation("u_lightPosition");
        u_lightColors = program.getUniformLocation("u_lightColor");
    }

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2014-04-07
    • 2020-02-29
    • 2014-05-19
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多