Hi,

I am trying to generate and merge normal map in runtime. I stuck upon adding rgb pixels of normal map.

Since opengl stores texture values in unsigned int format. I need to add texels signed. glBlendFunc(GL_ONE, GL_ONE). Doesnot help

Exact combining is required is ((R1-127)+(R2-127))+127 How can I acomplish this. (Using blendequations)

Thanks

**0**

4 replies to this topic

Sponsor:

###
#2
Members - Reputation: **817**

Posted 23 April 2012 - 05:12 PM

Your equation is number + number + offset. That is addative not merge. I'm confused as to what you are doing, if you want to blend two normal maps and then use it for a normal map you need a shader. glBlendFunc only blends things that go to the screen.

###
#3
Members - Reputation: **101**

Posted 24 April 2012 - 10:06 PM

Ok. let me explain. I started with programmaticality generating height map of my scene. Its straight forward. I am rendering small height sprites to the fbo. glBlendFunc(GL_ONE, GL_ONE). With additive function I get fairly nice heightmap. In order to use these heightmap. I am converting it to normalmap. programmability using a shader.

color.r=dFdx(color.r)

color.g=dFdy(color.g);

But this operation is hell of expensive and normal where not smooth. Now I want eliminate the hieghtmap and directly trying to generate normal map. (Using normal sprite?). However It seems that normals does not add up. Feels like I should drop the idea..

color.r=dFdx(color.r)

color.g=dFdy(color.g);

But this operation is hell of expensive and normal where not smooth. Now I want eliminate the hieghtmap and directly trying to generate normal map. (Using normal sprite?). However It seems that normals does not add up. Feels like I should drop the idea..

###
#4
Members - Reputation: **817**

Posted 24 April 2012 - 11:32 PM

If this is for a tool like a terrain editor, it doesn't need to be very fast, even though if you generate the normal map every frame, you should still be fairly good. When blending are you drawing every single paint stroke or do you keep the old copy and just apply 1 stroke each frame to the previous combined image.But this operation is hell of expensive and normal where not smooth. Now I want eliminate the hieghtmap and directly trying to generate normal map.

You want to sample the surrounding 8 pixels and current pixel, average them by dividing by 9 to smooth the normals. The two lines you posted are not what you want to generate a smooth normal map from a height map.But this operation is hell of expensive and normal where not smooth. Now I want eliminate the hieghtmap and directly trying to generate normal map

pixel_x_step = 1.0/TextureWidth;

PixelLeft = texture2D(x - pixel_x_step, y); //sample the pixel to the left of the current pixel

//repeat for all other 8 neighbors

As for getting rid of the heightmap, that doesn't even make sense, how will you know how high to shift your terrain verts. Normals are directions (-1 to 1) or (0 to 1), not displacements (0 to MAX_HEIGHT) for instance terrain might have a height of 10,000 if its a tall mountain.

###
#5
Members - Reputation: **101**

Posted 08 May 2012 - 01:51 AM

OK. I gave up bumpmaping. Cause I am trying to do all these calculation in real time, on a iphone. Not going to happan. Thanks for all.

I am writing bit of my shader code, for those who are interested. I hope its correct :?). It seems to working; but as I said before generated normalmap is not smooth.

vec3 normal= texture2D(heighttexture, gl_TexCoord[0].xy).xyz;

normal.r=dFdx(normal.x)*50.0;

normal.g=dFdy(normal.y)*50.0;

normal.b=0.0;

float z = abs(normal.r)+abs(normal.g);

if( z > 1.0 )

{

normal.rg/=z;

}

float blue = 1.0-length(normal);

normal=normal*0.5+0.5;

normal.b= blue;

This is another code that I tried, generates smooth normals, it makes again expensive.

const float width=0.01;

const float strength=1.0;

vec3 nomralfrombump(sampler2D atex,vec2 textcoord)

{

vec4 heretex = texture2D(atex, textcoord.xy);

if(heretex.a == 0.0)

return vec3(0.0,0.0,0.0);

vec2 dus= vec2(textcoord.x+width, textcoord.y);

vec2 dvs= vec2(textcoord.x, textcoord.y+width);

vec4 du4 = heretex - texture2D(atex,dus);

vec4 dv4 = heretex - texture2D(atex,dvs);

du4.x=du4.x*strength;

dv4.x=dv4.x*strength;

float z = abs(du4.x)+abs(dv4.x);

if( z > 1.0 )

{

du4.r/=z;

dv4.r/=z;

}

float dw = sqrt(1.0-du4.r*du4.r - dv4.r*dv4.r);

vec3 normal=vec3(du4.x*0.5+0.5,dv4.x*0.5+0.5,dw);

return normal;

}

I am writing bit of my shader code, for those who are interested. I hope its correct :?). It seems to working; but as I said before generated normalmap is not smooth.

vec3 normal= texture2D(heighttexture, gl_TexCoord[0].xy).xyz;

normal.r=dFdx(normal.x)*50.0;

normal.g=dFdy(normal.y)*50.0;

normal.b=0.0;

float z = abs(normal.r)+abs(normal.g);

if( z > 1.0 )

{

normal.rg/=z;

}

float blue = 1.0-length(normal);

normal=normal*0.5+0.5;

normal.b= blue;

This is another code that I tried, generates smooth normals, it makes again expensive.

const float width=0.01;

const float strength=1.0;

vec3 nomralfrombump(sampler2D atex,vec2 textcoord)

{

vec4 heretex = texture2D(atex, textcoord.xy);

if(heretex.a == 0.0)

return vec3(0.0,0.0,0.0);

vec2 dus= vec2(textcoord.x+width, textcoord.y);

vec2 dvs= vec2(textcoord.x, textcoord.y+width);

vec4 du4 = heretex - texture2D(atex,dus);

vec4 dv4 = heretex - texture2D(atex,dvs);

du4.x=du4.x*strength;

dv4.x=dv4.x*strength;

float z = abs(du4.x)+abs(dv4.x);

if( z > 1.0 )

{

du4.r/=z;

dv4.r/=z;

}

float dw = sqrt(1.0-du4.r*du4.r - dv4.r*dv4.r);

vec3 normal=vec3(du4.x*0.5+0.5,dv4.x*0.5+0.5,dw);

return normal;

}