Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the quake 2 md2 file format (theory)

Tags:

c++

c

opengl

i am trying to load md2 files in opengl but i noticed that most example programs just use a

precompiled list of normals. something like this.....

//table of precalculated normals  
{ -0.525731f,  0.000000f,  0.850651f },   
{ -0.442863f,  0.238856f,  0.864188f },   
{ -0.295242f,  0.000000f,  0.955423f },   
{ -0.309017f,  0.500000f,  0.809017f },   
...  
... 

Ok this may sound abit dumb, but i thought each model is made of different triangles, how then is it possible that you can use one set of precompiled normals to render all models? It seems abit strange and any ideas will be appreciated.

like image 494
Dr Deo Avatar asked Nov 18 '09 21:11

Dr Deo


2 Answers

You could use a precompiled table of normals, and use a lookup table to select one that is 'good enough' for a particular case. Each triangle is on a distinct plane, and it's that plane that has a normal, not the triangle itself.

For instance, lets imagine we have a point. Expand that point into a sphere for the purposes of this discussion, makes it a little easier to grasp conceptually. If you draw a perfect circle around that sphere on the y axis, then rotate that circle in the x axis 1 degree each time, you'll end up with 360 circles. If you take a normal at 1 degree intervals along each of those circles, you'll end up with 360 ** 2 points. From there, your normal is the vector from the center of the sphere to that point on the sphere, and it is a normal for a plane constructed tangential to point on the sphere. What you end up with if you calculate these two for every point on that sphere is a precalculated table of normals, which will almost certainly be good enough for most situations. Now you just need to design a lookup scheme for that data (plane -> normal).

like image 107
Matthew Scharley Avatar answered Oct 14 '22 17:10

Matthew Scharley


It's been answered already but I want to shed some more light on it.

The table contains vectors that cover the unit sphere's surface pretty uniformly. It seems the set of 162 vectors are the corners of a subdivided icosahedron. This is done to lossily compress 3D vectors of unit length to an index (8 bits), see vector quantization. For storing an arbitrary normal vector you can search the table for the closest match and store the index of this match instead. With this table of 162 well distributed vectors the angle between the original vector and the approximated one is expected to be below 11° which seems to be good enough for the Quake2 engine.

like image 30
sellibitze Avatar answered Oct 14 '22 17:10

sellibitze