You are on page 1of 7

LAPORAN PRAKTIKUM

GRAFIKA KOMPUTER

Oleh:

Ulima Inas Shabrina 2110181048

POLITEKNIK ELEKTRONIKA NEGERI SURABAYA


LAPORAN PRAKTIKUM

Code Percobaan :
#include <GL/glut.h>
#include <math.h>

typedef struct {
float x;
float y;
} Point2D_t;

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;

int sudut = -2;


Vector3D_t vec3D;
Matrix3D_t matrix3DX, matrix3DY, matrix3DZ, matrix3DW;
Point3D_t kotakRotasi[4] = {
{-50,-50},
{-50,50},
{50,50},
{50,-50}
};
Point3D_t kotakScaling[4] = {
{-5,-5},
{-5,5},
{5,5},
{5,-5}
};
Point3D_t kotakVertical[4] = {
{-320,-240},
{-320,-140},
{-220,-140},
{-220,-240}
};
Point3D_t kotakHorizotal[4] = {
{-50,-50},
{-50,50},
{50,50},
{50,-50}
};

void drawDot(float x, float y);


void drawLine(Point2D_t coordinate[], int n, Color_t color);
void drawPolygon3D(Point3D_t pnt[], int n, Color_t color);
void drawFillPolygon(Point3D_t pnt[], int n, Color_t color);
void timer(int value);
void sumbuKoordinat();
void display(void);
void initialize(void);
void segiempat_rotasi();
void segiempat_vertical();
void segiempat_horizontal();
void segiempat_scale();
Matrix3D_t createIdentity();
Matrix3D_t rotationZ(float theta);
Matrix3D_t scaling(float m);
Vector3D_t point2Vector3d(Point3D_t point);
Point3D_t vector2Point3d(Vector3D_t vector);
Vector3D_t operator*(Matrix3D_t a, Vector3D_t b);

int main(int argc, char** argv)


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

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

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) {


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

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


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

Matrix3D_t createIdentity() {
Matrix3D_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;
}

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 scaling(float m) {
Matrix3D_t u = createIdentity();
u.m[1][1] = m;
u.m[0][0] = m;
return u;
}

void segiempat_rotasi() {
int n = 4;
Color_t color = { 1,0,0 };
matrix3DZ = rotationZ(sudut);
drawPolygon3D(kotakRotasi, n, color);
for (int i = 0; i < n; i++)
{
vec3D = point2Vector3d(kotakRotasi[i]);
vec3D = operator*(matrix3DZ, vec3D);
kotakRotasi[i] = vector2Point3d(vec3D);
}
}

void segiempat_vertical() {
int n = 4;
Color_t color = { 0,0,1 };
drawFillPolygon(kotakVertical, n, color);
for (int i = 0; i < n; i++)
{
kotakVertical[i].y += 2;
}
}

void segiempat_scale() {
int n = 4;
Color_t color = { 0,1,0 };
matrix3DX = scaling(1.05);
drawPolygon3D(kotakScaling, n, color);
for (int i = 0; i < n; i++)
{
vec3D = point2Vector3d(kotakScaling[i]);
vec3D = operator*(matrix3DX, vec3D);
kotakScaling[i] = vector2Point3d(vec3D);
}
}

void segiempat_horizontal() {
int n = 4;
Color_t color = { 1,0,1 };
drawPolygon3D(kotakHorizotal, n, color);
for (int i = 0; i < n; i++)
{
kotakHorizotal[i].x += 2;
}
}

void drawDot(float x, float y) {


glColor3f(0, 0, 1);
glEnd();
}

void drawLine(Point2D_t coordinate[], int n, Color_t color) {


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

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


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

void timer(int value) {


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

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

void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
sumbuKoordinat();

segiempat_rotasi();
segiempat_scale();
segiempat_horizontal();
segiempat_vertical();

glutSwapBuffers();
}

void initialize(void) {
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutCreateWindow("2110181048 - Ulima Inas Shabrina");
glClearColor(1, 1, 1, 0);
gluOrtho2D(-320, 320, -240, 240);
}

Screenshot Program :

You might also like