Dhruvil vyas

11-27-2011, 09:06 PM

I am a newbie to opengl and this is my first post.I am trying to develop space simulator. I am trying to use sun as the light source My problem is that the lighting dosent work as expected. Maybe i am using the wrong calculation for the normals. I am using a single "createsphere" function to create a sphere, and then use different coordinates and sizes to display them. The problem is that all the sphere on the screen show almost the same effect. i.e the light rotates along with them. I am not sure where the problem is ...i am posting my code ...

void CreateSphere (double R, double X, double Y, double Z) {

int n,m;

double a;

double b;

n = 0;

m = 0;

//NORMALS* p = new NORMALS;

for( b = 0; b <= 90 - space; b+=space){

for( a = 0; a <= 360 - space; a+=space)

{

VERTEX[n].X = R * sin((a) / 180 * PI) * sin((b) / 180 * PI) - X;

VERTEX[n].Y = R * cos((a) / 180 * PI) * sin((b) / 180 * PI) + Y;

VERTEX[n].Z = R * cos((b) / 180 * PI) - Z;

VERTEX[n].V = (2 * b) / 360;

VERTEX[n].U = (a) / 360;

n++;

VERTEX[n].X = R * sin((a) / 180 * PI) * sin((b + space) / 180 * PI) - X;

VERTEX[n].Y = R * cos((a) / 180 * PI) * sin((b + space) / 180 * PI) + Y;

VERTEX[n].Z = R * cos((b + space) / 180 * PI) - Z;

VERTEX[n].V = (2 * (b + space)) / 360;

VERTEX[n].U = (a) / 360;

n++;

VERTEX[n].X = R * sin((a + space) / 180 * PI) * sin((b) / 180 * PI) - X;

VERTEX[n].Y = R * cos((a + space) / 180 * PI) * sin((b) / 180 * PI) + Y;

VERTEX[n].Z = R * cos((b) / 180 * PI) - Z;

VERTEX[n].V = (2 * b) / 360;

VERTEX[n].U = (a + space) / 360;

n++;

VERTEX[n].X = R * sin((a + space) / 180 * PI) * sin((b + space) /180 * PI) - X;

VERTEX[n].Y = R * cos((a + space) / 180 * PI) * sin((b + space) /180 * PI) + Y;

VERTEX[n].Z = R * cos((b + space) / 180 * PI) - Z;

VERTEX[n].V = (2 * (b + space)) / 360;

VERTEX[n].U = (a + space) / 360;

n++;

}

}

}

the display sphere function is given below

void DisplaySphere (double R, GLuint texture)

{

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

int b,m = 0;

glScalef (0.0125 * R, 0.0125 * R, 0.0125 * R);

glBindTexture (GL_TEXTURE_2D, texture);

glBegin (GL_TRIANGLE_STRIP);

for ( b = 0; b <VertexCount; b++)

{

glTexCoord2f (VERTEX[B].U, VERTEX[B].V);

glNormal3f(-VERTEX[B].X, -VERTEX[B].Y, -VERTEX[B].Z);

glVertex3f (VERTEX[B].Y, VERTEX[B].X, -VERTEX[B].Z);

}

m = 0;

for ( b = 0; b <VertexCount; b++)

{

glTexCoord2f (VERTEX[B].U, -VERTEX[B].V);

glNormal3f(-VERTEX[B].X, -VERTEX[B].Y, -VERTEX[B].Z);

glVertex3f (VERTEX[B].Y, VERTEX[B].X, VERTEX[B].Z);

}

glEnd();

glRotatef(angle,0,0,1);

}

the lighting code is given here

glPushMatrix();

gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); //defines a viewing transformation.

//GLdouble eyex, eyey,eyez, centerx,centery, centerz, upx, upy, upz..// have kept upz=1 so,

//to go left-right in the screen change x cood

//to go up(+z) and down(-z) to change z coord

//to go inside(-y) - outside(+y) the screen change y coord.

// Now translate to the sun

glTranslatef(0.0, -7.0, 3.0);

/* For LIGHT0 */

GLfloat lightZeroPosition[] = {0.0f, 0.0f, 0.0f, 1.0f};

/*GLfloat lightvec[] = {0.5f, 0.2f, 0.0f, 1.0f};*/

GLfloat lightZeroColor[] = {0.5f, 0.5f, 0.5f, 1.0f};

GLfloat amb[] = {1, 1, 1, 1};

GLfloat spec[] = {0.3, 0.3, 0.3, 1};

glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition);

glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);

