Advertisement

Heightmap to vertex normals

Started by June 11, 2015 07:27 PM
-1 comments, last by _WeirdCat_ 9 years, 8 months ago

basically i made an indexed mesh out of heightmap that is 64x64 grid.

my results so far are: nmpjk2.png

which for me seem very wrong, especially that blue pattern that repeats on the mesh.

i was trying to make a normal out of average 4 neighbour heights, where distance between every cell is equal to: grid_size / 64

additionally i set texcoords so that i will see whole normalmap in each cell i have no idea what i am doing wrong it was supposed to be even tileable pattern but it sux

here is my code that tries to accomplish this:


vec2 exact_texcoord(vec2 tx)
{
vec2 result;
result.x = tx.x - float(int(tx.x));
result.y = tx.y - float(int(tx.y));
return result;
}

vec3 getvertNormal()
{
float ttrans = 1.0/64.0;
//vec4 fh1 = texture2D(waterRefmap, exact_texcoord(texcoord)).r;
vec4 fh2 = texture2D(waterRefmap, exact_texcoord(texcoord+ttrans*vec2(1.0, 0.0)));
vec4 fh3 = texture2D(waterRefmap, exact_texcoord(texcoord*ttrans*vec2(-1.0, 0.0)));
vec4 fh4 = texture2D(waterRefmap, exact_texcoord(texcoord*ttrans*vec2(0.0, 1.0)));
vec4 fh5 = texture2D(waterRefmap, exact_texcoord(texcoord*ttrans*vec2(0.0, -1.0)));

//this is working code that gets a float from 4 unsigned chars (values are from 0 to 1)
float h2 = RGBA_FLOAT_TO_FLOAT(fh2);
float h3 = RGBA_FLOAT_TO_FLOAT(fh3);
float h4 = RGBA_FLOAT_TO_FLOAT(fh4);
float h5 = RGBA_FLOAT_TO_FLOAT(fh5);
//eof

float dst = MAX_HEIGHT - MIN_HEIGHT;


float trans = 1200.0*(1.0/64.0); //cell size
vec3 v0 = vertex_position;
vec3 v1 = vec3(vertex_position.x + trans, MIN_HEIGHT+dst*h2, vertex_position.z);
vec3 v2 = vec3(vertex_position.x, MIN_HEIGHT+dst*h4, vertex_position.z+trans);

vec3 v3 = vec3(vertex_position.x, MIN_HEIGHT+dst*h4, vertex_position.z+trans);
vec3 v4 = vec3(vertex_position.x-trans, MIN_HEIGHT+dst*h3, vertex_position.z);

vec3 v5 = vec3(vertex_position.x-trans, MIN_HEIGHT+dst*h3, vertex_position.z);
vec3 v6 = vec3(vertex_position.x, MIN_HEIGHT+dst*h5, vertex_position.z-trans);

vec3 v7 = vec3(vertex_position.x, MIN_HEIGHT+dst*h5, vertex_position.z-trans);
vec3 v8 = vec3(vertex_position.x+trans, MIN_HEIGHT+dst*h2, vertex_position.z);

vec3 A = vectorAB(v0,v1);
vec3 B = vectorAB(v0,v2);
vec3 n1 = cross(A,B);

A = vectorAB(v0,v3);
B = vectorAB(v0,v4);
vec3 n2 = cross(A,B);

A = vectorAB(v0,v5);
B = vectorAB(v0,v6);
vec3 n3 = cross(A,B);

A = vectorAB(v0,v7);
B = vectorAB(v0,v8);
vec3 n4 = cross(A,B);

return normalize((n1+n2+n3+n4)/4.0);
}


then i must neagte resulting normal to see that green color ;]

so 

gl_FragColor = vec4(-getvertNormal(), 1.0);
 

heres normalmap stretched to size of the grid:

nmpjk3.png

This topic is closed to new replies.

Advertisement