Anda di halaman 1dari 60

LAPORAN PRAKTIKUM 7

GRAFIKA KOMPUTER
MODUL 8
“OBJEK 3D”

Dosen Pengampu:

Rahmad Irsyada, M.Pd.

Nama Kelompok :
1.Ahmad Bahrul Ulum (2120190296)
Kelas: Teknik Informatika 5 A

PRODI TEKNIK INFORMATIKA


FAKULTAS SAINS DAN TEKNOLOGI
UNIVERSITAS NAHDLATUL ‘ULAMA SUNAN GIRI BOJONEGORO
2021
A. TUJUAN
 Memahami Inisialisasi dunia 3D.
 Memahami Object 3D (Wired).
 Memahami dasar menampilkan susunan objek 3D.

B. MANFAAT
Setelah melakukan praktikum keelapan ini, mahasiswa mampu memahami Inisialisasi dunia
3D dan Object 3D (Wired) serta memahami dasar menampilkan susunan objek 3D.

C. DASAR TEORI
1. Bentuk Wire
Fungsi Wire merupakan implementasi dari object 3D berupa kerangka benda yang
berpusat pada asal pemodelan sistem koordinat. Utara dan kutub selatan bola
berada di Z positif dan negatif sumbu-masing-masing dan meridian utama
persimpangan sumbu X positif.

Berikut adalah list untuk bangun kerangka pada 3D:


a. Kubus
Untuk membuat kubus, digunakan fungsi:
void glutWireCube(GLdouble size);
b. Bola
Untuk membuat bola digunakan fungsi:
glutWireSphere dan glutSolidSphere
Fungsi ini membuat bola berpusat pada asal pemodelan sistem koordinat.
Utara dan kutub selatan bola berada di Z positif dan negatif sumbu-masing-
masing dan meridian utama persimpangan sumbu X positif.
void glutWireSphere(GLdouble radius,GLint slices, GLint stacks);
c. Kerucut
Untuk membuat kerucut, digunakan fungsi
void glutWireCone(GLdouble base, GLdouble height,GLint
slices, GLint stacks);
Ukuran benda ditentukan dari dasar jari-jari alasnya.
d. Torus (Donat)
Untuk membuat bentuk torus (donat) digunakan fungsi
void glutWireTorus(GLdouble innerRadius, GLdouble
outerRadius, GLint nsides, GLint rings);
Render ditentukan melalui 12 sisi. Berpusat pada asal,dan dengan radius sama
dengan kuadrat dari 3.
e. Renders padat atau wireframe 12-sisi biasa padat. Berpusat di sekitar asal dan
dengan radius sama dengan akar kuadrat dari 3
void glutWireDodecahedron(void);
f. Renders padat atau wireframe 4-sisi biasa padat. Berpusat di sekitar asal dan
dengan radius sama dengan akar kuadrat dari 3.
glutWiredTetrahedron void (void);
g. Renders padat atau wireframe 8-sisi biasa padat. Berpusat di sekitar asal dan
dengan radius sebesar 1.0.
void glutWireOctahedron(void);
h. Renders padat atau wireframe 20-sisi biasa padat. Berpusat di sekitar asal dan
dengan radius sebesar 1.0.
void glutWireIcosahedron(void);
i. Render dengan membuat membuat poci the
void glutWireTeapot(GLdouble size);

D. KEGIATAN PRAKTIKUM
1. Buatlah sebuah fungsi yang dapat membuat objek 2D. tampilkan objek 2 D
tersebut pada layar dan berikan interaksi menggunakan keyboard untuk
memindahkan posisi objek pada KOORDINAT Z (mendekat dan menjauh).
Tampilkan 2 screen shot kondisi ketika objek 2D jauh dan dekat. Dan tampilkan
programnya (display function).
Script:
#include<stdlib.h>
#include<glut.h>

int w=0, h=0;


