Jump to content
  • Advertisement
Sign in to follow this  
cherryyosh

per pixel normals

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am woundering what is the best / easyest way to generate per pixel normals? and while im here, anyone know how to ( if its possible ) generate tangents on the gpu? thanks!!!

Share this post


Link to post
Share on other sites
Advertisement
You could pass the vertex normal from the vertex shader to the fragment shader, it will interpolate across the primitive much like vertex colors do.
You could get it from a texture, bump mapping gets its per pixel normal from a normal map, these can be generated from hi poly models, or directly from textures with programs like crazy bump, or nvidias photoshop filter, although crazy bump is better, but nvidias plugin is free.
You could probably generate tangents and binormals on the shader, but usually both are passed in as vertex attributes, or one is, and the other is generated from the vertex normal and binormal (or tangent) cross product.
You need textured models to generate them offline, because they are related to the direction of the texture coords.

Share this post


Link to post
Share on other sites
Quote:
Original post by NumberXaero
You could pass the vertex normal from the vertex shader to the fragment shader, it will interpolate across the primitive much like vertex colors do.


I currently do this, but the output is not what I what I am expecting..

Here is what im doing

[vertex]
Normal = normalize( gl_NormalMatrix * gl_Normal );

[fragment]
vec3 n = normalize( Normal );

gl_FragData[0] = vec4( n, 1.0 );


I expect this to draw a output like the one found here
http://www.filterforge.com/filters/6568-normal.jpg
(btw how do I use urls? )

but dont get anything like that..

Share this post


Link to post
Share on other sites
[Vertex]

varying vec3 vv_Normal;

void main(void)
{
gl_Position = ftransform();

vv_Normal = gl_NormalMatrix * gl_Normal;
}

[Fragment]

varying vec3 vv_Normal;

void main(void)
{
gl_FragColor = vec4(vv_Normal, 1.0);
}

Try ATi's RenderMonkey, great for quick tests like this.

from faq link top right of page

Links can be added to your posts using regular HTML. For example:
your_link_text
Don't forget the http://, or the link won't work correctly!

Share this post


Link to post
Share on other sites
Thanks for the help!
But is that really the output I should be getting, as thats the same output with the normalization, and its nothing like the normal maps I found else where.

Share this post


Link to post
Share on other sites
If you are render to your backbuffer, which is probably a RGBA8 format, it will clamp values [0..1]
You need to rescale values

varying vec3 vv_Normal;

void main(void)
{
vec3 normal = normalize(vv_Normal);
normal = normal * 0.5 + 0.5;
gl_FragColor = vec4(normal, 1.0);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by V-man
If you are render to your backbuffer, which is probably a RGBA8 format, it will clamp values [0..1]
You need to rescale values

varying vec3 vv_Normal;

void main(void)
{
vec3 normal = normalize(vv_Normal);
normal = normal * 0.5 + 0.5;
gl_FragColor = vec4(normal, 1.0);
}


Thanks for the help ^^
Im rendering to a MRT using Shader Maker ( renderman is windows only :[). The code you gave me looks MUCH better. Only diffrent is the colors are slightly off from what im expecting. See above for a link the the torus im expecting and im looking for this with a sphere
sphere

btw

[vertex]
varying vec3 Normal;

void main(){
Normal = gl_NormalMatrix * gl_Normal;
gl_Position = ftransform();
}

[frag]
varying vec3 Normal;

void main(){
vec3 n = normalize( Normal );
n = n * 0.5 + 0.5;

gl_FragData[0] = vec4( n, 1.0 );
}

Share this post


Link to post
Share on other sites
Hi, cherryyosh.

I'm a little bit curious. Why are you saying the screen shot you posted is better?

Per pixel normal itself is not something interesting for the end users to see. So, although it helps debugging application, visualizing them is probably not the final outcome. Didn't you try to plug your per pixel normals into the actual application?

I think you are probably working on the BRDF. So...

If you are trying to implement Phong shading (i.e. per pixel lighting), the way you calculating per pixel normal is correct already.

If you are trying to do deferred rendering (i.e. saving the attributes in some screen space textures) for more complicated effect, you'll need to render your per pixel normals to a floating point texture using FBO as render target. And, the way you calculating per pixel normal is still correct in this case.

By the way, tangent calculation had been discussed thoroughly many times in this forum. Just search this forum, you will get the answer you needed and the solution. (including why it is not useful to generate vertex tangent on the gpu)

Share this post


Link to post
Share on other sites
Quote:
Original post by ma_hty
Hi, cherryyosh.

I'm a little bit curious. Why are you saying the screen shot you posted is better?

Per pixel normal itself is not something interesting for the end users to see. So, although it helps debugging application, visualizing them is probably not the final outcome. Didn't you try to plug your per pixel normals into the actual application?

I think you are probably working on the BRDF. So...

If you are trying to implement Phong shading (i.e. per pixel lighting), the way you calculating per pixel normal is correct already.

If you are trying to do deferred rendering (i.e. saving the attributes in some screen space textures) for more complicated effect, you'll need to render your per pixel normals to a floating point texture using FBO as render target. And, the way you calculating per pixel normal is still correct in this case.

By the way, tangent calculation had been discussed thoroughly many times in this forum. Just search this forum, you will get the answer you needed and the solution. (including why it is not useful to generate vertex tangent on the gpu)



Thank you for the help, I am working on a deferred rendering, using the normal for the map, had found out earlly that that normals where correct, its just the pictures didnt show them head on. now im trying to figure out how to properly do the position map for it. sadly, it doesnt seem to be as easy as Position = gl_ModelViewMatrix * gl_Vertex; ( thought that would be nice if I need to know what quad it is in :P ).

Ill look on how to find the tangent, thanks for the information.

Share this post


Link to post
Share on other sites
Quote:
Original post by cherryyosh
... using the normal for the map, had found out earlly that that normals where correct, its just the pictures didnt show them head on. ...


Okay, I understood why you thought the image you posted is correct. However, it is considered correct because it is used for a while before. It can also be wrong already since it is written. By the way, it is probably wrong in the first place.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!