-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest3.py
175 lines (154 loc) · 6.92 KB
/
test3.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
import math
from pyopengltk import OpenGLFrame
from initialization import Init_Global_Para
from tkinter import *
class Draw(Init_Global_Para, OpenGLFrame):
def __init__(self, shapeObject, isTexture=False, isLighting=-1, typeDraw='face', *args, **kw):
Init_Global_Para.__init__(self, typeDraw=typeDraw)
OpenGLFrame.__init__(self, *args, **kw)
self.object = shapeObject
### Begin APHIN ###
self.aphinType = -1 # 0 is Scale; 1 is Rotate; 2 is Translate
self.varScale = (DoubleVar(), DoubleVar(), DoubleVar())
for rate in self.varScale:
rate.set(1)
self.varScale_tmp = (DoubleVar(), DoubleVar(), DoubleVar())
for rate in self.varScale_tmp:
rate.set(1)
self.mouse = (0.0, 0.0)
self.moveR = (0.0, 0.0)
self.moveT = (0.0, 0.0)
self.preMoveR = (0.0, 0.0)
self.preMoveT = (0.0, 0.0)
self.sizeObject = 0.0
self.isScaleFirst = 0
self.isTranslateFirst = 0
self.isRotateFirst = 0
self.isLightSourceFist = 0
self.bind('<Button-1>', self.tkRecordMouse)
if self.aphinType in range(4):
self.bind('<B1-Motion>', self.tkAphin)
### End APHIN ###
### Begin LIGHT ###
self.toggleLight = isLighting
self.lightSourceT = (0.0, 0.0)
self.preLightSourceT = (0.0, 0.0)
### End LIGHT ###
def tkSizeObject(self, event):
self.sizeObject = math.sqrt(math.pow((event.x - self.mouse[0]), 2) + math.pow((event.y-self.mouse[1]), 2))/170
def tkRecordMouse(self, event):
if self.aphinType == 0:
self.mouse = (event.x, event.y)
elif self.aphinType == 1:
self.mouse = (event.x - self.preMoveR[0], event.y + self.preMoveR[1])
elif self.aphinType == 2:
self.mouse = (event.x - self.preMoveT[0], event.y + self.preMoveT[1])
elif self.aphinType == 3:
self.mouse = (event.x - self.preLightSourceT[0], event.y + self.preLightSourceT[1])
def v3distsq(self, a, b):
d = (a[0] - b[0], a[1] - b[1], a[2] - b[2])
return d[0] * d[0] + d[1] * d[1] + d[2] * d[2]
def tkAphin(self, event):
win_height = max(1, self.winfo_height())
obj_c = (self.centerX.get(), self.centerY.get(), self.centerZ.get())
win = gluProject(*obj_c)
obj = gluUnProject(win[0], win[1] + 0.5 * win_height, win[2])
dist = math.sqrt(self.v3distsq(obj, obj_c))
scale = abs(dist / (0.5 * win_height))
tempX = event.x - self.mouse[0]
tempY = self.mouse[1] - event.y
if self.aphinType == 1:
self.moveR = (tempX/6, tempY/6)
self.preMoveR = (tempX, tempY)
elif self.aphinType == 2:
self.moveT = (tempX * scale, tempY * scale)
self.preMoveT = (tempX, tempY)
elif self.aphinType == 3:
self.lightSourceT = (tempX * scale, tempY * scale)
self.preLightSourceT = (tempX, tempY)
def redraw(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
self.drawLight()
self.drawTexture()
try:
glColor3fv(self.color/255)
except TypeError:
pass
if self.aphinType == 3:
self.lightPosX, self.lightPosY = self.lightSourceT[0], self.lightSourceT[1]
self.displayProject(self.fov, self.asp, self.zNear, self.zFar)
glLoadIdentity()
self.displayEye()
glPushMatrix()
# self.make_flat()
if self.aphinType == 0:
glScale(self.varScale[0].get(), self.varScale[1].get(), self.varScale[2].get())
if self.isTranslateFirst:
glTranslatef(self.moveT[0] , self.moveT[1], 0.0)
if self.isRotateFirst:
glRotatef(self.moveR[1], 1, 0, 0)
glRotatef(self.moveR[0], 0, 1, 0)
if self.isLightSourceFist:
self.lightPosX, self.lightPosY = self.lightSourceT[0], self.lightSourceT[1]
elif self.aphinType == 1:
if self.isScaleFirst:
glScale(self.varScale[0].get(), self.varScale[1].get(), self.varScale[2].get())
if self.isTranslateFirst:
glTranslatef(self.moveT[0] , self.moveT[1], 0.0)
glRotatef(self.moveR[1], 1, 0, 0)
glRotatef(self.moveR[0], 0, 1, 0)
if self.isLightSourceFist:
self.lightPosX, self.lightPosY = self.lightSourceT[0], self.lightSourceT[1]
elif self.aphinType == 2:
if self.isScaleFirst:
glScale(self.varScale[0].get(), self.varScale[1].get(), self.varScale[2].get())
glTranslatef(self.moveT[0] , self.moveT[1], 0.0)
if self.isRotateFirst:
glRotatef(self.moveR[1], 1, 0, 0)
glRotatef(self.moveR[0], 0, 1, 0)
if self.isLightSourceFist:
self.lightPosX, self.lightPosY = self.lightSourceT[0], self.lightSourceT[1]
elif self.aphinType == 3:
if self.isScaleFirst:
glScale(self.varScale[0].get(), self.varScale[1].get(), self.varScale[2].get())
if self.isTranslateFirst:
glTranslatef(self.moveT[0] , self.moveT[1], 0.0)
if self.isRotateFirst:
glRotatef(self.moveR[1], 1, 0, 0)
glRotatef(self.moveR[0], 0, 1, 0)
self.lightPosX, self.lightPosY = self.lightSourceT[0], self.lightSourceT[1]
if self.toggleRotating != -1:
glRotatef(self.rotAngle, 1.0, 1.0, 1.0)
self.rotAngle += 0.25
# self.make_truncatedCone(1, 2,3)
if self.object == 'cube':
self.make_cube(self.sizeObject * 0.3)
elif self.object == 'teapot':
self.make_teapot(self.sizeObject * 0.3)
elif self.object == 'sphere':
self.make_sphere(self.sizeObject * 0.3)
elif self.object == 'cylinder':
self.make_cylinder(self.sizeObject * 0.25, self.sizeObject * 3 * 0.25)
elif self.object == 'torus':
self.make_torus(self.sizeObject * 0.1, self.sizeObject * 2 * 0.25)
elif self.object == 'cone':
self.make_cone(self.sizeObject * 0.3, self.sizeObject * 2 * 0.3)
elif self.object == 'pyramid':
self.make_pyramid(self.sizeObject * 0.4, self.sizeObject * 0.5)
elif self.object == 'truncated cone':
self.make_truncatedCone(self.sizeObject * 0.3, self.sizeObject * 0.6, self.sizeObject * 0.6)
glPopMatrix()
glFlush()
def tkResize(self, evt):
self.width, self.height = evt.width, evt.height
if self.height > 0:
self.asp.set(self.width/self.height)
else:
self.asp.set(1)
if self.winfo_ismapped():
glViewport(0, 0, self.width, self.height)
self.initgl()