Jump to content
  • Advertisement
Sign in to follow this  
dejavutwo

GLSL Boarder help

This topic is 2583 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 trying to make a shader that creates a boarder around the object. To find where the boarder should be I was trying to find the dot between the normal and the eye vectors. The problem I am having is that dot(N, E) is the same across the entire sphere. Here is some of the code. I've looked at the normal and eye vectors by mapping them to colors, and they were both what I expected them to be, so I'm not sure why the dot product is not producing any variation.

vertex

varying vec3 normal, lightDir, eyeVec;

void main()
{
normal = gl_NormalMatrix * gl_Normal;

vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);

lightDir = vec3(gl_LightSource[0].position.xyz - vVertex);
eyeVec = -vVertex;

gl_Position = ftransform();
}


fragment

varying vec3 normal, lightDir, eyeVec;
uniform float lineWidth;
uniform int levels;

void main (void)
{
vec4 final_color =
(gl_FrontLightModelProduct.sceneColor * gl_FrontMaterial.ambient) +
(gl_LightSource[0].ambient * gl_FrontMaterial.ambient);

vec3 N = normalize(normal);
vec3 L = normalize(lightDir);
vec3 E = normalize(eyeVec);
vec3 R = reflect(L, N);
float lambertTerm = dot(N,L);


gl_FragColor = vec4(dot(N, E), 0.0, 0.0, 1.0);
}

Share this post


Link to post
Share on other sites
Advertisement
I am not sure what exactly you want to achieve but I guess you are trying to implement easy normal based edge detection

using dot(N,E) will produce correct result but if you want to display sharp edge you need to define some thresholds for example 0.3 for your step function
so in your shader you should use something like:


gl_FragColor = vec4( step(0.3,dot(N, E)) , 0.0, 0.0, 1.0);

Torus processed using 0.3 thresholds



torus.png


Share this post


Link to post
Share on other sites
Yes, that is exactly what I am trying to do. The issue I'm having with it, is that is dot(N, E) is returning the same value across the entire sphere, so I can't use it for the edge detection at all.

When I run it the entire sphere is either solid black, or if I reverse one of the vectors, solid red.

Also, thanks for the tip about step.

Share this post


Link to post
Share on other sites
I am glad you figure it out.
For better results you should also consider depth based edge detection (post-process z compare, where you use in second pass inverse projection matrix to reconstruct z in camera space from depth texture and compare distance to neighbour)
and probably easiest of all method which produce probably best results index based (post-process index compare, where you store index of every rendered object into alpha channel or use MRT in first pass and compare neighbour in second pass )

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!