Andy Gilbert

04-30-2010, 04:11 PM

Ok, ive ben trying for ages now to get this working but cant! And its driving me crazy!

Ok, i follow this to make my terrain, its simple and so far works well:

http://www.codeproject.com/KB/openGL/OPENGLTG.aspx

SDo ive done away with the height map and amde it so i change each height value using my mouse.

So, i thought i would have a go at calculating either face or vertex normals for the terrain.

And i am YET to get it working, even though its like at most 10 lines of extra code!

Ok, its simple, i have an array of X,Y,Z values and nromal values and i am updating each verts height (y) and normal as i alter them using the mouse.

But ignoring that, this is where i currently stand with it:

Using the code below, when i click on my terrain to alter the height or the Y value in the array, the correct face changes its normal to the newly calculated one, but the caluclate done seems to be always 0,0,0, so it changes the new vertex to just dark color!

Heres the normal calulation, its in lua but obviously easy to understand and see any mistakes.

function CalculateTerrainNormals(i,j) --i & j is the vert in the array X & Z where the mouse marker is.

vec1 = {x[i][j],y[i][j],z[i][j]} --Vector 1 }

vec2 = {x[i+1][j],y[i][j],z[i][j]} --Vector 2 }--These make the triangle face

vec3 = {x[i][j],y[i][j],z[i][j+1]} --Vector 3 }

--So if the i & j are 3 & 2.

--vec1 = 3,Y,2

--vec2 = 4,Y,2

--vec3 = 3,Y,3

-- 3,2 ------------ 4,2

-- | /

-- | /

-- | /

-- 3,3 |/

Uvec = {vec2[1] - vec1[1],vec2[2]-vec1[2],vec2[3]-vec1[3] } -- Could be simpler doing

Vvec = {vec3[1] - vec1[1],vec3[2]-vec1[2],vec3[3]-vec1[3] } -- vec2 - vec1, done this way for testing!!!

normalvec[1] = Uvec[2]*Vvec[3] - Uvec[3]*Vvec[2]

normalvec[2] = Uvec[3]*Vvec[1] - Uvec[1]*Vvec[3]

normalvec[3] = Uvec[1]*Vvec[2] - Uvec[2]*Vvec[1]

normal[i][j] = {normalvec[1],normalvec[2],normalvec[3]}

normal[i+1][j] = {normalvec[1],normalvec[2],normalvec[3]}

normal[i][j+1] = {normalvec[1],normalvec[2],normalvec[3]}

--in my draw loop is simple call glnormal(normal[i][j])

--Re-Create the terrain from the array, which now has our new normals in.

GenTerrain(hm,terrain_texture,terrain_size)

end

Hope you can help.

<object width="425" height="350"> <param name="movie" value="http://www.youtube.com/v/mCOWMZo462I"></param> <param name="wmode" value="transparent"></param> <embed src="http://www.youtube.com/v/mCOWMZo462I" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"> </embed></object>

Thanks

Andy

Ok, i follow this to make my terrain, its simple and so far works well:

http://www.codeproject.com/KB/openGL/OPENGLTG.aspx

SDo ive done away with the height map and amde it so i change each height value using my mouse.

So, i thought i would have a go at calculating either face or vertex normals for the terrain.

And i am YET to get it working, even though its like at most 10 lines of extra code!

Ok, its simple, i have an array of X,Y,Z values and nromal values and i am updating each verts height (y) and normal as i alter them using the mouse.

But ignoring that, this is where i currently stand with it:

Using the code below, when i click on my terrain to alter the height or the Y value in the array, the correct face changes its normal to the newly calculated one, but the caluclate done seems to be always 0,0,0, so it changes the new vertex to just dark color!

Heres the normal calulation, its in lua but obviously easy to understand and see any mistakes.

function CalculateTerrainNormals(i,j) --i & j is the vert in the array X & Z where the mouse marker is.

vec1 = {x[i][j],y[i][j],z[i][j]} --Vector 1 }

vec2 = {x[i+1][j],y[i][j],z[i][j]} --Vector 2 }--These make the triangle face

vec3 = {x[i][j],y[i][j],z[i][j+1]} --Vector 3 }

--So if the i & j are 3 & 2.

--vec1 = 3,Y,2

--vec2 = 4,Y,2

--vec3 = 3,Y,3

-- 3,2 ------------ 4,2

-- | /

-- | /

-- | /

-- 3,3 |/

Uvec = {vec2[1] - vec1[1],vec2[2]-vec1[2],vec2[3]-vec1[3] } -- Could be simpler doing

Vvec = {vec3[1] - vec1[1],vec3[2]-vec1[2],vec3[3]-vec1[3] } -- vec2 - vec1, done this way for testing!!!

normalvec[1] = Uvec[2]*Vvec[3] - Uvec[3]*Vvec[2]

normalvec[2] = Uvec[3]*Vvec[1] - Uvec[1]*Vvec[3]

normalvec[3] = Uvec[1]*Vvec[2] - Uvec[2]*Vvec[1]

normal[i][j] = {normalvec[1],normalvec[2],normalvec[3]}

normal[i+1][j] = {normalvec[1],normalvec[2],normalvec[3]}

normal[i][j+1] = {normalvec[1],normalvec[2],normalvec[3]}

--in my draw loop is simple call glnormal(normal[i][j])

--Re-Create the terrain from the array, which now has our new normals in.

GenTerrain(hm,terrain_texture,terrain_size)

end

Hope you can help.

<object width="425" height="350"> <param name="movie" value="http://www.youtube.com/v/mCOWMZo462I"></param> <param name="wmode" value="transparent"></param> <embed src="http://www.youtube.com/v/mCOWMZo462I" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"> </embed></object>

Thanks

Andy