Source Codenya (Update 2025-04-09)
RAW Source Code Download :
https://gist.githubusercontent.com/ifirdausku/5c73d7238eeb97e2fd2cef0e85f812df/raw/0f7ca8e74741efd4474d1fa0b00c78a444045823/kuliah-morphing-pesawat.cpp
Jika di atas gagal gunakan bawah ini
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <math.h> | |
#include "glut.h" | |
#define MAKS 55 | |
#ifndef M_PI | |
#define M_PI 3.141592653589793 | |
#endif | |
typedef struct { | |
float x; | |
float y; | |
} point2D_t; | |
typedef struct { | |
float r, g, b; | |
} color_t; | |
void display(void); | |
void userDraw(void); | |
void drawPolyLine(point2D_t[], int); | |
point2D_t interpolate(point2D_t, point2D_t, float); | |
int main(int argc, char **argv){ | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); | |
glutInitWindowPosition(100, 100); | |
glutInitWindowSize(640, 480); | |
glutCreateWindow("Project Morphing Obyek Teknik Informatika 4B Firdaus"); | |
glClearColor(0, 0, 0, 0); | |
gluOrtho2D(-320, 320, -240, 240); | |
glutIdleFunc(display); | |
glutDisplayFunc(display); | |
glutMainLoop(); | |
return 0; | |
} | |
void display(void){ | |
glClear(GL_COLOR_BUFFER_BIT); | |
userDraw(); | |
glutSwapBuffers(); | |
} | |
void userDraw(void){ | |
static int tick = 0; | |
int i, disp = (tick / 2110) % 3; | |
float rtick = (tick % 2110) / 100.; | |
color_t putih = { 1, 1, 1 }; | |
point2D_t model[MAKS]; | |
glColor3f(1, 1, 1); | |
point2D_t kura2[MAKS] = { | |
//kura2 | |
{-85,37},{-76,40},{-69,39},{-59,32},{-49,24},{-43,16},{-40,12},{-40,9},{-38,4},{-29,6}, | |
{-7,16},{5,17},{16,20},{32,19},{42,15},{51,12},{59,9},{70,0},{77,-7},{82,-12}, | |
{87,-14},{92,-19},{94,-21},{88,-23},{93,-31},{99,-36},{104,-42},{117,-43},{106,-43},{92,-42}, | |
{77,-45},{70,-44},{67,-48},{54,-52},{43,-51},{36,-46},{-18,-52},{-33,-47},{-39,-46},{-56,-51}, | |
{-78,-54},{-79,-48},{-68,-42},{-63,-36},{-58,-27},{-50,-24},{-49,-21},{-64,-9},{-73,-3},{-81,10}, | |
{-86,21},{-88,26},{-87,31},{-86,34},{-85,37}, | |
}; | |
point2D_t pesawat[MAKS] = { | |
//pesawat | |
{-154,-12,},{-131,0},{-119,8},{-119,9},{-60,9},{-45,9},{-11,59},{3,57},{-10,6},{78,5}, | |
{79,4},{97,14},{131,41},{148,43},{132,29},{123,7},{129,5},{129,1},{120,0},{137,9}, | |
{158,-17},{144,-17},{102,-6},{87,-11},{65,-16},{49,-21},{13,-23},{34,-35},{74,-51},{116,-64}, | |
{109,-72}, | |
{60,-65},{58,-67},{28,-71},{24,-67},{23,-63},{26,-58},{42,-55}, | |
{3,-43}, | |
{10,-47},{2,-52},{-8,-53},{-22,-54},{-29,-47},{-25,-44},{-4,-41}, | |
{-34,-27},{-63,-25},{-88,-27},{-106,-28},{-128,-26},{-143,-19},{-153,-16},{-154,-11},{-154,-12} | |
}; | |
//drawPolyLine(kura2, MAKS); //untuk menampilkan gambar tanpa tranformasi | |
switch (disp){ | |
case 0: | |
for (i = 0; i < MAKS; i++) | |
model[i] = interpolate(kura2[i], pesawat[i], rtick); | |
drawPolyLine(model, MAKS); break; | |
case 1: | |
for (i = 0; i < MAKS; i++) | |
model[i] = interpolate(pesawat[i], kura2[i], rtick); | |
drawPolyLine(model, MAKS); break; | |
default: | |
break; | |
} | |
tick++; | |
} | |
point2D_t interpolate(point2D_t a, point2D_t b, float r){ | |
if (r<0.0) | |
r = 0.0; | |
if (r>1.0) | |
r = 1.0; | |
point2D_t c; | |
float r1 = 1.0 - r; | |
c.x = r1 * a.x + r * b.x; | |
c.y = r1 * a.y + r * b.y; | |
return c; | |
} | |
void drawPolyLine(point2D_t point[], int n){ | |
int i; | |
glLineWidth(5); | |
glBegin(GL_LINE_STRIP); | |
for (i = 0; i < n; i++) | |
glVertex2f(point[i].x, point[i].y); | |
glEnd(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Jika di atas gagal gunakan bawah ini
Tidak ada komentar :
Posting Komentar