Started by May 13 2008 10:24 AM

,
9 replies to this topic

Posted 13 May 2008 - 10:24 AM

Hi, I am trying to create my own terrain class.So far I've done the heightmap loading function and index/vertexbuffer generation functions,it can draw itself too.But I stuck at calculating the normal vectors.I have searched a little bit and I found 2 approaches.
1)duplicating the vertex normals(msdn says it)
2)getting the avarage vertex normal
for the first solution I have no idea how to do it, my vertex struct can only hold one normal vector..
for the second I "imagined" a solution which requires more than 500.000 normal vector calculation for a 256*256 map, I don't think it is the proper way.And this is my first time writing a terrain engine so it seems a lil bit complex here.
What should I do? A vertex is used for 6 different triangles. should I make a buffer that holds all plane normal vectors for triangles(this makes, I suppose, 130k triangles) , and then call the neighboor triangles for every vertex and get their avarage?
Thanks, any help would be appreciated :)
btw: I am using vc++ 2008, and directx 9.0..

Posted 13 May 2008 - 10:32 AM

The most common approach is, I think, to use the neighboring vertices to calculate the normal. Basically, if you draw a line from the vertex just north of the one you are trying to find the normal for to the one just south and then calculate the normal to that line, you've got an approximate normal to your vertex.

Obviously, this is CPU (or GPU) intensive. Therefore, if your terrain does not deform, you should only calculate these normals once. If your terrain does deform, try to only recalculate the normals that have changed. If your terrain deforms heavily and over large areas, move the calculations onto the GPU.

Obviously, this is CPU (or GPU) intensive. Therefore, if your terrain does not deform, you should only calculate these normals once. If your terrain does deform, try to only recalculate the normals that have changed. If your terrain deforms heavily and over large areas, move the calculations onto the GPU.

-------------Please rate this post if it was useful.

Posted 13 May 2008 - 10:38 AM

hopefully my terrain doesn't change during the game(at least for now..) but I would be happy if you can explain it more, should I first calculate normal vectors of the lines and buffer them ? Than it becomes a little bit easier but I have my doubts if I could make a good "neighboor" calculation, I mean in the middle its not that hard but edges are harder.

Posted 13 May 2008 - 10:52 AM

As you know, you should just add up all the six adjoining edges of the triangles, and then devide that number by 6... But since this is CPU/GPU intensive, you could do two things:

1. Have them pre-calculated in a custom file format

2. Have them calculated on the fly, beginning at your current position working outwards.

If this concerns something like an explosion, you could very easily calculate this with a bounding sphere...

if ((VertexOnTerrain - sphere.center) * (VertexOnTerrain - sphere.center) < (sphere.radius * sphere.radius)) returns true

then you should recalculate the normal... To make this even more effective, add some quadtrees, or octrees, I don't know what kind of game you're making. Lighting might look strange if you did it another way.

Posted 13 May 2008 - 10:57 AM

Your heightmap is just a two dimensional image with height values. To compute the normal for each pixel you just need to do finite differencing.

The following pseudo code could expain it:

This will also work (maybe better)

You will have to special case the border pixels since they do not have the full set of neighbors.

The following pseudo code could expain it:

vec3f u(1.0f, 0.0f, map(x + 1, y));

vec3f v(0.0f, 1.0f, map(x, y + 1));

normals[x][y] = normalize(cross(u, v));

This will also work (maybe better)

vec3f u(1.0f, 0.0f, map(x + 1, y) - map(x - 1, y));

vec3f v(0.0f, 1.0f, map(x, y + 1) - map(x, y - 1));

normals[x][y] = normalize(cross(u, v));

You will have to special case the border pixels since they do not have the full set of neighbors.

Posted 14 May 2008 - 05:22 AM

Hey again, I was thinking about your solution Trenki. You basicly build 2 lines from the previous vertexes(according to z and y axes) to following vertexes and cross product them.

But I didnt understand where did 1.0f's and 0.0f's come from and which axe do you use as "height".I am now confused because I looked at some different coordinate systems. So save me from this vagueness please : )

But I didnt understand where did 1.0f's and 0.0f's come from and which axe do you use as "height".I am now confused because I looked at some different coordinate systems. So save me from this vagueness please : )

Posted 14 May 2008 - 06:40 AM

x increases horizontaly, y is in the vertical direction and the height is simply the height of the terrain which is stored in the 2d heightmap image.

The 0.0f and 1.0f values for the vectors x and y coordinates were just random values. You could add a scaling factor to these and get different results depending on the range of the heightmap values.