int z=0;
void drawQuads (int x, int y){
glBegin(GL_QUADS);
glVertex2i(x,y);
glVertex2i(x+50,y);
glVertex2i(x+50,y-20);
glVertex2i(x,y-20);
glEnd();
}

void Display(){ glClear(GL_COLOR_BUFFER_BIT|


GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,0,z);
drawQuads(30, 20);
glutSwapBuffers();
}

void Timer (int value){


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

void resize (int w1, int h1){


glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w1/(float) h1, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h, 1.,300.);
glMatrixMode(GL_MODELVIEW);
}

void myKeyboard(unsigned char key, int x, int y){


if (key=='a')
z+= 20;
else if(key=='d')
z+= -20;
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);

glutCreateWindow("Galih P && Miranti D K");


gluOrtho2D(-320,320,-320,320);
glutDisplayFunc(Display);
glutKeyboardFunc(myKeyboard);
glutReshapeFunc(resize);
init();
glutTimerFunc(50, Timer, 0);
glutMainLoop();
}
Output:
Setelah ditekan tombol ‘d’ Setelah ditekan tombol’a’

2. Buat juga sebuah program yang dapat menganimasikan objek 2D tersebut


menggunakan tombol (‘x’ = rotasi pada sumbu x, ‘y’ = rotasi pada sumbu y).
Sajikan screenshot hasil interaksi yang menunjukkan animasi rotasi-rotasi tersebut
dan tulis program pada fungsi display. Berilah kesimpulan.
Script:
#include<stdlib.h>
#include<glut.h>

int w=0, h=0;


int x=0, y=0, z=0;
int x1=0, y1=0, z1=0;
int sudut=0;
void drawQuads (int x, int y){
glBegin(GL_QUADS);
glVertex2i(x,y);
glVertex2i(x+50,y);
glVertex2i(x+50,y-20);
glVertex2i(x,y-20);
glEnd();
}

void Display(){ glClear(GL_COLOR_BUFFER_BIT|


GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(sudut,x1,y1,z1);
glColor3f(1,0,0.5);
drawQuads(30, 20);
glutSwapBuffers();
}

void Timer (int value){


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

void resize (int w1, int h1){


glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w1/(float) h1, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h, 1.,300.);
glMatrixMode(GL_MODELVIEW);
}

void myKeyboard(unsigned char key, int x, int y){


if (key=='d')
z+= 20;
if(key=='a')
z+= -20;
if(key=='x'){
y1=0;
x1=1;
sudut+=10;
}
if(key=='y'){
x1=0;
y1=1;
sudut+=10;
}
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);

glutCreateWindow("Galih P && Miranti D K");


gluOrtho2D(-320,320,-320,320);
glutDisplayFunc(Display);
glutKeyboardFunc(myKeyboard);
glutReshapeFunc(resize);
init();
glutTimerFunc(50, Timer, 0);
glutMainLoop();
}
Output:
Setelah itekan tombol ‘x’

Setelah ditekan tombol ‘y’


Kesimpulan:
Pada program di atas diberi kondisi:
if(key=='x'){
y1=0;
x1=1;
sudut+=10;
}
if(key=='y'){
x1=0;
y1=1;
sudut+=10;
}
Yang berarti:
1. Setiap tombol x ditekan maka pada glRotatef(sudut,x1,y1,z1); nilai sudut
akan terus bertambah sebesar 10o dan rotasi tersebut berpusat pada sumbu x,
dan
2. Setiap tombol y ditekan maka pada glRotatef(sudut,x1,y1,z1); nilai sudut
akan terus bertambah sebesar 10o dan rotasi tersebut berpusat pada sumbu y.

3. Buatlah sebuah program dengan menggunakan fungsi WireCube dimana tampilan


wirecube dapat diputar (sumbu putar x, y, z) menggunakan tombol keyboard x, y,
z
Tambahkan fungsi WIRECUBE,
Tampilkan ScreenShot Seperti di bawah ini

Script:
#include<stdlib.h>
#include<glut.h>
int w=0, h=0;
int x=0, y=0, z=0;
int x1=0, y1=0, z1=0;
int sudut=0;

