# Lighting a surface

• 05-03-2013, 11:11 AM
bananeeek
Lighting a surface
I've been following some tutorials about setting a simple source of light, but I still can't make it :S let's say I have a simple surface in 3D

const short a = 11; // width
const short b = 20; // length

glNormal3f(0,1,0);
glVertex3f(-b/2,0,-a/2);
glVertex3f(b/2,0,-a/2);
glVertex3f(b/2,0,a/2);
glVertex3f(-b/2,0,a/2);
glEnd(); //surface

And then following the tutorial I have to enable a source of light, then color it, position it etc, so I have this:

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);

GLfloat light_color[] = {1.0,0.0,0.0,1.0}; //red for example
GLfloat light_diff[] = {1.0,0.0,0.0,0.0};

glLightfv(GL_LIGHT0, GL_AMBIENT, light_color );
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diff );
glLightfv(GL_LIGHT0, GL_SPECULAR, light_color );

GLfloat light_pos[] = {0.0,3.0,0.0,1.0};
GLfloat light_dir[] = {0.0,0.0,0.0};

glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_dir );
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 60);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 128);

//glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 20); // not sure if I need it...

So I have this surface, I position the cone light above it (Y axis) and I want this cone of light to shine on the surface`s center. I normalized the vector, doublechecked everything, but all I got so far is that the said surface is dimmed, no source of light in sight...
I've been looking for a simple source code containing a simple light source, but I found none.
• 05-03-2013, 02:36 PM
Carmine
Quote:

Code :

``` GLfloat light_pos[] = {0.0,3.0,0.0,1.0}; GLfloat light_dir[] = {0.0,0.0,0.0};   glLightfv(GL_LIGHT0, GL_POSITION, light_pos); glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_dir ); glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 60); glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 128);```

I think the problem is with the light direction vector. You've entered the coordinates of
the point you'd like the light to look at. That's not how it works. You're supposed to enter the
DIRECTION the light is looking in. If you want a light at {0,3,0} to be looking back at
the origin, you should use {0,-1,0}. Good luck.
• 05-03-2013, 05:28 PM
bananeeek
Still doesn't work... Tried to change values to negative on every axis, even in the normal vector...
• 05-03-2013, 08:35 PM
Nowhere-01
try setting exponent to 0. 128 is a ridiculous value and your light will be visible only if it's very close to surface as a tiny bright spot.
• 05-04-2013, 03:42 AM
mobeen
Hi,
OpenGL fixed function pipeline calculates lighting per-vertex and for the example mesh you have only 4 vertices. May be your spot light is having some effect on the center of the mesh but since there is not vertex nothing is lit. I would suggest you either increase the mesh vertex density to have more vertices in between. Another thing u can do is try out a glutSolidTeapot instead to see if u get something on it.

See if this helps.
Mobeen
• 05-04-2013, 04:21 AM
IntelliSense
Quote:

Originally Posted by mobeen
Hi,
OpenGL fixed function pipeline calculates lighting per-vertex and for the example mesh you have only 4 vertices. May be your spot light is having some effect on the center of the mesh but since there is not vertex nothing is lit. I would suggest you either increase the mesh vertex density to have more vertices in between. Another thing u can do is try out a glutSolidTeapot instead to see if u get something on it.

See if this helps.
Mobeen

Hi
I have a header file like that. I'm willing to create a grid. By using OpenGl and C++. The problem is in there cpp file. I can't excatly draw. Hope you can help it.
In the for loop i don't know how to fill inside the vertex blocks

Quote:

#ifndef Grid_h
#define Grid_h
#include "Shape.h"

class Grid :: public Shape
{

public:

Grid();

Grid(int numberOfLines,int distanceBetweenLines);

void Draw();

int distanceBetweenLines;

int numberOfLines;

};

#endif

Quote:

#include "Grid.h"
#include <GL/freeglut.h>

Grid :: Grid()
{
numberOfLines = 0.0f;

distanceBetweenLines = 0.0f;
}
Grid:: Grid(int numberOfLines , int distanceBetweenLines )
{
cizimSekli = GL_LINES; //drawing way
cizgisayisi = 50; //number of Lines
renk = Renk(0.0f, 1.0f, 0.0f); //Colour
distanceBetweenLines = 20;

for(int x = 0; x < cizgisayisi; x++){

glBegin(GL_LINES);
glVertex2f(x,0);
glVertex2f(0,x);
glEnd();
};
for(int y = 0; y < cizgisayisi; y++){
glBegin(GL_LINES);
glVertex2f(y,0);
glVertex2f(0,y);
glEnd();
};
}
• 05-04-2013, 05:23 AM
bananeeek
So i took Nowhere`s and Mobeen`s advices, and first I changed exponent value to like 4, and then tried to set a light on a teapot. After some adjustments, I was able to get a circle of light on said teapot, but there's something wrong with position and direction of the light. If I set the values to:
GLfloat light_pos[] = {10,2,8,1.0};
GLfloat light_dir[] = {-10,-2,-8};
the source of light appears to shine from the side on the teapot's side, as expected. But if I want the light to lightn the top of the teapot, I have to set the position and direction to
GLfloat light_pos[] = {0,-8,0,1.0};
GLfloat light_dir[] = {0,8,0};
And it seems to me like the light should lighten the bottom of the teapot instead of its top...
Then I tried to lighten the surface, I changed it to GL_POLYGON and declared 4 times as many vertexes, coz I`m not sure how many wil do...
Then about the normal vector, should it still be (0,1,0) since the Y axis values seems to be reversed?
• 05-04-2013, 06:17 AM
Nowhere-01
AFAIK, direction vector for spotlights should be normalized(in range -1.0 to 1.0).
http://www.glprogramming.com/red/chapter05.html there's a section about spotlights.