OpenGL ES20 - 点亮一个立方体,如何获得法线? [英] OpenGL ES20 - Light a cube, how to get normals?

查看:31
本文介绍了OpenGL ES20 - 点亮一个立方体,如何获得法线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要向我的 OpenGL ES20 立方体添加一些闪电,我需要计算每个平面的法线.我找到了关于闪电的教程",但他们只是将法线硬编码到立方体中,在我看来这不是最佳选择,因为它似乎有限?

To add some lightning to my OpenGL ES20 cube, I need to calculate the normals for each plane. I've found a "tutorial" on lightning, but they simply hard-coded the normals into the cube, which appears to me as not the best option, since it seems limited?

所以我对立方体的处理方法如下:

So my approach to the cube is as follows:

private float[] mVertices = {
        -1, -1, -1, // bottom left
        1, -1, -1, // bottom right   back
        1, 1, -1, // top right
        -1, 1, -1, // top left
        -1, -1, 1, // bottom left
        1, -1, 1, // bottom right   front
        1, 1, 1, // top right
        -1, 1, 1 // top left
};

private float[] mColors = {
        0.6f, 0f, 0.6f,
        0.6f, 0f, 0.6f,
        0.6f, 0f, 0.6f,
        0.6f, 0f, 0.6f,

        0.8f, 0f, 0.6f,
        0.8f, 0f, 0.6f,
        0.8f, 0f, 0.6f,
        0.8f, 0f, 0.6f
};

private float[] mNormal = new float[?]; ?

private short[] mIndices = {
        0, 4, 5,
        0, 5, 1,

        1, 5, 6,
        1, 6, 2,

        2, 6, 7,
        2, 7, 3,

        3, 7, 4,
        3, 4, 0,

        4, 7, 6,
        4, 6, 5,

        3, 0, 1,
        3, 1, 2
};

即我定义了所有 8 个顶点以及如何将它们组合成三角形的索引.为了得到正常矩阵,我读过我应该反转矩阵,然后转置它.所以我试过这个:

i.e. i have all 8 vertices defined as well as indices, on how to combine them to triangles. To get the normal matrix, I've read that I am supposed to invert the matrix, then transpose it. So I've tried this:

    float[] mTempMatrix = new float[mVertices.length];
    Matrix.invertM(mTempMatrix, 0, mVertices, 0);
    Matrix.transposeM(mNormal, 0, mTempMatrix, 0);

但这总是用零填充,而我的立方体保持黑色.我应该如何计算法线矩阵?它应该与模型矩阵有关吗?如果是,我应该在哪里做这个,因为它们真正结合的唯一地方是在着色器中?还有其他更合适的方法吗?

But this is always filled with zeros, and my cube stays black. How example am I supposed to calculate the normal matrix? Should it be something with the model matrix? If yes, where am I supposed to do this, since the only place they are really combined is in the shader? Is there another way to do this which would be more appropriate?

推荐答案

您面临的问题是在立方体面之间共享顶点.法线是一个与表面平面正交的向量.

The issue you are facing is that you share vertices between cube faces. The normal is a vector that points othogonal to the plane of the surface.

以顶/右/前顶点为例(与前、右和顶面共享).

Consider the top/right/front vertex as an example (that you share with the front, right and top faces).

在正面使用时,法线需要指向你,0, 0, 1右脸使用时,法线需要指向右侧为1,0,0在顶面使用时,法线需要向上指向0, 1, 0

When used on the front face, the normal needs to point towards you as 0, 0, 1 When used on the right face, the normal needs to point to the right as 1, 0, 0 When used on the top face, the normal needs to point up as 0, 1, 0

那么如何调和呢?您可以将顶点的法线设置为从角落指向,例如如 0.707、0.707、0.707.这很可能会在拐角处为您提供有趣的照明效果,但可能不是您想要的.

So how to reconcile that? You could set the normal for the vertex to point out from the corner, e.g. as 0.707, 0.707, 0.707. That'll most likely give you an interesting lighting effect on the corner but is probably not what you're after.

另一个解决方案是不重复使用面之间的顶点.所以你需要 24 个(每边 4 个)而不是 8 个.然后你将有每个顶点的 3 个版本,但现在每个版本都只属于一个面,因此你可以将法向量设置为垂直于该面.您还可以只为该面设置颜色,因为您将不再与其他面共享顶点.

The other solution is not to re-use the vertices between faces. So you'll need 24 (4 per side) instead of 8. You'll then have 3 versions of each vertex but each one of those now belongs to just one face, hence you can set the normal vector as perpendicular to that face. You'll also be able to set the color for just that face as well since you'll no longer be sharing the vertex with other faces.

这篇关于OpenGL ES20 - 点亮一个立方体,如何获得法线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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