void Display(){ glClear(GL_COLOR_BUFFER_BIT|


GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(sudut,x1,y1,z1);
glutWireCube(10);
glutSwapBuffers();
}

void Timer (int value){


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

void resize (int w1, int h1){


glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w1/(float) h1, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h, 1.,300.);
glMatrixMode(GL_MODELVIEW);
}

void myKeyboard(unsigned char key, int x, int y){


if (key=='a')
z+= -20;
if(key=='d')
z+= 20;
if(key=='x'){
y1=0;
z1=0;
x1=1;
sudut+=10;
glColor3f(0.5,0,0.5);
}
if(key=='y'){
x1=0;
z1=0;
y1=1;
sudut+=10;
glColor3f(0,0.5,0.5);
}
if(key=='z'){
x1=0;
y1=0;
z1=1;
sudut+=10;
glColor3f(0.5,0.5,0);
}
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);

glutCreateWindow("Galih P && Miranti D K");


gluOrtho2D(-320,320,-320,320);
glutDisplayFunc(Display);
glutKeyboardFunc(myKeyboard);
glutReshapeFunc(resize);
init();
glutTimerFunc(50, Timer, 0);
glutMainLoop();
}

Output:

Setelah ditekan tombol ‘x’


Setelah ditekan tombol ‘y’

Setelah ditekan tombol ‘z’

4. Buatlah sebuah program dengan menggunakan fungsi WireSphere, dimana wire


sphere dapat diputar pada sumbu y dengan keyboard, dan ukuran wirespehre dapat
diperbesar dengan tombol panah atas, dan untuk memperkecil dengan tombol
panah bawah.

Script:
#include<stdlib.h>
#include<glut.h>

int w=0, h=0;


int x=0, y=0, z=0;
int x1=0, y1=0, z1=0;
int sudut=0;
int a=100;

void Display(){ glClear(GL_COLOR_BUFFER_BIT|


GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(sudut,x1,y1,z1);
glutWireSphere(a,50,50);
glutSwapBuffers();
}

void Timer (int value){


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

void resize (int w1, int h1){


glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w1/(float) h1, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h, 1.,300.);
glMatrixMode(GL_MODELVIEW);
}

void myKeyboard(unsigned char key, int x, int y){


if (key=='a')
z+= -10;
if(key=='d')
z+= 10;
if(key=='x'){
y1=0;
z1=0;
x1=1;
sudut+=10;
glColor3f(0.5,0,0.5);
}
if(key=='y'){
x1=0;
z1=0;
y1=1;
sudut+=10;
glColor3f(0,0.5,0.5);
}
if(key=='z'){
x1=0;
y1=0;
z1=1;
sudut+=10;
glColor3f(0.5,0.5,0);
}
}
void mySpecialKeyboard(int key, int x, int y){
switch(key){
case GLUT_KEY_DOWN :
a+= -5;
break;
case GLUT_KEY_UP :
a+= 5;
break;
}
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);

glutCreateWindow("Galih P && Miranti D K");


gluOrtho2D(-320,320,-320,320);
glutDisplayFunc(Display);
glutKeyboardFunc(myKeyboard);
glutSpecialFunc(mySpecialKeyboard);
glutReshapeFunc(resize);
init();
glutTimerFunc(50, Timer, 0);
glutMainLoop();
}

Output:

Setelah ditekan tombol fungsi ‘▼’


Setelah ditekan tombol fungsi ‘▲’

Setelah ditekan tombol ‘x’


Setelah ditekan tombol ‘y’

Setelah ditekan tombol ‘z’

5. Buatlah sebuah program dengan menggunakan fungsi WireCone dengan interaksi


rotasi pada 3 sumbu (tombol : x,y,z), mengubah tinggi (panah atas, panah bawah)
dan alas (panah kanan, panah kiri)
Script:
#include<stdlib.h>
#include<glut.h>

int w=0, h=0;


