Skip to content

Instantly share code, notes, and snippets.

@initrunlevel0
Last active December 29, 2015 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save initrunlevel0/7690978 to your computer and use it in GitHub Desktop.
Save initrunlevel0/7690978 to your computer and use it in GitHub Desktop.
Skrip pemroses berkas wavefront *.obj sederhana ke OpenGL (menggunakan PyGLET). Dibuat untuk mendukung FP Grafkomnya si Nisa # Dimodifikasi dengan menambahkan fitur rotasi
import pyglet
from pyglet.gl import *
import random
window = pyglet.window.Window(800,600)
file = 'lamp.obj'
point = []
rotate_x = 0
rotate_y = 0
@window.event
def on_draw():
window.clear()
glEnable(GL_DEPTH_TEST)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glEnable(GL_BLEND)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glRotatef( rotate_x, 1.0, 0.0, 0.0 )
glRotatef( rotate_y, 0.0, 1.0, 0.0 )
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-10, 10, -10, 10, -10, 10) # Atur agar arah shapenya pass
f = open(file, 'r')
for line in f:
splitted = line.split()
if len(splitted) > 0:
if splitted[0] == 'f':
glColor3f(0.0, 0.0, 1.0)
glBegin(GL_POLYGON)
for i in range(1, len(splitted)):
glVertex3f(point[int(splitted[i])-1][0], point[int(splitted[i])-1][1],point[int(splitted[i])-1][2])
glEnd()
glColor3f(0.0, 0.0, 0.0)
glBegin(GL_LINE_LOOP)
for i in range(1, len(splitted)):
glVertex3f(point[int(splitted[i])-1][0], point[int(splitted[i])-1][1],point[int(splitted[i])-1][2])
glEnd()
glFlush()
@window.event
def on_key_press(symbol, modifiers):
global rotate_x, rotate_y
if(symbol == pyglet.window.key.LEFT):
rotate_y = rotate_y + 5
elif(symbol == pyglet.window.key.RIGHT):
rotate_y = rotate_y - 5
elif(symbol == pyglet.window.key.UP):
rotate_x = rotate_x + 5
elif(symbol == pyglet.window.key.DOWN):
rotate_x = rotate_x - 5
def read_point():
f = open(file, 'r')
for line in f:
splitted = line.split()
if len(splitted) > 0:
if splitted[0] == 'v':
data = []
for i in range(1, len(splitted)):
data.append(float(splitted[i]))
point.append(data)
read_point()
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment