halfVector

Discussion about everything. New games, 3d math, development tips...
Post Reply
oldskoolPunk
Posts: 199
Joined: Wed Nov 29, 2006 4:07 am

halfVector

Post by oldskoolPunk »

Hi again!
Not sure where to post these types of questions I will try here.
I am learning GLSL stuff and I am having a little trouble with my specular component.
Many tutorials I have read use gl_LightSource[0].halfVector to compute the specular color of the pixel, but mine always seems to return zero.
Can someone take a peek below at when I am doing? The code works properly in Shader Designer, but the model appears solid red in Irrlicht.
Maybe there is somthing I need to do with the light in Irrlicht? Or maybe someone can show me how to compute the half vector myself?

Code: Select all

//my vert

varying vec3 Normal;
varying vec3 Light;
varying vec3 HalfVector;

void main (void)
{
Normal = normalize(gl_NormalMatrix * gl_Normal);
Light  = normalize(gl_LightSource[0].position.xyz);
vec4 pos = gl_ModelViewMatrix*gl_Vertex;

/////////////////////////////////////////////////////////////
///this is where I retrieve the light's half vector/////////
HalfVector = normalize(gl_LightSource[0].halfVector.xyz);//
//////////////////////////////////////////////////////////

gl_Position = ftransform();
}

Code: Select all

// my frag

varying vec3 Normal;
varying vec3 Light;
varying vec3 HalfVector;

void main(void)
{
Normal = normalize(Normal);

float Diffuse = max(dot(Normal, Light),0.0);

///////////////////////////////////////////////////////////////
// This is where I use the half vector to get the spec ///////
float Specular = pow(max(dot(Normal,HalfVector),0.0), 8.0);//
////////////////////////////////////////////////////////////
gl_FragColor = (vec4(0,0,1,1) * Diffuse)+Specular;

///////////////////////////////////////////////////////////
//This is my debug line, turning the mesh red//////
//if the spec calculation returns zero            //////
if (Specular == 0) gl_FragColor = vec4(1,0,0,1);
}
If you have read this far thank you very much :D
Signature? I ain't signin nuthin!
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

try commenting this code, see what happens.

that would be my guess.

Code: Select all


if (Specular == 0) gl_FragColor = vec4(1,0,0,1); 
} 


Image
oldskoolPunk
Posts: 199
Joined: Wed Nov 29, 2006 4:07 am

Post by oldskoolPunk »

Hey man I really appreciate you taking a look!

That line is for debug purposes. On my screen the model is always solid red. When I comment out that line, my model is always solid black. I think it is because gl_LightSource[0].halfVector always return (0,0,0,0)

Can you tell me what it does for you? Or can you tell me why gl_LightSource[0].halfVector always return (0,0,0,0), or maybe you can show me how to get the halfVector myself?
Signature? I ain't signin nuthin!
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

sorry, you need to drop another quarter. time is up.

hehe.

seriously, can't help you anymore, maybe someone with GSLS experience can help.
Image
oldskoolPunk
Posts: 199
Joined: Wed Nov 29, 2006 4:07 am

Post by oldskoolPunk »

I found some code here that says

Code: Select all

halfVector = normalize(VP + eye);
I think it says that the halfVector of the light is the lightVector plus the camera's vector?

So I tried this in my shader.....

Code: Select all

//my vert

uniform vec3 camPos;  //camera position from Irrlicht
varying vec3 Normal;
varying vec3 Light;
varying vec3 HalfVector;

void main (void)
{
Normal = normalize(gl_NormalMatrix * gl_Normal);
Light  = normalize(gl_LightSource[0].position.xyz);


vec4 pos = gl_ModelViewMatrix*gl_Vertex;

 /////////////////////////////////////////////////////
// Here is where I get the camera to vertex vector //
vec3 eyeVect = normalize (camPos - vec3(pos.xyz));

 ///////////////////////////////////////////
//Then here I get the halfVector         //
HalfVector = normalize(Light + eyeVect);

gl_Position = ftransform();
}

Code: Select all

// my frag

varying vec3 Normal;
varying vec3 Light;
varying vec3 HalfVector;

void main(void)
{
vec3 Nn = normalize(Normal);
vec3 Nh = normalize(HalfVector);

vec4 Ambient = vec4(0,0,0.4,1);

vec4 Diffuse = vec4(0,0,0.7,1);
Diffuse *= max(dot(Nn, Light),0.0);//diffuse term

vec4 Specular = vec4(0.5,0.7,1,1);
Specular *= pow(max(dot(Nn,Nh),0.0), 8.0);//spec term
 
gl_FragColor = Ambient+Diffuse+Specular;
}
And these are my results so far ...
Image

Does this look correct? Am I getting the halfVector properly?
Thanks for reading and happy shading!
Signature? I ain't signin nuthin!
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

awesome, you definitely know a lot.

so that's what specular shading looks like. you might want to try other shading as well, using the same demo.
Image
oldskoolPunk
Posts: 199
Joined: Wed Nov 29, 2006 4:07 am

Post by oldskoolPunk »

Thanks for the encouragement :D But I don't know anything yet I only just started 4 days ago. I would like to know how to get the halfVector from the light but oh well. On to textures!
Signature? I ain't signin nuthin!
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

i just found you used blinn-phong in the code.

see http://en.wikipedia.org/wiki/Blinn%E2%8 ... ding_model

keep it up.
Image
agi_shi
Posts: 122
Joined: Mon Feb 26, 2007 12:46 am

Post by agi_shi »

The half vector is the [eye plus the light dir] divided by 2. You're almost there.
Post Reply