|
#include <stdlib.h> |
|
#include <stdafx.h> |
|
#include "glut.h" |
|
#include <math.h> |
|
#include <time.h> |
|
#include <stdio.h> |
|
#include <string.h> |
|
|
|
typedef struct |
|
{ float x; |
|
float y; |
|
}point2D_t; |
|
|
|
typedef struct |
|
{ |
|
float v[3]; |
|
}vector2D_t; |
|
|
|
typedef struct |
|
{ |
|
float m[3][3]; |
|
} matrix2D_t; |
|
|
|
typedef struct |
|
{ |
|
float r,g,b; |
|
} color_t; |
|
|
|
|
|
point2D_t Vector2Point1(vector2D_t vec) |
|
{ |
|
point2D_t pnt; |
|
pnt.x=vec.v[0]; |
|
pnt.y=vec.v[1]; |
|
return pnt; |
|
} |
|
|
|
vector2D_t Point2Vector1(point2D_t pnt) |
|
{ |
|
vector2D_t vec; |
|
vec.v[0]=pnt.x; |
|
vec.v[1]=pnt.y; |
|
vec.v[2]=1.; |
|
return vec; |
|
} |
|
|
|
|
|
void setColor(float r,float g,float b) |
|
{ |
|
glColor3f(r,g,b); |
|
} |
|
|
|
void setColor(color_t col) |
|
{ |
|
glColor3f(col.r,col.g,col.b); |
|
} |
|
|
|
//fungsi untuk membuat titik |
|
void drawDot(float x, float y) |
|
{ |
|
|
|
glPointSize(3); |
|
glBegin(GL_POINTS); |
|
glVertex2f(x,y); |
|
glEnd(); |
|
} |
|
|
|
//fungsi untuk membuat garis |
|
void drawLine(float x1, float y1, float x2, float y2) |
|
{ |
|
|
|
glBegin(GL_LINES); |
|
glVertex2f(x1,y1); |
|
glVertex2f(x2,y2); |
|
glEnd(); |
|
} |
|
|
|
//fungsi yang digunakan untuk menggambar garis |
|
void drawPolyline(point2D_t p[], int n) |
|
{ |
|
int i; |
|
glBegin(GL_LINE_STRIP); |
|
for(i=0;i<n;i++) |
|
glVertex2f(p[i].x,p[i].y); |
|
glEnd(); |
|
} |
|
|
|
//fungsi untuk menggambar garis dengan bentuk kurva tertutup |
|
void drawPolygon(point2D_t p[], int n) |
|
{ |
|
int i; |
|
glBegin(GL_LINE_LOOP); |
|
for(i=0;i<n;i++) |
|
glVertex2f(p[i].x,p[i].y); |
|
glEnd(); |
|
} |
|
|
|
//fungsi untuk mewarnai area poligon dengan satu warna |
|
void fillPolygon(point2D_t pnt[],int n, color_t color) |
|
{ |
|
int i; |
|
setColor(color); |
|
glBegin(GL_POLYGON); |
|
for (i=0;i<n;i++) { |
|
glVertex2f(pnt[i].x, pnt[i].y); |
|
} |
|
glEnd(); |
|
} |
|
|
|
//fungsi yang menghasilkan efek gradasi jika digunakan untuk mewarnai pada poligon |
|
void gradatePolygon(point2D_t p[], int n,color_t col[]) |
|
{ |
|
int i; |
|
glBegin(GL_POLYGON); |
|
for(i=0;i<n;i++) |
|
{ |
|
setColor(col[i]); |
|
glVertex2f(p[i].x,p[i].y); |
|
} |
|
glEnd(); |
|
} |
|
|
|
//fungsi yang menghasilkan efek gradasi jika digunakan untuk mewarnai pada lingkaran |
|
void centerPolygon(point2D_t p[], point2D_t pc,color_t col,color_t colp, int n) |
|
{ |
|
int i; |
|
glBegin(GL_LINES); |
|
for(i=0;i<n;i++) |
|
{ |
|
setColor(colp); |
|
glVertex2f(pc.x,pc.y); |
|
setColor(col); |
|
glVertex2f(p[i].x,p[i].y); |
|
} |
|
glEnd(); |
|
} |
|
|
|
|
|
// Definisi dan pengolahan matrik |
|
matrix2D_t createIdentity(void) { |
|
matrix2D_t u; |
|
int i,j; |
|
for (i=0;i<3;i++) { |
|
for(j=0;j<3;j++) u.m[i][j]=0.; |
|
u.m[i][i]=1.; |
|
} |
|
return u; |
|
} |
|
|
|
// untuk membuat efek translasi(bergerak) |
|
matrix2D_t translationMTX(float dx,float dy) |
|
{ |
|
matrix2D_t trans=createIdentity(); |
|
trans.m[0][2]=dx; |
|
trans.m[1][2]=dy; |
|
return trans; |
|
} |
|
|
|
// untuk membuat efek scaling(berubah ukuran) |
|
matrix2D_t scalingMTX(float mx,float my) |
|
{ |
|
matrix2D_t scale=createIdentity(); |
|
scale.m[0][0]=mx; |
|
scale.m[1][1]=my; |
|
return scale; |
|
} |
|
|
|
// untuk membuat efek rotasi(berputar) |
|
matrix2D_t rotationMTX(float theta) |
|
{ |
|
matrix2D_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; |
|
} |
|
|
|
matrix2D_t operator * (matrix2D_t a, matrix2D_t b) |
|
{ |
|
matrix2D_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; |
|
} |
|
|
|
vector2D_t operator * (matrix2D_t a, vector2D_t b) |
|
{ |
|
vector2D_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; |
|
} |
|
|
|
// fungsi untuk menampilkan tulisan |
|
void bitmap_output(int x, int y, char *string, void *font,color_t color) |
|
{ |
|
int len, i; |
|
setColor(color); |
|
glRasterPos2f(x, y); |
|
len = (int) strlen(string); |
|
for (i = 0; i < len; i++) { |
|
glutBitmapCharacter(font, string[i]); |
|
} |
|
} |
|
|
|
// fungsi yang digunakan untuk membuat ellipse |
|
static void createEllipse(point2D_t p[],point2D_t p0,float r1,float r2) |
|
{ |
|
for(int i=0;i<360;i++){ |
|
p[i].x=p0.x+r1*cos(i/57.3); |
|
p[i].y=p0.y+r2*sin(i/57.3); |
|
} |
|
} |
|
|
|
|
|
void userdraw() |
|
{ |
|
matrix2D_t mat; |
|
vector2D_t vec; |
|
static float tick,tick2,tick3,tick4,tick5,t1,t2,t3,t4,t5,temp=0; |
|
int i; |
|
|
|
color_t |
|
putih={1,1,1}, |
|
hitam={0,0,0}; |
|
|
|
////////////////////////////////////BANGUNAN PERTAMA(SAMPING PESAWAT)/////////////////////////////////// |
|
//inisialisasi warna |
|
|
|
color_t |
|
wvert[4]={{0.8,0.81,0.18},{0.8,0.81,0.18},{1.0,1.0,1.0},{1.0,1.0,1.0}}, |
|
whor[4]={{0.8,0.81,0.18},{0.8,0.81,0.18},{1.0,1.0,1.0},{1.0,1.0,1.0}}; |
|
|
|
|
|
glColor3f(1,1,1); |
|
//inisialisasi bangunan arah vertikal |
|
point2D_t |
|
puncak[4] = {{-48,120},{-48,65},{-52,65},{-52,120}}, |
|
kotak1 [4]= {{-45,65},{-45,45},{-55,45},{-55,65}}, |
|
kotak2 [4]= {{-40,45},{-40,30},{-68,30},{-68,45}}, |
|
kotak3 [4]= {{-45,30},{-45,-180},{-63,-180},{-63,30}}; |
|
|
|
|
|
|
|
//warna bangunan arah vertikal |
|
gradatePolygon(puncak,4,wvert); |
|
gradatePolygon(kotak1,4,wvert); |
|
gradatePolygon(kotak2,4,wvert); |
|
gradatePolygon(kotak3,4,wvert); |
|
|
|
//inisialisasi bangunan arah horisontal |
|
point2D_t |
|
hor1 [4]= {{34,25},{29,15},{-75,15},{-75,25}}, //NB= dibuat translasi ke samping |
|
hor2bay [4]= {{-5,-22},{-5,-33},{-120,-33},{-120,-22}};//NB: translasi ke samping sebelum hor3 |
|
|
|
mat=translationMTX(0-tick5,0); |
|
for(i=0; i<4; i++) |
|
{ |
|
vec=Point2Vector1(hor1[i]); |
|
vec=mat*vec; |
|
hor1[i]=Vector2Point1(vec); |
|
} |
|
mat=translationMTX(0+t2,0); |
|
for(i=0; i<4; i++) |
|
{ |
|
vec=Point2Vector1(hor2bay[i]); |
|
vec=mat*vec; |
|
hor2bay[i]=Vector2Point1(vec); |
|
} |
|
|
|
mat=translationMTX(0-t4,0); // gerakan kembali ke posisi awal |
|
for(i=0; i<4; i++) |
|
{ |
|
vec=Point2Vector1(hor2bay[i]); |
|
vec=mat*vec; |
|
hor2bay[i]=Vector2Point1(vec); |
|
} |
|
|
|
|
|
//warna bangunan arah horisontal |
|
|
|
gradatePolygon(hor1,4,whor); |
|
gradatePolygon(hor2bay,4,whor); |
|
|
|
|
|
//////////////////////////////////////////// PESAWAT //////////////////////////////////////////////////// |
|
|
|
color_t |
|
wroket={0.93,0.51,0.09}, |
|
wroket2={0.93,0.71,0.09}, |
|
wroketbaw [4]={{0.93,0.51,0.09},{1,1,1},{1,1,1},{0.93,0.51,0.09}}, |
|
wpes={0.901,0.911,0.915},wpesbaw={0,0,0}; |
|
|
|
point2D_t |
|
//big roket |
|
roket[360],proket={52,0}, |
|
p1 [4] = {{36,-107},{36,20},{68,20},{68,-107}}, |
|
p2 [4] = {{36,-107},{32,-127},{72,-127},{68,-107}}, |
|
|
|
// pesawat |
|
pUtama [4] ={{15,-30},{15,-102},{36,-102},{36,-30}}, |
|
pes[360],ppes={30,-42}, |
|
|
|
|
|
//garis penghias |
|
p1hias1 [4] = {{36,5},{36,-12},{68,-12},{68,5}}; |
|
|
|
|
|
//implementasi |
|
createEllipse(pes,ppes,15,35); |
|
mat=translationMTX(0,0+tick4); |
|
for(i=0; i<360; i++) |
|
{ |
|
vec=Point2Vector1(pes[i]); |
|
vec=mat*vec; |
|
pes[i]=Vector2Point1(vec); |
|
} |
|
|
|
createEllipse(roket,proket,16,50); |
|
mat=translationMTX(0,0+tick4); |
|
for(i=0; i<360; i++) |
|
{ |
|
vec=Point2Vector1(roket[i]); |
|
vec=mat*vec; |
|
roket[i]=Vector2Point1(vec); |
|
} |
|
|
|
|
|
|
|
mat=translationMTX(0,0+tick4); |
|
for(i=0; i<4; i++) |
|
{ |
|
vec=Point2Vector1(p1[i]); |
|
vec=mat*vec; |
|
p1[i]=Vector2Point1(vec); |
|
} |
|
|
|
mat=translationMTX(0,0+tick4); |
|
for(i=0; i<4; i++) |
|
{ |
|
vec=Point2Vector1(p2[i]); |
|
vec=mat*vec; |
|
p2[i]=Vector2Point1(vec); |
|
} |
|
|
|
mat=translationMTX(0,0+tick4); |
|
for(i=0; i<4; i++) |
|
{ |
|
vec=Point2Vector1(pUtama[i]); |
|
vec=mat*vec; |
|
pUtama[i]=Vector2Point1(vec); |
|
} |
|
|
|
mat=translationMTX(0,0+tick4); |
|
for(i=0; i<4; i++) |
|
{ |
|
vec=Point2Vector1(p1hias1[i]); |
|
vec=mat*vec; |
|
p1hias1[i]=Vector2Point1(vec); |
|
} |
|
|
|
|
|
|
|
|
|
// pewarnaan |
|
fillPolygon(pes,360,wpes); |
|
fillPolygon(roket,360,wroket); |
|
fillPolygon(p1,4,wroket); |
|
gradatePolygon(p2,4,wroketbaw); |
|
fillPolygon(p1hias1,4,wroket2); |
|
fillPolygon(pUtama,4,wpes); |
|
|
|
|
|
/////////////////////////////// BANGUNAN KEDUA (BAWAH PESAWAT) ////////////////////////////////////////////// |
|
//inisialisasi |
|
point2D_t |
|
|
|
kotakbag2a [4] = {{20,-125},{20,-180},{-5,-180},{-5,-125}}, |
|
kotakbag2b [4] = {{80,-170},{80,-130},{22,-130},{22,-170}}, |
|
kotakbag2c [4] = {{102,-180},{102,-125},{82,-125},{82,-180}}; |
|
|
|
|
|
|
|
// implementasi |
|
gradatePolygon(kotakbag2a,4,whor); |
|
gradatePolygon(kotakbag2b,4,whor); |
|
gradatePolygon(kotakbag2c,4,whor); |
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
|
bitmap_output(-390,240, "TEKNIK INFORMATIKA[4B] = {{M. FIRDAUS HIDAYAT},{RAHMAD HENDI},{RENDI JULI},{M. BAYU MEGANTARA}};",GLUT_BITMAP_TIMES_ROMAN_10,putih); |
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
|
if(tick5==40) |
|
{ |
|
tick4+=0.5; //pesawat |
|
} |
|
|
|
if(t4==36) |
|
{ |
|
tick5+=0.3; //pangkalan atas |
|
if(tick5>40) tick5=40; |
|
} |
|
|
|
t1+=0.5; //lift |
|
if(t1>170) t1=170; |
|
|
|
if(t1==170) |
|
{ |
|
t2+=0.09; //pangkalan bawah |
|
if(t2>36) t2=36; |
|
} |
|
if(t2==36) |
|
{ |
|
t3+=0.09; //kotak ke pesawat |
|
if(t3>104) t3=104; |
|
} |
|
|
|
if(t3==104) |
|
{ |
|
t4+=0.09; // kotak balik |
|
if(t4>36) t4=36; |
|
} |
|
|
|
|
|
} |
|
|
|
|
|
void display(void) |
|
{ |
|
glClear(GL_COLOR_BUFFER_BIT); |
|
userdraw(); |
|
glutSwapBuffers(); |
|
} |
|
|
|
int main (int argc, char ** argv) |
|
{ |
|
// insert code here... |
|
glutInit(&argc,argv); //inisialisasi toolkit |
|
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); |
|
glutInitWindowPosition(100,50); |
|
glutInitWindowSize(800,600); // besar ukuran windows |
|
glutCreateWindow("Fakultas Teknik : Informatika 4B"); |
|
glClearColor(0.6,0.8,1.0,0); // warna background biru muda/tua |
|
gluOrtho2D(-400,400,-250,250); // ukuran layar yang dipake |
|
glutIdleFunc(display); |
|
glutDisplayFunc(display); |
|
glutMainLoop(); |
|
return 0; |
|
} |