Anda di halaman 1dari 37

# 4.

1 TUGAS
Deskripsi Percobaan
Tugas pertama membuat animasi perubahan bentuk dari persegi kedalam bentuk lain.
Analisa/Coding
/* Translasi
glTranslatef(dX, dY, dZ);
-> d = perubahan koordinat
Scaling
glScalef(mx, my, mz) -> m = skala terhadap sumbu Rotasi
glRotatef(angle, X, Y, Z);
*/
#include <GL/glut.h>
void tombol(unsigned char key, int x, int y) {
switch (key) {
case 'a':
case 'A':
//dx = -1 dX, dY, dZ
glTranslatef(-1, 3, 0);//kiri
break;
case 'm':
case 'M':
//skala mx, my, mz
glScalef(1, 1.2, 1);//besarkan y
break;
case 'r':
case 'R':
// sudut X, Y, X
glRotatef(-20, 3, 0, 23);//(-)searah arah jarum jam
break;
case 27 :

1
exit(0);
break;}
glutPostRedisplay();
} void tampil() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3ub(0, 255, 255);
glRectf(-20,-20, 20, 20);
glutSwapBuffers(); }

main() {
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(50,0);
glutInitWindowSize(500,500);
glutCreateWindow("Transformasi Persegi");
gluOrtho2D(-100.,100.,-100.,100.);
glClearColor(1.0, 1.0, 1.0, 1.0);
glutDisplayFunc(tampil);
glutKeyboardFunc(tombol);
glutMainLoop();
}
Output

2
Gambar : 4.4 tugas perubahan bentuk dengan transformasi

4.2 TUGAS
Deskripsi Percobaan
Tugas kedua membuat animasi mobil dengan perputaran roda.

Analisa/Coding
#include <stdio.h>

#include <math.h>

#include <GL/glut.h>

typedef struct {

float x,y;

} c2k;

typedef struct {

float m[3][3];

} matrix2D_t;

typedef struct {

3
float v[3];

} vector2D_t;

typedef struct {

float x;

float y;

} point2D_t;

typedef struct {

float r;

float g;

float b;

} color_t;

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;

## void drawLingkaran(c2k p[],int n){

glColor3f(0,0,0);

glBegin(GL_POLYGON);

for(int i=0;i<n;i++)

glVertex2f(p[i].x,p[i].y);

4
glEnd();

c2k ban[360];

c2k ban3[360];

## matrix2D_t translationMTX(float dx,float dy){

matrix2D_t trans=createIdentity();

trans.m[2][2]=dx;

trans.m[1][2]=dy;

return trans;}

point2D_t pnt;

pnt.x=vec.v[1];

pnt.y=vec.v[2];

return pnt;

vector2D_t vec;

vec.v[0]=pnt.x;

vec.v[1]=pnt.y;

vec.v[2]=1.;

return vec;}

## glColor3f(red, green, blue);}

5
void setColor(color_t col){

## void drawPolygon(point2D_t pnt[],int n){

int i;

glBegin(GL_LINE_LOOP);

for (i=0;i<n;i++) {

glVertex2f(pnt[i].x, pnt[i].y);

glEnd();

void userdraw(void);

void display(void)

glClear( GL_COLOR_BUFFER_BIT);

userdraw();

glutSwapBuffers();

void userdraw(void)

## static double tick=0;

// kepala

point2D_t ab[8]={

{-400+tick,160},{-400+tick,240},{-330+tick,300},{-240+tick,300},{-240+tick,240},

{-240+tick,160},{-360+tick,160},{-340+tick,160},

};

6

point2D_t ac[4]={

{-240+tick,160},{-240+tick,200},{0+tick,200},{0+tick,160},

};

// box

{-220+tick,200},{-220+tick,340},{0+tick,340},{0+tick,200},

};

for(int i=0;i<360;i++)

for(int a=0;a<360;a++)

vector2D_t vec[50];

matrix2D_t trans,rot,scale,refxaxis,refyaxis,reforg;

point2D_t buff[50];

7
int loop;

setColor(1,0,0);

drawPolygon(ab,8);

drawPolygon(ac,4);

drawLingkaran(ban,360);

drawLingkaran(ban3,360);

if(tick<0)

tick=800;

else

tick=tick-0.1;

## int main(int argc, char **argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);

glutInitWindowPosition(100,100);

glutInitWindowSize(640,480);

glutCreateWindow("Pa10 gaktuh");

glClearColor(0.25,0.4,0.21,0.0);

gluOrtho2D(0.,640.,0.,480.);

glutIdleFunc(display);

glutDisplayFunc(display);

glutMainLoop();

return 0;

8
}

Output

## Gambar : 4.5 tugas animasi pergerakan mobil dengan bannya

4.3 TUGAS
Deskripsi Percobaan
Tugas nomor 3 membuat animasi pemandangan dengan objek yang berjalan.

Analisa/Coding
#include <windows.h>

#include <gl/glut.h>

#include <math.h>

int i;

## void lingkaran(int jari2, int jumlah_titik, int x_tengah, int y_tengah) {

glBegin(GL_POLYGON);

for (i=0;i<=360;i++){

float sudut=i*(2*PI/jumlah_titik);

9
float x=x_tengah+jari2*cos(sudut);

float y=y_tengah+jari2*sin(sudut);

glVertex2f(x,y);

glEnd();

int r=1;

int ujung=1;

r--;

if(r==-1200){

ujung=0;}

else

## if (r<600 && ujung==0){

r++;

if (r==600){

ujung = 1;}

glutPostRedisplay();

glutTimerFunc(3,timer,3);

10
void latar(){

## glClearColor(0.0, 0.0, 0.3, 0.0);

glClear(GL_COLOR_BUFFER_BIT);

//#REL#

//tanah

glColor3f(0.5, 0, 0);

glVertex2i(0, -450);

glVertex2i(800, -450);

glColor3f(0.5, 0, 0.1);

glVertex2i(800, -500);

glVertex2i(0, -500);

glEnd();

//dasar rel

## glColor3f(0.7, 0.6, 0.7);

glVertex2i(0, -450);

glVertex2i(0, -425);

glVertex2i(800, -425);

glVertex2i(800, -450);

glEnd();

//rel

## glColor3f(0.3, 0.3, 0.3);

11
glVertex2i(0, -430);

glVertex2i(0, -435);

glVertex2i(800, -435);

glVertex2i(800, -430);

glEnd();

## glColor3f(0.3, 0.3, 0.3);

glVertex2i(0, -440);

glVertex2i(0, -445);

glVertex2i(800, -445);

glVertex2i(800, -440);

glEnd();

//pembatas rel

## glColor3f(0.2, 0.2, 0.2);

glVertex2i(0, -420);

glVertex2i(0, -425);

## glColor3f(0.3, 0.3, 0.1);

glVertex2i(800, -425);

glVertex2i(800, -420);

glEnd();

//#STASIUN#

//atap

## glColor3f(0.75, 0.1, 0.0);

12
glVertex2i(450, -175);

glVertex2i(800, -175);

## glColor3f(1.0, 0.5, 0.0);

glVertex2i(800, -225);

glVertex2i(400, -225);

glEnd();

//tembok

## glColor3f(0.3, 0.4, 0.2);

glVertex2i(425, -225);

glVertex2i(800, -225);

glVertex2i(800, -360);

glVertex2i(425, -360);

glEnd();

//lampu stasiun

glBegin(GL_LINES);

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(480, -226);

glVertex2i(480, -250);

glEnd();

glBegin(GL_TRIANGLES);

## glColor3f(0.2, 0.2, 0.2);

glVertex2i(480, -249);

glVertex2i(490, -275);

glVertex2i(470, -275);

13
glEnd();

## glColor3f(0.8, 0.8, 0.0);

glVertex2i(470, -275);

glVertex2i(490, -275);

## glColor3f(0.5, 0.5, 0.2);

glVertex2i(500, -290);

glVertex2i(460, -290);

glEnd();

//lantai

## glColor3f(0.8, 0.8, 0.8);

glVertex2i(325, -420);

glVertex2i(395, -360);

glVertex2i(800, -360);

glVertex2i(800, -420);

glEnd();

glBegin(GL_LINES);

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(325, -420);

glVertex2i(395, -360);

glVertex2i(395, -360);

glVertex2i(800, -360);

glEnd();

14
glColor3f(0.2, 0.2, 0.2);

glVertex2i(340, -420);

glVertex2i(800, -420);

## glColor3f(0.8, 0.8, 0.8);

glVertex2i(800, -372);

glVertex2i(400, -372);

glEnd();

glBegin(GL_LINES);

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(340, -420);

glVertex2i(400, -372);

glVertex2i(400, -372);

glVertex2i(800, -372);

glEnd();

//tiang

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(534, -385);

glVertex2i(534, -225);

glVertex2i(540, -225);

glVertex2i(540, -385);

glEnd();

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(529, -400);

15
glVertex2i(529, -385);

glVertex2i(545, -385);

glVertex2i(545, -400);

glEnd();

glBegin(GL_TRIANGLES);

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(537, -250);

glVertex2i(530, -225);

glVertex2i(545, -225);

glEnd();

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(684, -385);

glVertex2i(684, -225);

glVertex2i(690, -225);

glVertex2i(690, -385);

glEnd();

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(679, -400);

glVertex2i(679, -385);

glVertex2i(695, -385);

glVertex2i(695, -400);

glEnd();

glBegin(GL_TRIANGLES);

16
glColor3f(0.0, 0.0, 0.0);

glVertex2i(687, -250);

glVertex2i(680, -225);

glVertex2i(695, -225);

glEnd();

//pintu stasiun

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(577, -360);

glVertex2i(577, -275);

glVertex2i(647, -275);

glVertex2i(647, -360);

glEnd();

## glColor3f(0.8, 0.8, 0.0);

glVertex2i(580, -360);

glVertex2i(580, -275);

glVertex2i(644, -275);

glVertex2i(644, -360);

glEnd();

//jendela stasiun

17

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(712, -257);

glVertex2i(712, -313);

glVertex2i(783, -313);

glVertex2i(783, -257);

glEnd();

## glColor3f(0.8, 0.8, 0.0);

glVertex2i(725, -260);

glVertex2i(725, -310);

glVertex2i(770, -310);

glVertex2i(770, -260);

glEnd();

glBegin(GL_LINES);

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(748, -311);

glVertex2i(748, -259);

glVertex2i(725, -285);

glVertex2i(771, -285);

glEnd();

//#LANGIT#

//bulan

glColor3f(1,1,0.4);

18
lingkaran(25, 100, 75, -75);

void kereta(){

//roda

## lingkaran(6, 50, 916, -436);

19
lingkaran(6, 50, 1031, -436);

## lingkaran(6, 50, 1016, -436);

//gerbong 1

glBegin(GL_POLYGON);

glColor3f (0,0,0.5);

glVertex2i(280, -400);

glVertex2i(305, -345);

glVertex2i(315, -340);

glVertex2i(520, -340);

glVertex2i(530, -350);

glVertex2i(530, -400);

glEnd();

glBegin(GL_POLYGON);

glColor3f (0.85,0.85,0.85);

glVertex2i(310, -430);

glVertex2i(310, -440);

glVertex2i(295, -440);

glVertex2i(280, -400);

glVertex2i(530, -400);

glVertex2i(530, -430);

glEnd();

//gerbong 2

glBegin(GL_POLYGON);

glColor3f (0,0,0.5);

glVertex2i(533, -400);

20
glVertex2i(533, -350);

glVertex2i(543, -340);

glVertex2i(773, -340);

glVertex2i(783, -350);

glVertex2i(783, -400);

glEnd();

glBegin(GL_POLYGON);

glColor3f (0.85,0.85,0.85);

glVertex2i(533, -430);

glVertex2i(533, -400);

glVertex2i(783, -400);

glVertex2i(783, -430);

glEnd();

//gerbong 3

glBegin(GL_POLYGON);

glColor3f (0,0,0.5);

glVertex2i(786, -400);

glVertex2i(786, -350);

glVertex2i(796, -340);

glVertex2i(1046, -340);

glVertex2i(1076, -370);

glVertex2i(1076, -400);

glEnd();

glBegin(GL_POLYGON);

glColor3f (0.85,0.85,0.85);

21
glVertex2i(786, -430);

glVertex2i(786, -400);

glVertex2i(1076, -400);

glVertex2i(1056, -430);

glEnd();

//sambungan gerbong

glColor3f (0.2,0.2,0.2);

glVertex2i(530, -425);

glVertex2i(530, -350);

glVertex2i(533, -350);

glVertex2i(533, -425);

glEnd();

glColor3f (0.2,0.2,0.2);

glVertex2i(783, -425);

glVertex2i(783, -350);

glVertex2i(786, -350);

glVertex2i(786, -425);

glEnd();

//as roda

glColor3f (0.5,0.5,0.5);

glVertex2i(312, -432);

glVertex2i(312, -435);

22
glVertex2i(530, -435);

glVertex2i(530, -432);

glEnd();

glColor3f (0.5,0.5,0.5);

glVertex2i(533, -432);

glVertex2i(533, -435);

glVertex2i(783, -435);

glVertex2i(783, -432);

glEnd();

glColor3f (0.5,0.5,0.5);

glVertex2i(786, -432);

glVertex2i(786, -435);

glVertex2i(1056, -435);

glVertex2i(1056, -432);

glEnd();

//garis warna

//biru

glColor3f (0.0,0.0,0.3);

glVertex2i(365, -410);

glVertex2i(370, -405);

glVertex2i(530, -405);

glVertex2i(530, -410);

23
glEnd();

glColor3f (0.0,0.0,0.3);

glVertex2i(533, -410);

glVertex2i(533, -405);

glVertex2i(783, -405);

glVertex2i(783, -410);

glEnd();

glColor3f (0.0,0.0,0.3);

glVertex2i(786, -410);

glVertex2i(786, -405);

glVertex2i(975, -405);

glVertex2i(980, -410);

glEnd();

//merah

glColor3f (0.7,0.0,0.0);

glVertex2i(302, -423);

glVertex2i(300, -418);

glVertex2i(530, -418);

glVertex2i(530, -423);

glEnd();

glColor3f(0.7,0.0,0.0);

24
glVertex2i(533, -423);

glVertex2i(533, -418);

glVertex2i(783, -418);

glVertex2i(783, -423);

glEnd();

glColor3f(0.7,0.0,0.0);

glVertex2i(786, -423);

glVertex2i(786, -418);

glVertex2i(1050, -418);

glVertex2i(1045, -423);

glEnd();

//jendela kereta

glBegin(GL_TRIANGLES);

glColor3f (0.7,0.7,0.4);

glVertex2i(297, -390);

glVertex2i(315, -355);

glVertex2i(315, -390);

glEnd();

glColor3f (0.7,0.7,0.4);

glVertex2i(370, -390);

glVertex2i(370, -355);

glVertex2i(395, -355);

glVertex2i(395, -390);

25
glEnd();

glColor3f (0.7,0.7,0.4);

glVertex2i(410, -390);

glVertex2i(410, -355);

glVertex2i(435, -355);

glVertex2i(435, -390);

glEnd();

glColor3f (0.7,0.7,0.4);

glVertex2i(450, -390);

glVertex2i(450, -355);

glVertex2i(475, -355);

glVertex2i(475, -390);

glEnd();

glColor3f (0.7,0.7,0.4);

glVertex2i(490, -390);

glVertex2i(490, -355);

glVertex2i(515, -355);

glVertex2i(515, -390);

glEnd();

glColor3f (0.7,0.7,0.4);

glVertex2i(595, -390);

26
glVertex2i(595, -355);

glVertex2i(620, -355);

glVertex2i(620, -390);

glEnd();

glColor3f (0.7,0.7,0.4);

glVertex2i(640, -390);

glVertex2i(640, -355);

glVertex2i(665, -355);

glVertex2i(665, -390);

glEnd();

glColor3f (0.7,0.7,0.4);

glVertex2i(685, -390);

glVertex2i(685, -355);

glVertex2i(710, -355);

glVertex2i(710, -390);

glEnd();

glColor3f (0.7,0.7,0.4);

glVertex2i(800, -390);

glVertex2i(800, -355);

glVertex2i(825, -355);

glVertex2i(825, -390);

27
glEnd();

glColor3f (0.7,0.7,0.4);

glVertex2i(850, -390);

glVertex2i(850, -355);

glVertex2i(875, -355);

glVertex2i(875, -390);

glEnd();

glColor3f (0.7,0.7,0.4);

glVertex2i(900, -390);

glVertex2i(900, -355);

glVertex2i(925, -355);

glVertex2i(925, -390);

glEnd();

glColor3f (0.7,0.7,0.4);

glVertex2i(950, -390);

glVertex2i(950, -355);

glVertex2i(975, -355);

glVertex2i(975, -390);

glEnd();

glBegin(GL_POLYGON);

glColor3f (0.7,0.7,0.4);

glVertex2i(1035, -390);

28
glVertex2i(1035, -355);

glVertex2i(1047, -355);

glVertex2i(1060, -368);

glVertex2i(1060, -390);

glEnd();

//pintu kereta

glBegin(GL_LINES);

glColor3f (0.5,0.5,0.5);

glVertex2i(325, -350);

glVertex2i(325, -415);

glVertex2i(325, -415);

glVertex2i(355, -415);

glVertex2i(355, -415);

glVertex2i(355, -350);

glVertex2i(355, -350);

glVertex2i(325, -350);

glEnd();

glColor3f (0.5,0.5,0.5);

glVertex2i(330, -355);

glVertex2i(330, -395);

glVertex2i(350, -395);

glVertex2i(350, -355);

glEnd();

glBegin(GL_LINES);

29
glColor3f (0.5,0.5,0.5);

glVertex2i(545, -350);

glVertex2i(545, -415);

glVertex2i(545, -415);

glVertex2i(575, -415);

glVertex2i(575, -415);

glVertex2i(575, -350);

glVertex2i(575, -350);

glVertex2i(545, -350);

glEnd();

glColor3f (0.5,0.5,0.5);

glVertex2i(550, -355);

glVertex2i(550, -395);

glVertex2i(570, -395);

glVertex2i(570, -355);

glEnd();

glBegin(GL_LINES);

glColor3f (0.5,0.5,0.5);

glVertex2i(740, -350);

glVertex2i(740, -415);

glVertex2i(740, -415);

glVertex2i(770, -415);

glVertex2i(770, -415);

glVertex2i(770, -350);

30
glVertex2i(770, -350);

glVertex2i(740, -350);

glEnd();

glColor3f (0.5,0.5,0.5);

glVertex2i(745, -355);

glVertex2i(745, -395);

glVertex2i(765, -395);

glVertex2i(765, -355);

glEnd();

glBegin(GL_LINES);

glColor3f (0.5,0.5,0.5);

glVertex2i(990, -350);

glVertex2i(990, -415);

glVertex2i(990, -415);

glVertex2i(1020, -415);

glVertex2i(1020, -415);

glVertex2i(1020, -350);

glVertex2i(1020, -350);

glVertex2i(990, -350);

glEnd();

glColor3f (0.5,0.5,0.5);

glVertex2i(995, -355);

glVertex2i(995, -395);

31
glVertex2i(1015, -395);

glVertex2i(1015, -355);

glEnd();

void stopan(){

//dasar

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(64, -471);

glVertex2i(64, -465);

glVertex2i(112, -465);

glVertex2i(112, -471);

glEnd();

//pondasi

glBegin(GL_POLYGON);

## glColor3f(0.2, 0.2, 0.2);

glVertex2i(92, -465);

glVertex2i(92, -455);

glVertex2i(93, -455);

glVertex2i(93, -440);

glVertex2i(94, -440);

glVertex2i(94, -430);

glVertex2i(106, -430);

glVertex2i(106, -440);

32
glVertex2i(107, -440);

glVertex2i(107, -455);

glVertex2i(108, -455);

glVertex2i(108, -465);

glEnd();

//tiang

## glColor3f(1.0, 1.0, 1.0);

glVertex2i(98, -431);

glVertex2i(98, -251);

glVertex2i(102, -251);

glVertex2i(102, -431);

glEnd();

glBegin(GL_LINES);

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(98, -431);

glVertex2i(98, -251);

glVertex2i(102, -251);

glVertex2i(102, -431);

glEnd();

//kabel

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(72, -465);

glVertex2i(70, -465);

33
glVertex2i(90, -251);

glVertex2i(92, -251);

glEnd();

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(102, -251);

glVertex2i(102, -247);

glVertex2i(90, -247);

glVertex2i(90, -251);

glEnd();

//lampu

glBegin(GL_POLYGON);

## glColor3f(0.0, 0.0, 0.0);

glVertex2i(95, -251);

glVertex2i(95, -200);

glVertex2i(130, -200);

glVertex2i(120, -210);

glVertex2i(120, -225);

glVertex2i(130, -225);

glVertex2i(120, -240);

glVertex2i(120, -251);

glEnd();

glBegin(GL_POLYGON);

## glColor3f(0.5, 0.5, 0.0);

glVertex2i(123, -218);

34
glVertex2i(123, -213);

## glColor3f(0.1, 0.1, 0.1);

glVertex2i(175, -195);

glVertex2i(175, -240);

glEnd();

glBegin(GL_POLYGON);

## glColor3f(0.5, 0.5, 0.0);

glVertex2i(123, -248);

glVertex2i(123, -243);

## glColor3f(0.1, 0.1, 0.1);

glVertex2i(175, -225);

glVertex2i(175, -265);

glEnd();

void display(){

glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();

latar();

glTranslatef(r,0,0);

kereta();

glPopMatrix();

stopan();

glFlush();

35
int main(int argc, char** argv){

glutInit(&argc, argv);

glutInitWindowPosition(150,150);

glutInitWindowSize(800,500);

glutCreateWindow("KERETA MALAM");

gluOrtho2D(0,800,-500,0);

glutDisplayFunc(display);

timer(0);

glutMainLoop();

36
Output

## Gambar : 4.6 pemandangan dengan pergerakan objek.

37

### Dapatkan aplikasi gratis kami

Hak cipta © 2021 Scribd Inc.