-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathrender_utility.py
56 lines (43 loc) · 2.14 KB
/
render_utility.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
from enum import Enum
from typing import Callable, List, Optional, Union
from OpenGL.constant import (Constant, FloatConstant, IntConstant,
LongConstant, StringConstant)
from OpenGL.GL import (GL_ALL_BARRIER_BITS, GL_BLEND, GL_COLOR_BUFFER_BIT,
GL_DEPTH_BUFFER_BIT, GL_DEPTH_TEST, GL_MAX, GL_MIN,
GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA,
glBlendEquationSeparate, glBlendFunc, glClear,
glClearColor, glDisable, glDrawArrays,
glDrawArraysInstanced, glEnable, glLineWidth,
glMemoryBarrier, glPointSize)
def clear_screen(clear_color: List[float]) -> None:
glClearColor(clear_color[0], clear_color[1],
clear_color[2], clear_color[3])
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
class OGLRenderFunction(Enum):
ARRAYS = 1
ARRAYS_INSTANCED = 2
def generate_render_function(ogl_func: OGLRenderFunction,
primitive: Union[FloatConstant, IntConstant, LongConstant, StringConstant, Constant],
point_size: Optional[float] = None, line_width: Optional[float] = None, add_blending: bool = False,
depth_test: bool = False) -> Callable:
def render_func(element_count: int) -> None:
if add_blending:
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glBlendEquationSeparate(GL_MIN, GL_MAX)
else:
glDisable(GL_BLEND)
if depth_test:
glEnable(GL_DEPTH_TEST)
else:
glDisable(GL_DEPTH_TEST)
if point_size is not None:
glPointSize(point_size)
if line_width is not None:
glLineWidth(line_width)
if ogl_func is OGLRenderFunction.ARRAYS:
glDrawArrays(primitive, 0, element_count)
elif ogl_func is OGLRenderFunction.ARRAYS_INSTANCED:
glDrawArraysInstanced(primitive, 0, 1, element_count)
glMemoryBarrier(GL_ALL_BARRIER_BITS)
return render_func