// Light.cpp: implementation of the CLight class. // ////////////////////////////////////////////////////////////////////// #include "Light.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CLight::CLight():CObjectInScene() { Type=LIGHT; Name="light"; Visible=true; Selected=false; LightType=POINT; PosOrDir[4]=1.0; NormalColor[0]=1.0; NormalColor[1]=0.0; NormalColor[2]=0.0; } CLight::~CLight() { } void CLight::render() { float d[3],t; int i; if (isSelected()) glColor3fv(HighlightColor); else glColor3fv(NormalColor); GLUquadric* quad=gluNewQuadric(); GLdouble radius=0.3; GLint slices=10; GLint stacks=10; gluSphere(quad, radius,slices,stacks); for(i=0;i<3;i++) d[i]=PosOrDir[i]; t=0; for(i=0;i<3;i++) t+=d[i]*d[i]; t=sqrt(t); if (t==0) printf("Error in the direction of light"); else for(i=0;i<3;i++) d[i]/=t; switch(LightType){ case POINT: break; case DIRECTION: case SPOTLIGHT: glBegin(GL_LINES); glVertex3d(0.0,0.0,0.0); glVertex3d(3*d[0],3*d[1],3*d[2]); glEnd(); break; } } void CLight::readProperty(FILE *fp) { int i; char s[81]; float x,y,z; i = fseek( fp, 0L, SEEK_SET); if( i ) printf( "Fseek failed"); do { fscanf( fp, "%s", s ); i=strcmp( s,Name); }while(!feof(fp) && i!=0); if(!feof(fp)){ fscanf( fp, "%s", s ); fscanf( fp, "%s", s ); } if(!feof(fp)){ if(strcmp( s,"point")==0){ fscanf(fp, "%s\t%f,%f,%f\n",s,&x,&y,&z); PosOrDir[0]=x;PosOrDir[1]=y;PosOrDir[2]=z; }else if(strcmp(s,"direction")==0){ fscanf(fp, "%s\t%f,%f,%f\n",s,&x,&y,&z); PosOrDir[0]=x;PosOrDir[1]=y;PosOrDir[2]=z; }else if(strcmp( s,"spotlight")==0){ fscanf(fp, "%s\t%f,%f,%f\n",s,&x,&y,&z); PosOrDir[0]=x;PosOrDir[1]=y;PosOrDir[2]=z; fscanf(fp, "%s\t%f\n",s,&x); Exponent=x; fscanf(fp, "%s\t%f\n",s, &x); SpotCutOff=x; } fscanf(fp, "%s\t%f,%f,%f\n",s,&x,&y,&z); Diffuse[0]=x;Diffuse[1]=y;Diffuse[2]=z; fscanf(fp, "%s\t%f,%f,%f\n",s,&x,&y,&z); Specular[0]=x;Specular[1]=y;Specular[2]=z; fscanf(fp, "%s\t%f,%f,%f\n",s,&x,&y,&z); Ambient[0]=x;Ambient[1]=y; Ambient[2]=z; fscanf(fp, "%s\t%f\n",s, &x); Attenuation=x; } } void CLight::writeProperty(FILE *fp) { fprintf(fp, "%s\t%s\n","Name:", Name); switch(LightType){ case POINT: fprintf(fp, "%s\t%s\n","Type:", "point"); fprintf(fp, "%s\t%f,%f,%f\n","Position:", PosOrDir[0],PosOrDir[1],PosOrDir[2]); break; case DIRECTION: fprintf(fp, "%s\t%s\n","Type:", "direction"); fprintf(fp, "%s\t%f,%f,%f\n","Direction:", PosOrDir[0],PosOrDir[1],PosOrDir[2]); break; case SPOTLIGHT: fprintf(fp, "%s\t%s\n","Type:", "spotlight"); fprintf(fp, "%s\t%f,%f,%f\n","direction:", PosOrDir[0],PosOrDir[1],PosOrDir[2]); fprintf(fp, "%s\t%f\n","exponent:", Exponent); fprintf(fp, "%s\t%f\n","cutoff:", SpotCutOff); break; } fprintf(fp, "%s\t%f,%f,%f\n","diffuse:", Diffuse[0],Diffuse[1],Diffuse[2]); fprintf(fp, "%s\t%f,%f,%f\n","specular:", Specular[0],Specular[1],Specular[2]); fprintf(fp, "%s\t%f,%f,%f\n","ambient:", Ambient[0],Ambient[1], Ambient[2]); fprintf(fp, "%s\t%f\n\n","attenuation:", Attenuation); }