Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Spotlight in OpenGL

This topic is 2918 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 all,

I'm trying to implement a spotlight in the following manner:

1) Do all lighting calculations(im using a directional light and doing per-vertex calculations). Also, im specifying light in world space
2) Then, interpolate the vertex position . Then, in the fragment shader, check the distance of the light with the interpolated vertex position.
3) If distance is of a certain value, "brighten" it.
4) Since my light can only move in the x-z plane, i have done the following:

uniform vec4 lAmbient;
uniform vec4 mAmbient;
uniform vec4 lDiffuse;
uniform vec4 mDiffuse;
uniform vec4 lightPos;
uniform float waveAmp;
uniform vec4 lSpecular;
uniform vec4 mSpecular;
varying vec4 color;
varying vec3 fragPos;

]void main(void)
float spec = 0.0;

vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
vec4 lightPos1 = gl_ModelViewMatrix * lightPos;
vec3 lightVec = normalize(lightPos.xyz);
vec3 viewVec = vec3(normalize(-(gl_ModelViewMatrix * gl_Vertex)));
vec3 reflectVec = reflect(-lightVec, normal);

float lambertTerm = dot(normal, lightVec);

vec4 ambColor = lAmbient * mAmbient;
vec4 diffColor = lDiffuse * mDiffuse * lambertTerm;

if (lambertTerm > 0.0) {
spec = max(dot(reflectVec, viewVec), 0.0);
spec = pow(spec, 10.0);

vec4 specColor = mSpecular * lSpecular * spec; // Some problems with specColor. Anyway, i'm still trying it out.

color = ambColor + diffColor + specColor;

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
fragPos = viewVec;

// Fragment shader:
varying vec4 color;
varying vec3 fragPos;
uniform vec4 lightPos;

void main(void)
float dis = sqrt( (fragPos.y - lightPos.y) * (fragPos.y - lightPos.y)
+(fragPos.x - lightPos.x) * (fragPos.x - lightPos.x)

if (dis <= 1.0)
gl_FragColor = color * vec4(1.0, 1.0, 1.0, 1.0);
gl_FragColor = color * vec4(0.5, 0.5, 0.5, 1.0);

Now, this obviously seems too expensive to me. But i just wanted to know how expensive it is to do such calculations. The effect seems to be quite as expected. But i would like to take advice from you guys. Can anyone please suggest methods of improving it, or maybe tell me if this is really not a good approach.

PS: This is just something i wanted to experiment. I read the general implementation of spotlights(the one where they check if the light ray is in the cone). I just wanted to try something different.
Please let me know if this is fine.

Oh, and this is the effect i'm getting:

The quad is the light position.

Thanks a lot!

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!