You are on page 1of 14

LAPORAN RESMI

GRAFIKA KOMPUTER

OBYEK 2 DIMENSI BERGERAK

NAMA : CHANDRA KIRANA JATU INDRASWARI

NRP : 2110181033

KELAS : 3 D4 TEKNIK INFORMATIKA B

DEPARTEMEN TEKNIK INFORMATIKA DAN KOMPUTER

POLITEKNIK ELEKTRONIKA NEGERI SURABAYA


Jl. Raya ITS, Sukolilo, Kota Surabaya, 60111
TAHUN AJARAN 2019 - 2020
[Segitiga dan Bintang Bergerak]

1. Full Source Code

#include <GL/glut.h>
#include <math.h>

typedef struct {
float x;
float y;
float z;
} Point3D_t;

typedef struct {
float m[3][3];
} Matrix3D_t;

typedef struct {
float v[3];
} Vector3D_t;

typedef struct {
float r;
float g;
float b;
} Color_t;

void drawLine(Point3D_t[], int, Color_t);


void sumbuKoordinat();
void timer(int);
void display(void);
void initialize(void);
void drawPolygon(Point3D_t[], int, Color_t);
void drawFillPolygon(Point3D_t[], int, Color_t);
void Bintang_Rotasi();
void Bintang_Scale();
void Segitiga_Rotasi();
void Segitiga_Rotasi2();
void Segitiga_Vertical();
void Segitiga_Horizontal();
Matrix3D_t rotationZ(float);
Matrix3D_t scalingZ(float);
Matrix3D_t createIdentity();
Vector3D_t point2Vector3d(Point3D_t point);
Point3D_t vector2Point3d(Vector3D_t vector);
Vector3D_t operator*(Matrix3D_t, Vector3D_t);
Matrix3D_t operator*(Matrix3D_t, Matrix3D_t);
int sudut = 2;
Matrix3D_t matrix3DZ;
Vector3D_t vec3D;

Point3D_t bintangRotasi[10] = { {0,90}, {19,34}, {77,34}, {30,-1}, {48,-55},


{0,-22}, {-48,-55}, {-26,-1}, {-77,34}, {-17,34}
};

Point3D_t bintangZoom[10] = {
{0,90}, {19,34}, {77,34}, {30,-1}, {48,-55},
{0,-22}, {-48,-55}, {-26,-1}, {-77,34}, {-17,34}

CHANDRA KIRANA J 1
[Segitiga dan Bintang Bergerak]

};

Point3D_t segitigaRotasi[3] = {
{-100,0},{0,200},{100,0}
};

Point3D_t segitiga01[3] = { {-20,60},{20,60},{0,100} };


Point3D_t segitiga02[3] = { {-20,-60},{20,-60},{0,-100} };
Point3D_t segitiga03[3] = { {60,20},{60,-20},{100,0} };
Point3D_t segitiga04[3] = { {-60,20},{-60,-20},{-100,0} };
Point3D_t segitigaV1[3] = { {-300,0},{-275,50},{-250,0} };
Point3D_t segitigaV2[3] = { {-220,0},{-195,50},{-170,0} };
Point3D_t segitigaV3[3] = { {-140,0},{-115,50},{-90,0} };
Point3D_t segitigaV4[3] = { {-60,0},{-35,50},{-10,0} };
Point3D_t segitigaV5[3] = { {20,0},{45,50},{70,0} };
Point3D_t segitigaV6[3] = { {100,0},{125,50},{150,-0} };
Point3D_t segitigaH1[3] = { {-300,190},{-275,240},{-250,190} };
Point3D_t segitigaH2[3] = { {-300,130},{-275,180},{-250,130} };
Point3D_t segitigaH3[3] = { {-300,70},{-275,120},{-250,70} };
Point3D_t segitigaH4[3] = { {-300,10},{-275,60},{-250,10} };
Point3D_t segitigaH5[3] = { {-300,-50},{-275,0},{-250,-50} };
Point3D_t segitigaH6[3] = { {-300,-110},{-275,-60},{-250,-110} };

int main(int argc, char** argv)


{
glutInit(&argc, argv);
initialize();
glutDisplayFunc(display);
glutTimerFunc(1, timer, 0);
glutMainLoop();
return 0;
}

