Bagaimana sih cara membuat piramida 3 dimensi? Langsung saja ya kita bahas.
Sebuah piramida terdiri dari : 1 alas, 4 sisi, dan 5 sudut. Berikut langkah-langkahnya :
1. Buat fungsi rgb() untuk memudahkan konversi format dari warna rgb ke format warna PyOpenGL.
# Fungsi konversi rgb
def rgb(n):
return n / 255.0
2. Buat fungsi pivot() untuk tampilan garis pivot x,y,z untuk mempermudah dalam membuat bentuk 3D.
# Fungsi Pivot
def pivot():
glBegin(GL_LINES)
glColor3f(1,0,0) #merah x
glVertex3f(10,0,0)
glVertex3f(-10,0,0)
glColor3f(0,1,0) #hijau y
glVertex3f(0,10,0)
glVertex3f(0,-10,0)
glColor3f(0,0,1) #biru x
glVertex3f(0,0,10)
glVertex3f(0,-0,-10)
glEnd()
3. Buat fungsi piramida() untuk membuat bentuk piramida 3D.
# Bentuk Piramida 3D
def piramida():
# Depan
glBegin(GL_LINE_STRIP)
glColor3f(rgb(231),rgb(1),rgb(10))
glVertex3f(-1.0, -1.0, 1.0)
glVertex3f(1.0, -1.0, 1.0)
glVertex3f(0.0, 1.0, 0.0) # Point
glVertex3f(-1.0, -1.0, 1.0)
glEnd()
# Belakang
glBegin(GL_LINE_STRIP)
glColor3f(rgb(231),rgb(1),rgb(10))
glVertex3f(-1.0, -1.0, -1.0)
glVertex3f(1.0, -1.0, -1.0)
glVertex3f(0.0, 1.0, 0.0) # Point
glVertex3f(-1.0, -1.0, -1.0)
glEnd()
# Kanan
glBegin(GL_LINE_STRIP)
glColor3f(rgb(166),rgb(0),rgb(0))
glVertex3f(1.0, -1.0, 1.0)
glVertex3f(1.0, -1.0, -1.0)
glVertex3f(0.0, 1.0, 0.0) # Point
glVertex3f(1.0, -1.0, 1.0)
glEnd()
# Kiri
glBegin(GL_LINE_STRIP)
glColor3f(rgb(166),rgb(0),rgb(0))
glVertex3f(-1.0, -1.0, 1.0)
glVertex3f(-1.0, -1.0, -1.0)
glVertex3f(0.0, 1.0, 0.0) # Point
glVertex3f(-1.0, -1.0, 1.0)
glEnd()
4. Deklarasikan fungsi pivot() dan piramida() dalam fungsi mDisplay() agar bisa ditampilkan di window.
def myDisplay():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0, 0, -7.0)
glRotatef(45.0, 0.0, 1.0, 0.0)
pivot()
piramida()
glutSwapBuffers()
glFlush()
5. Untuk membuat viewport, buatlah fungsi reshape()
def reshape(width, height):
aspect = width / height
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, aspect, 0.1, 100.0)
Secara keseluruhan, source code akan seperti ini :
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def init():
glClearColor(0.,0.,0.,0.)
glEnable(GL_DEPTH_TEST)
gluOrtho2D(-20.0, 20.0, -20.0, 20.0)
# Fungsi konversi rgb
def rgb(n):
return n / 255.0
# Fungsi Pivot
def pivot():
glBegin(GL_LINES)
glColor3f(1,0,0) #merah x
glVertex3f(10,0,0)
glVertex3f(-10,0,0)
glColor3f(0,1,0) #hijau y
glVertex3f(0,10,0)
glVertex3f(0,-10,0)
glColor3f(0,0,1) #biru x
glVertex3f(0,0,10)
glVertex3f(0,-0,-10)
glEnd()
# Bentuk Piramida 3D
def piramida():
# Depan
glBegin(GL_POLYGON)
glColor3f(rgb(231),rgb(1),rgb(10))
glVertex3f(-1.0, -1.0, 1.0)
glVertex3f(1.0, -1.0, 1.0)
glVertex3f(0.0, 1.0, 0.0) # Point
glVertex3f(-1.0, -1.0, 1.0)
glEnd()
# Belakang
glBegin(GL_POLYGON)
glColor3f(rgb(231),rgb(1),rgb(10))
glVertex3f(-1.0, -1.0, -1.0)
glVertex3f(1.0, -1.0, -1.0)
glVertex3f(0.0, 1.0, 0.0) # Point
glVertex3f(-1.0, -1.0, -1.0)
glEnd()
# Kanan
glBegin(GL_POLYGON)
glColor3f(rgb(166),rgb(0),rgb(0))
glVertex3f(1.0, -1.0, 1.0)
glVertex3f(1.0, -1.0, -1.0)
glVertex3f(0.0, 1.0, 0.0) # Point
glVertex3f(1.0, -1.0, 1.0)
glEnd()
# Kiri
glBegin(GL_POLYGON)
glColor3f(rgb(166),rgb(0),rgb(0))
glVertex3f(-1.0, -1.0, 1.0)
glVertex3f(-1.0, -1.0, -1.0)
glVertex3f(0.0, 1.0, 0.0) # Point
glVertex3f(-1.0, -1.0, 1.0)
glEnd()
def myDisplay():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0, 0, -7.0)
glRotatef(45.0, 0.0, 1.0, 0.0)
pivot()
piramida()
glutSwapBuffers()
glFlush()
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)
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE)
glutInitWindowSize(500,500)
glutInitWindowPosition(100,100)
glutCreateWindow("Piramida 3D")
glutDisplayFunc(myDisplay)
glutReshapeFunc(reshape)
init()
glutMainLoop()
main()
Outputnya adalah sebagai berikut :
Sekian dan selamat mencoba.
Komentar
Posting Komentar