• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Terrain from a heightmap...normals

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

## Recommended Posts

Hello, everybody. I''ve created a small terrain engine, that generates a terrain out of a heightmap. But when enabling lighting, everything looks blocky. Could somebody modify the source code to make it look good? The function that generates the terrain looks like this:
 ` char BuildTerrain() { terr=glGenLists(1); glNewList(terr,GL_COMPILE); glBegin(GL_TRIANGLE_STRIP); for(unsigned int x=0;x
--- marius http://fr.ee/linux

##### Share on other sites
Hi there,

take the normals for the four faces adjoining the vertex
and average these ie.

*---*---*
|n1 | n2|
*---p---*
|n3 | n4|
*---*---*

p.n = (n1 + n2 + n3 + n4) * .25

Hope this helps

##### Share on other sites
just one thing.

If you are using quads, do as MButcher.

If you are using triangles or triange strips, average the normals of all six surounding triangles. This will look even better.

.-o-o
|/|/|
o-O-o
|/|/|
o-o-.

##### Share on other sites
Hi,

Can you send me the code for your engine ?

Leyder Dylan
dylan.leyder@ibelgique.com

##### Share on other sites
In fact there is an additional optimization that can be made. The most visually appealing way to calculate normals uses a weighted average - I think it weights the normal of each neighboring polygon based on the size of the angle at the particular vertex, but I don''t recall exactly. Does anyone know the algorithm I am thinking of?

##### Share on other sites
I''ve uploaded the entire terrain demo to:
http://www.hot.ee/marius/terrain/terrain.tar.gz

It''s compressed with tar and gz, but winzip (and possibly others) still know how to open it.

It''s created on a linux mandrake 7.2 system, but can be compiled for windows with the mingw32 compiler
it can be found at:
http://www.libsdl.org/Xmingw32/index.html

also I''m having HUGE problems with walking on the terrain. If anybody could help me here, I would be really thankful.

I''ll try the normal calculations soon...

---
marius
http://fr.ee/linux

##### Share on other sites
What I was doing on my orriginal terrain engine (I''m currently rewriting it to add new features and improve speed) was as pez said. calculating the normals for each of the six adjacent triangles then simply add all the values together and divide by 6. That gave excelent results in my engine.

##### Share on other sites
ok, i''ll try the method pez and avian suggested.
I''ll post back soon...

---
marius
http://fr.ee/linux

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634047
• Total Posts
3015228
×