void drawLine(Point3D_t pnt[], int n, Color_t color) {


glColor3f(color.r, color.g, color.b);
glBegin(GL_LINES);
for (size_t i = 0; i < n; i++)
{
glVertex2d(pnt[i].x, pnt[i].y);
}
glEnd();
}

void sumbuKoordinat() {
Point3D_t sumbuX[2] = { {-320,0},{320,0} };
Point3D_t sumbuY[2] = { {0,-240},{0,240} };
Color_t col = { 0,0,1 };
drawLine(sumbuX, 2, col);
drawLine(sumbuY, 2, col);
}

void timer(int value) {


glutPostRedisplay();
glutTimerFunc(50, timer, 0);
}

void display(void) {

CHANDRA KIRANA J 2
[Segitiga dan Bintang Bergerak]

glClear(GL_COLOR_BUFFER_BIT);
sumbuKoordinat();
//Bintang_Rotasi();
//Bintang_Scale();
//Segitiga_Rotasi();
//Segitiga_Rotasi2();
//Segitiga_Vertical();
Segitiga_Horizontal();
glutSwapBuffers();
}

void initialize(void) {
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutCreateWindow("2110181033_Chandra Kirana_Segita dan Bintang Bergerak");
glClearColor(1, 1, 1, 0);
gluOrtho2D(-320, 320, -240, 240);
}

void drawPolygon(Point3D_t pnt[], int n, Color_t color) {


glColor3f(color.r, color.g, color.b);
glBegin(GL_LINE_LOOP);
for (size_t i = 0; i < n; i++)
{
glVertex2f(pnt[i].x, pnt[i].y);
}
glEnd();
}

void drawFillPolygon(Point3D_t pnt[], int n, Color_t color) {


glColor3f(color.r, color.g, color.b);
glBegin(GL_POLYGON);
for (size_t i = 0; i < n; i++)
{
glVertex2f(pnt[i].x, pnt[i].y);
}
glEnd();
}

void Bintang_Rotasi() {
Color_t color = { 1,0,0 };
matrix3DZ = rotationZ(sudut);
drawPolygon(bintangRotasi, 10, color);
for (size_t i = 0; i < 10; i++)
{
vec3D = point2Vector3d(bintangRotasi[i]);
vec3D = operator*(matrix3DZ, vec3D);
bintangRotasi[i] = vector2Point3d(vec3D);
}
}

void Bintang_Scale() {
Color_t color = { 1,0,0 };
matrix3DZ = scalingZ(1.05);
drawPolygon(bintangZoom, 10, color);
for (size_t i = 0; i < 10; i++)
{

CHANDRA KIRANA J 3
[Segitiga dan Bintang Bergerak]

vec3D = point2Vector3d(bintangZoom[i]);
vec3D = operator*(matrix3DZ, vec3D);
bintangZoom[i] = vector2Point3d(vec3D);
}
}

void Segitiga_Rotasi() {
Color_t color = { 1,0,0 };
matrix3DZ = rotationZ(sudut);
drawFillPolygon(segitigaRotasi, 3, color);
for (size_t i = 0; i < 3; i++)
{
vec3D = point2Vector3d(segitigaRotasi[i]);
vec3D = operator*(matrix3DZ, vec3D);
segitigaRotasi[i] = vector2Point3d(vec3D);
}
}

void Segitiga_Rotasi2() {
Color_t color = { 1,0,0 };
matrix3DZ = rotationZ(sudut);
drawFillPolygon(segitiga01, 3, color);
drawFillPolygon(segitiga02, 3, color);
drawFillPolygon(segitiga03, 3, color);
drawFillPolygon(segitiga04, 3, color);
for (size_t i = 0; i < 3; i++)
{
vec3D = point2Vector3d(segitiga01[i]);
vec3D = operator*(matrix3DZ, vec3D);
segitiga01[i] = vector2Point3d(vec3D);
vec3D = point2Vector3d(segitiga02[i]);
vec3D = operator*(matrix3DZ, vec3D);
segitiga02[i] = vector2Point3d(vec3D);
vec3D = point2Vector3d(segitiga03[i]);
vec3D = operator*(matrix3DZ, vec3D);
segitiga03[i] = vector2Point3d(vec3D);
vec3D = point2Vector3d(segitiga04[i]);
vec3D = operator*(matrix3DZ, vec3D);
segitiga04[i] = vector2Point3d(vec3D);
}
}

