@@ -36,28 +36,24 @@ def __exit__(self, exc_type, exc_value, traceback):
36
36
pop_matrix ()
37
37
38
38
39
- matrix_stack = []
40
-
41
-
42
39
def push_matrix ():
43
40
"""Pushes the current transformation matrix onto the matrix stack.
44
41
"""
45
- matrix_stack . append ( p5 .renderer .transform_matrix . copy () )
42
+ p5 .renderer .push_matrix ( )
46
43
return _MatrixContext ()
47
44
48
45
49
46
def pop_matrix ():
50
47
"""Pops the current transformation matrix off the matrix stack.
51
48
"""
52
- assert len (matrix_stack ) > 0 , "No matrix to pop"
53
- p5 .renderer .transform_matrix = matrix_stack .pop ()
49
+ p5 .renderer .pop_matrix ()
54
50
55
51
56
52
def reset_transforms ():
57
53
"""Reset all transformations to their default state.
58
54
59
55
"""
60
- p5 .renderer .transform_matrix = np . identity ( 4 )
56
+ p5 .renderer .reset_transforms ( )
61
57
62
58
63
59
def translate (x , y , z = 0 ):
@@ -82,9 +78,7 @@ def translate(x, y, z=0):
82
78
:rtype: np.ndarray
83
79
84
80
"""
85
- tmat = matrix .translation_matrix (x , y , z )
86
- p5 .renderer .transform_matrix = p5 .renderer .transform_matrix .dot (tmat )
87
- return tmat
81
+ return p5 .renderer .translate (x , y , z )
88
82
89
83
90
84
def rotate (theta , axis = np .array ([0 , 0 , 1 ])):
@@ -100,10 +94,7 @@ def rotate(theta, axis=np.array([0, 0, 1])):
100
94
:rtype: np.ndarray
101
95
102
96
"""
103
- axis = np .array (axis [:])
104
- tmat = matrix .rotation_matrix (axis , theta )
105
- p5 .renderer .transform_matrix = p5 .renderer .transform_matrix .dot (tmat )
106
- return tmat
97
+ return p5 .renderer .rotate (theta )
107
98
108
99
109
100
def rotate_x (theta ):
@@ -116,7 +107,7 @@ def rotate_x(theta):
116
107
:rtype: np.ndarray
117
108
118
109
"""
119
- rotate ( theta , axis = np . array ([ 1 , 0 , 0 ]) )
110
+ p5 . renderer . rotate_x ( theta )
120
111
121
112
122
113
def rotate_y (theta ):
@@ -129,7 +120,7 @@ def rotate_y(theta):
129
120
:rtype: np.ndarray
130
121
131
122
"""
132
- rotate ( theta , axis = np . array ([ 0 , 1 , 0 ]) )
123
+ p5 . renderer . rotate_y ( theta )
133
124
134
125
135
126
def rotate_z (theta ):
@@ -140,9 +131,8 @@ def rotate_z(theta):
140
131
141
132
:returns: The rotation matrix used to apply the transformation.
142
133
:rtype: np.ndarray
143
-
144
- """
145
- rotate (theta , axis = np .array ([0 , 0 , 1 ]))
134
+ """
135
+ p5 .renderer .rotate_z (theta )
146
136
147
137
148
138
def scale (sx , sy = None , sz = None ):
@@ -160,14 +150,7 @@ def scale(sx, sy=None, sz=None):
160
150
:returns: The transformation matrix used to appy the transformation.
161
151
:rtype: np.ndarray
162
152
"""
163
- if sy is None and sz is None :
164
- sy = sx
165
- sz = sx
166
- elif sz is None :
167
- sz = 1
168
- tmat = matrix .scale_transform (sx , sy , sz )
169
- p5 .renderer .transform_matrix = p5 .renderer .transform_matrix .dot (tmat )
170
- return tmat
153
+ return p5 .renderer .scale (sx , sy , sz )
171
154
172
155
173
156
def apply_matrix (transform_matrix ):
@@ -176,20 +159,19 @@ def apply_matrix(transform_matrix):
176
159
:param transform_matrix: The new transform matrix.
177
160
:type transform_matrix: np.ndarray (or a 4×4 list)
178
161
"""
179
- tmatrix = np .array (transform_matrix )
180
- p5 .renderer .transform_matrix = p5 .renderer .transform_matrix .dot (tmatrix )
162
+ p5 .renderer .apply_matrix ()
181
163
182
164
183
165
def reset_matrix ():
184
166
"""Reset the current transform matrix.
185
167
"""
186
- p5 .renderer .transform_matrix = np . identity ( 4 )
168
+ p5 .renderer .reset_matrix ( )
187
169
188
170
189
171
def print_matrix ():
190
172
"""Print the transform matrix being used by the sketch.
191
173
"""
192
- print (p5 .renderer .transform_matrix )
174
+ print (p5 .renderer .print_matrix () )
193
175
194
176
195
177
def shear_x (theta ):
@@ -202,10 +184,7 @@ def shear_x(theta):
202
184
:rtype: np.ndarray
203
185
204
186
"""
205
- shear_mat = np .identity (4 )
206
- shear_mat [0 , 1 ] = np .tan (theta )
207
- p5 .renderer .transform_matrix = p5 .renderer .transform_matrix .dot (shear_mat )
208
- return shear_mat
187
+ return p5 .renderer .shear_x (theta )
209
188
210
189
211
190
def shear_y (theta ):
@@ -218,10 +197,7 @@ def shear_y(theta):
218
197
:rtype: np.ndarray
219
198
220
199
"""
221
- shear_mat = np .identity (4 )
222
- shear_mat [1 , 0 ] = np .tan (theta )
223
- p5 .renderer .transform_matrix = p5 .renderer .transform_matrix .dot (shear_mat )
224
- return shear_mat
200
+ return p5 .renderer .shear_y (theta )
225
201
226
202
227
203
def camera (* args , ** kwargs ):
@@ -275,29 +251,7 @@ def camera(*args, **kwargs):
275
251
:type up_vector: tuple
276
252
277
253
"""
278
- def real_camera (position = (0 , 0 , p5 .sketch .size [1 ] / math .tan (math .pi / 6 )),
279
- target_position = (0 , 0 , 0 ), up_vector = (0 , 1 , 0 )):
280
- p5 .renderer .lookat_matrix = matrix .look_at (
281
- np .array (position ),
282
- np .array (target_position ),
283
- np .array (up_vector ))
284
- p5 .renderer .camera_pos = np .array (position )
285
-
286
- if len (args ) == 9 : # If using non-tuple arguments
287
- kwargs ['position' ] = args [:3 ]
288
- kwargs ['target_position' ] = args [3 :6 ]
289
- kwargs ['up_vector' ] = args [6 :]
290
- elif len (args ) <= 3 : # If using tuple arguments
291
- if len (args ) >= 1 :
292
- kwargs ['position' ] = args [0 ]
293
- if len (args ) >= 2 :
294
- kwargs ['target_position' ] = args [1 ]
295
- if len (args ) >= 3 :
296
- kwargs ['up_vector' ] = args [2 ]
297
- else :
298
- raise ValueError ("Unexpected number of arguments passed to camera()" )
299
-
300
- real_camera (** kwargs )
254
+ p5 .renderer .camera (* args , ** kwargs )
301
255
302
256
303
257
def perspective (fovy , aspect , near , far ):
@@ -316,12 +270,7 @@ def perspective(fovy, aspect, near, far):
316
270
:param far: frustum far plane length
317
271
:type far: float
318
272
"""
319
- p5 .renderer .projection_matrix = matrix .perspective_matrix (
320
- fovy ,
321
- aspect ,
322
- near ,
323
- far
324
- )
273
+ p5 .renderer .perspective (fovy , aspect , near , far )
325
274
326
275
327
276
def ortho (left , right , bottom , top , near , far ):
@@ -348,12 +297,7 @@ def ortho(left, right, bottom, top, near, far):
348
297
:param far: camera frustum far plane
349
298
:type far: float
350
299
"""
351
- p5 .renderer .projection_matrix = np .array ([
352
- [2 / (right - left ), 0 , 0 , - (right + left ) / (right - left )],
353
- [0 , 2 / (top - bottom ), 0 , - (top + bottom ) / (top - bottom )],
354
- [0 , 0 , - 2 / (far - near ), - (far + near ) / (far - near )],
355
- [0 , 0 , 0 , 1 ],
356
- ])
300
+ p5 .renderer .ortho (left , right , bottom , top , near , far )
357
301
358
302
359
303
def frustum ():
0 commit comments