model-converter-python/viewer.py

180 lines
4.8 KiB
Python
Raw Normal View History

2016-11-22 10:55:13 +00:00
#!/usr/bin/env python3
import sys
import ctypes
2016-11-22 15:49:36 +00:00
import argparse
import os
2016-11-22 16:17:37 +00:00
import math
2016-11-22 10:55:13 +00:00
2016-11-25 13:56:37 +00:00
from d3.model.tools import load_model
from d3.geometry import Vector
2016-11-25 14:41:52 +00:00
from d3.controls import TrackBallControls, OrbitControls
2016-11-25 13:28:39 +00:00
from d3.camera import Camera
2016-11-25 13:56:37 +00:00
from d3.shader import DefaultShader
2016-11-22 15:49:36 +00:00
2016-12-01 12:50:04 +00:00
# Test dependencies
try:
import pygame as pg
import pygame.locals as pgl
except:
print('You need to install pygame', file=sys.stderr)
sys.exit(-1)
try:
import OpenGL.GL as gl
import OpenGL.GLU as glu
except:
print('You need to install pyopengl', file=sys.stderr)
sys.exit(-1)
try:
import numpy
except:
print('You need to install numpy', file=sys.stderr)
sys.exit(-1)
2016-11-22 10:55:13 +00:00
WINDOW_WIDTH = 1024
2016-12-01 09:55:41 +00:00
WINDOW_HEIGHT = 1024
def resize(width, height):
length = min(width, height)
offset = int( math.fabs(width - height) / 2)
2016-12-01 10:47:18 +00:00
# Ugly AF
pg.display.set_mode((width, height), pg.DOUBLEBUF | pg.RESIZABLE | pg.OPENGL)
2016-12-01 09:55:41 +00:00
if width < height:
gl.glViewport(0, offset, length, length)
else:
gl.glViewport(offset, 0, length, length)
2016-11-22 10:55:13 +00:00
2016-11-22 15:49:36 +00:00
def main(args):
2016-11-22 10:55:13 +00:00
2016-11-30 14:37:58 +00: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-12-02 09:13:16 +00:00
if args.verbose:
def log(*args, **kwargs):
print(*args, **kwargs)
else:
def log(*args, **kwargs):
pass
2016-11-30 14:37:58 +00:00
2016-11-25 13:56:37 +00:00
camera = Camera(Vector(0,0,5), Vector(0,0,0))
2016-11-25 14:41:52 +00:00
controls = OrbitControls()
2016-11-22 16:17:37 +00:00
2016-11-25 14:03:03 +00:00
pg.init()
2016-12-01 12:50:04 +00:00
pg.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), pgl.DOUBLEBUF | pgl.OPENGL | pgl.RESIZABLE)
2016-11-29 14:42:21 +00:00
pg.display.set_caption('Model-Converter')
2016-11-22 10:55:13 +00:00
# OpenGL init
2016-12-01 09:55:41 +00:00
gl.glMatrixMode(gl.GL_MODELVIEW)
gl.glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
2016-11-25 14:03:03 +00:00
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
glu.gluPerspective(45, (WINDOW_WIDTH / WINDOW_HEIGHT), 0.1, 50.0)
2016-11-22 10:55:13 +00:00
2016-11-25 14:03:03 +00: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 10:55:13 +00:00
running = True
2016-11-30 09:08:37 +00:00
# Load and parse the model
2016-12-02 09:13:16 +00:00
log('Loading model...', file=sys.stderr, end='')
2016-11-30 14:37:58 +00:00
model = load_model(args.input, up_conversion)
2016-11-30 09:08:37 +00:00
2016-12-02 09:13:16 +00:00
log(' done!\nInitializing OpenGL textures...', file=sys.stderr, end='')
2016-11-30 09:08:37 +00:00
# Initializes OpenGL textures
model.init_textures()
2016-11-30 09:08:37 +00:00
# Compute normals if not already computed
if len(model.normals) == 0:
2016-12-02 09:13:16 +00:00
log(' done!\nComputing normals...', file=sys.stderr, end='')
2016-11-30 09:08:37 +00:00
model.generate_vertex_normals()
# Generate vbos for smooth rendering
2016-12-02 09:13:16 +00:00
log(' done!\nGenerating vbos...', file=sys.stderr, end='')
model.generate_vbos()
2016-11-22 10:55:13 +00:00
2016-11-30 09:08:37 +00:00
shader = DefaultShader()
2016-11-25 10:27:05 +00:00
2016-12-02 09:13:16 +00:00
log(' Done!\nReady!', file=sys.stderr)
2016-12-01 12:50:04 +00:00
2016-11-22 10:55:13 +00:00
while running:
2016-11-25 14:03:03 +00:00
for event in pg.event.get():
2016-12-01 09:55:41 +00:00
controls.apply_event(event)
2016-11-25 14:03:03 +00:00
if event.type == pg.QUIT:
pg.quit()
2016-11-22 10:55:13 +00:00
quit()
2016-11-25 14:03:03 +00:00
elif event.type == pg.KEYUP:
if event.key == pg.K_ESCAPE:
pg.quit()
2016-11-22 10:55:13 +00:00
quit()
2016-11-25 14:03:03 +00:00
elif event.type == pg.MOUSEBUTTONDOWN:
2016-11-22 16:29:25 +00:00
if event.button == 1:
2016-11-25 14:03:03 +00:00
pg.mouse.get_rel()
2016-12-01 09:55:41 +00:00
elif event.type == pg.VIDEORESIZE:
resize(event.size[0], event.size[1])
2016-11-22 10:55:13 +00:00
2016-11-23 11:07:47 +00:00
# Update physics
controls.update()
2016-11-25 14:03:03 +00:00
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glMatrixMode(gl.GL_MODELVIEW)
gl.glLoadIdentity()
2016-11-25 10:53:35 +00:00
camera.look()
2016-11-22 16:17:37 +00:00
2016-11-25 14:03:03 +00:00
gl.glPushMatrix()
2016-11-22 16:17:37 +00:00
controls.apply()
2016-12-01 09:55:41 +00:00
2016-12-01 10:48:32 +00:00
# gl.glBegin(gl.GL_LINES)
# gl.glColor3f (1.0,0.0,0.0)
# gl.glVertex3f(0.0,0.0,0.0)
# gl.glVertex3f(2.0,0.0,0.0)
# gl.glColor3f (0.0,1.0,0.0)
# gl.glVertex3f(0.0,0.0,0.0)
# gl.glVertex3f(0.0,2.0,0.0)
# gl.glColor3f (0.0,0.0,1.0)
# gl.glVertex3f(0.0,0.0,0.0)
# gl.glVertex3f(0.0,0.0,2.0)
# gl.glEnd()
2016-12-01 09:55:41 +00:00
2016-11-30 09:08:37 +00:00
shader.bind()
2016-11-25 16:08:57 +00:00
model.draw()
2016-11-30 09:08:37 +00:00
shader.unbind()
2016-11-25 14:03:03 +00:00
gl.glPopMatrix()
2016-11-22 16:17:37 +00:00
2016-11-25 14:03:03 +00:00
gl.glFlush()
pg.display.flip()
2016-11-22 16:17:37 +00:00
2016-11-23 11:07:47 +00:00
# Sleep
2016-11-25 14:03:03 +00:00
pg.time.wait(10)
2016-11-22 10:55:13 +00:00
if __name__ == '__main__':
2016-12-01 12:50:04 +00:00
2016-11-22 15:49:36 +00: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 14:37:58 +00: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-12-02 09:13:16 +00:00
parser.add_argument('-V', '--verbose', default=False, action='store_true',
help="Verbose output")
2016-11-30 14:37:58 +00:00
2016-11-22 15:49:36 +00:00
args = parser.parse_args()
args.func(args)