Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

Let's say I have an object where some vertices will have the same normal and some won't. Are these two equivalent? Note I'm working with an old OpenGL version (2.3 I think).

Option 1

glNormal3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( 1.0,  1.0, 1.0);
glVertex3f(-1.0,  1.0, 1.0);

glNormal3f(0.1, 0.0, -1.0); 
glVertex3f( 1.0, -1.0, -1.0);
glNormal3f(0.2, 0.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glNormal3f(0.3, 0.0, -1.0);
glVertex3f(-1.0,  1.0, -1.0);

Option 2

glNormal3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f( 1.0,  1.0, 1.0);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(-1.0,  1.0, 1.0);

glNormal3f(0.1, 0.0, -1.0); 
glVertex3f( 1.0, -1.0, -1.0);
glNormal3f(0.2, 0.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glNormal3f(0.3, 0.0, -1.0);
glVertex3f(-1.0,  1.0, -1.0);
share|improve this question
up vote 3 down vote accepted

What happens if I don't call glNormal for some vertices?

It will use the last glNormal value that you have set when you call glVertex3f.

You can view OpenGL as a state machine. Calling glNormal will change the state of the 'current normal', and that's all. As it can be read in MSDN documentation:

The current color, normal, and texture coordinates are associated with the vertex when glVertex is called.

And about your question:

Are these two equivalent?

You'll achieve the same effect, so effect-wise, yes, they are equivalent.

However, if this situation will occur often during the run-time, it would be wise to reduce the number of unnecessary calls to OpenGL to increase performance. Thus they're not equivalent efficiency-wise; you should use Option 1 as much as possible.

share|improve this answer
5  
If you really cared about performance, you'd go with Option 3: Use Vertex Buffer Objects. – Colonel Thirty Two Mar 6 at 6:23

glNormal just sets the current normal, just like glColor sets the current color, et cetera. When you call glVertex, the current normal, color, texture coordinates, and so on are associated with the specified vertex.

So you don't need to call glNormal for every vertex if you want your vertices to reuse whatever the current normal is.

(In other words, yes, those two options you posted are equivalent.)

share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.