-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3dtrans.py
More file actions
132 lines (119 loc) · 3.87 KB
/
Copy path3dtrans.py
File metadata and controls
132 lines (119 loc) · 3.87 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
import numpy as np
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from math import *
verticies = (
(1, -1, -1),
(1, 1, -1),
(-1, 1, -1),
(-1, -1, -1),
(1, -1, 1),
(1, 1, 1),
(-1, -1, 1),
(-1, 1, 1)
)
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7)
)
def Cube(vertices):
glBegin(GL_LINE)
vertices = tuple(map(tuple, vertices))
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex])
glEnd()
def overall_scalling(vert):
vert = np.hstack((vert, np.ones((vert.shape[0], 1), dtype=vert.dtype)))
print("Enter the scaling factor")
s = float(input())
trasn = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, s]], dtype=float)
vert = vert.dot(trasn)
vert=vert/s;
vert=np.delete(vert,3,1)
print(vert)
return vert
def local_scalling(vert):
vert = np.hstack((vert, np.ones((vert.shape[0], 1), dtype=vert.dtype)))
print("Enter the scaling factor in x,y and z")
x,y,z = [float(x) for x in input().split()]
trans = np.array([[x, 0, 0, 0], [0, y, 0, 0], [0, 0, z, 0], [0, 0, 0, 1]], dtype=float)
vert = vert.dot(trans)
vert=np.delete(vert,3,1)
print(vert)
return vert
def translation(vert):
vert = np.hstack((vert, np.ones((vert.shape[0], 1), dtype=vert.dtype)))
x,y,z = [float(x) for x in input("Enter the translation in x,y and z").split()]
trans = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [x, y, z, 1]], dtype=float)
vert = vert.dot(trans)
vert=np.delete(vert,3,1)
print(vert)
return vert
def rotation(vert):
vert = np.hstack((vert, np.ones((vert.shape[0], 1), dtype=vert.dtype)))
angle = float(input("Enter the angle"))*3.14/180
axis=input("Enter the axis of roatation:")
if axis =='x':
trans = np.array([[0, 0, 1, 0], [ cos(angle), sin(angle),0, 0], [ -sin(angle), cos(angle),0, 0], [0, 0, 0, 1]], dtype=float)
if axis =='y':
trans = np.array([[cos(angle), 0, -sin(angle), 0], [0, 1, 0, 0], [sin(angle), 0, cos(angle), 0], [0, 0, 0, 1]], dtype=float)
if axis =='z':
trans = np.array([[cos(angle), sin(angle), 0, 0], [ -sin(angle), cos(angle),0, 0], [ 0, 0,1, 0], [0, 0, 0, 1]], dtype=float)
vert = vert.dot(trans)
vert=np.delete(vert,3,1)
print(vert)
return vert
def main():
pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0,0.0, -5)
vert = np.asarray(verticies, dtype=float)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type==KEYDOWN:
if event.key==K_UP:
glRotatef(15,1,0,0)
if event.key==K_DOWN:
glRotatef(-15,1,0,0)
if event.key==K_LEFT:
glRotatef(15,0,1,0)
if event.key==K_RIGHT:
glRotatef(-15,0,1,0)
if event.key==K_RSHIFT:
glTranslatef(0.0, 0.0, -3)
if event.key==K_RCTRL:
glTranslatef(-3, 0.0, 0)
if event.key==K_LCTRL:
glTranslatef(3, 0.0, 0)
if event.key==K_s:
vert = overall_scalling(vert)
if event.key==K_l:
vert = local_scalling(vert)
if event.key==K_t:
vert=translation(vert)
if event.key==K_r:
vert=rotation(vert)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Cube(verticies)
Cube(vert)
pygame.display.flip()
pygame.time.wait(10)
main()