void Segitiga_Vertical() {
Color_t color = { 1,0,0 };
drawFillPolygon(segitigaV1, 3, color);
drawFillPolygon(segitigaV2, 3, color);
drawFillPolygon(segitigaV3, 3, color);
drawFillPolygon(segitigaV4, 3, color);
drawFillPolygon(segitigaV5, 3, color);
drawFillPolygon(segitigaV6, 3, color);
for (size_t i = 0; i < 3; i++)
{
segitigaV1[i].y += 2;
segitigaV2[i].y += 2;
segitigaV3[i].y += 2;
segitigaV4[i].y += 2;
segitigaV5[i].y += 2;

CHANDRA KIRANA J 4
[Segitiga dan Bintang Bergerak]

segitigaV6[i].y += 2;
}
}

void Segitiga_Horizontal() {
Color_t color = { 1,0,0 };
drawFillPolygon(segitigaH1, 3, color);
drawFillPolygon(segitigaH2, 3, color);
drawFillPolygon(segitigaH3, 3, color);
drawFillPolygon(segitigaH4, 3, color);
drawFillPolygon(segitigaH5, 3, color);
drawFillPolygon(segitigaH6, 3, color);
for (size_t i = 0; i < 3; i++)
{
segitigaH1[i].x += 2;
segitigaH2[i].x += 2;
segitigaH3[i].x += 2;
segitigaH4[i].x += 2;
segitigaH5[i].x += 2;
segitigaH6[i].x += 2;
}
}

Matrix3D_t createIdentity() {
Matrix3D_t a;
for (size_t i = 0; i < 3; i++) {
for (size_t j = 0; j < 3; j++)
a.m[i][j] = 0;
a.m[i][i] = 1;
} return a;
}

Matrix3D_t rotationZ(float theta) {


Matrix3D_t rotate = createIdentity();
float cs = cos(theta / 57.3);
float sn = sin(theta / 57.3);
rotate.m[0][0] = cs; rotate.m[0][1] = -sn;
rotate.m[1][0] = sn; rotate.m[1][1] = cs;
return rotate;
}

Matrix3D_t scalingZ(float m) {
Matrix3D_t u = createIdentity();
u.m[1][1] = m;
u.m[0][0] = m;
return u;
}

Vector3D_t point2Vector3d(Point3D_t point) {


Vector3D_t vec;
vec.v[0] = point.x;
vec.v[1] = point.y;
vec.v[2] = point.z;
return vec;
}

Point3D_t vector2Point3d(Vector3D_t vector) {

CHANDRA KIRANA J 5
[Segitiga dan Bintang Bergerak]

Point3D_t pnt;
pnt.x = vector.v[0];
pnt.y = vector.v[1];
pnt.z = vector.v[2];
return pnt;
}

Vector3D_t operator*(Matrix3D_t a, Vector3D_t b){


Vector3D_t c;
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;
}

Matrix3D_t operator*(Matrix3D_t a, Matrix3D_t b) {


Matrix3D_t c;
for (size_t i = 0; i < 3; i++) {
for (size_t j = 0; j < 3; j++) {
c.m[i][j] = 0;
for (size_t k = 0; k < 3; k++) {
c.m[i][j] = a.m[i][k] * b.m[k][j];
}
}
} return c;
}

2. Bintang Rotasi

Source Code

void Bintang_Rotasi() {
Color_t color = { 1,0,0 };
matrix3DZ = rotationZ(sudut);
drawPolygon(bintangRotasi, 10, color);
for (size_t i = 0; i < 10; i++)
{
vec3D = point2Vector3d(bintangRotasi[i]);
vec3D = operator*(matrix3DZ, vec3D);
bintangRotasi[i] = vector2Point3d(vec3D);
}
}

CHANDRA KIRANA J 6
[Segitiga dan Bintang Bergerak]

Capture

