model-converter-python/modelviewer.py

138 lines
3.1 KiB
Python
Raw Normal View History

2016-11-22 11:55:13 +01:00
#!/usr/bin/env python3
import sys
import ctypes
import pygame
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
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from conv3d.loadmodel import load_model
2016-11-22 17:17:37 +01:00
from conv3d.model import Vertex
2016-11-22 11:55:13 +01:00
2016-11-22 16:49:36 +01:00
2016-11-22 11:55:13 +01:00
WINDOW_WIDTH = 1024
WINDOW_HEIGHT = 768
x = -0.5
y = 0.5
width = 1
height = 1
2016-11-22 17:17:37 +01:00
class TrackBallControls:
def __init__(self):
self.vertex = Vertex()
self.theta = 0
def apply(self):
glRotatef(self.theta * 180 / math.pi, self.vertex.x, self.vertex.y, self.vertex.z)
def update(self, time = 10):
if not pygame.mouse.get_pressed()[0]:
return
coeff = 0.001
move = pygame.mouse.get_rel()
dV = Vertex(move[1] * time * coeff, move[0] * time * coeff, 0)
dTheta = dV.norm2()
2016-11-23 12:07:47 +01:00
if abs(dTheta) < 0.00001:
2016-11-22 17:17:37 +01:00
return
dV.normalize()
cosT2 = math.cos(self.theta / 2)
sinT2 = math.sin(self.theta / 2)
cosDT2 = math.cos(dTheta / 2)
sinDT2 = math.sin(dTheta / 2)
A = cosT2 * sinDT2 * dV + cosDT2 * sinT2 * self.vertex + sinDT2 * sinT2 * Vertex.cross_product(dV, self.vertex)
self.theta = 2 * math.acos(cosT2 * cosDT2 - sinT2 * sinDT2 * Vertex.dot(dV, self.vertex))
self.vertex = A
self.vertex.normalize()
2016-11-22 11:55:13 +01:00
def init_frame():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
2016-11-22 17:17:37 +01:00
gluLookAt(0,0,5,0,0,0,0,1,0)
2016-11-22 11:55:13 +01:00
2016-11-22 16:49:36 +01:00
def main(args):
2016-11-22 11:55:13 +01:00
2016-11-22 17:17:37 +01:00
controls = TrackBallControls()
2016-11-22 11:55:13 +01:00
pygame.init()
display = (WINDOW_WIDTH, WINDOW_HEIGHT)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
# OpenGL init
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, (WINDOW_WIDTH / WINDOW_HEIGHT), 0.1, 50.0)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
glEnable(GL_BLEND)
glClearColor(0, 0, 0, 0)
2016-11-22 15:13:35 +01:00
glLightfv(GL_LIGHT0, GL_POSITION, [10,5,7])
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
2016-11-22 11:55:13 +01:00
running = True
2016-11-22 16:49:36 +01:00
model = load_model(args.input)
model.generate_vbos()
2016-11-22 11:55:13 +01:00
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
2016-11-22 17:17:37 +01:00
elif event.type == pygame.MOUSEBUTTONDOWN:
2016-11-22 17:29:25 +01:00
if event.button == 1:
pygame.mouse.get_rel()
2016-11-22 11:55:13 +01:00
2016-11-23 12:07:47 +01:00
# Update physics
controls.update()
# Draw frame
2016-11-22 11:55:13 +01:00
init_frame()
2016-11-22 17:17:37 +01:00
glPushMatrix()
controls.apply()
2016-11-22 11:55:13 +01:00
model.gl_draw()
2016-11-22 17:17:37 +01:00
glPopMatrix()
2016-11-22 11:55:13 +01:00
glFlush()
pygame.display.flip()
2016-11-22 17:17:37 +01:00
2016-11-23 12:07:47 +01:00
# Sleep
2016-11-22 11:55:13 +01:00
pygame.time.wait(10)
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')
args = parser.parse_args()
args.func(args)