Professional Documents
Culture Documents
GRAFIKA KOMPUTER
Oleh:
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;
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 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 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 :