- PyOpenGL
Source Code :
# Nama : Rizqi Maulana Bahtiar
# NIM : 17102068
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) # digunakan untuk membentuk penutup alas persegi piramid
# Bagian alas segitiga atau piramida (y = -1.0)
glColor3f(2.2, 2.6, 1.8);
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) # digunakan untuk membentuk segitiga piramid
# Bagian Depan Piramida atau front face (z = 1.0)
glColor3f(1.5, 4.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(0.0, 0.3, 6.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(8.0, 2.5, 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(0.4, 3.0, 1.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 += 5
elif key == GLUT_KEY_LEFT:
piramid_a -= 5
elif key == GLUT_KEY_UP:
piramid_b += 5
elif key == GLUT_KEY_DOWN:
piramid_b -= 5
def update(value):
glutPostRedisplay()
glutTimerFunc(10,update,0)
# Fungsi di atas berguna untuk bahwa objek benar adalah 3D dan menggunakan kontrol key UP, DOWN, RIGHT, LEFT untuk mengatur pergerakan piramid.
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():
# Initialize GLUT
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE)
glutInitWindowSize(500,500)
glutInitWindowPosition(100,100)
glutCreateWindow("Pertemuan 11 - Piramid 3D")
glutSpecialFunc(putar)
glutTimerFunc(50, update, 0)
glutDisplayFunc(myDisplay)
glutReshapeFunc(reshape)
init()
glutMainLoop()
main()
Output :