model-converter-python/viewer.py

117 lines
3.0 KiB
Python
Raw Normal View History

2016-11-22 11:55:13 +01:00
#!/usr/bin/env python3
import sys
import ctypes
2016-11-22 16:49:36 +01:00
import argparse
import os
2016-11-22 17:17:37 +01:00
import math
2016-11-22 11:55:13 +01:00
2016-11-25 15:03:03 +01:00
import pygame as pg
import pygame.locals as pgl
import OpenGL.GL as gl
import OpenGL.GLU as glu
2016-11-22 11:55:13 +01:00
2016-11-25 14:56:37 +01:00
from d3.model.tools import load_model
from d3.geometry import Vector
2016-11-25 15:41:52 +01:00
from d3.controls import TrackBallControls, OrbitControls
2016-11-25 14:28:39 +01:00
from d3.camera import Camera
2016-11-25 14:56:37 +01:00
from d3.shader import DefaultShader
2016-11-22 16:49:36 +01:00
2016-11-22 11:55:13 +01:00
WINDOW_WIDTH = 1024
WINDOW_HEIGHT = 768
2016-11-22 16:49:36 +01:00
def main(args):
2016-11-22 11:55:13 +01:00
2016-11-30 15:37:58 +01:00
if (args.from_up is None) != (args.to_up is None):
raise Exception("from-up and to-up args should be both present or both absent")
up_conversion = None
if args.from_up is not None:
up_conversion = (args.from_up, args.to_up)
2016-11-25 14:56:37 +01:00
camera = Camera(Vector(0,0,5), Vector(0,0,0))
2016-11-25 15:41:52 +01:00
controls = OrbitControls()
2016-11-22 17:17:37 +01:00
2016-11-25 15:03:03 +01:00
pg.init()
2016-11-22 11:55:13 +01:00
display = (WINDOW_WIDTH, WINDOW_HEIGHT)
2016-11-25 15:03:03 +01:00
pg.display.set_mode(display, pgl.DOUBLEBUF | pgl.OPENGL)
2016-11-29 15:42:21 +01:00
pg.display.set_caption('Model-Converter')
2016-11-22 11:55:13 +01:00
# OpenGL init
2016-11-25 15:03:03 +01:00
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
glu.gluPerspective(45, (WINDOW_WIDTH / WINDOW_HEIGHT), 0.1, 50.0)
2016-11-22 11:55:13 +01:00
2016-11-25 15:03:03 +01:00
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glEnable(gl.GL_CULL_FACE)
gl.glEnable(gl.GL_BLEND)
gl.glClearColor(0, 0, 0, 0)
2016-11-22 11:55:13 +01:00
running = True
2016-11-30 10:08:37 +01:00
# Load and parse the model
2016-11-30 15:37:58 +01:00
model = load_model(args.input, up_conversion)
2016-11-30 10:08:37 +01:00
# Initializes OpenGL textures
model.init_textures()
2016-11-30 10:08:37 +01:00
# Compute normals if not already computed
if len(model.normals) == 0:
model.generate_vertex_normals()
# Generate vbos for smooth rendering
model.generate_vbos()
2016-11-22 11:55:13 +01:00
2016-11-30 10:08:37 +01:00
shader = DefaultShader()
2016-11-25 11:27:05 +01:00
2016-11-22 11:55:13 +01:00
while running:
2016-11-25 15:03:03 +01:00
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
2016-11-22 11:55:13 +01:00
quit()
2016-11-25 15:03:03 +01:00
elif event.type == pg.KEYUP:
if event.key == pg.K_ESCAPE:
pg.quit()
2016-11-22 11:55:13 +01:00
quit()
2016-11-25 15:03:03 +01:00
elif event.type == pg.MOUSEBUTTONDOWN:
2016-11-22 17:29:25 +01:00
if event.button == 1:
2016-11-25 15:03:03 +01:00
pg.mouse.get_rel()
2016-11-22 11:55:13 +01:00
2016-11-23 12:07:47 +01:00
# Update physics
controls.update()
2016-11-25 15:03:03 +01:00
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glMatrixMode(gl.GL_MODELVIEW)
gl.glLoadIdentity()
2016-11-25 11:53:35 +01:00
camera.look()
2016-11-22 17:17:37 +01:00
2016-11-25 15:03:03 +01:00
gl.glPushMatrix()
2016-11-22 17:17:37 +01:00
controls.apply()
2016-11-30 10:08:37 +01:00
shader.bind()
2016-11-25 17:08:57 +01:00
model.draw()
2016-11-30 10:08:37 +01:00
shader.unbind()
2016-11-25 15:03:03 +01:00
gl.glPopMatrix()
2016-11-22 17:17:37 +01:00
2016-11-25 15:03:03 +01:00
gl.glFlush()
pg.display.flip()
2016-11-22 17:17:37 +01:00
2016-11-23 12:07:47 +01:00
# Sleep
2016-11-25 15:03:03 +01:00
pg.time.wait(10)
2016-11-22 11:55:13 +01:00
if __name__ == '__main__':
2016-11-22 16:49:36 +01:00
parser = argparse.ArgumentParser()
parser.set_defaults(func=main)
parser.add_argument('-v', '--version', action='version', version='1.0')
parser.add_argument('-i', '--input', metavar='input', default=None, help='Input model')
2016-11-30 15:37:58 +01:00
parser.add_argument('-fu', '--from-up', metavar='fup', default=None,
help="Initial up vector")
parser.add_argument('-tu', '--to-up', metavar='fup', default=None,
help="Output up vector")
2016-11-22 16:49:36 +01:00
args = parser.parse_args()
args.func(args)