Membuat desain gambar rumah dengan PyOpenGL
- Tools yang digunakan :
- Python 3.8.2
- Idle (Python Editor)
Modul yang digunakan:
- PyOpenGL-3.1.5-cp38-cp38-win32.whl
- PyOpenGL_accelerate-3.1.5-cp38-cp38-win32.whl
Download Library : https://www.lfd.uci.edu/~gohlke/pythonlibs/
note: Download di sesuaikan dengan system (32 Bit / 64 Bit)
Penjelasan:
- Import modul OpenGL
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
- Fungsi init() digunakan untuk mengatur window
- glClearColor digunakan untuk mengatur warna background
- gluOrtho2d() digunakan untuk mengatur ukuran koordinat kartesius grafik
def init():
glClearColor(1.0, 0.0, 0.0, 0.0)
gluOrtho2D(-50.0, 50.0, -50.0, 50.0)
- Ploting digunakan untuk membuat objek gambar sesuai dengan titik koordinat yang sudah di tentukan
- glColor3f digunakan untuk mengatur warna objek
- glBegin digunakan untuk memulai gambar sesuai dengan jenis pola objek
- glVertex2f digunakan untuk menetukan nilai titik x dan y
- glEnd() digunakan untuk mengakhiri gambar objek
def ploting():
glClear(GL_COLOR_BUFFER_BIT)
#Atas dari Jendela
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-2.0, 23.0)
glVertex2f(2.0, 23.0)
glVertex2f(2.0, 24.0)
glVertex2f(-2.0, 24.0)
glVertex2f(-2.0,23.0)
glEnd()
glFlush()
#Atas Jendela
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-3.0, 22.0)
glVertex2f(3.0, 22.0)
glVertex2f(3.0, 23.0)
glVertex2f(-3.0, 23.0)
glVertex2f(-3.0,22.0)
glEnd()
glFlush()
#Jendela
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-2.0, 22.0)
glVertex2f(2.0, 22.0)
glVertex2f(2.0, 16.0)
glVertex2f(-2.0, 16.0)
glVertex2f(-2.0,22.0)
glEnd()
glFlush()
#kotak dalam jendela 1
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-2.0, 19.0)
glVertex2f(-1.0, 19.0)
glVertex2f(-1.0, 16.0)
glVertex2f(-2.0, 16.0)
glVertex2f(-2.0, 19.0)
glEnd()
glFlush()
#kotak dalam jendela 2
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-2.0, 22.0)
glVertex2f(-1.0, 22.0)
glVertex2f(-1.0, 19.0)
glVertex2f(-2.0, 19.0)
glVertex2f(-2.0, 22.0)
glEnd()
glFlush()
#kotak dalam jendela 3
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(2.0, 19.0)
glVertex2f(1.0, 19.0)
glVertex2f(1.0, 16.0)
glVertex2f(2.0, 16.0)
glVertex2f(2.0, 19.0)
glEnd()
glFlush()
#kotak dalam jendela 4
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(2.0, 22.0)
glVertex2f(1.0, 22.0)
glVertex2f(1.0, 19.0)
glVertex2f(2.0, 19.0)
glVertex2f(2.0, 22.0)
glEnd()
glFlush()
#pintu
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-4.0, 4.0)
glVertex2f(4.0, 4.0)
glVertex2f(4.0, -4.0)
glVertex2f(-4.0, -4.0)
glVertex2f(-4.0, 4.0)
glEnd()
glFlush()
#atas pintu
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-4.0, 6.0)
glVertex2f(-4.0, 8.0)
glVertex2f(4.0, 8.0)
glVertex2f(4.0, 6.0)
glVertex2f(-4.0,6.0)
glEnd()
glFlush()
#tengah pintu
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-5.0, 6.0)
glVertex2f(5.0, 6.0)
glVertex2f(5.0, 4.0)
glVertex2f(-5.0, 4.0)
glVertex2f(-5.0,6.0)
glEnd()
glFlush()
#kotak jendela kanan
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(12.0, 8.0)
glVertex2f(18.0, 8.0)
glVertex2f(18.0, 0.0)
glVertex2f(12.0, 0.0)
glVertex2f(12.0,8.0)
glEnd()
glFlush()
#kotak jendela kiri
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-12.0, 8.0)
glVertex2f(-18.0, 8.0)
glVertex2f(-18.0, 0.0)
glVertex2f(-12.0, 0.0)
glVertex2f(-12.0,8.0)
glEnd()
glFlush()
#isi kotak jendela kanan 1
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(12.0, 8.0)
glVertex2f(14.0, 8.0)
glVertex2f(14.0, 4.0)
glVertex2f(12.0, 4.0)
glVertex2f(12.0,8.0)
glEnd()
glFlush()
#isi kotak jendela kanan 2
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(12.0, 0.0)
glVertex2f(14.0, 0.0)
glVertex2f(14.0, 4.0)
glVertex2f(12.0, 4.0)
glVertex2f(12.0,0.0)
glEnd()
glFlush()
#isi kotak jendela 3
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(16.0, 4.0)
glVertex2f(18.0, 4.0)
glVertex2f(18.0, 0.0)
glVertex2f(16.0, 0.0)
glVertex2f(16.0, 4.0)
glEnd()
glFlush()
#isi kotak jendela kanan 4
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(16.0, 8.0)
glVertex2f(18.0, 8.0)
glVertex2f(18.0, 4.0)
glVertex2f(16.0, 4.0)
glVertex2f(16.0,8.0)
glEnd()
glFlush()
#isi kotak jendela kiri 1
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-12.0, 8.0)
glVertex2f(-14.0, 8.0)
glVertex2f(-14.0, 4.0)
glVertex2f(-12.0, 4.0)
glVertex2f(-12.0, 8.0)
glEnd()
glFlush()
#isi kotak jendela kiri 2
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-12.0, 0.0)
glVertex2f(-14.0, 0.0)
glVertex2f(-14.0, 4.0)
glVertex2f(-12.0, 4.0)
glVertex2f(-12.0, 0.0)
glEnd()
glFlush()
#isi kotak jendela kiri 3
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-16.0, 4.0)
glVertex2f(-18.0, 4.0)
glVertex2f(-18.0, 0.0)
glVertex2f(-16.0, 0.0)
glVertex2f(-16.0, 4.0)
glEnd()
glFlush()
#isi kotak jendela kiri 4
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-16.0, 8.0)
glVertex2f(-18.0, 8.0)
glVertex2f(-18.0, 4.0)
glVertex2f(-16.0, 4.0)
glVertex2f(-16.0, 8.0)
glEnd()
glFlush()
#Tembok persegi panjang
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-20.0, 14.0)
glVertex2f(20.0, 14.0)
glVertex2f(20.0, -4.0)
glVertex2f(-20.0, -4.0)
glVertex2f(-20.0, 14.0)
glEnd()
glFlush()
#Atap
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-16, 14.0)
glVertex2f(-24, 14.0)
glVertex2f(-22.0, 24.0)
glVertex2f(-6.0, 24.0)
glVertex2f(-16.0, 14.0)
glVertex2f(16.0, 14.0)
glVertex2f(6.0, 24.0)
glVertex2f(22.0, 24.0)
glVertex2f(24.0, 14.0)
glVertex2f(16.0, 14.0)
glEnd()
glFlush()
#Segitiga atap
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(6.0, 24.0)
glVertex2f(0.0, 30.0)
glVertex2f(-6.0, 24.0)
glEnd()
glFlush()
#Pondasi
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(-24, -4.0)
glVertex2f(24.0, -4.0)
glVertex2f(24.0, -10.0)
glVertex2f(-24.0, -10.0)
glVertex2f(-24.0, -4.0)
glEnd()
glFlush()
#Tangga 1
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(8.0, -4.0)
glVertex2f(8.0, -6.0)
glVertex2f(-8.0, -6.0)
glVertex2f(-8.0, -4.0)
glVertex2f(8.0,-4.0)
glEnd()
glFlush()
#Tangga 2
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(10.0, -6.0)
glVertex2f(10.0, -8.0)
glVertex2f(-10.0, -8.0)
glVertex2f(-10.0, -6.0)
glVertex2f(10.0, -6.0)
glEnd()
glFlush()
#Tangga 3
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINE_STRIP)
glVertex2f(12.0, -8.0)
glVertex2f(12.0, -10.0)
glVertex2f(-12.0, -10.0)
glVertex2f(-12.0, -8.0)
glVertex2f(12.0, -8.0)
glEnd()
glFlush()
Fungsi main() digunakan untuk menjalankan program inti, di dalamnya terdiri:
- glutInit() digunakan untuk inisiasi pembuatan window.
- glutInitDisplayMode() digunakan untuk menginisiasi window menggunakan OpenGL.
- glutInitWindowSize() digunakan untuk mengatur ukuran tampilan Window.
- glutInitWindowPosition() digunakan untuk mengatur posisi tampilan window.
- glutCreateWindow() digunakan untuk memberi nama Window.
- glutDisplayFunc(ploting) digunakan untuk melakukan semua perintah yang ada di fungsi ploting().
- glutMainLoop() digunakan untuk looping fungsi main.
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
glutInitWindowSize(800,800)
glutInitWindowPosition(10,10)
glutCreateWindow("Rumah")
glutDisplayFunc(ploting)
init()
glutMainLoop()
main()
Tidak ada komentar:
Posting Komentar