int x=0, y=0, z=0;
int x1=0, y1=0, z1=0;
int sudut=0;
int a=10, b=20;

void Display(){ glClear(GL_COLOR_BUFFER_BIT|


GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(sudut,x1,y1,z1);
glutWireCone(a,b,50,50);
glutSwapBuffers();
}

void Timer (int value){


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

void resize (int w1, int h1){


glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w1/(float) h1, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h, 1.,300.);
glMatrixMode(GL_MODELVIEW);
}

void mySpecialKeyboard(int key, int x, int y){


switch(key){
case GLUT_KEY_LEFT:
a+=-5;
glColor3f(1,1,1);
break;
case GLUT_KEY_RIGHT:
a+=5;
glColor3f(1,0,0);
break;
case GLUT_KEY_UP:
b+=5;
glColor3f(0,1,0);
break;
case GLUT_KEY_DOWN:
b+=-5;
glColor3f(0,0,1);
break;
}
}

void myKeyboard(unsigned char key, int x, int y){


if (key=='a')
z+= -20;
if(key=='d')
z+= 20;
if(key=='x'){
y1=0;
z1=0;
x1=1;
sudut+=10;
glColor3f(0.5,0,0.5);
}
if(key=='y'){
x1=0;
z1=0;
y1=1;
sudut+=10;
glColor3f(0,0.5,0.5);
}
if(key=='z'){
x1=0;
y1=0;
z1=1;
sudut+=10;
glColor3f(0.5,0.5,0);
}
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);

glutCreateWindow("Galih P && Miranti D K");


gluOrtho2D(-320,320,-320,320);
glutDisplayFunc(Display);
glutKeyboardFunc(myKeyboard);
glutSpecialFunc(mySpecialKeyboard);
glutReshapeFunc(resize);
init();
glutTimerFunc(50, Timer, 0);
glutMainLoop();
}
Output:

Setelah ditekan tombol ‘x’


Setelah ditekan tombol ‘y’

Setelah ditekan tombol ‘z’


Setelah ditekan tombol fungsi ‘▲’

Setelah ditekan tombol fungsi ‘▼’

Setelah ditekan tombol fungsi ‘◄’


Setelah ditekan tombol fungsi ‘►’

6. Buatlah sebuah program dengan menggunakan fungsi WireTorus, dengan


interaksi rotasi pada 3 sumbu (tombol : x,y,z), mengubah diameter dalam (panah
atas, panah bawah) dan diameter luar (panah kanan, panah kiri)

Script:
#include<stdlib.h>
#include<glut.h>

int w=0, h=0;


int x=0, y=0, z=0;
int x1=0, y1=0, z1=0;
int sudut=0;
int a=10, b=20;

void Display(){ glClear(GL_COLOR_BUFFER_BIT|


GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(sudut,x1,y1,z1);
glutWireTorus(b,a,50,50);
glutSwapBuffers();
}

void Timer (int value){


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

void resize (int w1, int h1){


glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w1/(float) h1, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h, 1.,300.);
glMatrixMode(GL_MODELVIEW);
}

void mySpecialKeyboard(int key, int x, int y){


switch(key){
case GLUT_KEY_LEFT:
a+=-5;
glColor3f(1,1,1);
break;
case GLUT_KEY_RIGHT:
a+=5;
glColor3f(1,0,0);
break;
case GLUT_KEY_UP:
b+=5;
glColor3f(0,1,0);
break;
case GLUT_KEY_DOWN:
b+=-5;
glColor3f(0,0,1);
break;
}
}

void myKeyboard(unsigned char key, int x, int y){


if (key=='a')
z+= -20;
if(key=='d')
z+= 20;
if(key=='x'){
y1=0;
z1=0;
x1=1;
sudut+=10;
glColor3f(0.5,0,0.5);
}
if(key=='y'){
x1=0;
z1=0;
y1=1;
sudut+=10;
glColor3f(0,0.5,0.5);
}
if(key=='z'){
x1=0;
y1=0;
z1=1;
sudut+=10;
glColor3f(0.5,0.5,0);
}
}
void main (int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);

glutCreateWindow("Galih P && Miranti D K");


gluOrtho2D(-320,320,-320,320);
glutDisplayFunc(Display);
glutKeyboardFunc(myKeyboard);
glutSpecialFunc(mySpecialKeyboard);
glutReshapeFunc(resize);
init();
glutTimerFunc(50, Timer, 0);
glutMainLoop();
}
Output:

