PDA

View Full Version : Strange CG shading model problem



Shao tian
03-09-2011, 01:29 PM
Hey guys:
I am trying to shade opengl point sprites to make them look like spheres. The shading model I am using is (color = emissive + ambient + diffuse + specular). Could any one help to see what kind of mistake might cause this problem. To see what problem is, please have a look at attached screenshot. They supposed to look like spheres with lighting effect. But the current resulting effect looks so strange.

I also attach the CG fragment program code here.




vec4 mesh_fragment(uniform float4 c:COLOR,
float2 texCoord:TEXCOORD0,
uniform sampler2D decal):COLOR
{

float4 position=float4(texCoord,0,1);

float2 normal;
normal= texCoord.xy* 2.0 - vec2(1.0);

float mag = dot(normal.xy, normal.xy);
if (mag > 1.0) discard; //clip the point sprite to make it a circle

//start initializing shading model parameters
float3 Ke={0,0,0};
float3 Ka={0,0,0};
float3 Ks={1, 1, 1};
float3 Kd={1, 1, 1};
float3 globalAmbient={1,1,1};
float3 lightColor={1,1,1};

vec3 lightPosition=vec3(-100,100,-100);

float3 eyePosition={0, 0, 13};

float shininess=32.0;


//Start performing shading model algorithm
float3 P = position.xyz;
float3 N = float3(normalize(normal),0);

// Compute emissive term
float3 emissive = Ke;

// Compute ambient term
float3 ambient = Ka * globalAmbient;

// Compute the diffuse term
float3 L = normalize(lightPosition - P);
float diffuseLight = max(dot(N,L), 0);
float3 diffuse = Kd * lightColor * diffuseLight;

// Compute the specular term
float3 V = normalize(eyePosition - P);
float3 H = normalize(L + V);
float specularLight = pow(max(dot(N,H), 0), shininess);
if (diffuseLight <= 0) specularLight = 0;
float3 specular = Ks * lightColor * specularLight;

float4 color;
color.xyz = emissive + ambient + diffuse + specular;
color.w = 1;

return color;
}

mobeen
03-09-2011, 02:08 PM
Check this http://mmmovania.blogspot.com/2011/01/point-sprites-as-spheres-in-opengl33.html

Shao tian
03-09-2011, 06:01 PM
Thanks. I converted it to CG fragment program, and the result seem not correct. The shape of point sprites are circle which is right, but the lighting effect looks strange. Attached is the screenshot.

Is that becasue I used predefined light direction? I set lightDir=vec3(100,100,100)

mobeen
03-09-2011, 06:15 PM
Thanks. I converted it to CG fragment program, and the result seem not correct. The shape of point sprites are circle which is right, but the lighting effect looks strange. Attached is the screenshot.

Is that becasue I used predefined light direction? I set lightDir=vec3(100,100,100)
I do the lighting calc in eye space. The lightDir is assumed to be in eye space. Could u try changing lightDir to (0,0,1) and see if this helps?

Shao tian
03-10-2011, 09:54 AM
Yes! it worked when lightdir={0,0,1}. Thanks! it looks beautiful