Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

opengl-es

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.

like image 593
ace Avatar asked Feb 10 '11 16:02

ace


2 Answers

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

like image 113
ryanm Avatar answered Oct 19 '22 17:10

ryanm


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

like image 32
jwir3 Avatar answered Oct 19 '22 17:10

jwir3