PDA

View Full Version : Lighting Problem for the sphere



swethasharma
03-23-2012, 12:50 AM
Hello,

I am Posting my Problem again.I am trying to give Lighting for my sphere.The sphere is lit but it is not a smooth output .If somebody can point out where the problem is it would be nice.

Initializing the lighting position


void init(void)
{
int glewinitialize;
GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess[]={50.0};
GLfloat light_position[]={-500.0,500.0,1.0,0.0};
GLfloat light_position1[]={1.0,1.0,1.0,0.0};
GLfloat diffuse[]={1.0,1.0,1.0,1.0};
//GLfloat specular_light[]={0.5,0.5,0.5,1.0};
GLfloat lambient[]={0.1,0.1,0.1,1.0};
glClearColor(0.0,0.0,0.0,0.0);
glClearDepth(1.0);
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_spec ular);
glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_sh ininess);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightfv(GL_LIGHT0,GL_AMBIENT,lambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,diffuse);

glLightfv(GL_LIGHT1,GL_POSITION,light_position1);
glLightfv(GL_LIGHT1,GL_AMBIENT,lambient);
glLightfv(GL_LIGHT1,GL_DIFFUSE,diffuse);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_NORMALIZE);
glewinitialize=glewInit();
if(glewinitialize==GLEW_OK)
{
printf("GLEW is available\n");

}

}



creating sphere



void createsphere(int R,int H,int K,int Z)
{
int n;
int a;
int b;
int debug=0;
int d=0;
double mod;
double B;


n=0;
for(b=0;b<=180-space;b+=space)
{
for(a=0;a<=360-space;a+=space)
{

vertex[n].x=R*(sin((a*PI)/180))*(sin((b*PI)/180))-H;
vertex[n].z=R*(cos((b*PI)/180))*(sin((a*PI)/180))-Z;
//B=cos((a*PI)/180);
vertex[n].y=R*(cos((a*PI)/180))-K;
setNormals(n);
n++;

vertex[n].x=R*(sin((a*PI)/180))*(sin(((b+space)*PI)/180))-H;
vertex[n].z=R*(cos(((b+space)*PI)/180))*(sin((a*PI)/180))-Z;
vertex[n].y=R*(cos((a*PI)/180))-K;
setNormals(n);
n++;


setting up normals




void setNormals(int n)
{
double mod;
double v1[3],v2[3];
if((n==0)||(n==1))
{
mod=sqrt(vertex[n].x*vertex[n].x+vertex[n].y*vertex[n].y+vertex[n].z*vertex[n].z);
normalvec[n].x=-vertex[n].x/mod;
normalvec[n].y=-vertex[n].y/mod;
normalvec[n].z=-vertex[n].z/mod;
}
else
{
v1[0]=vertex[n-2].x-vertex[n].x;
v1[1]=vertex[n-2].y-vertex[n].y;
v1[2]=vertex[n-2].z-vertex[n].z;

v2[0]=vertex[n-1].x-vertex[n].x;
v2[1]=vertex[n-1].y-vertex[n].y;
v2[2]=vertex[n-1].z-vertex[n].z;

normalvec[n].x=((v1[1]*v2[2])-(v1[2]*v2[1]));
normalvec[n].y=((v1[2]*v2[0])-(v1[0]*v2[2]));
normalvec[n].z=((v1[0]*v2[1])-(v1[1]*v2[0]));

mod=sqrt(normalvec[n].x*normalvec[n].x+normalvec[n].y*normalvec[n].y+normalvec[n].z*normalvec[n].z);
normalvec[n].x=normalvec[n].x/mod;
normalvec[n].y=normalvec[n].y/mod;
normalvec[n].z=normalvec[n].z/mod;
}
}



Displaying sphere



void displaysphere(double R)
{
int b,b1,b2,b3,b4;
int theta;
int phi;

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

for(theta=0;theta<90;theta=theta+10)
{
for(phi=10;phi<=350;phi=phi+10)
{
b=(phi/10)*2+(theta/10)*72;
b1=((phi-10)/10)*2+(theta/10)*72;
b2=((phi+10)/10)*2+(theta/10)*72;
b3=(phi/10)*2+((theta+10)/10)*72;
b4=(phi/10)*2+((theta+10)/10)*72-2;

normalvec[B].x=(normalvec[B].x+normalvec[b1].x+normalvec[b2].x+normalvec[b3].x+normalvec[b4].x)/5;
normalvec[B].y=(normalvec[B].y+normalvec[b1].y+normalvec[b2].y+normalvec[b3].y+normalvec[b4].y)/5;
normalvec[B].z=(normalvec[B].z+normalvec[b1].z+normalvec[b2].z+normalvec[b3].z+normalvec[b4].z)/5;

}
}

glBegin(GL_TRIANGLE_STRIP);

for(b=1;b<=vertexcount;b++)
{
glNormal3f(normalvec[B].x,normalvec[B].y,normalvec[B].z);
glVertex3f(vertex[B].x,vertex[B].y,vertex[B].z);

}