Basically what this code does is to create two vectors (one in the x and one in the y direction) which points from the current heightmap sample to the adjacent sample. From these two vectors you can compute the cross product to get another vector which is perpendicular to both. This one can be used as the normal.

In practice this is no different than making a quad or triangle mesh from the heightmap image and computing the normals for each vertex but since the input is already a heightmap we can make a shortcut.

The 0.0f and 1.0f values for the vectors x and y coordinates were just random values. You could add a scaling factor to these and get different results depending on the range of the heightmap values.

Basically what this code does is to create two vectors (one in the x and one in the y direction) which points from the current heightmap sample to the adjacent sample. From these two vectors you can compute the cross product to get another vector which is perpendicular to both. This one can be used as the normal.

In practice this is no different than making a quad or triangle mesh from the heightmap image and computing the normals for each vertex but since the input is already a heightmap we can make a shortcut.

Posted 14 May 2008 - 09:27 AM

I wrote an algorithm using your idea and I think it works, but I have encountered a further problem, as you can see on the image the base heightmap is drawn but there are lots of other meaningless vertexes.Do you know what causes this? I am positive that you have seen many of these "errors" but this is my first time ^^

where is the error ? maybe at the loading code or vertex/index buffer creation code, or maybe even at the drawing code?

I will post if you wish a part of my code.

here is the screenshot:

http://img117.imageshack.us/img117/153/adszpw7.jpg

btw: I can draw a flat heightmap without any problems.

where is the error ? maybe at the loading code or vertex/index buffer creation code, or maybe even at the drawing code?

I will post if you wish a part of my code.

here is the screenshot:

http://img117.imageshack.us/img117/153/adszpw7.jpg

btw: I can draw a flat heightmap without any problems.

Posted 14 May 2008 - 01:17 PM

Well, I've seen like 100 open source heightmap loaders, those must include what you want, right?

http://www.videotutorialsrock.com/opengl_tutorial/terrain/home.php

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=34

http://www.swiftless.com/tutorials/terrain/terraintuts.html

I'm not sure if this helps, but it's my effort to help lol. I've never dealt with heightmaps btw.

http://www.videotutorialsrock.com/opengl_tutorial/terrain/home.php

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=34

http://www.swiftless.com/tutorials/terrain/terraintuts.html

I'm not sure if this helps, but it's my effort to help lol. I've never dealt with heightmaps btw.

Posted 29 May 2008 - 04:13 AM

Hi, I am implementing ambient occlusion as a project at university (FFPipeline) and I have a problem with normal calculation. I have a TGA image with unsigned bytes (0, 255). I load the image with a helper function filling the 1-dimension container as a vector, which has all the vertices in once (3 coordinates by three, in order to use it alongside as vertex array later).

So I have calculated the faces of the mess as

Face 1: (Pi,j , Pi,j+1 , Pi+1,j+1) and Face 2:(Pi,j , Pi+1,j , Pi+1,j+1).

I have calculated all the normal vectors per face and now I have to combine them in order to keep the normal vectors per vertex, but I can't find an efficient algorithm no matter what I have tried. Internal vertices of the 3D Terrain are adjacent to 6 faces each. Is there a common technique calculating this?

For example, consider the following drawing. Do you have in mind a technique that iteratively will find that for the internal pointed vertex the 0,1,2,12,13,14 faced are adjacent?

http://i23.photobucket.com/albums/b352/grnemo/0001.jpg

So my problem is algorithmic, somehow :P

Thank you very much!!!

edit: Just melting on googling and found this

The Sliding Six Algorithm <http://www.gamedev.net/reference/articles/article2264.asp>

Thx GameDev.net

[Edited by - grnemo on May 29, 2008 10:13:43 AM]

So I have calculated the faces of the mess as

Face 1: (Pi,j , Pi,j+1 , Pi+1,j+1) and Face 2:(Pi,j , Pi+1,j , Pi+1,j+1).

I have calculated all the normal vectors per face and now I have to combine them in order to keep the normal vectors per vertex, but I can't find an efficient algorithm no matter what I have tried. Internal vertices of the 3D Terrain are adjacent to 6 faces each. Is there a common technique calculating this?

For example, consider the following drawing. Do you have in mind a technique that iteratively will find that for the internal pointed vertex the 0,1,2,12,13,14 faced are adjacent?

http://i23.photobucket.com/albums/b352/grnemo/0001.jpg

So my problem is algorithmic, somehow :P

Thank you very much!!!

edit: Just melting on googling and found this

The Sliding Six Algorithm <http://www.gamedev.net/reference/articles/article2264.asp>

Thx GameDev.net

[Edited by - grnemo on May 29, 2008 10:13:43 AM]