OpenGL的ES20 - 光立方,如何让正常人? [英] OpenGL ES20 - Light a cube, how to get normals?

查看:287
本文介绍了OpenGL的ES20 - 光立方,如何让正常人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要一些闪电添加到我的OpenGL ES20立方,我需要计算法线为每架飞机。我发现一个闪电教程,但他们只是硬codeD法线成立方体,它因为它似乎限制在我看来是不是最好的选择,?

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?

推荐答案

您所面临的问题是,你共享立方体面之间的顶点。正常是指向othogonal到表面的平面的矢量

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天全站免登陆