-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-5-6-3.py
120 lines (99 loc) · 3.36 KB
/
test-5-6-3.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
# import standard library
import sys
# import third party library
# import local library
from core.base import Base, baseApp
from core.renderer import Renderer
from core.scene import Scene
from core.camera import Camera
from core.mesh import Mesh
from geometry.boxGeometry import BoxGeometry
from material.surfaceMaterial import SurfaceMaterial
from core.texture import Texture
from material.textureMaterial import TextureMaterial
from geometry.rectangleGeometry import RectangleGeometry
from material.material import Material
# render a basic scene
class Test(Base):
def __init__(self, screenSize=[512, 512], title=""):
super().__init__(screenSize, title)
def initializeGL(self):
super().initializeGL()
self.renderer = Renderer(self)
self.scene = Scene()
self.camera = Camera(aspectRatio=800 / 600)
self.camera.setPosition([0, 0, 1.5])
vsCode = """
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
in vec3 vertexPosition;
in vec2 vertexUV;
out vec2 UV;
void main()
{
vec4 pos = vec4(vertexPosition, 1.0);
gl_Position = projectionMatrix * viewMatrix * modelMatrix * pos;
UV = vertexUV;
}
"""
fsCode = """
// return a random value in [0, 1]
float random(vec2 UV)
{
return fract(235711.0 * sin(14.337*UV.x + 42.418*UV.y));
}
float boxRandom(vec2 UV, float scale)
{
vec2 iScaleUV = floor(scale * UV);
return random(iScaleUV);
}
float smoothRandom(vec2 UV, float scale)
{
vec2 iScaleUV = floor(scale * UV);
vec2 fScaleUV = fract(scale * UV);
float a = random(iScaleUV);
float b = random(round(iScaleUV + vec2(1, 0)));
float c = random(round(iScaleUV + vec2(0, 1)));
float d = random(round(iScaleUV + vec2(1, 1)));
return mix(mix(a, b, fScaleUV.x), mix(c, d, fScaleUV.x), fScaleUV.y);
}
// add smooth random values at different scales
// wighted (amplitudes) so that sum is approximately 1.0
float fractalRandom(vec2 UV, float scale)
{
float value = 0.0;
float amplitude = 0.5;
for (int i = 0; i < 6; i++)
{
value += amplitude * smoothRandom(UV, scale);
scale *= 2.0;
amplitude *= 0.5;
}
return value;
}
in vec2 UV;
out vec4 fragColor;
void main()
{
float r = fractalRandom(UV, 4);;
fragColor = vec4(r, r, r, 1);
}
"""
material = Material(vsCode, fsCode)
material.locateUniforms()
geometry = RectangleGeometry()
mesh = Mesh(geometry, material)
self.scene.add(mesh)
def paintGL(self):
super().paintGL()
# self.mesh.rotateY(0.0514)
# self.mesh.rotateX(0.0337)
self.renderer.render(self.scene, self.camera)
def main():
app = baseApp(sys.argv)
window = Test([800, 600], "Test-5-6-3")
window.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()