Better wheel events

This commit is contained in:
Thomas FORGIONE 2016-12-01 13:50:04 +01:00
parent 2077cfb311
commit 02cfdbf1b9
No known key found for this signature in database
GPG Key ID: 2A210FFC062E00C3
3 changed files with 47 additions and 11 deletions

View File

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

View File

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

View File

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