Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Calculating vertex normals with OpenGL

This thread is locked; no one can reply to it. rss feed Print
Calculating vertex normals with OpenGL
Goodbytes
Member #448
June 2000
avatar

How do I get OpenGL to calculate vertex normals automatically? I'm pretty sure there is a way to do this...

I want to use smooth lighting in my program but I don't want to use up all the extra memory to store them in my vertex structs(I realize that the normals use up space in the OpenGL buffer anyways but it's still less space).


--
~Goodbytes

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

I don't want to use up all the extra memory to store them in my vertex structs

Yes, it's much better to recalculate them every frame. Most 3D games don't run better than 3 fps nowadays anyway ;)

Seriously, store them. To recalculate every single flippin' normal every single flippin' frame would reduce your frame rate to a joke.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Bob
Free Market Evangelist
September 2000
avatar

goodbytes: Look up glEnable(GL_AUTO_NORMAL), glEvalCoord, glMap1 and glMap2.

--
- Bob
[ -- All my signature links are 404 -- ]

Thomas Harte
Member #33
April 2000
avatar

Just a few things - OpenGL can't do 'vertex normals' for polygon meshes automatically, because strictly speaking such things do not exist. If the mesh was not generated from a function (in which case the derivative can be used) then they are usually approximated by averaging the normals of all the surfaces that meet at a point.

However in the general case OpenGL only knows about each vertex in relation to the surface it is currently occupied with. This isn't anything like always true, but is the lowest common denominator and certainly true if you're just using Vertex?[f/i/v] calls between Begin/Ends. So it can only use the single surface normal, giving the effect of 'flat shading'.

As for the memory overheads, most modern games rely on function based model simplification, especially on the world rather than individual characters, so the normals can be cheaply knocked up per re-meshing, often without any of the highly expensive square roots associated with surface normals.

Goodbytes
Member #448
June 2000
avatar

23yrold3yrold: Alright, alright. I'll store them, then.

Bob: I looked up those functions, but unfortunately the description is pretty cryptic to me and there are no examples.

The 3D modeling software that I use automatically outputs vertex normals anyway, so why am I so worried about this? Good question. ;)

I guess if calculating the normals takes as long as I feared it might, I probably should just store them.

Thomas Harte: Thanks for the info. But what do you mean by "function based model simplification?"


--
~Goodbytes

Thomas Harte
Member #33
April 2000
avatar

Sorry about that, being unclear is one of my favourite things! That and Irn Bru.

What I wrote should be read as (function based model) simplification rather than perhaps as function based (model simplification), since the latter implies you give me your complex model and I give you a simple version.

I was referring to higher order surfaces, in most current cases Beziers or NURB, for which you can dynamically select how many flat polygons you want to represent the surface, usually as a function of size taken up by the surface on screen. For these surfaces you can get the normals simply by calculating the derivative of the function at the point, which is a lot simpler than it sounds - usually just a few adds in an iterative solution.

23yrold3yrold
Member #1,134
March 2001
avatar

I'll take back what I said: if OpenGL has functions for automatic normal generating, it's news to me. I do my 3D the old-fashioned way :) Still, plenty of people store the normals, so that's not a big deal, and I have a hard time believing OpenGL goes just as fast calculating normals in realtime as opposed to just reading stored values.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Go to: