1
+ import math
1
2
import moderngl
2
3
3
4
from demosys .effects import effect
4
5
from demosys import geometry
5
6
from pyrr import matrix44
6
7
7
8
8
- class Empty (effect .Effect ):
9
+ def calculate_triangles (n : int ):
10
+ """
11
+ Calculate the number of triangles for the barycentric subdivision of a
12
+ single triangle (where the inner and outer subdivision is equal
13
+ """
14
+ if n < 0 :
15
+ return 1
16
+ if n == 0 :
17
+ return 0
18
+ return ((2 * n - 2 ) * 3 ) + calculate_triangles (n - 2 )
19
+
20
+
21
+ class TerrainTessellation (effect .Effect ):
9
22
10
23
def __init__ (self ):
11
24
self .terrain = geometry .plane_xz (size = (400 , 400 ), resolution = (8 , 8 ))
@@ -20,6 +33,12 @@ def __init__(self):
20
33
self .heights = self .program ['heightmap' ]
21
34
22
35
self .sys_camera .velocity = 50.0
36
+ self .tess_level = 64
37
+
38
+ original_triangles = 8 * 8 * 2
39
+ tess_triangles = calculate_triangles (self .tess_level )
40
+ print ("Number of triangles:" , original_triangles )
41
+ print ("Total triangles with tessellation:" , original_triangles * tess_triangles )
23
42
24
43
def draw (self , time , frametime , target ):
25
44
self .ctx .enable_only (moderngl .DEPTH_TEST )
@@ -29,15 +48,14 @@ def draw(self, time, frametime, target):
29
48
m_mv = matrix44 .multiply (m_mv , self .sys_camera .view_matrix )
30
49
31
50
self .ctx .patch_vertices = 3
32
- # self.ctx.wireframe = True
51
+ self .ctx .wireframe = True if math . fmod ( time , 10 ) < 5.0 else False
33
52
34
53
self .heightmap .use (location = 0 )
35
54
self .heights .value = 0
36
55
self .proj_matrix .write (m_proj .astype ('f4' ).tobytes ())
37
56
self .mv_matrix .write (m_mv .astype ('f4' ).tobytes ())
38
57
39
- tess_level = 64.0
40
- self .program ['TessLevelInner' ].value = tess_level
41
- self .program ['TessLevelOuter' ].value = tess_level
58
+ self .program ['TessLevelInner' ].value = self .tess_level
59
+ self .program ['TessLevelOuter' ].value = self .tess_level
42
60
43
61
self .terrain .render (self .program , mode = moderngl .PATCHES )
0 commit comments