glNormal3d overhead?

Hi, I have a couple of questions regarding glNormal3d().

I have some generic drawing code that renders either to a 2D or 3D view. In the 2D view the z value is always 0.0 so everything is fine. Now I am introducing the 3D stuff and I have realised that facet normals or vertex normals are needed. This is the lowest level of my code and performance is an issue as I am receiving realtime data from some instrumentation and rendering it immediately. Obviously, I’m keeping my conditional statements to a minimum.

My question is, what is the overhead of putting in calls to glNormal3d() when it is not really needed in the 2D code and will it screw up the 2D rendering if the vertex or facet norm is zero length?

Actaully, that was two questions cunningly disguised as one. I hope no-one noticed. :wink:



glNormal() is a function call that brings all the overhead of any function. Then you can add whatever code that may be in it. So it would seem reasonalble to think it is more expensive than a simple if statement.

My recommendation have two drawing functions, one for 2D and one for 3D, and use one if statement to choose which one is applicable. Otherwise you’ll have to experiment to see if using glNormal is more expensive than controlling it with an if statement.

Using imediate mode (glNormal, glVertex, glColor etc) is always slow, the reason? function calls! OpenGL provides other mechanisms to draw polygons without as much overhead of function calls, namely vertex arrays and display lists.

Display lists are the fastest on most implimentations, but have the draw back of being un-editable once created. Vertex arrays have that advantage, and are suggested if you need to change your geometry.

Hmm, thanks.

my code is a hierarchy of classes. I appreciate the advantages of display lists, however, the objects that I am drawing are selectable, so I am stuck with immediate mode. I suppose that I could override display lists with immediate mode rendering if the user interacts with the model. Maybe that could come as a future enhancement.

Right now, I store m_x, m_y and m_z as members of my class and have get() and set() methods on each of them. Are you saying that glVertex3v() is significantly faster than glVertex3f()?

If so, I can rejig my vertex wrapper class to store the vertices as GLdouble m_xyz[3]; and provide a getvector() method.

Thanks for the advice, plenty to think about.


glVertex3fv would be faster than glVertex3f for the simple reason that your passing one parameter instead of 3. But there is a special function of OpenGL that handles specially designed Vertex Arrays. Do some research on glDrawElements and its related functions or check out the last part of Chapter 2 of the redbook.

Originally posted by mcsellski:
My question is, what is the overhead of putting in calls to glNormal3d() when it is not really needed in the 2D code and will it screw up the 2D rendering if the vertex or facet norm is zero length?

I have three answers to your two questions.

The calls to glNormal could be insignificant compared to the overhead of lighting.

Split the order of your drawing into 2D and 3D and turn off lighting for the 2D. Then you don’t need to worry about normals and the 2D won’t be slowed down by the lighting.

If you are worried that adding a tiny thing like a call to glNormal will cause a problem then maybe you should step back and rethink how you are drawing the scene. Try these optimizations:
Have you profiled your program to find out where the time is spent? If no, then you have no way of knowing if anything you do will make any difference at all. If yes,…
Is there a less expensive way of displaying your data? If no,…
Are you drawing things that can’t be seen and could easily be culled? If no,…
Are you using strips and fans to draw polygons? If yes,…
Are you using display lists or vertex arrays for geometry that never changes shape? If yes,…
Are you drawing front to back if there is lots of overdraw? If yes,…
Is your scene drawn in steps that minimizes the number of state changes? If yes, then assume there must be a better way and try again starting with the profiling.

[This message has been edited by Jambolo (edited 03-10-2002).]

That’s great guys, thanks for the tips.

My 2D and 3D code is separated, the reason that I asked was that I wondered if I could combine my 2D and 3D code in some places.

I haven’t profiled the 3D stuff yet. Thanks for the prompt, I’ll do it today.

Like I said before, I haven’t used dispaly lists as I need user selection. Of course, I could use display list and only use immediate mode for selection, but that would have to come much later due to the extra work involved.

Thanks again for the advice.



Dispaly lists should not affect selection. If it does it may be that too many items are grouped in the list. In that case break your list down into lists that each represent selectable objects.