From 02cfdbf1b9c6a4260bd677c2aa0ee81803a902d2 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Thu, 1 Dec 2016 13:50:04 +0100 Subject: [PATCH] Better wheel events --- README.md | 12 ++++++++++++ d3/controls.py | 10 ++++++---- viewer.py | 36 +++++++++++++++++++++++++++++------- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f3ab10c..56041c4 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,18 @@ A few utilities to manage 3D models : - `convert.py` that converts any type of model to any other - `viewer.py` which is a simple script that renders a 3d model +# Dependencies + +This project is written in python 3. The `convert.py` script is made for +needing nothing else than python. However, the `viewer.py` script has a few +dependencies, you'll need : + + - pip (to install the other dependencies) `sudo apt-get install python3-pip` + for example + - numpy `sudo pip install numpy` + - pygame `sudo pip install pygame` + - PyOpenGL `sudo pip install pyopengl` + ## Supported formats - Wavefront `.obj` - Stanford `.ply` diff --git a/d3/controls.py b/d3/controls.py index 09aa960..d061c23 100644 --- a/d3/controls.py +++ b/d3/controls.py @@ -58,10 +58,11 @@ class OrbitControls(Controls): super().__init__() self.phi = 0 self.theta = 0 - self.scale = 1 + self.scale_log = 0 def apply(self): - gl.glScalef(self.scale, self.scale, self.scale) + scale = math.exp(self.scale_log) + gl.glScalef(scale, scale, scale) gl.glRotatef(self.theta * 180 / math.pi, 1.0, 0.0, 0.0) gl.glRotatef(self.phi * 180 / math.pi, 0.0, 1.0, 0.0) @@ -69,9 +70,10 @@ class OrbitControls(Controls): if event.type == pygame.MOUSEBUTTONDOWN: # Wheel up if event.button == 4: - self.scale += 0.2 + self.scale_log += 0.1 + # Wheel down elif event.button == 5: - self.scale -= 2 + self.scale_log -= 0.1 def update(self, time = 10): diff --git a/viewer.py b/viewer.py index 62321bd..7ba5a00 100755 --- a/viewer.py +++ b/viewer.py @@ -6,17 +6,33 @@ import argparse import os import math -import pygame as pg -import pygame.locals as pgl -import OpenGL.GL as gl -import OpenGL.GLU as glu - from d3.model.tools import load_model from d3.geometry import Vector from d3.controls import TrackBallControls, OrbitControls from d3.camera import Camera from d3.shader import DefaultShader +# 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) + WINDOW_WIDTH = 1024 WINDOW_HEIGHT = 1024 @@ -46,8 +62,7 @@ def main(args): controls = OrbitControls() pg.init() - display = (WINDOW_WIDTH, WINDOW_HEIGHT) - pg.display.set_mode(display, pgl.DOUBLEBUF | pgl.OPENGL | pgl.RESIZABLE) + pg.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), pgl.DOUBLEBUF | pgl.OPENGL | pgl.RESIZABLE) pg.display.set_caption('Model-Converter') # OpenGL init @@ -67,20 +82,26 @@ def main(args): running = True # Load and parse the model + print('Loading model...', file=sys.stderr, end='') model = load_model(args.input, up_conversion) + print(' done!\nInitializing OpenGL textures...', file=sys.stderr, end='') # Initializes OpenGL textures model.init_textures() # Compute normals if not already computed if len(model.normals) == 0: + print(' done!\nComputing normals...', file=sys.stderr, end='') model.generate_vertex_normals() # Generate vbos for smooth rendering + print(' done!\nGenerating vbos...', file=sys.stderr, end='') model.generate_vbos() shader = DefaultShader() + print(' Done!\nReady!', file=sys.stderr) + while running: for event in pg.event.get(): @@ -136,6 +157,7 @@ def main(args): if __name__ == '__main__': + parser = argparse.ArgumentParser() parser.set_defaults(func=main) parser.add_argument('-v', '--version', action='version', version='1.0')