What are the normals for 3d cube as used in OpenGL ES?

二次信任 提交于 2019-12-09 05:44:46

问题


I have a cube defined as :

  float vertices[] = {
            //Vertices according to faces
                -1.0f, -1.0f, 1.0f, //Vertex 0
                1.0f, -1.0f, 1.0f,  //v1
                -1.0f, 1.0f, 1.0f,  //v2
                1.0f, 1.0f, 1.0f,   //v3

                1.0f, -1.0f, 1.0f,  //...
                1.0f, -1.0f, -1.0f,         
                1.0f, 1.0f, 1.0f,
                1.0f, 1.0f, -1.0f,

                1.0f, -1.0f, -1.0f,
                -1.0f, -1.0f, -1.0f,            
                1.0f, 1.0f, -1.0f,
                -1.0f, 1.0f, -1.0f,

                -1.0f, -1.0f, -1.0f,
                -1.0f, -1.0f, 1.0f,         
                -1.0f, 1.0f, -1.0f,
                -1.0f, 1.0f, 1.0f,

                -1.0f, -1.0f, -1.0f,
                1.0f, -1.0f, -1.0f,         
                -1.0f, -1.0f, 1.0f,
                1.0f, -1.0f, 1.0f,

                -1.0f, 1.0f, 1.0f,
                1.0f, 1.0f, 1.0f,           
                -1.0f, 1.0f, -1.0f,
                1.0f, 1.0f, -1.0f,
                                    };

What are the normals for this cube? I need the actual values for the normals.

Do we need 6 or 12 normals? Since OpenGL ES uses only triangles which means we need 12 normals but I could be wrong.


回答1:


Normals are specified per-vertex, and since the normals for the three faces that share each vertex are orthogonal, you'll get some really wonky looking results by specifying a cube with just 8 vertices and averaging the three face normals to get the vertex normal. It'll be shaded as a sphere, but shaped like a cube.

You'll instead need to specify 24 vertices, so each face of the cube is drawn without sharing vertices with any other.

As to the values, 'tis dead easy. If we assume that x increases to the right, y increases as we go up, and z increases as we move forwards, the normal for the right-hand side is (1, 0, 0), left is (-1, 0, 0), top side is (0,1,0), etc, etc

To summarise: don't draw a cube, draw 6 quads that just happen to have coincident vertices




回答2:


The normal of a surface is simply a direction vector. Since the normal will be the same for two surfaces that are coplanar, you will only need 6 surface normals. However, often, it's the case that normals are expected to be defined per vertex, in which case you'll need 36 (one for each vertex of each triangle on each face of the cube).

To compute the normals, simply use the following calculation: http://www.opengl.org/wiki/Calculating_a_Surface_Normal



来源:https://stackoverflow.com/questions/4959654/what-are-the-normals-for-3d-cube-as-used-in-opengl-es

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!