Better wheel events
This commit is contained in:
parent
2077cfb311
commit
02cfdbf1b9
12
README.md
12
README.md
|
@ -11,6 +11,18 @@ A few utilities to manage 3D models :
|
||||||
- `convert.py` that converts any type of model to any other
|
- `convert.py` that converts any type of model to any other
|
||||||
- `viewer.py` which is a simple script that renders a 3d model
|
- `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
|
## Supported formats
|
||||||
- Wavefront `.obj`
|
- Wavefront `.obj`
|
||||||
- Stanford `.ply`
|
- Stanford `.ply`
|
||||||
|
|
|
@ -58,10 +58,11 @@ class OrbitControls(Controls):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.phi = 0
|
self.phi = 0
|
||||||
self.theta = 0
|
self.theta = 0
|
||||||
self.scale = 1
|
self.scale_log = 0
|
||||||
|
|
||||||
def apply(self):
|
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.theta * 180 / math.pi, 1.0, 0.0, 0.0)
|
||||||
gl.glRotatef(self.phi * 180 / math.pi, 0.0, 1.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:
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
# Wheel up
|
# Wheel up
|
||||||
if event.button == 4:
|
if event.button == 4:
|
||||||
self.scale += 0.2
|
self.scale_log += 0.1
|
||||||
|
# Wheel down
|
||||||
elif event.button == 5:
|
elif event.button == 5:
|
||||||
self.scale -= 2
|
self.scale_log -= 0.1
|
||||||
|
|
||||||
def update(self, time = 10):
|
def update(self, time = 10):
|
||||||
|
|
||||||
|
|
36
viewer.py
36
viewer.py
|
@ -6,17 +6,33 @@ import argparse
|
||||||
import os
|
import os
|
||||||
import math
|
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.model.tools import load_model
|
||||||
from d3.geometry import Vector
|
from d3.geometry import Vector
|
||||||
from d3.controls import TrackBallControls, OrbitControls
|
from d3.controls import TrackBallControls, OrbitControls
|
||||||
from d3.camera import Camera
|
from d3.camera import Camera
|
||||||
from d3.shader import DefaultShader
|
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_WIDTH = 1024
|
||||||
WINDOW_HEIGHT = 1024
|
WINDOW_HEIGHT = 1024
|
||||||
|
|
||||||
|
@ -46,8 +62,7 @@ def main(args):
|
||||||
controls = OrbitControls()
|
controls = OrbitControls()
|
||||||
|
|
||||||
pg.init()
|
pg.init()
|
||||||
display = (WINDOW_WIDTH, WINDOW_HEIGHT)
|
pg.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), pgl.DOUBLEBUF | pgl.OPENGL | pgl.RESIZABLE)
|
||||||
pg.display.set_mode(display, pgl.DOUBLEBUF | pgl.OPENGL | pgl.RESIZABLE)
|
|
||||||
pg.display.set_caption('Model-Converter')
|
pg.display.set_caption('Model-Converter')
|
||||||
|
|
||||||
# OpenGL init
|
# OpenGL init
|
||||||
|
@ -67,20 +82,26 @@ def main(args):
|
||||||
running = True
|
running = True
|
||||||
|
|
||||||
# Load and parse the model
|
# Load and parse the model
|
||||||
|
print('Loading model...', file=sys.stderr, end='')
|
||||||
model = load_model(args.input, up_conversion)
|
model = load_model(args.input, up_conversion)
|
||||||
|
|
||||||
|
print(' done!\nInitializing OpenGL textures...', file=sys.stderr, end='')
|
||||||
# Initializes OpenGL textures
|
# Initializes OpenGL textures
|
||||||
model.init_textures()
|
model.init_textures()
|
||||||
|
|
||||||
# Compute normals if not already computed
|
# Compute normals if not already computed
|
||||||
if len(model.normals) == 0:
|
if len(model.normals) == 0:
|
||||||
|
print(' done!\nComputing normals...', file=sys.stderr, end='')
|
||||||
model.generate_vertex_normals()
|
model.generate_vertex_normals()
|
||||||
|
|
||||||
# Generate vbos for smooth rendering
|
# Generate vbos for smooth rendering
|
||||||
|
print(' done!\nGenerating vbos...', file=sys.stderr, end='')
|
||||||
model.generate_vbos()
|
model.generate_vbos()
|
||||||
|
|
||||||
shader = DefaultShader()
|
shader = DefaultShader()
|
||||||
|
|
||||||
|
print(' Done!\nReady!', file=sys.stderr)
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
for event in pg.event.get():
|
for event in pg.event.get():
|
||||||
|
|
||||||
|
@ -136,6 +157,7 @@ def main(args):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.set_defaults(func=main)
|
parser.set_defaults(func=main)
|
||||||
parser.add_argument('-v', '--version', action='version', version='1.0')
|
parser.add_argument('-v', '--version', action='version', version='1.0')
|
||||||
|
|
Loading…
Reference in New Issue