#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2024/04/22 10:13:44 +0900> """ Draw wavefront obj by OpenGL, GLFW3, PyWavefront Windows10 x64 22H2 + Python 3.10.10 64bit + PyOpenGL 3.1.6 + PyOpenGL-accelerate 3.1.6 + glfw 2.7.0 + PyWavefront 1.3.3 """ import glfw from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * import pywavefront model_kind = 2 modeldata = [ {"file": "./cube01.obj", "scale": 2.0}, {"file": "./suzanne01.obj", "scale": 5.0}, {"file": "./car.obj", "scale": 7.0}, ] SCRW, SCRH = 1280, 720 WDWTITLE = "Draw wavefront obj" FOV = 50.0 light_pos = [1.0, 1.0, 1.0, 0.0] light_ambient = [0.2, 0.2, 0.2, 1.0] light_diffuse = [0.9, 0.9, 0.9, 1.0] light_specular = [0.5, 0.5, 0.5, 1.0] winw, winh = SCRW, SCRH ang = 0.0 obj = None def init_animation(infile): global ang, obj ang = 0.0 obj = pywavefront.Wavefront(infile) def render(): global ang, obj ang += 45.0 / 60.0 # init OpenGL glViewport(0, 0, winw, winh) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(FOV, float(winw) / float(winh), 1.0, 1000.0) # clear screen glClearDepth(1.0) glClearColor(0, 0, 0, 1) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glDepthFunc(GL_LESS) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glEnable(GL_NORMALIZE) glEnable(GL_CULL_FACE) glFrontFace(GL_CCW) # glCullFace(GL_FRONT) glCullFace(GL_BACK) # set material glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) glEnable(GL_COLOR_MATERIAL) # set lighting glLightfv(GL_LIGHT0, GL_POSITION, light_pos) glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient) glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse) glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) # obj move and rotate glTranslatef(0.0, 0.0, -20.0) scale = modeldata[model_kind]["scale"] glScalef(scale, scale, scale) glRotatef(20.0, 1, 0, 0) # glRotatef(ang * 0.5, 1, 0, 0) glRotatef(ang, 0, 1, 0) # draw obj for mesh in obj.mesh_list: for mat in mesh.materials: r, g, b, a = mat.diffuse glColor4f(r, g, b, a) gl_floats = (GLfloat * len(mat.vertices))(*mat.vertices) count = len(mat.vertices) / mat.vertex_size glInterleavedArrays(GL_T2F_N3F_V3F, 0, gl_floats) glDrawArrays(GL_TRIANGLES, 0, int(count)) def key_callback(window, key, scancode, action, mods): if action == glfw.PRESS: if key == glfw.KEY_ESCAPE or key == glfw.KEY_Q: glfw.set_window_should_close(window, True) def resize(window, w, h): if h == 0: return set_view(w, h) def set_view(w, h): global winw, winh winw, winh = w, h glViewport(0, 0, w, h) def main(): if not glfw.init(): raise RuntimeError("Could not initialize GLFW3") return window = glfw.create_window(SCRW, SCRH, WDWTITLE, None, None) if not window: glfw.terminate() raise RuntimeError("Could not create an window") return glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 1) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 1) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.DEPTH_BITS, 24) glfw.set_key_callback(window, key_callback) glfw.set_window_size_callback(window, resize) glfw.make_context_current(window) glfw.swap_interval(1) set_view(SCRW, SCRH) # get csv filepath infile = modeldata[model_kind]["file"] if len(sys.argv) != 2 else sys.argv[1] init_animation(infile) # main loop while not glfw.window_should_close(window): render() glfw.swap_buffers(window) glfw.poll_events() glfw.destroy_window(window) glfw.terminate() if __name__ == "__main__": main()