3. Bintang Scale
Source Code

void Bintang_Scale() {
Color_t color = { 1,0,0 };
matrix3DZ = scalingZ(1.05);
drawPolygon(bintangZoom, 10, color);
for (size_t i = 0; i < 10; i++)
{
vec3D = point2Vector3d(bintangZoom[i]);
vec3D = operator*(matrix3DZ, vec3D);
bintangZoom[i] = vector2Point3d(vec3D);
}
}

CHANDRA KIRANA J 7
[Segitiga dan Bintang Bergerak]

Output

4. Segitiga Rotasi
Source Code

void Segitiga_Rotasi() {
Color_t color = { 1,0,0 };
matrix3DZ = rotationZ(sudut);
drawFillPolygon(segitigaRotasi, 3, color);
for (size_t i = 0; i < 3; i++)
{
vec3D = point2Vector3d(segitigaRotasi[i]);
vec3D = operator*(matrix3DZ, vec3D);
segitigaRotasi[i] = vector2Point3d(vec3D);
}
}

CHANDRA KIRANA J 8
[Segitiga dan Bintang Bergerak]

Output

5. Segitiga Rotasi Kedua


Source Code

void Segitiga_Rotasi2() {
Color_t color = { 1,0,0 };
matrix3DZ = rotationZ(sudut);
drawFillPolygon(segitiga01, 3, color);
drawFillPolygon(segitiga02, 3, color);
drawFillPolygon(segitiga03, 3, color);
drawFillPolygon(segitiga04, 3, color);
for (size_t i = 0; i < 3; i++)
{
vec3D = point2Vector3d(segitiga01[i]);
vec3D = operator*(matrix3DZ, vec3D);
segitiga01[i] = vector2Point3d(vec3D);
vec3D = point2Vector3d(segitiga02[i]);

CHANDRA KIRANA J 9
[Segitiga dan Bintang Bergerak]

vec3D = operator*(matrix3DZ, vec3D);


segitiga02[i] = vector2Point3d(vec3D);
vec3D = point2Vector3d(segitiga03[i]);
vec3D = operator*(matrix3DZ, vec3D);
segitiga03[i] = vector2Point3d(vec3D);
vec3D = point2Vector3d(segitiga04[i]);
vec3D = operator*(matrix3DZ, vec3D);
segitiga04[i] = vector2Point3d(vec3D);
}
}

Output

CHANDRA KIRANA J 10
[Segitiga dan Bintang Bergerak]

6. Segitiga Vertical
Source Code

void Segitiga_Vertical() {
Color_t color = { 1,0,0 };
drawFillPolygon(segitigaV1, 3, color);
drawFillPolygon(segitigaV2, 3, color);
drawFillPolygon(segitigaV3, 3, color);
drawFillPolygon(segitigaV4, 3, color);
drawFillPolygon(segitigaV5, 3, color);
drawFillPolygon(segitigaV6, 3, color);
for (size_t i = 0; i < 3; i++)
{
segitigaV1[i].y += 2;
segitigaV2[i].y += 2;
segitigaV3[i].y += 2;
segitigaV4[i].y += 2;
segitigaV5[i].y += 2;
segitigaV6[i].y += 2;
}
}

Output

CHANDRA KIRANA J 11
[Segitiga dan Bintang Bergerak]

7. Segitiga Horizontal
Source Code

void Segitiga_Horizontal() {
Color_t color = { 1,0,0 };
drawFillPolygon(segitigaH1, 3, color);
drawFillPolygon(segitigaH2, 3, color);
drawFillPolygon(segitigaH3, 3, color);
drawFillPolygon(segitigaH4, 3, color);
drawFillPolygon(segitigaH5, 3, color);
drawFillPolygon(segitigaH6, 3, color);
for (size_t i = 0; i < 3; i++)
{
segitigaH1[i].x += 2;
segitigaH2[i].x += 2;
segitigaH3[i].x += 2;
segitigaH4[i].x += 2;
segitigaH5[i].x += 2;
segitigaH6[i].x += 2;

CHANDRA KIRANA J 12
[Segitiga dan Bintang Bergerak]

}
}

Output

CHANDRA KIRANA J 13

You might also like