Setelah menekan tombol fungsi ‘▲’


Setelah menekan tombol fungsi ‘▼’

Setelah menekan tombol fungsi ‘►’

Setelah menekan tombol fungsi ‘◄’


Setelah menekan tombol ‘x’

Setelah menekan tombol ‘y’


Setelah menekan tombol ‘z’

7. Buatlah sebuah program dengan menggunakan fungsi WireDodecahedron, dengan


interaksi rotasi pada 3 sumbu (tombol : x,y,z)

Script:
#include<stdlib.h>
#include<glut.h>

int w=0, h=0;


int x=0, y=0, z=0;
int x1=0, y1=0, z1=0;
int sudut=0;
int a=10, b=20;
void Display(){ glClear(GL_COLOR_BUFFER_BIT|
GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(sudut,x1,y1,z1);
glutWireDodecahedron();
glutSwapBuffers();
}

void Timer (int value){


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

void resize (int w1, int h1){


glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w1/(float) h1, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h, 1.,300.);
glMatrixMode(GL_MODELVIEW);
}

void myKeyboard(unsigned char key, int x, int y){


if (key=='a')
z+= -10;
if(key=='d')
z+= 10;
if(key=='x'){
y1=0;
z1=0;
x1=1;
sudut+=10;
glColor3f(0.5,0,0.5);
}
if(key=='y'){
x1=0;
z1=0;
y1=1;
sudut+=10;
glColor3f(0,0.5,0.5);
}
if(key=='z'){
x1=0;
y1=0;
z1=1;
sudut+=10;
glColor3f(0.5,0.5,0);
}
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);

glutCreateWindow("Galih P && Miranti D K");


gluOrtho2D(-320,320,-320,320);
glutDisplayFunc(Display);
glutKeyboardFunc(myKeyboard);
glutReshapeFunc(resize);
init();
glutTimerFunc(50, Timer, 0);
glutMainLoop();
}

Output:

Setelah ditekan tombol ‘x’


Setelah ditekan tombol ‘y’

Setelah ditekan tombol ‘z’


8. Buatlah sebuah program dengan menggunakan fungsi WireTetrahedron, dengan
interaksi rotasi pada 3 sumbu (tombol : x,y,z)

Script:
#include<stdlib.h>
#include<glut.h>

int w=0, h=0;


int x=0, y=0, z=0;
int x1=0, y1=0, z1=0;
int sudut=0;
int a=10, b=20;

void Display(){ glClear(GL_COLOR_BUFFER_BIT|


GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(sudut,x1,y1,z1);
glutWireTetrahedron();
glutSwapBuffers();
}

void Timer (int value){


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

void resize (int w1, int h1){


glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w1/(float) h1, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h, 1.,300.);
glMatrixMode(GL_MODELVIEW);
}

void myKeyboard(unsigned char key, int x, int y){


if (key=='a')
z+= -4;
if(key=='d')
z+= 4;
if(key=='x'){
y1=0;
z1=0;
x1=1;
sudut+=10;
glColor3f(0.5,0,0.5);
}
if(key=='y'){
x1=0;
z1=0;
y1=1;
sudut+=10;
glColor3f(0,0.5,0.5);
}
if(key=='z'){
x1=0;
y1=0;
z1=1;
sudut+=10;
glColor3f(0.5,0.5,0);
}
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);

glutCreateWindow("Galih P && Miranti D K");


gluOrtho2D(-320,320,-320,320);
glutDisplayFunc(Display);
glutKeyboardFunc(myKeyboard);
glutReshapeFunc(resize);
init();
glutTimerFunc(50, Timer, 0);
glutMainLoop();
}
Output:

