Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can some one point me to Java code ( in Java not C or C++) that calculates all the normals for all the vertices of a mesh for OpenGL ES application. I need this for lighting. Lets say I have a cube with following vertices and indices:

float vertices[] = { -width, -height, -depth, // 0
                      width, -height, -depth, // 1
                      width,  height, -depth, // 2
                     -width,  height, -depth, // 3
                     -width, -height,  depth, // 4
                      width, -height,  depth, // 5
                      width,  height,  depth, // 6
                     -width,  height,  depth  // 7
short indices[] = { 0, 2, 1,
                    0, 3, 2,

In above specific example how many normals we need?

share|improve this question
You need one normal per triangle. Calculating normals is covered here:… – bummzack Feb 16 '11 at 12:18
@bummzack Is there a way in OpenGL-ES to only specify one normal per triangle? I thought you could only pass them in as a vertex attribute to your Vertex Shader (and do per vertex lighting). Right? – Chris Smith Feb 16 '11 at 15:28
Yeah, you'll have to set the normal per vertex. For a cube, all vertices of one side share the same normal though. Therefore you can calculate the normal once per side and then pass that to all vertices belonging to that side. – bummzack Feb 16 '11 at 16:44

You can calculate normals by taking two edges, and cross product for each vertex. A cube actually has 24 vertices not 8, the reason is that each corner vertex is shared with different faces, because openGL takes per vertex attributes, you need to duplicate a vertex if you find that it has 2 different normals, assuming you want hard edges.

this is a C++ code but its self explanatory.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.