-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
164 lines (136 loc) · 4.97 KB
/
main.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
import pyglet
from pyglet import image
from defines import *
from geometry import *
import device
import camera
import light
import rtmath
import base
"""
3D流水线:物体->多边形->顶点->变换->光栅化->屏幕
1. MVP变换
2. 背面剔除
3. 屏幕映射
4. 光栅化
a. 剔除屏幕外的点
b. 纹理映射
c. 光照
d. 深度剔除
规定:
1. 左手系,观察空间右手系(与Unity相同)
2. 右乘、列矩阵
"""
g_fDegree = 0
# @base.DocProfile("./cpf_run.prof")
lVertex = [
# 上
CVertex(vector([1, 1, 1, 1]), vector([0, 1, 0, 0]), vector([0, 0])),
CVertex(vector([-1, 1, 1, 1]), vector([0, 1, 0, 0]), vector([1, 0])),
CVertex(vector([1, 1, -1, 1]), vector([0, 1, 0, 0]), vector([0, 1])),
CVertex(vector([1, 1, -1, 1]), vector([0, 1, 0, 0]), vector([0, 1])),
CVertex(vector([-1, 1, 1, 1]), vector([0, 1, 0, 0]), vector([1, 0])),
CVertex(vector([-1, 1, -1, 1]), vector([0, 1, 0, 0]), vector([1, 1])),
# 下
CVertex(vector([1, -1, 1, 1]), vector([0, -1, 0, 0]), vector([1, 0])),
CVertex(vector([1, -1, -1, 1]), vector([0, -1, 0, 0]), vector([1, 1])),
CVertex(vector([-1, -1, 1, 1]), vector([0, -1, 0, 0]), vector([0, 0])),
CVertex(vector([-1, -1, 1, 1]), vector([0, -1, 0, 0]), vector([0, 0])),
CVertex(vector([1, -1, -1, 1]), vector([0, -1, 0, 0]), vector([1, 1])),
CVertex(vector([-1, -1, -1, 1]), vector([0, -1, 0, 0]), vector([0, 1])),
# 前
CVertex(vector([-1, 1, 1, 1]), vector([-1, 0, 0, 0]), vector([0, 1])),
CVertex(vector([-1, -1, 1, 1]), vector([-1, 0, 0, 0]), vector([0, 0])),
CVertex(vector([-1, -1, -1, 1]), vector([-1, 0, 0, 0]), vector([1, 0])),
CVertex(vector([-1, 1, 1, 1]), vector([-1, 0, 0, 0]), vector([0, 1])),
CVertex(vector([-1, -1, -1, 1]), vector([-1, 0, 0, 0]), vector([1, 0])),
CVertex(vector([-1, 1, -1, 1]), vector([-1, 0, 0, 0]), vector([1, 1])),
# 后
CVertex(vector([1, 1, 1, 1]), vector([1, 0, 0, 0]), vector([1, 1])),
CVertex(vector([1, -1, -1, 1]), vector([1, 0, 0, 0]), vector([0, 0])),
CVertex(vector([1, -1, 1, 1]), vector([1, 0, 0, 0]), vector([1, 0])),
CVertex(vector([1, 1, 1, 1]), vector([1, 0, 0, 0]), vector([1, 1])),
CVertex(vector([1, 1, -1, 1]), vector([1, 0, 0, 0]), vector([0, 1])),
CVertex(vector([1, -1, -1, 1]), vector([1, 0, 0, 0]), vector([0, 0])),
# 左
CVertex(vector([1, 1, 1, 1]), vector([0, 0, 1, 0]), vector([0, 1])),
CVertex(vector([1, -1, 1, 1]), vector([0, 0, 1, 0]), vector([0, 0])),
CVertex(vector([-1, -1, 1, 1]), vector([0, 0, 1, 0]), vector([1, 1])),
CVertex(vector([1, 1, 1, 1]), vector([0, 0, 1, 0]), vector([0, 1])),
CVertex(vector([-1, -1, 1, 1]), vector([0, 0, 1, 0]), vector([1, 1])),
CVertex(vector([-1, 1, 1, 1]), vector([0, 0, 1, 0]), vector([1, 0])),
# 右
CVertex(vector([1, -1, -1, 1]), vector([0, 0, -1, 0]), vector([1, 0])),
CVertex(vector([1, 1, -1, 1]), vector([0, 0, -1, 0]), vector([1, 1])),
CVertex(vector([-1, 1, -1, 1]), vector([0, 0, -1, 0]), vector([0, 1])),
CVertex(vector([1, -1, -1, 1]), vector([0, 0, -1, 0]), vector([1, 0])),
CVertex(vector([-1, 1, -1, 1]), vector([0, 0, -1, 0]), vector([0, 1])),
CVertex(vector([-1, -1, -1, 1]), vector([0, 0, -1, 0]), vector([0, 0])),
]
def main():
oGameWindow = pyglet.window.Window(I_WIDTH, I_HEIGHT)
# 顶点
# 索引
lIndice = [
[0, 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],
]
# lVertex = [
# CVertex(vector([0, 2, 0, 1]), vector([1, 0, 0, 0]), vector([0, 0])),
# CVertex(vector([0, 0, 2, 1]), vector([1, 0, 0, 0]), vector([1, 0])),
# CVertex(vector([0, -2, 0, 1]), vector([1, 0, 0, 0]), vector([0, 1])),
# ]
#
# lIndice = [
# [0, 1, 2],
# ]
# 纹理(棋盘格)
mTexture = np.ones((256, 256, 4), dtype="uint8")
grid_size = 32
for i in range(8):
# 每隔1个格子
for j in [x * 2 for x in range(4)]:
mTexture[i * grid_size: (i + 1) * grid_size, (j + i % 2) * grid_size: (j + i % 2 + 1) * grid_size, :] = vector([1. / 255, 128. / 255, 1, 1])
oDevice = device.CDevice(I_WIDTH, I_HEIGHT, mTexture)
oCameraMgr = camera.CMgr()
oCamera = oCameraMgr.GetCamera(camera.TYPE_NORMAL)
oCamera.SetEye(vector([-4, 0, 0, 1]))
oCamera.SetLookAt(vector([-2, 0, 0, 1]))
oCamera.SetUp(vector([0, 1, 0, 0]))
oCamera.SetAspect(1.0)
oCamera.SetFar(300.0)
oLight = light.CPointLight(1)
oLightMgr = light.CMgr()
oLightMgr.AddLight(oLight)
oFrame = image.create(I_WIDTH, I_HEIGHT)
@oGameWindow.event
def on_draw():
"""
每帧调用
"""
oGameWindow.clear()
oDevice.ClearFrameBuffer(vector([128., 33., 78., 1]))
oDevice.ClearZBuffer()
global g_fDegree
mModelTrans = rtmath.getRotateMatrix(vector([1, 1, 1, 0]), g_fDegree / np.pi * 180)
oDevice.SetModelTrans(mModelTrans)
g_fDegree += 0.0005
g_fDegree %= 180
oDevice.DrawMesh(lVertex, lIndice)
mFrameBuffer = oDevice.GetFramebuffer()
oFrame.set_data("RGBA", I_WIDTH * 4, mFrameBuffer.tostring())
oFrame.blit(0, 0)
pyglet.clock.schedule_interval(lambda dt: None, 1 / 60.0)
pyglet.app.run()
if __name__ == '__main__':
main()