Setelah menekan tombol ‘x’


Setelah menekan tombol ‘y’

Setelah menekan tombol ‘z’


9. Buatlah sebuah program dengan menggunakan fungsi WireOctahedron, dengan
interaksi rotasi pada 3 sumbu (tombol : x,y,z)

a. WireOctahedron
Script:
#include<stdlib.h>
#include<glut.h>

int w=0, h=0;


int x=0, y=0, z=0;
int x1=0, y1=0, z1=0;
int sudut=0;
int a=10, b=20;

void Display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(sudut,x1,y1,z1);
glutWireOctahedron();
glutSwapBuffers();

void Timer (int value){


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

void resize (int w1, int h1){


glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w1/(float) h1, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h, 1.,300.);
glMatrixMode(GL_MODELVIEW);
}

void myKeyboard(unsigned char key, int x, int y){


if (key=='a')
z+= -4;
if(key=='d')
z+= 4;
if(key=='x'){
y1=0;
z1=0;
x1=1;
sudut+=10;
glColor3f(0.5,0,0.5);
}
if(key=='y'){
x1=0;
z1=0;
y1=1;
sudut+=10;
glColor3f(0,0.5,0.5);
}
if(key=='z'){
x1=0;
y1=0;
z1=1;
sudut+=10;
glColor3f(0.5,0.5,0);
}
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);

glutCreateWindow("Galih P && Miranti D K");


gluOrtho2D(-320,320,-320,320);
glutDisplayFunc(Display);
glutKeyboardFunc(myKeyboard);
glutReshapeFunc(resize);
init();
glutTimerFunc(50, Timer, 0);
glutMainLoop();
}
Output:

Setelah ditekan tombol ‘x’


Setelah ditekan tombol ‘y’

Setelah ditekan tombol ‘z’


b. WireIcosahedron
Script:
#include<stdlib.h>
#include<glut.h>

int w=0, h=0;


int x=0, y=0, z=0;
int x1=0, y1=0, z1=0;
int sudut=0;
int a=10, b=20;

void Display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(sudut,x1,y1,z1);
glutWireIcosahedron();
glutSwapBuffers();

void Timer (int value){


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

void resize (int w1, int h1){


glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w1/(float) h1, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h, 1.,300.);
glMatrixMode(GL_MODELVIEW);
}
void myKeyboard(unsigned char key, int x, int y){
if (key=='a')
z+= -4;
if(key=='d')
z+= 4;
if(key=='x'){
y1=0;
z1=0;
x1=1;
sudut+=10;
glColor3f(0.5,0,0.5);
}
if(key=='y'){
x1=0;
z1=0;
y1=1;
sudut+=10;
glColor3f(0,0.5,0.5);
}
if(key=='z'){
x1=0;
y1=0;
z1=1;
sudut+=10;
glColor3f(0.5,0.5,0);
}
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);

glutCreateWindow("Galih P && Miranti D K");


gluOrtho2D(-320,320,-320,320);
glutDisplayFunc(Display);
glutKeyboardFunc(myKeyboard);
glutReshapeFunc(resize);
init();
glutTimerFunc(50, Timer, 0);
glutMainLoop();
}
Output:
Setelah ditekan tombol ‘x’

Setelah ditekan tombol ‘y’


Setelah ditekan tombol ‘z’

10. Buatlah sebuah program dengan menggunakan fungsi WireTeapot, dengan


interaksi rotasi pada 3 sumbu (tombol : x,y,z) dan mengubah ukurannya dengan
tombol panah atas dan panah bawah
Script:
#include<stdlib.h>
#include<glut.h>

int w=0, h=0;


int x=0, y=0, z=0;
int x1=0, y1=0, z1=0;
int sudut=0;
int a=50;

