From 1eedef65eef18b328a625b3b0dacc35d2863a835 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Tue, 22 Nov 2016 17:17:37 +0100 Subject: [PATCH] Added track ball controls --- conv3d/model.py | 9 ++++++++ modelviewer.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/conv3d/model.py b/conv3d/model.py index 968fc36..95b8681 100644 --- a/conv3d/model.py +++ b/conv3d/model.py @@ -17,6 +17,12 @@ class Vertex: def __add__(self, other): return Vertex(self.x + other.x, self.y + other.y, self.z + other.z) + def __mul__(self, other): + return Vertex(self.x * other, self.y * other, self.z * other) + + def __rmul__(self, other): + return self.__mul__(other) + def norm2(self): return self.x * self.x + self.y * self.y + self.z * self.z @@ -45,6 +51,9 @@ class Vertex: def __str__(self): return '(' + ", ".join([str(self.x), str(self.y), str(self.z)]) + ")" + def dot(self, other): + return self.x * other.x + self.y * other.y + self.z * other.z + Normal = Vertex TexCoord = Vertex diff --git a/modelviewer.py b/modelviewer.py index 1179239..0706680 100755 --- a/modelviewer.py +++ b/modelviewer.py @@ -5,6 +5,7 @@ import ctypes import pygame import argparse import os +import math from pygame.locals import * from OpenGL.GL import * @@ -12,6 +13,7 @@ from OpenGL.GLU import * from OpenGL.GLUT import * from conv3d.loadmodel import load_model +from conv3d.model import Vertex WINDOW_WIDTH = 1024 @@ -22,15 +24,55 @@ y = 0.5 width = 1 height = 1 +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() + + if abs(dTheta) < 0.01: + 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() + + + def init_frame(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_MODELVIEW) glLoadIdentity() - gluLookAt(5,5,5,0,0,0,0,1,0) + gluLookAt(0,0,5,0,0,0,0,1,0) def main(args): + controls = TrackBallControls() + pygame.init() display = (WINDOW_WIDTH, WINDOW_HEIGHT) pygame.display.set_mode(display, DOUBLEBUF|OPENGL) @@ -63,11 +105,22 @@ def main(args): if event.key == pygame.K_ESCAPE: pygame.quit() quit() + elif event.type == pygame.MOUSEBUTTONDOWN: + pygame.mouse.get_rel() init_frame() + + glPushMatrix() + controls.apply() model.gl_draw() + glPopMatrix() + glFlush() pygame.display.flip() + + # Update physics + controls.update() + pygame.time.wait(10)