Jump to content
  • Advertisement
Sign in to follow this  
crims

OpenGL GLSL spotlight problem [SOLVED]

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

It points correctly at 0/359 degrees but seems to rotate twice of what is necessary at other angles.

[media]
[/media]

Is there something wrong with this?

spotEffect = dot(normalize(-lightDir), normalize(gl_LightSource[0].spotDirection));



flashlight3.vert
varying vec4 diffuse,ambientGlobal, ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;
varying vec4 ecPos;

void main()
{
vec3 aux;

normal = normalize(gl_NormalMatrix * gl_Normal);

/* now normalize the light's direction. Note that according to the
OpenGL specification, the light is stored in eye space.*/
ecPos = gl_ModelViewMatrix * gl_Vertex;
aux = vec3(gl_LightSource[0].position-ecPos);
lightDir = normalize(aux);

dist = length(aux);

halfVector = normalize(gl_LightSource[0].halfVector.xyz);

diffuse = gl_FrontMaterial.diffuse; // * gl_LightSource[0].diffuse;
ambient = gl_FrontMaterial.ambient; // * gl_LightSource[0].ambient;
ambientGlobal = gl_LightModel.ambient; // * gl_FrontMaterial.ambient;

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord0;

gl_Position = ftransform();
}


flashlight3.frag
varying vec4 diffuse,ambientGlobal, ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;

uniform sampler2D texture1;
uniform sampler2D texture2;

void main()
{
vec3 n,halfV;
float NdotL,NdotHV;
vec4 color = vec4(0,0,0,1);
float att,spotEffect;
n = normalize(normal);

NdotL = max(dot(n,normalize(lightDir)),0.0);

if (NdotL > 0.0) {

spotEffect = dot(normalize(-lightDir), normalize(gl_LightSource[0].spotDirection));

if (spotEffect > gl_LightSource[0].spotCosCutoff) {

color += spotEffect * (diffuse * NdotL + ambient);


halfV = normalize(halfVector);
NdotHV = max(dot(n,halfV),0.0);
color += gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV,gl_FrontMaterial.shininess);
}
}

gl_FragColor = color * texture2D(texture1, gl_TexCoord[0].st) * texture2D(texture2, gl_TexCoord[1].st);
}


flashlight.h



// Set up light
void InitFlashlight()
{
float ambience[4] = {0.0f, 0.0f, 0.0f, 1.0};
float diffuse[4] = {1,1,1,1};
glLightfv( GL_LIGHT0, GL_AMBIENT, ambience );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
glLightfv( GL_LIGHT0, GL_SPECULAR, diffuse );

glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 20.0);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 2.0);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.01f);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.01f);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.000001f);

float LightPosition[4];
LightPosition[0] = g_Camera->Position().x;
LightPosition[1] = g_Camera->Position().y;
LightPosition[2] = g_Camera->Position().z;
LightPosition[3] = 1;

glLightfv( GL_LIGHT0, GL_POSITION, LightPosition );
glEnable( GL_LIGHT0 );
glEnable( GL_LIGHTING );
glEnable(GL_COLOR_MATERIAL);

GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
glShadeModel (GL_SMOOTH);

glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}

// Update each frame
void UpdateFlashlight()
{
float LightPosition[4];
LightPosition[0] = g_Camera->Position().x;
LightPosition[1] = g_Camera->Position().y;
LightPosition[2] = g_Camera->Position().z;
LightPosition[3] = 1;

CVector3 direction = Normalize(g_Camera->View() - g_Camera->Position());
float LightDirection[3];
LightDirection[0] = direction.x;
LightDirection[1] = direction.y;
LightDirection[2] = direction.z;

glLightfv( GL_LIGHT0, GL_POSITION, LightPosition );
glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, LightDirection );
}

Share this post


Link to post
Share on other sites
Advertisement
The shader look fine.
You should probably verify your "LightDirection" that you send to
glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, LightDirection );

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!