Added track ball controls

This commit is contained in:
Thomas FORGIONE 2016-11-22 17:17:37 +01:00
parent 7f8e68fe96
commit 1eedef65ee
2 changed files with 63 additions and 1 deletions

View File

@ -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

View File

@ -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)