Wednesday, May 6, 2020

Membuat Event Heading dengan OpenGL

Halo kawan-kawan saya berniat berbagi ilmu lagi dengan kalian tentang Membuat Event Heading dengan OpenGL. sebelum kita mylai tentunya kita harus tau Event Heading itu apa. Event Heading adalah fungsi di OpenGL yang menyediakan fungsi yang didefinisikan oleh user dan dipanggil pada saat ada event yang bersesuaian. OpenGL memonitor peralatan input, ketika ada sesuatu yang terjadi, misalkan saat mouse down, keypress, dan lain-lain.
Berikut adalah souce codenya

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

# Koordinat x dan y untuk posisi kotak
pos_x = 0
pos_y = 0

# Warna Kotak
merah = 0
hijau = 0
biru = 0


def init():
    glClearColor(0.0, 0.0, 0.0, 1.0)
    gluOrtho2D(-500.0, 500.0, -500.0, 500.0)

def kotak():
    global pos_x, pos_y
    glColor3f(merah,hijau,biru)
   
    glBegin(GL_POLYGON)
    # kuadran 1
    glVertex2f(50 + pos_x,-50 + pos_y)
    # kuadran 2
    glVertex2f(-50 + pos_x,-50 + pos_y)
    # kuadran 3
    glVertex2f(-50 + pos_x,50 + pos_y)
    # kuadran 4
    glVertex2f(50 + pos_x,50 + pos_y)
    glEnd()


def display():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(1.,1.0,1.0)
    glBegin(GL_LINES)
    glVertex2f(-500.0, 0.0)
    glVertex2f(500.0, 0.0)
    glVertex2f(0.0, 500.0)
    glVertex2f(0.0, -500.0)
    glEnd()

    glPushMatrix()
    kotak()
    glPopMatrix()


    glFlush()

def input_mouse(button, state, x, y):
    global merah, hijau, biru
    if button == GLUT_RIGHT_BUTTON and state == GLUT_DOWN:
        if hijau < 1:
            merah = 0
            hijau = 1
            biru = 0
        elif biru < 1:
            merah = 0
            hijau = 0
            biru = 1
        print("Klik Kanan ditekan ", "(", x, ",", y, ")")
    elif button == GLUT_LEFT_BUTTON and state == GLUT_DOWN:
        if merah < 1:
            merah = 1
            hijau = 0
            biru = 0
        else:
            merah = 0
            hijau = 0
            biru = 0
        print("Klik Kiri ditekan ", "(", x, ",", y, ")")

def input_keyboard(key,x,y):
    global pos_x, pos_y

    #disini algoritma untuk menentukan bachround tiap kuadran

       # Background kuadran 1 berubah warna menjadi Hijau
    if pos_x > 0 and pos_y > 0:
        glClearColor(0.0, 1.0, 0.0, 1.0)
       
    # Background kuadran 2 berubah warna menjadi Merah
    if pos_x < 0 and pos_y > 0:
        glClearColor(1.0, 0.0, 0.0, 1.0)

    # Background kuadran 3 berubah warna menjadi biru muda
    if pos_x < 0 and pos_y < 0:
        glClearColor(0.0,0.0,0.0,0.0)


    # Background kuadran 4 berubah warna menjadi Biru
    if pos_x > 0 and pos_y < 0:
        glClearColor(0.0,0.0,1.0,1.0)


#disini adalah algoritma untuk mengubah posisi kotak
    if key == GLUT_KEY_UP:
        pos_y += 5
        print("Tombol Atas ditekan ", "x : ", pos_x, " y : ", pos_y)
    elif key == GLUT_KEY_DOWN:
        pos_y -= 5
        print("Tombol Bawah ditekan ", "x : ", pos_x, " y : ", pos_y)
    elif key == GLUT_KEY_RIGHT:
        pos_x += 5
        print("Tombol Kanan ditekan ", "x : ", pos_x, " y : ", pos_y)
    elif key == GLUT_KEY_LEFT:
        pos_x -= 5
        print("Tombol Kiri ditekan ", "x : ", pos_x, " y : ", pos_y)


   

def update(value):
    glutPostRedisplay()
    glutTimerFunc(10,update,0)

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
    glutInitWindowSize(500,500)
    glutInitWindowPosition(100,100)
    glutCreateWindow("Event handling")
    glutDisplayFunc(display)

    glutSpecialFunc(input_keyboard)
    glutMouseFunc(input_mouse)

    glutTimerFunc(50, update, 0)

   

    init()
    glutMainLoop()
   
main()
 dan di situ telah terdapat beberapa penjelasan programnya

Berikut outputnya 
















Share:

0 comments:

Post a Comment

Definition List

Unordered List

Support