Jump to content
  • Advertisement
Sign in to follow this  
Owenx92

OpenGL Specular Lighting?

This topic is 2606 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

Hello,
I have a problem about specular lighting.
I don't know what I'm doing wrong sad.gif

Here is my GLSL Shaders.. (Some Parts..)

VERTEX SHADER:

void VS( in A2V IN, out V2P OUT )
{
OUT.Pos = gl_ModelViewProjectionMatrix * gl_Vertex; // or ftransform();
//OUT.WorldPos = gl_ModelViewMatrix * gl_Vertex; // Don't need?
OUT.WorldNormal = gl_Normal ;

OUT.LightDir = vec3( LightPosition - gl_Vertex );
OUT.ViewDir = vec3( CameraPosition - gl_Vertex );

OUT.Diffuse = LightColor;
OUT.TexCoord0 = IN.Tex0;
}
in main function:

VS( temp_IN, temp_OUT);
gl_Position = vec4( temp_OUT.Pos);



FRAGMENT SHADER:

vec4 PS( in V2P IN ) {

vec4 Ambient = vec4(0.05,0.05,0.05,0.0);
vec4 TexColor = vec4( texture2D( textureSampler, IN.TexCoord0).xyz , 1.00000);

float LenSqrt = dot( IN.LightDir, IN.LightDir);

IN.WorldNormal = normalize(IN.WorldNormal);
IN.LightDir = normalize( IN.LightDir );
IN.ViewDir = normalize( IN.ViewDir );

float Attenuation = min( ((LightFalloff * LightFalloff) / LenSqrt), 1.00000);
vec4 Illumination = saturate( dot(IN.WorldNormal, IN.LightDir)) * Attenuation * IN.Diffuse;

vec3 HalfAngle = normalize(IN.ViewDir + IN.LightDir);
vec4 Specular = pow( saturate( dot(IN.WorldNormal, HalfAngle)), 16.0 ) * IN.Diffuse;

return ( TexColor * (Illumination + Specular + Ambient)); // returning to gl_FragColor
}

saturate function is defined..
-------------------------------------------

float saturate( float x) {
return clamp( x, 0.0, 1.0);
}

vec2 saturate( vec2 x) {
return clamp( x, 0.0, 1.0);
}

vec3 saturate( vec3 x) {
return clamp( x, 0.0, 1.0);
}
.
.
. also as vec4, mat2, mat3, mat4 ...
-------------------------------------------

And Application side.. :


float LightPos[4] = {0.0 ,0.0, 0.0 ,1.0};
float cameraPos[4] = {camera.x, camera.y, camera.z, 1.0};
float LightColor[4] = {1.0, 0.7, 0.0, 1.0};

glUseProgram(gShader);
glUniform1i( glGetUniformLocation(gShader, "textureSampler"), 0);
glUniform1f( glGetUniformLocation(gShader, "LightFalloff"), 50.0);
glUniform4fv( glGetUniformLocation(gShader, "LightPosition"), 1, LightPos);
glUniform4fv( glGetUniformLocation(gShader, "CameraPosition"), 1, cameraPos);
glUniform4fv( glGetUniformLocation(gShader, "LightColor"), 1, LightColor);


glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colorTexture);

.
.
.
DRAWING THINGS..

Point Lighting is OK. But why there is no Specular?? What I'm doing wrong.. I Need Help!..

BTW, Please ignore my bad English.. smile.gif
OpenGL version: 3.3.0
OS: Ubuntu 11.04 -- GNU/Linux x86_64

Share this post


Link to post
Share on other sites
Advertisement

Are you transforming your camera into object space before uploading it to the vertex shader?


I'm not sure..
How can I do

You Mean glTranslatef(.. ?? if so.. Yes

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!