# OpenGL Normal Map Generation

This topic is 3744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a file which has a only vertex information - (x,y,z) corresponding to each verticies.I need to generate Normal Map, so as to do Normal Mapping. How can I generate Normal map. I don't want to use Blender or similar tools for the Normal Map generation, rather I want to use C/C++/OpenGL for it. How to generate Normal Maps?

##### Share on other sites
You need two versions of the model to generate a normal map:
1) A lowpoly model with a uv map
2) A highpoly version of the same model

The algorithm:
normal_map = new texture(width,height)for each pixel(x,y) in normal_map do    u = x/width , v = y/height    find position(=p) and normal(=n) on lowpoly model corresponding to (u,v)    find the nearest point(=np) to p on the highpoly model (along n)    read the normal at np from the highpoly model and write it to the normal_mapend

##### Share on other sites

Thanks a lot kambiz.

Quote:
 Original post by KambizYou need two versions of the model to generate a normal map:1) A lowpoly model with a uv map2) A highpoly version of the same model

What is the difference between lowpoly model and highpoly model.
Is it possible to generate them from a give 3D Model file such as *.obj ( wavefront).I only have vertex(x,y,z) information of the polygon and I need to generate Normal Map.

##### Share on other sites
Hm, I have the feeling that you aren't looking for normal map generation but for vertex normal generation if you only have the positions of the vertices.

There are different approaches to automatically generate normals for a vertex. For example, you could do this:

- calculate the normals of all faces which the vertex is part of
- take the average of those normals as the vertex normal.

To calculate the normal of a face with vertices A, B and C, you create two vectors AB = B-A and AC = C-A and take their cross product:

face.normal = normalize(cross(face.B - face.A, face.C - face.A));

The only problem is the winding order of the vertices, because depending on the correct order of A, B and C you'd get a normal that's pointing inwards or outwards the mesh. For purely convex meshes that's easy, because the normal will always be pointing away from the center point (= dot(center-face.A, face.normal) < 0)

##### Share on other sites
Are you sure you are looking for normal maps?

(Image from www.game-artist.net)

Per-vertex normals can be calculated as VizOne described, although they should ideally exported from the modeling program. To make sharp edges light correctly you also need to use a maximum smoothing angle. Modeling tools give you a lot of control, you can use different smoothing groups with different smoothing angles... so don't expect to get the same result calculating the normals yourself.

##### Share on other sites

Quote:
 Original post by KambizAre you sure you are looking for normal maps?(Image from www.game-artist.net)

Yes this is what I want.But I don't have the required input.
Ok No problems. I will attempt as suggested by kambiz.
I will seek the help of fellow classmate to generate the poly models.

But I have few basic doubts.

Quote:
 find position(=p) and normal(=n) on lowpoly model corresponding to (u,v)

Does this involve a linear search of position in the lowpoly model file corresponding to the (u,v) coordinate?

Quote:
 find the nearest point(=np) to p on the highpoly model (along n)

Does it involve distance compuatation between the p and highpoly model?

What is the Optimized way?

##### Share on other sites
If you want to generate a normal map for a texture map all the graphics guys where I work are loving CrazyBump

don't know if this is what you want but its a good tool to generate normal maps for things that already have a UV texture map.

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633363
• Total Posts
3011514
• ### Who's Online (See full list)

There are no registered users currently online

×