// How To Make sphere // explanation of sphere mesh modeling // Gouraud shading // T. Kosaka CS TNCT 2001 #include #include #include #ifndef M_PI #define M_PI 3.141592653589793 #endif typedef struct { float m[4][4]; } matrix3D_t; typedef struct { float v[4]; } vector3D_t; typedef struct { float x; float y; float z; } point3D_t; typedef struct { float x; float y; } point2D_t; typedef struct { float r; float g; float b; } color_t; ////////////////// matrices and vectors 3D ver 2 ///////////////// matrix3D_t createIdentity(void) { matrix3D_t u; int i,j; for (i=0;i<4;i++) { for(j=0;j<4;j++) u.m[i][j]=0.; u.m[i][i]=1.; } return u; } matrix3D_t operator * (matrix3D_t a,matrix3D_t b) { matrix3D_t c;//c=a*b int i,j,k; for (i=0;i<4;i++) for (j=0;j<4;j++) { c.m[i][j]=0; for (k=0;k<4;k++) c.m[i][j]+=a.m[i][k]*b.m[k][j]; } return c; } vector3D_t operator * (matrix3D_t a, vector3D_t b) { vector3D_t c;//c=a*b int i,j; for (i=0;i<4;i++) { c.v[i]=0; for (j=0;j<4;j++) c.v[i]+=a.m[i][j]*b.v[j]; } return c; } matrix3D_t translationMTX(float dx,float dy,float dz) { matrix3D_t trans=createIdentity(); trans.m[0][3]=dx; trans.m[1][3]=dy; trans.m[2][3]=dz; return trans; } matrix3D_t rotationXMTX(float theta) { matrix3D_t rotate=createIdentity(); float cs=cos(theta); float sn=sin(theta); rotate.m[1][1]=cs; rotate.m[1][2]=-sn; rotate.m[2][1]=sn; rotate.m[2][2]=cs; return rotate; } matrix3D_t rotationYMTX(float theta) { matrix3D_t rotate=createIdentity(); float cs=cos(theta); float sn=sin(theta); rotate.m[0][0]=cs; rotate.m[0][2]=sn; rotate.m[2][0]=-sn; rotate.m[2][2]=cs; return rotate; } matrix3D_t rotationZMTX(float theta) { matrix3D_t rotate=createIdentity(); float cs=cos(theta); float sn=sin(theta); rotate.m[0][0]=cs; rotate.m[0][1]=-sn; rotate.m[1][0]=sn; rotate.m[1][1]=cs; return rotate; } matrix3D_t scalingMTX(float factorx,float factory,float factorz) { matrix3D_t scale=createIdentity(); scale.m[0][0]=factorx; scale.m[1][1]=factory; scale.m[2][2]=factorz; return scale; } matrix3D_t perspectiveMTX(float eyelength) { matrix3D_t perspective=createIdentity(); perspective.m[3][2]=-1./eyelength; return perspective; } point2D_t Vector2Point2D(vector3D_t vec) { point2D_t pnt; pnt.x=vec.v[0]; pnt.y=vec.v[1]; return pnt; } point3D_t Vector2Point3D(vector3D_t vec) { point3D_t pnt; pnt.x=vec.v[0]; pnt.y=vec.v[1]; pnt.z=vec.v[2]; return pnt; } vector3D_t Point2Vector(point3D_t pnt) { vector3D_t vec; vec.v[0]=pnt.x; vec.v[1]=pnt.y; vec.v[2]=pnt.z; vec.v[3]=1.; return vec; } vector3D_t homogenizeVector(vector3D_t vec) { int i; for (i=0;i<3;i++) { vec.v[i]/=vec.v[3]; } vec.v[3]=1.; return vec; } vector3D_t unitVector(vector3D_t vec) { int i; float vec2=0.; float vec1,invvec1; for (i=0;i<3;i++) { vec2+=vec.v[i]*vec.v[i]; } vec1=sqrt(vec2); if (vec1!=0.) { invvec1=1./vec1; for (i=0;i<3;i++) { vec.v[i]*=invvec1; } } vec.v[3]=1.; return vec; } // inner product (dot product) of homogeneous vector float operator * (vector3D_t a, vector3D_t b) { float c;//c=a*b int i; c=0; for (i=0;i<3;i++) { c+=a.v[i]*b.v[i]; } return c; } // outer product (cross product ) of homogeneous vector // i j k // a0 a1 a2 // b0 b1 b2 vector3D_t operator ^ (vector3D_t a, vector3D_t b) { vector3D_t c;//c=a*b c.v[0]=a.v[1]*b.v[2]-a.v[2]*b.v[1]; c.v[1]=a.v[2]*b.v[0]-a.v[0]*b.v[2]; c.v[2]=a.v[0]*b.v[1]-a.v[1]*b.v[0]; c.v[3]=1.; return c; } vector3D_t operator - (vector3D_t v1,vector3D_t v0) { vector3D_t c;//c=v1-v0 c.v[0]=v1.v[0]-v0.v[0]; c.v[1]=v1.v[1]-v0.v[1]; c.v[2]=v1.v[2]-v0.v[2]; c.v[3]=1.; return c; } vector3D_t operator - (vector3D_t v) { vector3D_t c;//c=-v c.v[0]=-v.v[0]; c.v[1]=-v.v[1]; c.v[2]=-v.v[2]; c.v[3]=1.; return c; } vector3D_t operator * (float r, vector3D_t b) { vector3D_t c;//c=r*b int i; for (i=0;i<3;i++) { c.v[i]=r*b.v[i]; } c.v[3]=1.; return c; } vector3D_t operator * (vector3D_t b, float r) { vector3D_t c;//c=r*b int i; for (i=0;i<3;i++) { c.v[i]=r*b.v[i]; } c.v[3]=1.; return c; } float funcPositive(float x) { if (0.