glLightfv(GL_LIGHT0, GL_SPECULAR, spec);

//glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightvec );

//glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 20);

glEnable(GL_LIGHT0);

glRotatef(angle,0,0,1);

DisplaySphere(5,textures);// function to display the sun

am attaching some images also

void CreateSphere (double R, double X, double Y, double Z) {

int n,m;

double a;

double b;

n = 0;

m = 0;

//NORMALS* p = new NORMALS;

for( b = 0; b <= 90 - space; b+=space){

for( a = 0; a <= 360 - space; a+=space)

{

VERTEX[n].X = R * sin((a) / 180 * PI) * sin((b) / 180 * PI) - X;

VERTEX[n].Y = R * cos((a) / 180 * PI) * sin((b) / 180 * PI) + Y;

VERTEX[n].Z = R * cos((b) / 180 * PI) - Z;

VERTEX[n].V = (2 * b) / 360;

VERTEX[n].U = (a) / 360;

n++;

VERTEX[n].X = R * sin((a) / 180 * PI) * sin((b + space) / 180 * PI) - X;

VERTEX[n].Y = R * cos((a) / 180 * PI) * sin((b + space) / 180 * PI) + Y;

VERTEX[n].Z = R * cos((b + space) / 180 * PI) - Z;

VERTEX[n].V = (2 * (b + space)) / 360;

VERTEX[n].U = (a) / 360;

n++;

VERTEX[n].X = R * sin((a + space) / 180 * PI) * sin((b) / 180 * PI) - X;

VERTEX[n].Y = R * cos((a + space) / 180 * PI) * sin((b) / 180 * PI) + Y;

VERTEX[n].Z = R * cos((b) / 180 * PI) - Z;

VERTEX[n].V = (2 * b) / 360;

VERTEX[n].U = (a + space) / 360;

n++;

VERTEX[n].X = R * sin((a + space) / 180 * PI) * sin((b + space) /180 * PI) - X;

VERTEX[n].Y = R * cos((a + space) / 180 * PI) * sin((b + space) /180 * PI) + Y;

VERTEX[n].Z = R * cos((b + space) / 180 * PI) - Z;

VERTEX[n].V = (2 * (b + space)) / 360;

VERTEX[n].U = (a + space) / 360;

n++;

}

}

}

the display sphere function is given below

void DisplaySphere (double R, GLuint texture)

{

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

int b,m = 0;

glScalef (0.0125 * R, 0.0125 * R, 0.0125 * R);

glBindTexture (GL_TEXTURE_2D, texture);

glBegin (GL_TRIANGLE_STRIP);

for ( b = 0; b <VertexCount; b++)

{

glTexCoord2f (VERTEX[B].U, VERTEX[B].V);

glNormal3f(-VERTEX[B].X, -VERTEX[B].Y, -VERTEX[B].Z);

glVertex3f (VERTEX[B].Y, VERTEX[B].X, -VERTEX[B].Z);

}

m = 0;

for ( b = 0; b <VertexCount; b++)

{

glTexCoord2f (VERTEX[B].U, -VERTEX[B].V);

glNormal3f(-VERTEX[B].X, -VERTEX[B].Y, -VERTEX[B].Z);

glVertex3f (VERTEX[B].Y, VERTEX[B].X, VERTEX[B].Z);

}

glEnd();

glRotatef(angle,0,0,1);

}

the lighting code is given here

glPushMatrix();

gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); //defines a viewing transformation.

//GLdouble eyex, eyey,eyez, centerx,centery, centerz, upx, upy, upz..// have kept upz=1 so,

//to go left-right in the screen change x cood

//to go up(+z) and down(-z) to change z coord

//to go inside(-y) - outside(+y) the screen change y coord.

// Now translate to the sun

glTranslatef(0.0, -7.0, 3.0);

/* For LIGHT0 */

GLfloat lightZeroPosition[] = {0.0f, 0.0f, 0.0f, 1.0f};

/*GLfloat lightvec[] = {0.5f, 0.2f, 0.0f, 1.0f};*/

GLfloat lightZeroColor[] = {0.5f, 0.5f, 0.5f, 1.0f};

GLfloat amb[] = {1, 1, 1, 1};

GLfloat spec[] = {0.3, 0.3, 0.3, 1};

glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition);

glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);

glLightfv(GL_LIGHT0, GL_SPECULAR, spec);

//glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightvec );

//glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 20);

glEnable(GL_LIGHT0);

glRotatef(angle,0,0,1);

DisplaySphere(5,textures);// function to display the sun

am attaching some images also