void Display(){ glClear(GL_COLOR_BUFFER_BIT|


GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(sudut,x1,y1,z1);
glutWireTeapot(a);
glutSwapBuffers();
}

void Timer (int value){


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

void resize (int w1, int h1){


glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w1/(float) h1, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h, 1.,300.);
glMatrixMode(GL_MODELVIEW);
}

void mySpecialKeyboard(int key, int x, int y){


switch(key){
case GLUT_KEY_UP:
a+=5;
glColor3f(0.5,0,0);
break;
case GLUT_KEY_DOWN:
a+=-5;
glColor3f(0,0.5,0);
break;
}
}

void myKeyboard(unsigned char key, int x, int y){


if (key=='a')
z+= -20;
if(key=='d')
z+= 20;
if(key=='x'){
y1=0;
z1=0;
x1=1;
sudut+=10;
glColor3f(0.5,0,0.5);
}
if(key=='y'){
x1=0;
z1=0;
y1=1;
sudut+=10;
glColor3f(0,0.5,0.5);
}
if(key=='z'){
x1=0;
y1=0;
z1=1;
sudut+=10;
glColor3f(0.5,0.5,0);
}
}

void main (int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);

glutCreateWindow("Galih P && Miranti D K");


gluOrtho2D(-320,320,-320,320);
glutDisplayFunc(Display);
glutKeyboardFunc(myKeyboard);
glutSpecialFunc(mySpecialKeyboard);
glutReshapeFunc(resize);
init();
glutTimerFunc(50, Timer, 0);
glutMainLoop();
}
Output:

Setelah ditekan tombol fungsi ‘▲’

Setelah ditekan tombol fungsi ‘▼’


Setelah ditekan tombol ‘x’

Setelah ditekan tombol ‘y’


Setelah ditekan tombol ‘z’

E. TUGAS
1. Buatlah Fungsi untuk membuat BALOK 3D, dengan parameter input fungsinya
berupa panjang, lebar, dan tinggi yang dapat di ubah. Sajikan source code dan
screenshotnya.
Script:
#include<stdlib.h>
#include<glut.h>
#include<stdio.h>
#include<conio.h>

int w=0, h=0;


int x=0, y=0, z=0;
int x1=0, y1=0, z1=0;
int sudut=0;
int a=0, b=0, c=0;

void Display(){ glClear(GL_COLOR_BUFFER_BIT|


GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(sudut,x1,y1,z1);
glScalef(a,b,c);
glutWireCube(50);
glutSwapBuffers();
}

void Timer (int value){


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

void resize (int w1, int h1){


glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w1/(float) h1, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h, 1.,1000.);
glMatrixMode(GL_MODELVIEW);
}

void myKeyboard(unsigned char key, int x, int y){


if (key=='a')
z+= -20;
if(key=='d')
z+= 20;
if(key=='x'){
y1=0;
z1=0;
x1=1;
sudut+=10;
}
if(key=='y'){
x1=0;
z1=0;
y1=1;
sudut+=10;
}
if(key=='z'){
x1=0;
y1=0;
z1=1;
sudut+=10;
}
//Mengubah Panjang | memperpanjang panjang pakek 'p' | memperpendek
panjang pakek 'l'
if(key=='p'){
a+=1;
glColor3f(0.75,0.75,0);
}
else if(key=='l'){
a+=-0.1;
glColor3f(0.75,0,0.75);
}
//Mengubah Lebar | memperpanjang lebar pakek 'o' | memperpendek lebar
pakek 'k'
else if(key=='o'){
b+=1;
glColor3f(0,0.75,0.75);
}
else if(key=='k'){
b+=-0.1;
glColor3f(0.75,0,0);
}
//Mengubah Tinggi | memperpanjang tinggi pakek 'i' | memperpendek tinggi
pakek 'j'
else if(key=='i'){
c+=1;
glColor3f(0,0.75,0);
}
else if(key=='j'){
c+=-0.1;
glColor3f(0,0,0.75);
}
if(key == ' '){
printf("\n\nINPUTKAN PARAMETER\n");
printf("Panjang : ");
scanf("%d",&a);
printf("Lebar : ");
scanf("%d",&c);
printf("Tinggi : ");
scanf("%d",&b);fflush(stdin); printf("\
n\nEKSEKUSI\n");
}
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);

glutCreateWindow("Galih P && Miranti D K");


glutDisplayFunc(Display);
glutKeyboardFunc(myKeyboard);
glutReshapeFunc(resize);
init();
glutTimerFunc(50, Timer, 0);
glutMainLoop();
}
Output:

