OBJECT 3D PIRAMIDA
Pada kesempatan
kali ini, saya membuat sebuah object 3D berbentuk piramida dengan menggunakan OpenGL
dalam bahasa Python. Tidak berbeda jauh dari praktikum sebelumnya, kita tidak
perlu menginstall sesuatu yang baru lagi. Jadi langsung saja pada penjelasan
program :
Pertama import OpenGL :
from
OpenGL.GL import *
from
OpenGL.GLU import *
from
OpenGL.GLUT import *
Lalu inisialisasi
kan posisi pyramid a dan pyramid b :
piramid_a
= 0
piramid_b
= 0
Selanjutnya
mendefinisikan window dimana didalamnya terdapat glClearColor dan gluOrtho2D.
pada glClearColor kita harus menentukan warna background pada layar, disini
kita gunakan warna hitam sebagai background (0.0, 0.0, 0.0, 0.0) dan gluOrtho2D
adalah untuk penempatan koordinat objek.
def
init():
glClearColor(0.,0.,0.,0.)
glEnable(GL_DEPTH_TEST)
gluOrtho2D(-20.0, 20.0, -20.0, 20.0)
Setelah itu
membuat fungsi untuk menampilkan bentuk, warna, dan letak dari piramida.
def
myDisplay():
global piramid_a, piramid_b
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0, 0, -7.0);
glRotatef( piramid_a, 1.0, 0.0, 0.0 )
glRotatef( piramid_b, 0.0, 1.0, 0.0 )
glBegin(GL_QUADS)
#
Bagian alas segitiga atau piramida (y = -1.0)
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);
glEnd();
glBegin(GL_TRIANGLES)
# Bagian Depan Piramida atau front face (z
= 1.0)
glColor3f(1.0, 0.0, 0.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( -1.0, -1.0, 1.0);
glVertex3f(0, 1.0, 0);
# Bagian kiri atau Left face (x = -1.0)
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(0, 1.0, 0);
# Bagian Kanan atau Right face (x = 1.0)
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f(0, 1.0, 0);
# Bagian Belakang Back face (z = -1.0)
glColor3f(1.0, 0.0, 0.0);
glVertex3f( -1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f(0, 1.0, 0);
glEnd();
glutSwapBuffers()
glFlush()
Selanjutnya adalah
membuat fungsi agar objek bisa berotasi. Disini jika kita menekan tombol kanan maka
objek akan berotasi ke bawah sebaliknya jika menekan tombol kiri. Dan jika kita
menekan tombol up maka objek akan berotasi ke kanan dan sebaliknya jika menekan
tombol down.
def
putar(key,x,y):
global piramid_a, piramid_b
if key == GLUT_KEY_RIGHT:
piramid_a += 10
elif key == GLUT_KEY_LEFT:
piramid_a -= 10
elif key == GLUT_KEY_UP:
piramid_b += 10
elif key == GLUT_KEY_DOWN:
piramid_b -= 10
Lalu membuat
fungsi update yang di dalamnya terdapat glutPostRedisplay dan glutTimerFunc. glutPostRedisplay
adalah perintah untuk mengaktifkan display secara looping. Sedangkan glutPostRedisplay
adalah perintah untuk mengatur waktu objek untuk berpindah posisi dari titik
awal ke titik tujuan.
def
update(value):
glutPostRedisplay()
glutTimerFunc(10,update,0)
Setelah itu ada
fungsi reshape yang berfungsi apabila layar diperbesar atau diperkecil dan besar
objek tetap sama.
def
reshape(width, height):
aspect = width / height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, aspect, 0.1, 100.0);
Dan yang terakhir
adalah fungsi main dimana digunakan untuk memanggil fungsi-fungsi yang sudah
dibuat diatas.
def
main():
glutInit(sys.argv) # Initialize GLUT
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE)
glutInitWindowSize(500,500)
glutInitWindowPosition(100,100)
glutCreateWindow("3D OBJECT (PIRAMIDA)
|| ALMA ALFIATUL INAYAH")
glutSpecialFunc(putar)
glutTimerFunc(50, update, 0)
glutDisplayFunc(myDisplay)
glutReshapeFunc(reshape)
init()
glutMainLoop()
Source Code :
from
OpenGL.GL import *
from
OpenGL.GLU import *
from
OpenGL.GLUT import *
piramid_a
= 0
piramid_b
= 0
def
init():
glClearColor(0.,0.,0.,0.)
glEnable(GL_DEPTH_TEST)
gluOrtho2D(-20.0, 20.0, -20.0, 20.0)
def
myDisplay():
global piramid_a, piramid_b
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0, 0, -7.0);
glRotatef( piramid_a, 1.0, 0.0, 0.0 )
glRotatef( piramid_b, 0.0, 1.0, 0.0 )
glBegin(GL_QUADS)
# Bagian alas segitiga atau piramida (y =
-1.0)
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);
glEnd();
glBegin(GL_TRIANGLES)
# Bagian Depan Piramida atau front face (z
= 1.0)
glColor3f(1.0, 0.0, 0.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( -1.0, -1.0, 1.0);
glVertex3f(0, 1.0, 0);
# Bagian kiri atau Left face (x = -1.0)
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(0, 1.0, 0);
# Bagian Kanan atau Right face (x = 1.0)
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f(0, 1.0, 0);
# Bagian Belakang Back face (z = -1.0)
glColor3f(1.0, 0.0, 0.0);
glVertex3f( -1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f(0, 1.0, 0);
glEnd();
glutSwapBuffers()
glFlush()
def
putar(key,x,y):
global piramid_a, piramid_b
if key == GLUT_KEY_RIGHT:
piramid_a += 10
elif key == GLUT_KEY_LEFT:
piramid_a -= 10
elif key == GLUT_KEY_UP:
piramid_b += 10
elif key == GLUT_KEY_DOWN:
piramid_b -= 10
def
update(value):
glutPostRedisplay()
glutTimerFunc(10,update,0)
def
reshape(width, height):
aspect = width / height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, aspect, 0.1, 100.0);
def
main():
glutInit(sys.argv) # Initialize GLUT
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE)
glutInitWindowSize(500,500)
glutInitWindowPosition(100,100)
glutCreateWindow("3D OBJECT (PIRAMIDA)
|| ALMA ALFIATUL INAYAH")
glutSpecialFunc(putar)
glutTimerFunc(50, update, 0)
glutDisplayFunc(myDisplay)
glutReshapeFunc(reshape)
init()
glutMainLoop()
main()
Output :
Klik tombol Up
Klik tombol Kanan
Klik tombol Kiri
Klik tombol Down
*Maaf pak, laptop saya
tidak bisa mengambil screen record ☹