brikut code nya, semoga bermanfaat.
#include <glut.h>
#include <math.h>
#include <stdlib.h>
#ifndef M_PI
//#define M_PI 3.14
#define M_PI 22/7
#endif
typedef struct {
float v[3];
} vektor2D_t;
typedef struct {
float m[3][3];
} matrik2D_t;
typedef struct {
float x,y;
} point2D_t;
typedef struct {
float r,g,b;
} warna_t;
point2D_t interpolasi(point2D_t a,point2D_t b, float m)
{
point2D_t c;
c.x=m*a.x+(1.-m)*b.x;
c.y=m*a.y+(1.-m)*b.y;
return c;
}
point2D_t konversi(point2D_t p,float a)
{
point2D_t q;
q.x=a*p.x;
q.y=a*p.y;
return(q);
}
// --------------
// Operasi Matrik
// --------------
matrik2D_t BuatMatrikIdentitas()
{
int i,j;
matrik2D_t I;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
I.m[i][j]=0.;
I.m[i][i]=1.;
}
return I;
}
matrik2D_t Translasi(float tambahx, float tambahy)
{
matrik2D_t a;
a=BuatMatrikIdentitas();
a.m[0][2]=tambahx;
a.m[1][2]=tambahy;
return a;
}
matrik2D_t Scaling(float rx,float ry)
{
matrik2D_t a;
a=BuatMatrikIdentitas();
a.m[0][0]=rx;
a.m[1][1]=ry;
return a;
}
matrik2D_t Rotasi(float sudut)
{
matrik2D_t a;
a=BuatMatrikIdentitas();
a.m[0][0]=cos(sudut); a.m[0][1]=-sin(sudut);
a.m[1][0]=sin(sudut); a.m[1][1]=cos(sudut);
return a;
}
matrik2D_t operator * (matrik2D_t a, matrik2D_t b)
{
matrik2D_t c;//c=a*b
int i,j,k;
for (i=0;i<3;i++) for (j=0;j<3;j++) {
c.m[i][j]=0;
for (k=0;k<3;k++) c.m[i][j]+=a.m[i][k]*b.m[k][j];
}
return c;
}
vektor2D_t operator * (matrik2D_t a, vektor2D_t b)
{
vektor2D_t c;//c=a*b
int i,j;
for (i=0;i<3;i++) {
c.v[i]=0;
for (j=0;j<3;j++) c.v[i]+=a.m[i][j]*b.v[j];
}
return c;
}
// ------------------------
// Pemindahan Struktur Data
// ------------------------
vektor2D_t point2vektor (point2D_t a)
{
vektor2D_t b;
b.v[0]=a.x;
b.v[1]=a.y;
b.v[2]=1.;
return b;
}
point2D_t vektor2point (vektor2D_t a)
{
point2D_t b;
b.x=a.v[0];
b.y=a.v[1];
return b;
}
typedef struct {
float m[4][4];
} matrix3D_t;
typedef struct {
float v[4];
} vector3D_t;
typedef struct {
float x,y,z;
} point3D_t;
typedef struct {
float r,g,b;
} color_t;
matrix3D_t createIdentity()
{
int i,j;
matrix3D_t u;
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;
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;
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;
}
point2D_t Vector2Point3D(vector3D_t vec)
{
point2D_t pnt;
pnt.x=vec.v[0];
pnt.y=vec.v[1];
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;
}
float operator*(vector3D_t a, vector3D_t b)
{
float c;
int i;
c=0;
for(i=0;i<3;i++)
{
c+=a.v[i]*b.v[i];
}
return c;
}
vector3D_t operator^(vector3D_t a, vector3D_t b)
{
vector3D_t c;
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.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[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;
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;
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<x)
return x;
else
return 0;
}
float power(float x, float y)
{
if(x==0)
return 0;
return exp(y*log(x));
}
color_t operator+(color_t c1, color_t c2)
{
color_t col;
col.r=c1.r+c2.r;
col.g=c1.g+c2.g;
col.b=c1.b+c2.b;
return col;
}
color_t operator*(float r, color_t c)
{
color_t col;
col.r=r*c.r;
col.g=r*c.g;
col.b=r*c.b;
return col;
}
color_t operator*(color_t c, float r)
{
color_t col;
col.r=r*c.r;
col.g=r*c.g;
col.b=r*c.b;
return col;
}
color_t PhongModel(vector3D_t Light, vector3D_t Normal, vector3D_t View, color_t col)
{
float kspe=0.7;
float kdif=0.6;
float kamb=0.4;
float tmp,NL,RV;
color_t ColWhite={1,1,1};
vector3D_t ReflectionVector=(2*(Light*Normal)*Normal)-Light;
tmp=Normal*Light;
NL=funcPositive(tmp);
tmp=ReflectionVector*View;
RV=funcPositive(tmp);
return kdif*NL*col+kspe*power(RV,4)*ColWhite+kamb*col;
}
void setColor(float red, float green, float blue)
{
glColor3f(red,green,blue);
}
void setColor(color_t col)
{
glColor3f(col.r,col.g,col.b);
}
void drawDot(float x, float y)
{
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}
void drawLine(float x1,float y1, float x2, float y2)
{
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
}
void drawline(point2D_t p1,point2D_t p2)
{
drawLine(p1.x,p1.y,p2.x,p2.y);
}
void drawPolyline(point2D_t a[],int n)
{
int i;
glBegin(GL_LINE_STRIP);
for(i=0;i<n;i++)
glVertex2f(a[i].x,a[i].y);
glEnd();
}
void drawPolygon(point2D_t a[],int n)
{
int i;
glBegin(GL_LINE_LOOP);
for(i=0;i<n;i++)
glVertex2f(a[i].x,a[i].y);
glEnd();
}
void fillPolygon(point2D_t a[],int n, color_t w)
{
int i;
setColor(w);
glBegin(GL_POLYGON);
for(i=0;i<n;i++)
glVertex2f(a[i].x,a[i].y);
glEnd();
}
void gradatePolygon(point2D_t a[],color_t w[],int n)
{
int i;
glBegin(GL_POLYGON);
for(i=0;i<n;i++)
{
glColor3f(w[i].r,w[i].g,w[i].b);
glVertex2f(a[i].x,a[i].y);
}
glEnd();
}
void userdraw(void);
void display(void)
{
glClear( GL_COLOR_BUFFER_BIT);
userdraw();
glutSwapBuffers();
}
void drawcharX(float x, float y)
{
drawLine(x,y,x+10,y+12);
drawLine(x,y+12,x+10,y);
}
void drawcharY(float x, float y)
{
drawLine(x+5,y,x+5,y+7);
drawLine(x,y+12,x+5,y+7);
drawLine(x+10,y+12,x+5,y+7);
}
void drawcharZ(float x, float y)
{
drawLine(x,y+12,x+10,y+12);
drawLine(x+10,y+12,x,y);
drawLine(x,y,x+10,y);
}
void drawAxes(matrix3D_t view)
{
#define HALFAXIS 220
#define HALFAXIS1 (HALFAXIS-10)
point3D_t axes[14]={{-HALFAXIS,0,0},{HALFAXIS,0,0},{HALFAXIS1,5,0},{HALFAXIS1,0,0},{0,0,0},{0,-HALFAXIS,0},{0,HALFAXIS,0},{0,HALFAXIS1,5},{0,HALFAXIS1,0},{0,0,0},{0,0,-HALFAXIS},{0,0,HALFAXIS},{5,0,HALFAXIS1},{0,0,HALFAXIS1}};
vector3D_t vec[14];
point2D_t buff[14];
int i;
for(i=0;i<14;i++)
{
vec[i]=Point2Vector(axes[i]);
vec[i]=view*vec[i];
buff[i]=Vector2Point2D(vec[i]);
}
drawPolyline(buff,14);
drawcharX(buff[1].x,buff[1].y);
drawcharY(buff[6].x,buff[6].y);
drawcharZ(buff[11].x-14,buff[11].y);
}
typedef struct
{
int NumberofVertices;
short int pnt[32];
}face_t;
typedef struct
{
int NumberofVertices;
point3D_t pnt[1000];
int NumberofFaces;
face_t fc[500];
}object3D_t;
void userdraw()
{
static int tick=0;
// glLineWidth(4.0);
float teta=0.3;
matrix3D_t tilting=rotationYMTX(-teta)*rotationXMTX(teta);
// drawAxes(tilting);
matrix3D_t mat;
vector3D_t vec[100],vecbuff[100];
point2D_t titik2D[100];
int i,j,k=25;
//kotak
object3D_t kotak={4,{{-100,80,0},{-30,80,0},{-30,80,-200},
{-100,80,-200}},
1,
{{4,{0,1,2,3}}}};
//tombol
object3D_t tombol={4,{{-10,80,-160},{0,80,-160},{0,80,-180},
{-10,80,-180}},
1,
{{4,{0,1,2,3}}}};
//tombol tengah
object3D_t tombol1={4,{{10,80,-160},{30,80,-160},{30,80,-180},
{10,80,-180}},
1,
{{4,{0,1,2,3}}}};
//tombol tengah1
object3D_t tombol2={4,{{40,80,-160},{60,80,-160},{60,80,-180},
{40,80,-180}},
1,
{{4,{0,1,2,3}}}};
//tombol kananne tengah1
object3D_t tombol3={4,{{70,80,-160},{80,80,-160},{80,80,-180},
{70,80,-180}},
1,
{{4,{0,1,2,3}}}};
//tombol tengah besar
object3D_t tombol4={4,{{10,80,-120},{60,80,-120},{60,80,-140},
{10,80,-140}},
1,
{{4,{0,1,2,3}}}};
//tombol tengah depane besar
object3D_t tombol5={4,{{30,80,-80},{50,80,-80},{50,80,-100},
{30,80,-100}},
1,
{{4,{0,1,2,3}}}};
//copcopan kabel monitor
object3D_t cop1={6,{{0,10,-200},{-10,15,-200},{-5,20,-200},
{30,20,-200},{35,15,-200},{20,10,-200}},
1,
{{6,{0,1,2,3,4,5}}}};
//copcopan kabel power
object3D_t cop={6,{{-100,10,-200},{-100,30,-200},{-90,40,-200},
{-70,40,-200},{-60,30,-200},{-60,10,-200}},
1,
{{6,{0,1,2,3,4,5}}}};
//proyektor
object3D_t pro={16,{
{-100,0,0},{-120,20,0},{-120,60,0},{-100,80,0},
{100,80,0},{120,60,0},{120,20,0},{100,0,0},
{-100,0,-200},{-120,20,-200},{-120,60,-200},{-100,80,-200},
{100,80,-200},{120,60,-200},{120,20,-200},{100,0,-200}
},
10,
{{8,{0,1,2,3,4,5,6,7}},{8,{8,9,10,11,12,13,14,15}},
{4,{0,1,9,8}},{4,{1,2,10,9}},
{4,{2,3,11,10}},{4,{3,4,12,11}},
{4,{4,5,13,12}},{4,{5,6,14,13}},
{4,{6,7,15,14}},{4,{7,0,8,15}}}};
//kerangka depanbelakang proyektor
object3D_t pro1={16,{
{-100,0,0},{-120,20,0},{-120,60,0},{-100,80,0},
{100,80,0},{120,60,0},{120,20,0},{100,0,0},
{-100,0,-200},{-120,20,-200},{-120,60,-200},{-100,80,-200},
{100,80,-200},{120,60,-200},{120,20,-200},{100,0,-200}
},
2,
{{8,{0,1,2,3,4,5,6,7}},{8,{8,9,10,11,12,13,14,15}
}}};
mat=tilting;
mat=translationMTX(0,+100,0)*rotationZMTX(3.14)*tilting;
mat=rotationYMTX(tick/57.3)*mat;
glLineWidth(4.0);
//cetak proyektor
color_t a={0.9,0.9,0.9};
for(i=0;i<pro.NumberofVertices;i++)
{
setColor(0,0,0);
vec[i]=Point2Vector(pro.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<pro.NumberofFaces;i++)
{
for(j=0;j<pro.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[pro.fc[i].pnt[j]];
for(j=0;j<pro.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[pro.fc[i].pnt[j]]);
fillPolygon(titik2D,pro.fc[i].NumberofVertices,a);
}
//cetak kerangka depanbelakang proyektor
for(i=0;i<pro1.NumberofVertices;i++)
{
setColor(0,0,0);
vec[i]=Point2Vector(pro.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<pro1.NumberofFaces;i++)
{
for(j=0;j<pro1.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[pro1.fc[i].pnt[j]];
for(j=0;j<pro1.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[pro1.fc[i].pnt[j]]);
drawPolygon(titik2D,pro1.fc[i].NumberofVertices);
}
glLineWidth(1.0);
//cetak kotak atas
color_t ktas={0.1,0.1,0.1};
for(i=0;i<kotak.NumberofVertices;i++)
{ setColor(1,1,1);
vec[i]=Point2Vector(kotak.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<kotak.NumberofFaces;i++)
{
for(j=0;j<kotak.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[kotak.fc[i].pnt[j]];
for(j=0;j<kotak.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[kotak.fc[i].pnt[j]]);
fillPolygon(titik2D,kotak.fc[i].NumberofVertices,ktas);
}
color_t tmbl={0,0.9,0};
//cetak tombol
for(i=0;i<tombol.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,1,0);
vec[i]=Point2Vector(tombol.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<tombol.NumberofFaces;i++)
{
for(j=0;j<tombol.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[tombol.fc[i].pnt[j]];
for(j=0;j<tombol.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[tombol.fc[i].pnt[j]]);
fillPolygon(titik2D,tombol.fc[i].NumberofVertices,tmbl);
}
//cetak tombol tengah
for(i=0;i<tombol1.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,1,0);
vec[i]=Point2Vector(tombol1.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<tombol1.NumberofFaces;i++)
{
for(j=0;j<tombol1.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[tombol1.fc[i].pnt[j]];
for(j=0;j<tombol1.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[tombol1.fc[i].pnt[j]]);
fillPolygon(titik2D,tombol1.fc[i].NumberofVertices,tmbl);
}
//cetak tombol tengah1
for(i=0;i<tombol2.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,1,0);
vec[i]=Point2Vector(tombol2.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<tombol2.NumberofFaces;i++)
{
for(j=0;j<tombol2.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[tombol2.fc[i].pnt[j]];
for(j=0;j<tombol2.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[tombol2.fc[i].pnt[j]]);
fillPolygon(titik2D,tombol2.fc[i].NumberofVertices,tmbl);
}
//cetak tombol kanane tengah1
for(i=0;i<tombol3.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,1,0);
vec[i]=Point2Vector(tombol3.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<tombol3.NumberofFaces;i++)
{
for(j=0;j<tombol3.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[tombol.fc[i].pnt[j]];
for(j=0;j<tombol3.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[tombol3.fc[i].pnt[j]]);
fillPolygon(titik2D,tombol3.fc[i].NumberofVertices,tmbl);
}
//cetak tombol tengah besar
for(i=0;i<tombol4.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,1,0);
vec[i]=Point2Vector(tombol4.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<tombol.NumberofFaces;i++)
{
for(j=0;j<tombol4.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[tombol4.fc[i].pnt[j]];
for(j=0;j<tombol4.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[tombol4.fc[i].pnt[j]]);
fillPolygon(titik2D,tombol4.fc[i].NumberofVertices,tmbl);
}
//cetak tombol tengah depane besar
for(i=0;i<tombol5.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,1,0);
vec[i]=Point2Vector(tombol5.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<tombol5.NumberofFaces;i++)
{
for(j=0;j<tombol5.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[tombol5.fc[i].pnt[j]];
for(j=0;j<tombol5.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[tombol5.fc[i].pnt[j]]);
fillPolygon(titik2D,tombol5.fc[i].NumberofVertices,tmbl);
}
//cetak cop copan kabel power
for(i=0;i<cop.NumberofVertices;i++)
{ glLineWidth(4.0);
setColor(0,0,0);
vec[i]=Point2Vector(cop.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<cop.NumberofFaces;i++)
{
for(j=0;j<cop.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[cop.fc[i].pnt[j]];
for(j=0;j<cop.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[cop.fc[i].pnt[j]]);
drawPolygon(titik2D,cop.fc[i].NumberofVertices);
}
//cetak cop copan kabel monitor
for(i=0;i<cop1.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,0,0);
vec[i]=Point2Vector(cop1.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<cop1.NumberofFaces;i++)
{
for(j=0;j<cop1.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[cop1.fc[i].pnt[j]];
for(j=0;j<cop1.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[cop1.fc[i].pnt[j]]);
drawPolygon(titik2D,cop1.fc[i].NumberofVertices);
}
static point2D_t bola[360];
matrik2D_t mat1,mat2;
vektor2D_t vektor;
mat1=Translasi(-70,38);
point2D_t orbit1[360],orbit2[360];
//warna matahari
warna_t w1={1.,0.3,0.};
//warna bumi
warna_t w2={0.,1.,0.};
//warna bulan
warna_t w3={1.0,0.,0.};
for(k=0;k<360;k++)
{
bola[k].x=cos(k+5*M_PI/180.);
bola[k].y=sin(k+5*M_PI/180.);
}
for(k=0;k<360;k++)
{
orbit1[k]=konversi(bola[k],30);
orbit2[k]=konversi(bola[k],60);
}
for(int m=0;m<360;m++)
{
vektor=point2vektor(orbit2[m]);
vektor=mat1*vektor;
orbit2[m]=vektor2point(vektor);
vektor=point2vektor(orbit1[m]);
vektor=mat1*vektor;
orbit1[m]=vektor2point(vektor);
}
glColor3f(0.,1.,1.);
drawPolygon(orbit1,360);
// drawPolygon(orbit2,360);
tick++;
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowPosition(100,100);
glutInitWindowSize(680,480);
glutCreateWindow ("");
glClearColor(1, 0.0, 0.6,0);
gluOrtho2D(-320., 320., -240.0, 240.0);
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
#include <glut.h>
#include <math.h>
#include <stdlib.h>
#ifndef M_PI
//#define M_PI 3.14
#define M_PI 22/7
#endif
typedef struct {
float v[3];
} vektor2D_t;
typedef struct {
float m[3][3];
} matrik2D_t;
typedef struct {
float x,y;
} point2D_t;
typedef struct {
float r,g,b;
} warna_t;
point2D_t interpolasi(point2D_t a,point2D_t b, float m)
{
point2D_t c;
c.x=m*a.x+(1.-m)*b.x;
c.y=m*a.y+(1.-m)*b.y;
return c;
}
point2D_t konversi(point2D_t p,float a)
{
point2D_t q;
q.x=a*p.x;
q.y=a*p.y;
return(q);
}
// --------------
// Operasi Matrik
// --------------
matrik2D_t BuatMatrikIdentitas()
{
int i,j;
matrik2D_t I;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
I.m[i][j]=0.;
I.m[i][i]=1.;
}
return I;
}
matrik2D_t Translasi(float tambahx, float tambahy)
{
matrik2D_t a;
a=BuatMatrikIdentitas();
a.m[0][2]=tambahx;
a.m[1][2]=tambahy;
return a;
}
matrik2D_t Scaling(float rx,float ry)
{
matrik2D_t a;
a=BuatMatrikIdentitas();
a.m[0][0]=rx;
a.m[1][1]=ry;
return a;
}
matrik2D_t Rotasi(float sudut)
{
matrik2D_t a;
a=BuatMatrikIdentitas();
a.m[0][0]=cos(sudut); a.m[0][1]=-sin(sudut);
a.m[1][0]=sin(sudut); a.m[1][1]=cos(sudut);
return a;
}
matrik2D_t operator * (matrik2D_t a, matrik2D_t b)
{
matrik2D_t c;//c=a*b
int i,j,k;
for (i=0;i<3;i++) for (j=0;j<3;j++) {
c.m[i][j]=0;
for (k=0;k<3;k++) c.m[i][j]+=a.m[i][k]*b.m[k][j];
}
return c;
}
vektor2D_t operator * (matrik2D_t a, vektor2D_t b)
{
vektor2D_t c;//c=a*b
int i,j;
for (i=0;i<3;i++) {
c.v[i]=0;
for (j=0;j<3;j++) c.v[i]+=a.m[i][j]*b.v[j];
}
return c;
}
// ------------------------
// Pemindahan Struktur Data
// ------------------------
vektor2D_t point2vektor (point2D_t a)
{
vektor2D_t b;
b.v[0]=a.x;
b.v[1]=a.y;
b.v[2]=1.;
return b;
}
point2D_t vektor2point (vektor2D_t a)
{
point2D_t b;
b.x=a.v[0];
b.y=a.v[1];
return b;
}
typedef struct {
float m[4][4];
} matrix3D_t;
typedef struct {
float v[4];
} vector3D_t;
typedef struct {
float x,y,z;
} point3D_t;
typedef struct {
float r,g,b;
} color_t;
matrix3D_t createIdentity()
{
int i,j;
matrix3D_t u;
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;
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;
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;
}
point2D_t Vector2Point3D(vector3D_t vec)
{
point2D_t pnt;
pnt.x=vec.v[0];
pnt.y=vec.v[1];
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;
}
float operator*(vector3D_t a, vector3D_t b)
{
float c;
int i;
c=0;
for(i=0;i<3;i++)
{
c+=a.v[i]*b.v[i];
}
return c;
}
vector3D_t operator^(vector3D_t a, vector3D_t b)
{
vector3D_t c;
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.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[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;
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;
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<x)
return x;
else
return 0;
}
float power(float x, float y)
{
if(x==0)
return 0;
return exp(y*log(x));
}
color_t operator+(color_t c1, color_t c2)
{
color_t col;
col.r=c1.r+c2.r;
col.g=c1.g+c2.g;
col.b=c1.b+c2.b;
return col;
}
color_t operator*(float r, color_t c)
{
color_t col;
col.r=r*c.r;
col.g=r*c.g;
col.b=r*c.b;
return col;
}
color_t operator*(color_t c, float r)
{
color_t col;
col.r=r*c.r;
col.g=r*c.g;
col.b=r*c.b;
return col;
}
color_t PhongModel(vector3D_t Light, vector3D_t Normal, vector3D_t View, color_t col)
{
float kspe=0.7;
float kdif=0.6;
float kamb=0.4;
float tmp,NL,RV;
color_t ColWhite={1,1,1};
vector3D_t ReflectionVector=(2*(Light*Normal)*Normal)-Light;
tmp=Normal*Light;
NL=funcPositive(tmp);
tmp=ReflectionVector*View;
RV=funcPositive(tmp);
return kdif*NL*col+kspe*power(RV,4)*ColWhite+kamb*col;
}
void setColor(float red, float green, float blue)
{
glColor3f(red,green,blue);
}
void setColor(color_t col)
{
glColor3f(col.r,col.g,col.b);
}
void drawDot(float x, float y)
{
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}
void drawLine(float x1,float y1, float x2, float y2)
{
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
}
void drawline(point2D_t p1,point2D_t p2)
{
drawLine(p1.x,p1.y,p2.x,p2.y);
}
void drawPolyline(point2D_t a[],int n)
{
int i;
glBegin(GL_LINE_STRIP);
for(i=0;i<n;i++)
glVertex2f(a[i].x,a[i].y);
glEnd();
}
void drawPolygon(point2D_t a[],int n)
{
int i;
glBegin(GL_LINE_LOOP);
for(i=0;i<n;i++)
glVertex2f(a[i].x,a[i].y);
glEnd();
}
void fillPolygon(point2D_t a[],int n, color_t w)
{
int i;
setColor(w);
glBegin(GL_POLYGON);
for(i=0;i<n;i++)
glVertex2f(a[i].x,a[i].y);
glEnd();
}
void gradatePolygon(point2D_t a[],color_t w[],int n)
{
int i;
glBegin(GL_POLYGON);
for(i=0;i<n;i++)
{
glColor3f(w[i].r,w[i].g,w[i].b);
glVertex2f(a[i].x,a[i].y);
}
glEnd();
}
void userdraw(void);
void display(void)
{
glClear( GL_COLOR_BUFFER_BIT);
userdraw();
glutSwapBuffers();
}
void drawcharX(float x, float y)
{
drawLine(x,y,x+10,y+12);
drawLine(x,y+12,x+10,y);
}
void drawcharY(float x, float y)
{
drawLine(x+5,y,x+5,y+7);
drawLine(x,y+12,x+5,y+7);
drawLine(x+10,y+12,x+5,y+7);
}
void drawcharZ(float x, float y)
{
drawLine(x,y+12,x+10,y+12);
drawLine(x+10,y+12,x,y);
drawLine(x,y,x+10,y);
}
void drawAxes(matrix3D_t view)
{
#define HALFAXIS 220
#define HALFAXIS1 (HALFAXIS-10)
point3D_t axes[14]={{-HALFAXIS,0,0},{HALFAXIS,0,0},{HALFAXIS1,5,0},{HALFAXIS1,0,0},{0,0,0},{0,-HALFAXIS,0},{0,HALFAXIS,0},{0,HALFAXIS1,5},{0,HALFAXIS1,0},{0,0,0},{0,0,-HALFAXIS},{0,0,HALFAXIS},{5,0,HALFAXIS1},{0,0,HALFAXIS1}};
vector3D_t vec[14];
point2D_t buff[14];
int i;
for(i=0;i<14;i++)
{
vec[i]=Point2Vector(axes[i]);
vec[i]=view*vec[i];
buff[i]=Vector2Point2D(vec[i]);
}
drawPolyline(buff,14);
drawcharX(buff[1].x,buff[1].y);
drawcharY(buff[6].x,buff[6].y);
drawcharZ(buff[11].x-14,buff[11].y);
}
typedef struct
{
int NumberofVertices;
short int pnt[32];
}face_t;
typedef struct
{
int NumberofVertices;
point3D_t pnt[1000];
int NumberofFaces;
face_t fc[500];
}object3D_t;
void userdraw()
{
static int tick=0;
// glLineWidth(4.0);
float teta=0.3;
matrix3D_t tilting=rotationYMTX(-teta)*rotationXMTX(teta);
// drawAxes(tilting);
matrix3D_t mat;
vector3D_t vec[100],vecbuff[100];
point2D_t titik2D[100];
int i,j,k=25;
//kotak
object3D_t kotak={4,{{-100,80,0},{-30,80,0},{-30,80,-200},
{-100,80,-200}},
1,
{{4,{0,1,2,3}}}};
//tombol
object3D_t tombol={4,{{-10,80,-160},{0,80,-160},{0,80,-180},
{-10,80,-180}},
1,
{{4,{0,1,2,3}}}};
//tombol tengah
object3D_t tombol1={4,{{10,80,-160},{30,80,-160},{30,80,-180},
{10,80,-180}},
1,
{{4,{0,1,2,3}}}};
//tombol tengah1
object3D_t tombol2={4,{{40,80,-160},{60,80,-160},{60,80,-180},
{40,80,-180}},
1,
{{4,{0,1,2,3}}}};
//tombol kananne tengah1
object3D_t tombol3={4,{{70,80,-160},{80,80,-160},{80,80,-180},
{70,80,-180}},
1,
{{4,{0,1,2,3}}}};
//tombol tengah besar
object3D_t tombol4={4,{{10,80,-120},{60,80,-120},{60,80,-140},
{10,80,-140}},
1,
{{4,{0,1,2,3}}}};
//tombol tengah depane besar
object3D_t tombol5={4,{{30,80,-80},{50,80,-80},{50,80,-100},
{30,80,-100}},
1,
{{4,{0,1,2,3}}}};
//copcopan kabel monitor
object3D_t cop1={6,{{0,10,-200},{-10,15,-200},{-5,20,-200},
{30,20,-200},{35,15,-200},{20,10,-200}},
1,
{{6,{0,1,2,3,4,5}}}};
//copcopan kabel power
object3D_t cop={6,{{-100,10,-200},{-100,30,-200},{-90,40,-200},
{-70,40,-200},{-60,30,-200},{-60,10,-200}},
1,
{{6,{0,1,2,3,4,5}}}};
//proyektor
object3D_t pro={16,{
{-100,0,0},{-120,20,0},{-120,60,0},{-100,80,0},
{100,80,0},{120,60,0},{120,20,0},{100,0,0},
{-100,0,-200},{-120,20,-200},{-120,60,-200},{-100,80,-200},
{100,80,-200},{120,60,-200},{120,20,-200},{100,0,-200}
},
10,
{{8,{0,1,2,3,4,5,6,7}},{8,{8,9,10,11,12,13,14,15}},
{4,{0,1,9,8}},{4,{1,2,10,9}},
{4,{2,3,11,10}},{4,{3,4,12,11}},
{4,{4,5,13,12}},{4,{5,6,14,13}},
{4,{6,7,15,14}},{4,{7,0,8,15}}}};
//kerangka depanbelakang proyektor
object3D_t pro1={16,{
{-100,0,0},{-120,20,0},{-120,60,0},{-100,80,0},
{100,80,0},{120,60,0},{120,20,0},{100,0,0},
{-100,0,-200},{-120,20,-200},{-120,60,-200},{-100,80,-200},
{100,80,-200},{120,60,-200},{120,20,-200},{100,0,-200}
},
2,
{{8,{0,1,2,3,4,5,6,7}},{8,{8,9,10,11,12,13,14,15}
}}};
mat=tilting;
mat=translationMTX(0,+100,0)*rotationZMTX(3.14)*tilting;
mat=rotationYMTX(tick/57.3)*mat;
glLineWidth(4.0);
//cetak proyektor
color_t a={0.9,0.9,0.9};
for(i=0;i<pro.NumberofVertices;i++)
{
setColor(0,0,0);
vec[i]=Point2Vector(pro.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<pro.NumberofFaces;i++)
{
for(j=0;j<pro.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[pro.fc[i].pnt[j]];
for(j=0;j<pro.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[pro.fc[i].pnt[j]]);
fillPolygon(titik2D,pro.fc[i].NumberofVertices,a);
}
//cetak kerangka depanbelakang proyektor
for(i=0;i<pro1.NumberofVertices;i++)
{
setColor(0,0,0);
vec[i]=Point2Vector(pro.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<pro1.NumberofFaces;i++)
{
for(j=0;j<pro1.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[pro1.fc[i].pnt[j]];
for(j=0;j<pro1.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[pro1.fc[i].pnt[j]]);
drawPolygon(titik2D,pro1.fc[i].NumberofVertices);
}
glLineWidth(1.0);
//cetak kotak atas
color_t ktas={0.1,0.1,0.1};
for(i=0;i<kotak.NumberofVertices;i++)
{ setColor(1,1,1);
vec[i]=Point2Vector(kotak.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<kotak.NumberofFaces;i++)
{
for(j=0;j<kotak.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[kotak.fc[i].pnt[j]];
for(j=0;j<kotak.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[kotak.fc[i].pnt[j]]);
fillPolygon(titik2D,kotak.fc[i].NumberofVertices,ktas);
}
color_t tmbl={0,0.9,0};
//cetak tombol
for(i=0;i<tombol.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,1,0);
vec[i]=Point2Vector(tombol.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<tombol.NumberofFaces;i++)
{
for(j=0;j<tombol.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[tombol.fc[i].pnt[j]];
for(j=0;j<tombol.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[tombol.fc[i].pnt[j]]);
fillPolygon(titik2D,tombol.fc[i].NumberofVertices,tmbl);
}
//cetak tombol tengah
for(i=0;i<tombol1.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,1,0);
vec[i]=Point2Vector(tombol1.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<tombol1.NumberofFaces;i++)
{
for(j=0;j<tombol1.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[tombol1.fc[i].pnt[j]];
for(j=0;j<tombol1.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[tombol1.fc[i].pnt[j]]);
fillPolygon(titik2D,tombol1.fc[i].NumberofVertices,tmbl);
}
//cetak tombol tengah1
for(i=0;i<tombol2.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,1,0);
vec[i]=Point2Vector(tombol2.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<tombol2.NumberofFaces;i++)
{
for(j=0;j<tombol2.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[tombol2.fc[i].pnt[j]];
for(j=0;j<tombol2.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[tombol2.fc[i].pnt[j]]);
fillPolygon(titik2D,tombol2.fc[i].NumberofVertices,tmbl);
}
//cetak tombol kanane tengah1
for(i=0;i<tombol3.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,1,0);
vec[i]=Point2Vector(tombol3.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<tombol3.NumberofFaces;i++)
{
for(j=0;j<tombol3.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[tombol.fc[i].pnt[j]];
for(j=0;j<tombol3.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[tombol3.fc[i].pnt[j]]);
fillPolygon(titik2D,tombol3.fc[i].NumberofVertices,tmbl);
}
//cetak tombol tengah besar
for(i=0;i<tombol4.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,1,0);
vec[i]=Point2Vector(tombol4.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<tombol.NumberofFaces;i++)
{
for(j=0;j<tombol4.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[tombol4.fc[i].pnt[j]];
for(j=0;j<tombol4.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[tombol4.fc[i].pnt[j]]);
fillPolygon(titik2D,tombol4.fc[i].NumberofVertices,tmbl);
}
//cetak tombol tengah depane besar
for(i=0;i<tombol5.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,1,0);
vec[i]=Point2Vector(tombol5.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<tombol5.NumberofFaces;i++)
{
for(j=0;j<tombol5.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[tombol5.fc[i].pnt[j]];
for(j=0;j<tombol5.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[tombol5.fc[i].pnt[j]]);
fillPolygon(titik2D,tombol5.fc[i].NumberofVertices,tmbl);
}
//cetak cop copan kabel power
for(i=0;i<cop.NumberofVertices;i++)
{ glLineWidth(4.0);
setColor(0,0,0);
vec[i]=Point2Vector(cop.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<cop.NumberofFaces;i++)
{
for(j=0;j<cop.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[cop.fc[i].pnt[j]];
for(j=0;j<cop.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[cop.fc[i].pnt[j]]);
drawPolygon(titik2D,cop.fc[i].NumberofVertices);
}
//cetak cop copan kabel monitor
for(i=0;i<cop1.NumberofVertices;i++)
{ glLineWidth(3.0);
setColor(0,0,0);
vec[i]=Point2Vector(cop1.pnt[i]);
vec[i]=mat*vec[i];
}
for(i=0;i<cop1.NumberofFaces;i++)
{
for(j=0;j<cop1.fc[i].NumberofVertices;j++)
vecbuff[j]=vec[cop1.fc[i].pnt[j]];
for(j=0;j<cop1.fc[i].NumberofVertices;j++)
titik2D[j]=Vector2Point2D(vec[cop1.fc[i].pnt[j]]);
drawPolygon(titik2D,cop1.fc[i].NumberofVertices);
}
static point2D_t bola[360];
matrik2D_t mat1,mat2;
vektor2D_t vektor;
mat1=Translasi(-70,38);
point2D_t orbit1[360],orbit2[360];
//warna matahari
warna_t w1={1.,0.3,0.};
//warna bumi
warna_t w2={0.,1.,0.};
//warna bulan
warna_t w3={1.0,0.,0.};
for(k=0;k<360;k++)
{
bola[k].x=cos(k+5*M_PI/180.);
bola[k].y=sin(k+5*M_PI/180.);
}
for(k=0;k<360;k++)
{
orbit1[k]=konversi(bola[k],30);
orbit2[k]=konversi(bola[k],60);
}
for(int m=0;m<360;m++)
{
vektor=point2vektor(orbit2[m]);
vektor=mat1*vektor;
orbit2[m]=vektor2point(vektor);
vektor=point2vektor(orbit1[m]);
vektor=mat1*vektor;
orbit1[m]=vektor2point(vektor);
}
glColor3f(0.,1.,1.);
drawPolygon(orbit1,360);
// drawPolygon(orbit2,360);
tick++;
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowPosition(100,100);
glutInitWindowSize(680,480);
glutCreateWindow ("");
glClearColor(1, 0.0, 0.6,0);
gluOrtho2D(-320., 320., -240.0, 240.0);
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Komentar