파이썬[Python]: kivy, OpenGL ES - 육면체 그리기

kivy.graphics 모듈 - OpenGL ES


/// 설명

육면체를 그립니다.

참고: OpenGL ES 모듈 목록

reference
https://www.youtube.com/playlist?list=PL1P11yPQAo7qEnF_EysHOBUfF0AzUz3jh

/// 예제 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
import kivy
 
kivy.require('2.0.0')
 
from kivy.app import App
from kivy.core.window import Window
from kivy.resources import resource_find
from kivy.graphics import *
from kivy.graphics.opengl import *
from kivy.graphics.transformation import Matrix
from kivy.uix.widget import Widget
 
Window.clearcolor = (1111)
Window.size = (5631001)
Window.top, Window.left = 302900
 
#           positions          colors
vertices = [-0.5-0.5,  0.5,  1.00.00.0,
             0.5-0.5,  0.5,  0.01.00.0,
             0.5,  0.5,  0.5,  0.00.01.0,
            -0.5,  0.5,  0.5,  1.01.01.0,
 
            -0.5-0.5-0.5,  1.00.00.0,
             0.5-0.5-0.5,  0.01.00.0,
             0.5,  0.5-0.5,  0.00.01.0,
            -0.5,  0.5-0.5,  1.01.01.0]
 
indices = [012230,
           456674,
           451104,
           673326,
           562215,
           740037]
 
fmt = [(b'pos'3'float'),   # position
       (b'color'3'float')] # color
 
 
class FantasticApp(App):
    _angle = 0.0174533 * 45  # 1 degree = 0.0174533 radian
 
    def scene(self):
        PushMatrix()
        Mesh(vertices=vertices, indices=indices,
             mode='triangles', fmt=fmt)
        PopMatrix()
 
    def build(self):
        self.w = w = Widget()
 
        w.canvas = RenderContext()
        w.canvas.shader.source = resource_find('./cube.glsl')
 
        with w.canvas:
            self.scene()
 
        self.m = Matrix()  # create new matrix
        w.canvas['transform'= self.m  # using uniform mat4 transform in glsl
        glEnable(GL_DEPTH_TEST)
 
        self.m.rotate(angle=self._angle, x=1, y=1, z=0)  # rotate matrix
        #self.m.scale(x=0.5, y=0.5, z=0.5)  # scale matrix
        #self.m.translate(x=0.2, y=0.2, z=0.2)  # translate matrix
 
        return w
 
 
FantasticApp().run()
 
cs

/// 예제 cube.glsl

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
---VERTEX SHADER
#ifdef GL_ES
    precision highp float;
#endif
 
attribute vec3 pos;
attribute vec3 color;
 
uniform mat4 transform;
 
varying vec4 newColor;
 
void main(void) {
    gl_Position = transform * vec4(pos, 1.0);
    newColor = vec4(color, 1.0);
}
 
---FRAGMENT SHADER
#ifdef GL_ES
    precision highp float;
#endif
 
varying vec4 newColor;
 
void main(void) {
    gl_FragColor = newColor;
}
cs

/// 출력


* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

파이썬[Python]: 내장함수 - bit_length 메서드

C 언어: sin 함수, cos 함수, tan 함수