GLSL Boarder help

Started by
5 comments, last by SaTANO 12 years, 5 months ago
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);
}
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


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.
Output a debug color : glFragColor = N, 1.0 and then glFragColor E, 1.0

Figure out why that is not working.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Here are the debug colors.

Something I just now noticed while doing this, is that -N and E give the same outputs.
Looks like I got it working by changing the view mode to perspective. Thanks for the help everyone.
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 )

This topic is closed to new replies.

Advertisement