#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2024/11/23 09:33:38 +0900> """ Draw wavefront obj by OpenGL, GLFW3, PyWavefront Usage: python draw_opengl_glfw.py [-m N] [--obj INPUT.obj] [--scale N] -m N, --model N : model type [0 - 2] -o FILE, --obj FILE : set .obj filename -s N, --scale N : set scale * Windows10 x64 22H2 * Python 3.10.10 64bit * PyOpenGL 3.1.7 * PyOpenGL-accelerate 3.1.7 * glfw 2.7.0 * PyWavefront 1.3.3 """ import glfw from OpenGL.GL import * from OpenGL.GLU import * import pywavefront import argparse model_kind = 1 modeldata = [ {"file": "./models/cube01.obj", "scale": 2.0}, {"file": "./models/suzanne01.obj", "scale": 5.0}, {"file": "./models/car.obj", "scale": 7.0}, ] SCRW, SCRH = 1280, 720 WDWTITLE = "Draw wavefront obj" FOV = 50.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(scale): 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 lighting light_pos = [1.0, 1.0, 1.0, 0.0] # light_ambient = [0.2, 0.2, 0.2, 1.0] light_ambient = [0.5, 0.5, 0.5, 1.0] light_diffuse = [1.0, 1.0, 1.0, 1.0] light_specular = [0.8, 0.8, 0.8, 1.0] 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) glScalef(scale, scale, scale) glRotatef(20.0, 1, 0, 0) # glRotatef(ang * 0.5, 1, 0, 0) glRotatef(ang, 0, 1, 0) # set material glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) # glColorMaterial(GL_FRONT, GL_DIFFUSE) glEnable(GL_COLOR_MATERIAL) # draw obj for mesh in obj.mesh_list: for mat in mesh.materials: # glMaterialfv(GL_FRONT, GL_AMBIENT, mat.ambient) # glMaterialfv(GL_FRONT, GL_DIFFUSE, mat.diffuse) # glMaterialfv(GL_FRONT, GL_SPECULAR, mat.specular) glColor4fv(mat.diffuse) 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: # ESC or Q key to exit 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(): global model_kind objname = "" scale = 1.0 parser = argparse.ArgumentParser() parser.add_argument("-m", "--model", type=int, help="Model type 0 - 2") parser.add_argument("-o", "--obj", type=str, help=".obj filename") parser.add_argument("-s", "--scale", type=float, help="Scale") args = parser.parse_args() objname = modeldata[model_kind]["file"] scale = modeldata[model_kind]["scale"] if args.model is not None: model_kind = args.model if model_kind < len(modeldata): objname = modeldata[model_kind]["file"] scale = modeldata[model_kind]["scale"] else: if args.obj is not None: objname = args.obj if args.scale is not None: scale = args.scale 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 # set OpenGL 1.1 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) init_animation(objname) # main loop while not glfw.window_should_close(window): render(scale) glfw.swap_buffers(window) glfw.poll_events() glfw.destroy_window(window) glfw.terminate() if __name__ == "__main__": main()