Advertisement

Point/Spotlights

Started by January 26, 2003 03:47 AM
5 comments, last by Dtag 22 years, 1 month ago
Hey Can anyone tell me how to create simple spot/pointlights with opengl? I have looked at different tutorials and a function reference and I cant seem to find the right functions... for it. TIA
well, this will put a spotlight at the camera position (like a headlight on camera)Put this before any coordinate transformation
. If you want the light elsewhere, it will get transformed with your objects.

glDisable(GL_LIGHTING);//I am almost positive this is required
GLfloat LightDiffuse2[]=(1.0f, 1.0f, 1.0f,1.0f };//eg:white
GLfloat LightPosition2[]={0,0,0, 1.0f };//eg: at camera
glLightf(GL_LIGHT2,GL_SPOT_CUTOFF,15);//eg: 15 degrees
GLfloat spot_direction[]={0,0,-1};//eg down local z axis
glLightfv(GL_LIGHT2, GL_AMBIENT, LightAmbient2);
glLightfv(GL_LIGHT2, GL_DIFFUSE, LightDiffuse2);
glLightfv(GL_LIGHT2, GL_POSITION,LightPosition2);
glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION,spot_direction);
//glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION,1);// 1=default
//glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION,0);// 0=default
//glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION,0);//0=default
glEnable(GL_LIGHT2);
glEnable(GL_LIGHTING);
Advertisement
there are some subtleties when using lighting in GL,
this post I made a while ago points some of them out.

| - Project-X - my mega project.. big things comming soon - | - adDeath - an ad blocker I made - | - email me - |
Hmmm how exactly would I implement a pointlight ( like the one of a flying rocket or something ) that doesnt look as irregulary as my lights do? Would this light go through walls? Im currently only setting the GL_DIFFUSE and GL_SPOT_CUTOFF parameters of my light ( Diffuse to some color and Cutoff to 0 ) and I cant see any light. If I dont set the Cutoff, I can see thelight but it looks just crappy ( very dark, not all vertices hit by the light etc... )
Well, the Cuttoff is the half-angle of the beam of light so if you set it to zero, you have no beam at all- no light.

Re, the darkness, you can add some ambient light.

try this and increase/decrease the .5 values until you get what you like:
GLfloat LightAmbient2[]=(.5f, .5f, .5f,1.0f };
GLfloat LightDiffuse2[]=(1.0f, 1.0f, 1.0f,1.0f };
GLfloat LightPosition2[]={0,0,0, 1.0f };//eg: at camera
glLightf(GL_LIGHT2,GL_SPOT_CUTOFF,45);//
GLfloat spot_direction[]={0,0,-1};//eg down local z axis
glLightfv(GL_LIGHT2, GL_AMBIENT, LightAmbient2);
glLightfv(GL_LIGHT2, GL_DIFFUSE, LightDiffuse2);
glLightfv(GL_LIGHT2, GL_POSITION,LightPosition2);
glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION,spot_direction);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHTING);
Why do I need a direction for a spotlight? Why does my light look so crappy ( not all vertices properly lightened etc... )
Advertisement
You may need to calculate normals, which is something i still have not figured out till this day

This topic is closed to new replies.

Advertisement