|
-
Jul 15th, 2006, 03:34 AM
#1
Thread Starter
Lively Member
[RESOLVED] Problems with Specular light and Spotlight in OpenGL
Hi,
I am using OpenGL to draw a rotating cube where light is shone on it. I used a positional light and a spotlight. The problem is that these 2 light components don't seem to show up properly.
The cube is located at (1,1,-10) and the positional light is located at (1,1,-5). I've added ambient,diffuse and specular light components. In addition, I used glMaterials() to make the material of the cube shiny.
The code snippet for the positional light is shown below:
GLfloat lightDir[] = {1.0,1.0,-5.0,1.0}; // Light Direction
glLightfv(GL_LIGHT0,GL_POSITION,lightDir);
GLfloat white[] = {1.0,1.0,1.0,1.0}; // Light colours
GLfloat blue[] = {0.0,0.0,1.0,1.0};
glLightfv(GL_LIGHT0,GL_AMBIENT,blue);
glLightfv(GL_LIGHT0,GL_DIFFUSE,blue);
glLightfv(GL_LIGHT0,GL_SPECULAR,white);
glMaterialf(GL_FRONT,GL_SHININESS,128); // Use a shiny material
The ambient and diffuse components show up properly but the specular component didnt appear. The lighting appeared to be the same regardless of whether I enabled specular lighting or not.
As for the spotlight, it didnt show up at all. The effect I wanted is for the spotlight to show up as a round area of light on the cube's surface. I customized the spotlight's direction, cutoff and exponent. The code snippet for the spotlight is shown below:
GLfloat spotLightColour[] = {0.0,1.0,0.0}; //spot light colour
GLfloat spotlightDirection[] = {1.0,1.0,-6.0}; // spot light direction
glLightf(GL_LIGHT1,GL_SPOT_CUTOFF,5.0f); // spot light cut-off
glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,spotlightDirection);
glLightfv(GL_LIGHT1,GL_SPECULAR,spotLightColour);
glLightf(GL_LIGHT1,GL_SPOT_EXPONENT,70.0f); // spot light exponent
For the cube, its length on the x axis starts from -1 to 1. The same goes for the width and height on the y and z axis.
What could be the problem regarding the specular component of the positional light (there was no shiny area on the cube) and the spotlight (it didnt appear at all) ?
-
Jul 17th, 2006, 08:52 AM
#2
Hyperactive Member
Re: Problems with Specular light and Spotlight in OpenGL
Did you set normals?
If you did not set the normals it will look very flat. Is that the problem? You can set the normals like this. The X,Y,Z should be perpendicular to the polygon you are drawing.
As for the spotlight not being visible:
Maybe you should split up your object in more triangles. Personally I wrote a SplitIn4() function for this . It improves lightning a lot in my project. I got the original idea from here
Did you set material properties?
You can also set a lot of material properties. Did you set other material properties besides the one you mentioned?
Last edited by BramVandenbon; Jul 17th, 2006 at 09:01 AM.
____________________________________________
Please rate my messages. Thank you!
____________________________________________
Bram Vandenbon
http://www.bramvandenbon.com
-
Jul 27th, 2006, 03:24 AM
#3
Thread Starter
Lively Member
Re: Problems with Specular light and Spotlight in OpenGL
Hi,
About the specular problem, I have solved it by adding a GL_SPECULAR argument to glMaterial. The shiny part appears now.
For spotlight, it seems that your method is the way to go.
Thanks for the advice!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|