-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneOne.py
More file actions
124 lines (106 loc) · 5 KB
/
Copy pathSceneOne.py
File metadata and controls
124 lines (106 loc) · 5 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
"""
Define a fixed scene with rotating lights
First version in 11/08/2021
:author: micou(Zezhou Sun)
:version: 2021.1.1
"""
import math
import numpy as np
import ColorType
from Animation import Animation
from SceneType import Scene
from Component import Component
from Light import Light
from Material import Material
from Point import Point
from PIL import Image
import GLUtility
from DisplayableCube import DisplayableCube
from DisplayableSphere import DisplayableSphere
from DisplayableTorus import DisplayableTorus
from DisplayableCylinder import DisplayableCylinder
class SceneOne(Scene, Animation):
lRadius = None
lAngles = None
lTransformations = None
def __init__(self, shaderProg):
Scene.__init__(self, shaderProg)
self.shaderProg = shaderProg
self.glutility = GLUtility.GLUtility()
self.lTransformations = [self.glutility.translate(0, 2, 0, False),
self.glutility.rotate(60, [0, 0, 1], False),
self.glutility.rotate(120, [0, 0, 1], False)]
self.lRadius = 3
self.lAngles = [0, 0, 0]
sphere = Component(Point((-1, 0, 0)), DisplayableSphere(shaderProg, 1.0))
m1 = Material(np.array((0.1, 0.1, 0.1, 0.1)), np.array((0.5, 0.5, 0.5, 1)),
np.array((0.8, 0.8, 0.8, 0.1)), 16)
sphere.setDefaultAngle(-90, sphere.uAxis)
sphere.setDefaultAngle(180, sphere.wAxis)
sphere.setMaterial(m1)
sphere.setTexture(self.shaderProg, 'assets/earth.jpg')
sphere.renderingRouting = "lighting_texture"
sphere.setNormalMap(self.shaderProg, 'assets/earth_normal.png')
self.addChild(sphere)
torus = Component(Point((1, 0, 0)), DisplayableTorus(shaderProg, 0.25, 0.5, 36, 36))
m2 = Material(np.array((0.1, 0.1, 0.1, 0.1)), np.array((0.2, 0.2, 0.2, 1)),
np.array((0, 0, 0, 1.0)), 64)
torus.setMaterial(m2)
torus.renderingRouting = "lighting_texture"
torus.rotate(90, torus.uAxis)
torus.setTexture(self.shaderProg, 'assets/marble.jpg')
torus.setNormalMap(self.shaderProg, 'assets/normalmap.jpg')
self.addChild(torus)
cylinder = Component(Point((1, 0, -2)), DisplayableCylinder(shaderProg, 0.3, 0.7, 1, 36))
m3 = Material(np.array((0.1, 0.1, 0.1, 0.1)), np.array((0.2, 0.2, 0.2, 1)),
np.array((1, 1, 1, 1.0)), 16)
cylinder.setMaterial(m3)
cylinder.renderingRouting = "lighting"
cylinder.rotate(90, torus.uAxis)
self.addChild(cylinder)
def turn_on(component):
component.renderingRouting = "vertex"
def turn_off(component):
component.renderingRouting = "pure"
l0 = Light(self.lightPos(self.lRadius, self.lAngles[0], self.lTransformations[0]),
np.array((*ColorType.SOFTRED, 1.0)))
lightCube0 = Component(Point((0, 0, 0)), DisplayableCube(shaderProg, 0.1, 0.1, 0.1, ColorType.SOFTRED))
lightCube0.renderingRouting = "vertex"
lightCube0.turn_on = turn_on
lightCube0.turn_off = turn_off
l1 = Light(self.lightPos(self.lRadius, self.lAngles[1], self.lTransformations[1]),
np.array((*ColorType.SOFTBLUE, 1.0)))
lightCube1 = Component(Point((0, 0, 0)), DisplayableCube(shaderProg, 0.1, 0.1, 0.1, ColorType.SOFTBLUE))
lightCube1.renderingRouting = "vertex"
lightCube1.turn_on = turn_on
lightCube1.turn_off = turn_off
l2 = Light(self.lightPos(self.lRadius, self.lAngles[2], self.lTransformations[2]),
np.array((*ColorType.SOFTGREEN, 1.0)))
lightCube2 = Component(Point((0, 0, 0)), DisplayableCube(shaderProg, 0.1, 0.1, 0.1, ColorType.SOFTGREEN))
lightCube2.renderingRouting = "vertex"
lightCube2.turn_on = turn_on
lightCube2.turn_off = turn_off
self.addChild(lightCube0)
self.addChild(lightCube1)
self.addChild(lightCube2)
self.lights = [l0, l1, l2]
self.lightCubes = [lightCube0, lightCube1, lightCube2]
def lightPos(self, radius, thetaAng, transformationMatrix):
r = np.zeros(4)
r[0] = radius * math.cos(thetaAng / 180 * math.pi)
r[2] = radius * math.sin(thetaAng / 180 * math.pi)
r[3] = 1
r = transformationMatrix @ r
return r[0:3]
def animationUpdate(self):
self.lAngles[0] = (self.lAngles[0] + 0.5) % 360
self.lAngles[1] = (self.lAngles[1] + 0.7) % 360
self.lAngles[2] = (self.lAngles[2] + 1.0) % 360
for i, v in enumerate(self.lights):
lPos = self.lightPos(self.lRadius, self.lAngles[i], self.lTransformations[i])
self.lightCubes[i].setCurrentPosition(Point(lPos))
self.lights[i].setPosition(lPos)
self.shaderProg.setLight(i, v)
for c in self.children:
if isinstance(c, Animation):
c.animationUpdate()