PDA

View Full Version : Lighting Problem for the sphere



swethasharma
03-21-2012, 05:17 AM
Hello,

I have created a sphere with lighting options enabled .My sphere is not smooth,it is faceted.

My code is as below:


#define GLEW_STATIC
#include <stdio.h>
#include <stdlib.h>
#include <gl/glew.h>
#include <glfw.h>
#include <math.h>


#define space 10
#define vertexcount (180/space)*(360/space)*2



const double PI=3.1415926535897;

struct Vertices
{
double x;
double y;
double z;
} vertex [vertexcount];
struct Vertices normalvec[vertexcount];
void init(void)
{
int glewinitialize;
GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess[]={50.0};
GLfloat light_position[]={100.0,10.0,-1.0,0.0};
GLfloat diffuse[]={0.8,0.8,0.8,1.0};
GLfloat specular_light[]={0.5,0.5,0.5,1.0};
GLfloat lambient[]={0.5,0.5,0.5,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,specular_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
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");

}

}

void GLFWCALL reshape(int width,int height)
{

glViewport(0,0,500,500);
glMatrixMode(GL_PROJECTION);

glLoadIdentity();
glOrtho(-10.0,(GLdouble)10.0,-10.0,(GLdouble)10.0,-10.0,200.0);


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);
}

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]));
}
}
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++;


}
}
}

void displaysphere(double R)
{
int b,b1,b2,b3,b4;
int theta;
int phi;
GLfloat mat[16];
glScalef(0.0125*R,0.0125*R,0.0125*R);
glGetFloatv(GL_MODELVIEW_MATRIX,mat);


glColor3f(1.0,0.5,0.0);



for(theta=0;theta<90;theta=theta+10)
{
for(phi=10;phi<=350;phi=phi+10)
{
if(theta==0)
{
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=((360-phi)/10)*2+((theta+10)/10)*72;
}
else
{
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;

}
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);

}

glEnd();

}
void display(void)
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,0.5,0.0);



glDepthFunc(GL_LEQUAL);

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glPushMatrix();
// glTranslatef(0.0,0.0,-10.0);
glColor3f(1.0,0.5,0.0);
createsphere(20,0,0,0);
displaysphere(15);
glPopMatrix();

glPushMatrix();
glColor3f(0.0,1.0,0.0);
createsphere(20,0,0,0);
glTranslatef(10.0,10.0,-50.0);
//displaysphere(10);
glPopMatrix();

glfwSwapBuffers();
}
int main(int argc,char** argv)
{

int wndstate,wndopenstate;

wndstate=glfwInit();

if(wndstate==1)
{
wndopenstate=glfwOpenWindow(640,480,0,0,0,0,4,0,GL FW_WINDOW);

if(wndopenstate==0)
{

glfwTerminate();
}
else
{
glfwSetWindowTitle("Hello Opengl");
glfwSetWindowPos(30,30);
init();
glfwSetWindowSizeCallback(reshape);

while(1)
{
display();
if(glfwGetKey(GLFW_KEY_ESC))
{
glfwTerminate();

}

}
}
}

glfwTerminate();
return 0;

}

Please tell me where i have gone wrong?
-swetha