PDA

View Full Version : Problem with lighting



Aquileo
12-18-2003, 03:30 AM
I have my own mesh and I put the light but if I put the coordenates in (20.0f,0.0f,0.0f) when I run my program the light surface is the opposite like if I put the light in (-20.0,0.0f,0.0f).
I also put a sphere with gluSphere and it is illuminated well.

I make a function for put the light:

void TMDIChild::SetupLighting()
{
//static const int x=0, y=1, z=2, alfa=3;
float colAmbiente[4];
float colDifusa[4];
float colEspecular[4];

float materiales[][13] = {{0.329412,0.223529,0.027451,1.0,0.780392,0.50.4, 1.0, 10}};

THorma *Horma = Model->GetHorma();
int material = Horma->propiedadesHorma->iNumeroMaterial;
// Introducimos el gl_ambient
for (int veces=0; veces<4; veces++)
colAmbiente[veces] = materiales[material][veces];

// Ahora el gl_diffuse
for (int veces=4; veces<8; veces++)
colDifusa[veces-4] = materiales[material][veces];

// Ahora de gl_specular
for (int veces = 8; veces<12; veces++)
colEspecular[veces-8] = materiales[material][veces];

float brillo = materiales[material][12];
glMaterialfv(GL_FRONT, GL_AMBIENT, colAmbiente);
glMaterialfv(GL_FRONT, GL_DIFFUSE, colDifusa);
glMaterialfv(GL_FRONT, GL_SPECULAR, colEspecular);
glMaterialf(GL_FRONT, GL_SHININESS, brillo);

GLfloat col_luz[] = {1.0f, 1.0f, 1.0f, 1.0f};

// Hagase la luz
glLightfv(GL_LIGHT0, GL_POSITION, posicion_luz);
glLightfv(GL_LIGHT0, GL_AMBIENT, col_luz);
glLightfv(GL_LIGHT0, GL_DIFFUSE, col_luz);
glLightfv(GL_LIGHT0, GL_SPECULAR, col_luz);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}


And my code to calculate normal is:

void calculaNormal(float vector1[3], float vector2[3], float vector3[3], float salida[3], bool reducir)
{
//TODO: Add your source code here
float v1[3], v2[3];
static const int x = 0;
static const int y = 1;
static const int z = 2;

// Calcula dos vectores de los tres puntos
v1[x] = vector2[x] - vector1[x];
v1[y] = vector2[y] - vector1[y];
v1[z] = vector2[z] - vector1[z];

v2[x] = vector3[x] - vector1[x];
v2[y] = vector3[y] - vector1[y];
v2[z] = vector3[z] - vector1[z];

// Obtenemos el resultado
salida[x] = v1[y]*v2[z] - v1[z]*v2[y];
salida[y] = v1[x]*v2[z] - v1[z]*v2[x];
salida[z] = v1[x]*v2[y] - v1[y]*v2[x];

// Ahora normalizamos el vector, lo convertimos a longitud 1
if (reducir)
ReducirAUnidad(salida);
}

void ReducirAUnidad(float vector[3])
{
float longitud;

// Calcula la longitud de un vector
longitud = (float) sqrt((vector[0]*vector[0])+
(vector[1]*vector[1])+
(vector[2]*vector[2]));

// Nos aseguramos que la longitud sea mayor que 0
if (longitud == 0.0f)
longitud = 1.0f;

// Dividimos cada elemento por la longitud de su resultante
vector[0] /= longitud;
vector[1] /= longitud;
vector[2] /= longitud;
}


Please, I need help.
Thank you.