Menu

[6bb4c7]: / gvxrGUI / Viewport.py  Maximize  Restore  History

Download this file

136 lines (110 with data), 4.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
try:
# The Python OpenGL package can be found at
# http://PyOpenGL.sourceforge.net/
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
except ImportError:
print("Could not import the Python OpenGL package")
exit()
from gvxrPython3 import gvxr
class Viewport:
# x,y,w,h in screen coordinates
# GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal
# the type 0 = xy-plane, 1 = yz-plane, 2 = zx-plane, 3 = perspective
def __init__(self, x, y, w, h, type):
self.x = x;
self.y = y;
self.w = w;
self.h = h;
self.type = type;
self.use_perspective = False;
self.use_orthographic = False;
def zoomIn(self):
if self.use_orthographic:
self.left *= 0.9;
self.right *= 0.9;
self.bottom *= 0.9
self.top *= 0.9;
self.loadProjectionMatrix();
def zoomOut(self):
if self.use_orthographic:
self.left *= 1.1;
self.right *= 1.1;
self.bottom *= 1.1
self.top *= 1.1;
self.loadProjectionMatrix();
def lookAt(self, eye_x, eye_y, eye_z, centre_x, centre_y, centre_z, up_x, up_y, up_z):
self.eye = [eye_x, eye_y, eye_z];
self.centre = [centre_x, centre_y, centre_z];
self.up = [up_x, up_y, up_z];
def perspective(self, fovy, aspect, zNear, zFar):
self.use_perspective = True;
self.use_orthographic = False;
self.fovy = fovy;
self.aspect = aspect;
self.zNear = zNear;
self.zFar = zFar;
def orthographic(self, left, right, bottom, top, near, far):
self.use_perspective = False;
self.use_orthographic = True;
self.left = left;
self.right = right;
self.bottom = bottom;
self.top = top;
self.zNear = near;
self.zFar = far;
def glViewport(self):
glViewport(self.x, self.y, self.w, self.h);
def loadProjectionMatrix(self):
if self.use_perspective:
gvxr.perspective(self.fovy, self.aspect, self.zNear, self.zFar);
elif self.use_orthographic:
gvxr.orthographic(self.left,
self.right,
self.bottom,
self.top,
self.zNear,
self.zFar);
def loadModelviewMatrix(self):
gvxr.lookAt(self.eye[0], self.eye[1], self.eye[2],
self.centre[0], self.centre[1], self.centre[2],
self.up[0], self.up[1], self.up[2]);
def isEventInViewport(self, x, y):
if self.x <= x and x <= self.x + self.w and \
self.y <= y and y <= self.y + self.h:
return True;
else:
return False;
def screenCoords2worldCoords(self, x, y):
X = 0;
Y = 0;
Z = 0;
normalised_x = (x - self.x) / self.w;
normalised_y = (y - self.y) / self.h;
#print (self.type)
if self.type == 0: # xy-plane
X = normalised_x * (self.right - self.left) + self.left;
Y = normalised_y * (self.top - self.bottom) + self.bottom;
elif self.type == 1: # yz-plane
Y = (1.0 - normalised_x) * (self.right - self.left) + self.left;
Z = normalised_y * (self.top - self.bottom) + self.bottom;
elif self.type == 2: # zx-plane
Z = normalised_x * (self.right - self.left) + self.left;
X = normalised_y * (self.top - self.bottom) + self.bottom;
elif self.type == 3:
raise ("Operation not permitted")
return X, Y, Z;
def getType(self):
return self.type;
def getTypeLabel(self):
if self.type == 0:
return "xy-plane";
elif self.type == 1:
return "yz-plane";
elif self.type == 2:
return "zx-plane";
elif self.type == 3:
return "perspective";
else:
return "Unknown";
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.