From cfe9f7e91344166d6a52caaf4dcb083f930289d9 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Fri, 25 Nov 2016 14:28:39 +0100 Subject: [PATCH] Some cleaning --- {models => assets/models}/cube.obj | 0 {models => assets/models}/link.obj | 0 {shaders => assets/shaders}/shader.frag | 0 {shaders => assets/shaders}/shader.vert | 0 d3/{cameras => }/camera.py | 2 +- d3/cameras/__init__.py | 0 d3/{controls => }/controls.py | 2 +- d3/controls/__init__.py | 0 d3/conv/model.py | 53 +---------------------- d3/geometry.py | 57 +++++++++++++++++++++++++ viewer.py | 8 ++-- 11 files changed, 65 insertions(+), 57 deletions(-) rename {models => assets/models}/cube.obj (100%) rename {models => assets/models}/link.obj (100%) rename {shaders => assets/shaders}/shader.frag (100%) rename {shaders => assets/shaders}/shader.vert (100%) rename d3/{cameras => }/camera.py (94%) delete mode 100644 d3/cameras/__init__.py rename d3/{controls => }/controls.py (97%) delete mode 100644 d3/controls/__init__.py create mode 100644 d3/geometry.py diff --git a/models/cube.obj b/assets/models/cube.obj similarity index 100% rename from models/cube.obj rename to assets/models/cube.obj diff --git a/models/link.obj b/assets/models/link.obj similarity index 100% rename from models/link.obj rename to assets/models/link.obj diff --git a/shaders/shader.frag b/assets/shaders/shader.frag similarity index 100% rename from shaders/shader.frag rename to assets/shaders/shader.frag diff --git a/shaders/shader.vert b/assets/shaders/shader.vert similarity index 100% rename from shaders/shader.vert rename to assets/shaders/shader.vert diff --git a/d3/cameras/camera.py b/d3/camera.py similarity index 94% rename from d3/cameras/camera.py rename to d3/camera.py index 22e5610..8fbfa51 100644 --- a/d3/cameras/camera.py +++ b/d3/camera.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from ..conv.model import Vertex +from .conv.model import Vertex from OpenGL.GL import * from OpenGL.GLU import * diff --git a/d3/cameras/__init__.py b/d3/cameras/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/d3/controls/controls.py b/d3/controls.py similarity index 97% rename from d3/controls/controls.py rename to d3/controls.py index e9355aa..670a89f 100644 --- a/d3/controls/controls.py +++ b/d3/controls.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from ..conv.model import Vertex +from .conv.model import Vertex import pygame diff --git a/d3/controls/__init__.py b/d3/controls/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/d3/conv/model.py b/d3/conv/model.py index cd30deb..3591a81 100644 --- a/d3/conv/model.py +++ b/d3/conv/model.py @@ -2,58 +2,9 @@ from math import sqrt -class Vertex: - def __init__(self, x = 0.0, y = 0.0, z = 0.0): - self.x = x - self.y = y - self.z = z - - def from_array(self, arr): - self.x = float(arr[0]) if len(arr) > 0 else None - self.y = float(arr[1]) if len(arr) > 1 else None - self.z = float(arr[2]) if len(arr) > 2 else None - return self - - 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 - - def norm(self): - return sqrt(self.norm2()) - - def normalize(self): - norm = self.norm() - if abs(norm) > 0.0001: - self.x /= norm - self.y /= norm - self.z /= norm - - def cross_product(v1, v2): - return Vertex( - v1.y * v2.z - v1.z * v2.y, - v1.z * v2.x - v1.x * v2.z, - v1.x * v2.y - v1.y * v2.x) - - def from_points(v1, v2): - return Vertex( - v2.x - v1.x, - v2.y - v1.y, - v2.z - v1.z) - - 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 +from ..geometry import Vector +Vertex = Vector Normal = Vertex TexCoord = Vertex diff --git a/d3/geometry.py b/d3/geometry.py new file mode 100644 index 0000000..a30d7bf --- /dev/null +++ b/d3/geometry.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +from math import sqrt + +class Vector: + def __init__(self, x = 0.0, y = 0.0, z = 0.0): + self.x = x + self.y = y + self.z = z + + def from_array(self, arr): + self.x = float(arr[0]) if len(arr) > 0 else None + self.y = float(arr[1]) if len(arr) > 1 else None + self.z = float(arr[2]) if len(arr) > 2 else None + return self + + def __add__(self, other): + return Vector(self.x + other.x, self.y + other.y, self.z + other.z) + + def __mul__(self, other): + return Vector(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 + + def norm(self): + return sqrt(self.norm2()) + + def normalize(self): + norm = self.norm() + if abs(norm) > 0.0001: + self.x /= norm + self.y /= norm + self.z /= norm + + def cross_product(v1, v2): + return Vector( + v1.y * v2.z - v1.z * v2.y, + v1.z * v2.x - v1.x * v2.z, + v1.x * v2.y - v1.y * v2.x) + + def from_points(v1, v2): + return Vector( + v2.x - v1.x, + v2.y - v1.y, + v2.z - v1.z) + + 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 + + diff --git a/viewer.py b/viewer.py index 6f77345..aa66099 100755 --- a/viewer.py +++ b/viewer.py @@ -14,8 +14,8 @@ from OpenGL.GLU import * from d3.conv.loadmodel import load_model from d3.conv.model import Vertex -from d3.controls.controls import TrackBallControls -from d3.cameras.camera import Camera +from d3.controls import TrackBallControls +from d3.camera import Camera WINDOW_WIDTH = 1024 WINDOW_HEIGHT = 768 @@ -53,9 +53,9 @@ def main(args): VERTEX_SHADER = None FRAGMENT_SHADER = None - with open('shaders/shader.vert') as f: + with open('assets/shaders/shader.vert') as f: VERTEX_SHADER = compileShader(f.read(), GL_VERTEX_SHADER) - with open('shaders/shader.frag') as f: + with open('assets/shaders/shader.frag') as f: FRAGMENT_SHADER = compileShader(f.read(), GL_FRAGMENT_SHADER) SHADER = compileProgram(VERTEX_SHADER, FRAGMENT_SHADER)