Jump to content

  • Log In with Google      Sign In   
  • Create Account

GLSL spotlight problem [SOLVED]


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 crims   Members   -  Reputation: 151

Like
0Likes
Like

Posted 01 May 2012 - 04:35 PM

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

http://www.youtube.com/watch?v=AbH4h4tD62U

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 );
}


Sponsor:

#2 V-man   Members   -  Reputation: 805

Like
1Likes
Like

Posted 02 May 2012 - 05:35 AM

The shader look fine.
You should probably verify your "LightDirection" that you send to
glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, LightDirection );
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#3 crims   Members   -  Reputation: 151

Like
0Likes
Like

Posted 02 May 2012 - 04:27 PM

Needed to offset GL_POSITION by (0,0,-32). Weird.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS