Pada postingan ini saya akan membuat bentuk piramida 3D menggunakan PyOpenGL
Source Code:
1. Buat fungsi import modul
2. Membuat variabel untuk mengatur sisi piramida_a dan sisi piramida_b. Kemudian mengatur latar belakang warna dan membuat ordo 2D
4. Membuat fungsi putar yang berguna untuk merotasi piramid
5. Membuat fungsi update
6. Membuat fungsi reshape agar objek berubah bentuk
7. Membuat fungsi main untuk memanggil semua fungsi
OUTPUT
Source Code:
1. Buat fungsi import modul
from OpenGL.GL import*
from OpenGL.GLU import:*
from OpenGL.GLUT import*
from OpenGL.GLU import:*
from OpenGL.GLUT import*
2. Membuat variabel untuk mengatur sisi piramida_a dan sisi piramida_b. Kemudian mengatur latar belakang warna dan membuat ordo 2D
piramida_a = 0
piramida_b = 0
def init():
glClearColor(0.,0.,0.,0.)
glEnable(GL_DEPTH_TEST)
gluOrtho2D(-20.0, 20.0, -20.0, 20.0)
piramida_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 piramida_a, piramida_b
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0, 0, -5.0);
glRotatef( piramida_a, 1.0, 0.0, 0.0 )
glRotatef( piramida_b, 0.0, 1.0, 0.0 )
glBegin(GL_QUADS)
# Bagian alas segitiga atau piramida (y = -1.0)
glColor3f(1.0, 1.0, 0.0); #yellow
glVertex3f( 1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0); #yellow
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0); #yellow
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(1.0, 1.0, 0.0); #yellow
glVertex3f( 1.0, -1.0, -1.0);
glEnd();
glBegin(GL_TRIANGLES)
# Bagian Depan Piramida atau front face (z = 1.0)
glColor3f(1.0, 1.0, 0.5); #lemon
glVertex3f( 1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.5); #lemon
glVertex3f( -1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.5); #lemon
glVertex3f(0, 1.0, 0);
# Bagian kiri atau Left face (x = -1.0)
glColor3f(0.0, 0.5, 1.0); #biru sayan blue
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(0.0, 0.5, 1.0); #biru sayan blue
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.0, 0.5, 1.0); #biru sayan blue
glVertex3f(0, 1.0, 0);
# Bagian Kanan atau Right face (x = 1.0)
glColor3f(1.0, 0.0, 0.0); #red
glVertex3f( 1.0, -1.0, 1.0);
glColor3f(1.0, 0.0, 0.0); #red
glVertex3f( 1.0, -1.0, -1.0);
glColor3f(1.0, 0.0, 0.0); #red
glVertex3f(0, 1.0, 0);
# Bagian Belakang Back face (z = -1.0)
glColor3f(0.0, 1.0, 0.0); #green
glVertex3f( -1.0, -1.0, -1.0);
glColor3f(0.0, 1.0, 0.0); #green
glVertex3f( 1.0, -1.0, -1.0);
glColor3f(0.0, 1.0, 0.0); #green
glVertex3f(0, 1.0, 0);
glEnd();
glutSwapBuffers()
glFlush()
global piramida_a, piramida_b
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0, 0, -5.0);
glRotatef( piramida_a, 1.0, 0.0, 0.0 )
glRotatef( piramida_b, 0.0, 1.0, 0.0 )
glBegin(GL_QUADS)
# Bagian alas segitiga atau piramida (y = -1.0)
glColor3f(1.0, 1.0, 0.0); #yellow
glVertex3f( 1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0); #yellow
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0); #yellow
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(1.0, 1.0, 0.0); #yellow
glVertex3f( 1.0, -1.0, -1.0);
glEnd();
glBegin(GL_TRIANGLES)
# Bagian Depan Piramida atau front face (z = 1.0)
glColor3f(1.0, 1.0, 0.5); #lemon
glVertex3f( 1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.5); #lemon
glVertex3f( -1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.5); #lemon
glVertex3f(0, 1.0, 0);
# Bagian kiri atau Left face (x = -1.0)
glColor3f(0.0, 0.5, 1.0); #biru sayan blue
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(0.0, 0.5, 1.0); #biru sayan blue
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.0, 0.5, 1.0); #biru sayan blue
glVertex3f(0, 1.0, 0);
# Bagian Kanan atau Right face (x = 1.0)
glColor3f(1.0, 0.0, 0.0); #red
glVertex3f( 1.0, -1.0, 1.0);
glColor3f(1.0, 0.0, 0.0); #red
glVertex3f( 1.0, -1.0, -1.0);
glColor3f(1.0, 0.0, 0.0); #red
glVertex3f(0, 1.0, 0);
# Bagian Belakang Back face (z = -1.0)
glColor3f(0.0, 1.0, 0.0); #green
glVertex3f( -1.0, -1.0, -1.0);
glColor3f(0.0, 1.0, 0.0); #green
glVertex3f( 1.0, -1.0, -1.0);
glColor3f(0.0, 1.0, 0.0); #green
glVertex3f(0, 1.0, 0);
glEnd();
glutSwapBuffers()
glFlush()
def rotate(key,x,y):
global piramida_a, piramida_b
if key == GLUT_KEY_RIGHT:
piramida_a += 15
elif key == GLUT_KEY_LEFT:
piramida_a -= 15
elif key == GLUT_KEY_UP:
piramida_b += 15
elif key == GLUT_KEY_DOWN:
piramida_b -= 15
global piramida_a, piramida_b
if key == GLUT_KEY_RIGHT:
piramida_a += 15
elif key == GLUT_KEY_LEFT:
piramida_a -= 15
elif key == GLUT_KEY_UP:
piramida_b += 15
elif key == GLUT_KEY_DOWN:
piramida_b -= 15
def update(value):
glutPostRedisplay()
glutTimerFunc(15,update,0)
glutPostRedisplay()
glutTimerFunc(15,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)
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")
glutSpecialFunc(rotate)
glutTimerFunc(50, update, 0)
glutDisplayFunc(myDisplay)
glutReshapeFunc(reshape)
init()
glutMainLoop()
main()
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE)
glutInitWindowSize(500,500)
glutInitWindowPosition(100,100)
glutCreateWindow("Piramida 3D")
glutSpecialFunc(rotate)
glutTimerFunc(50, update, 0)
glutDisplayFunc(myDisplay)
glutReshapeFunc(reshape)
init()
glutMainLoop()
main()
Comments
Post a Comment