114 lines
2.8 KiB
Python
Executable File
114 lines
2.8 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
import sys
|
|
import os
|
|
from PySFML import sf
|
|
from wfobj import WFObj
|
|
import getopt
|
|
from OpenGL.GL import *
|
|
from OpenGL.GLU import *
|
|
|
|
def make_program(v_fname, f_fname):
|
|
vfh = open(v_fname, 'r')
|
|
v_source = vfh.read()
|
|
vfh.close()
|
|
ffh = open(f_fname, 'r')
|
|
f_source = ffh.read()
|
|
ffh.close()
|
|
|
|
v_id = glCreateShader(GL_VERTEX_SHADER)
|
|
f_id = glCreateShader(GL_FRAGMENT_SHADER)
|
|
|
|
glShaderSource(v_id, [v_source])
|
|
glShaderSource(f_id, [f_source])
|
|
|
|
glCompileShader(v_id)
|
|
glCompileShader(f_id)
|
|
|
|
p_id = glCreateProgram()
|
|
glAttachShader(p_id, v_id)
|
|
glAttachShader(p_id, f_id)
|
|
|
|
glLinkProgram(p_id)
|
|
|
|
glDeleteShader(v_id)
|
|
glDeleteShader(f_id)
|
|
|
|
return p_id
|
|
|
|
def main(argv):
|
|
options, args = getopt.getopt(argv[1:], '')
|
|
if len(args) == 0:
|
|
sys.stderr.write('Specify object file to load\n')
|
|
return 2
|
|
|
|
obj = WFObj(args[0])
|
|
|
|
window = sf.Window(sf.VideoMode(800, 600, 32),
|
|
'Python Wavefront Object Viewer')
|
|
window.UseVerticalSync(True)
|
|
window.SetActive(True)
|
|
|
|
plain_program = make_program('v_shader.glsl', 'f_shader.glsl')
|
|
tex_program = make_program('v_tex_shader.glsl', 'f_tex_shader.glsl')
|
|
|
|
if plain_program == 0:
|
|
sys.stderr.write('plain_program did not build successfully\n')
|
|
return 2
|
|
if tex_program == 0:
|
|
sys.sterr.write('tex_program did not build successfully\n')
|
|
return 2
|
|
|
|
glClearDepth(1)
|
|
glClearColor(0, 0, 0, 0)
|
|
glEnable(GL_LIGHTING)
|
|
glEnable(GL_LIGHT0)
|
|
glEnable(GL_COLOR_MATERIAL)
|
|
glColor3f(1, 0.6, 0)
|
|
|
|
glEnable(GL_DEPTH_TEST)
|
|
glDepthMask(GL_TRUE)
|
|
|
|
glMatrixMode(GL_PROJECTION)
|
|
glLoadIdentity()
|
|
gluPerspective(60.0, 800.0 / 600.0, 1.0, 1000.0)
|
|
|
|
clock = sf.Clock()
|
|
|
|
while window.IsOpened():
|
|
event = sf.Event()
|
|
|
|
while window.GetEvent(event):
|
|
if event.Type == sf.Event.Closed:
|
|
window.Close()
|
|
elif event.Type == sf.Event.Resized:
|
|
glViewport(0, 0, event.Size.Width, event.Size.Height)
|
|
glMatrixMode(GL_PROJECTION)
|
|
glLoadIdentity()
|
|
gluPerspective(60.0,
|
|
float(event.Size.Width) / float(event.Size.Height),
|
|
1.0, 1000.0)
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
|
glMatrixMode(GL_MODELVIEW)
|
|
glLoadIdentity()
|
|
glTranslatef(0, 0, -50)
|
|
glRotatef(clock.GetElapsedTime() * 90, 0, 1, 0)
|
|
|
|
glBegin(GL_TRIANGLES)
|
|
|
|
for material in obj.faces:
|
|
for f in obj.faces[material]:
|
|
for vr in f:
|
|
if vr[2] >= 0:
|
|
glNormal3f(*obj.normals[vr[2]])
|
|
glVertex3f(*obj.vertices[vr[0]])
|
|
|
|
glEnd()
|
|
|
|
window.Display()
|
|
|
|
return 0
|
|
|
|
sys.exit(main(sys.argv))
|