You are on page 1of 6

Hasna Ayu Roida / 2103181009 / 3 D3 IT A

 Source Code
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>
#include <math.h>

//deklarasi lingkaran
const double PI = 3.142857143;
int i, radius, jumlah_titik, x_tengah, y_tengah;

//deklarasi gerakan otomatis


int a = 0;
int b = 0;

bool kiri = true;


bool kanan = true;

void animasi()
{
glClear(GL_COLOR_BUFFER_BIT);
//latar
glBegin(GL_QUADS); //karena menggunakan latar terdiri 4 titik
glColor3ub(0,191,255);
glVertex2d(-20, -20);
glVertex2d(20, -20);
glColor3b(255,255,255);
glVertex2d(20, 20);
glVertex2d(-20, 20);
glEnd();
glFlush(); //tanda objek yang berbeda

//jalan
glBegin(GL_POLYGON);
glColor3f(0.3,0.3,0.3);
glVertex2d(20, -22);
glVertex2d(-20, -22);
glVertex2d(-20, -13);
glVertex2d(20, -13);
glEnd();
glFlush();

//garis jalan
glLineWidth(2);
glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex2d(-18, -16);
glVertex2d(-15, -16);
glVertex2d(-12, -16);
glVertex2d(-9, -16);
glVertex2d(-6, -16);
glVertex2d(-3, -16);
glVertex2d(0, -16);
glVertex2d(3, -16);
glVertex2d(6, -16);
glVertex2d(9, -16);
glVertex2d(12, -16);
glVertex2d(15, -16);
glEnd();
glFlush();

//matahari
glColor3ub(255,255,0);
glBegin(GL_POLYGON);

radius = 40;
jumlah_titik = 60;
x_tengah = 2.5;
y_tengah = 140;

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


{
float sudut=i*(2*PI/jumlah_titik);
float x=x_tengah+radius*cos(sudut);
float y=y_tengah+radius*sin(sudut);
glVertex2d(x/10, y/10);
}
glEnd();
glFlush();

//gunung 1
glBegin(GL_TRIANGLES);
glColor3ub(50,205,50);
glVertex2d(-10, 7);
glVertex2d(0, -4);
glVertex2d(-20, -4);
glEnd();
glFlush();

//gunung 2
glBegin(GL_TRIANGLES);
glColor3ub(50,205,50);
glVertex2d(10, 7);
glVertex2d(0, -4);
glVertex2d(20, -4);
glEnd();
glFlush();

//perintah gerak awan


glPushMatrix();
glTranslatef(b, 0, 0);

//awan 1
glColor3f(1,1,1);
glBegin(GL_POLYGON);

radius = 18;
jumlah_titik = 60;
x_tengah = -150;
y_tengah = 90;

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


{
float sudut=i*(2*PI/jumlah_titik);
float x=x_tengah+radius*cos(sudut);
float y=y_tengah+radius*sin(sudut);
glVertex2d(x/10, y/10);
}

glEnd();

glColor3f(1,1,1);
glBegin(GL_POLYGON);

radius = 18;
jumlah_titik = 60;
x_tengah = -100;
y_tengah = 95;

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


{
float sudut=i*(2*PI/jumlah_titik);
float x=x_tengah+radius*cos(sudut);
float y=y_tengah+radius*sin(sudut);
glVertex2d(x/10, y/10);
}

glEnd();
glColor3f(1,1,1);
glBegin(GL_POLYGON);

radius = 30;
jumlah_titik = 60;
x_tengah = -125;
y_tengah = 100;

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


{
float sudut=i*(2*PI/jumlah_titik);
float x=x_tengah+radius*cos(sudut);
float y=y_tengah+radius*sin(sudut);
glVertex2d(x/10, y/10);
}
glEnd();
glFlush();

//awan 2
glColor3f(1,1,1);
glBegin(GL_POLYGON);

radius = 18;
jumlah_titik = 60;
x_tengah = 30;
y_tengah = 55;

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


{
float sudut=i*(2*PI/jumlah_titik);
float x=x_tengah+radius*cos(sudut);
float y=y_tengah+radius*sin(sudut);
glVertex2d(x/10, y/10);
}

glEnd();

glColor3f(1,1,1);
glBegin(GL_POLYGON);

radius = 18;
jumlah_titik = 60;
x_tengah = -30;
y_tengah = 55;

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


{
float sudut=i*(2*PI/jumlah_titik);
float x=x_tengah+radius*cos(sudut);
float y=y_tengah+radius*sin(sudut);
glVertex2d(x/10, y/10);
}

glEnd();

glColor3f(1,1,1);
glBegin(GL_POLYGON);

radius = 30;
jumlah_titik = 60;
x_tengah = 0;
y_tengah = 60;

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


{
float sudut=i*(2*PI/jumlah_titik);
float x=x_tengah+radius*cos(sudut);
float y=y_tengah+radius*sin(sudut);
glVertex2d(x/10, y/10);
}
glEnd();
glFlush();
glPopMatrix();
}
//waktu lintas objek
void timer(int t)
{
if (kiri)
{
a -= 1;
}
else
{
a += 1;
}
if (a < -40)
{
kiri = false;
}
else if(a > 20)
{
kiri = true;
}
if (kanan)
{
b += 1;
}
else
{
b -= 1;
}
if (b > 40)
{
kanan = false;
}
else if(b < -20)
{
kanan = true;
}
glutPostRedisplay();
//semakin tinggi timer, semakin lambat transformasi
glutTimerFunc(50,timer,0);
}

int main(int argc, char **argv)


{
glutCreateWindow ("Animation");
glClearColor(0, 0, 0, 0.0);
gluOrtho2D(-20, 20, -20, 20);
glutTimerFunc(1,timer,0);
glutDisplayFunc(animasi);
glutMainLoop();
}

 Capture Hasil Percobaan

You might also like