Menekan tombol ‘spasi’ untuk mengatur ukuran balok

Menambah nilai panjang dengan menekan tombol ‘p’


Mengurangi nilai panjang dengan menekan tombol ‘l’

Menambah nilai lebar dengan menekan tombol ‘o’


Mengurangi nilai lebar dengan menekan tombol ‘k’
Menambah nilai tinggi dengan menekan tombol ‘i’

Mengurangi nilai tinggi dengan menekan tombol ‘j’


2. Buatlah Manusia Salju 3 dimensi dengan interaksi memutar di 3 sumbu. Sajikan
source code dan screenshotnya.
Script:
#include<stdlib.h>
#include<glut.h>
int w=400, h=400, z=0;
int x1=0, y1=0, sudut=0, z1=0, skalaX=0, skalaY=0;

void Display(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(0,0,z);
glRotatef(sudut,x1,y1,z1);
//Badan
glPushMatrix();
glTranslatef(0,0,0);
glColor3f(1,1,1);
glutWireSphere(3,200,50);
glPopMatrix();
//Kepala
glPushMatrix();
glTranslatef(0,4,0);
glColor3f(1,1,1);
glutWireSphere(2,200,50);
glPopMatrix();
//Hidung
glPushMatrix();
glTranslatef(0,4,0);
glColor3f(1,0,0);
glutWireCone(1,3,200,50);
glPopMatrix();
//Mata
glPushMatrix();
glTranslatef(1,4.5,2);
glColor3f(0,0,0);
glutWireSphere(0.1,200,50);
glPopMatrix();
glPushMatrix();
glTranslatef(-1,4.5,2);
glColor3f(0,0,0);
glutWireSphere(0.1,200,50);
glPopMatrix();
//Topi
glPushMatrix();
glTranslatef(0,5,0);
glRotatef(-90,1,0,0);
glColor3f(0,0,1);
glutWireCone(2,4,200,50);
glPopMatrix();

glutSwapBuffers();
}

void Timer (int value){


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

void resize (int w1, int h1){


glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float) w1/(float) h1, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h, 1.,300.);
glMatrixMode(GL_MODELVIEW);
}

void myKeyboard(unsigned char key, int x, int y){


if (key=='a')
z+= -10;
if(key=='d')
z+= 10;
if(key=='x'){
y1=0;
z1=0;
x1=1;
sudut+=10;
}
if(key=='y'){
x1=0;
z1=0;
y1=1;
sudut+=10;
}
if(key=='z'){
x1=0;
y1=0;
z1=1;
sudut+=10;
}
}

void main (int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);

glutCreateWindow("Galih P && Miranti D K");


gluOrtho2D(-320,320,-320,320);
glutDisplayFunc(Display);
glutKeyboardFunc(myKeyboard);
glutReshapeFunc(resize);
init();
glutTimerFunc(50, Timer, 0);
glutMainLoop();
}
Output:
Setelah ditekan tombol ‘x’

Setelah ditekan tombol ‘y’


F. DAFTAR PUSTAKA
Modul VIII Objek 3D. Praktikum Grafika Komputer Jurusan Teknik Elektro
Universitas Negeri Malang 2013.
Alivi. 2010. Objek 3D pada openGL (online). [http://blog.um.ac.id/alivi/informatika-
3/grafika-komputer/objek-3d-pada-opengl/, diakses pada 20 Maret 2013

Anda mungkin juga menyukai