-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain_pygame_background.py
177 lines (149 loc) · 5.15 KB
/
main_pygame_background.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
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import pygame
# import live2d.v3 as live2d
import live2d.v2 as live2d
import resources
import OpenGL.GL as GL
from PIL import Image
import numpy as np
def compile_shader(shader_src, shader_type):
shader = GL.glCreateShader(shader_type)
GL.glShaderSource(shader, shader_src)
GL.glCompileShader(shader)
status = GL.glGetShaderiv(shader, GL.GL_COMPILE_STATUS)
if not status:
msg = GL.glGetShaderInfoLog(shader)
raise RuntimeError(msg)
return shader
def create_program(vs, fs):
vertex_shader = compile_shader(vs, GL.GL_VERTEX_SHADER)
frag_shader = compile_shader(fs, GL.GL_FRAGMENT_SHADER)
program = GL.glCreateProgram()
GL.glAttachShader(program, vertex_shader)
GL.glAttachShader(program, frag_shader)
GL.glLinkProgram(program)
status = GL.glGetProgramiv(program, GL.GL_LINK_STATUS)
if not status:
msg = GL.glGetProgramInfoLog(program)
raise RuntimeError(msg)
return program
def create_vao(v_pos, uv_coord):
vao = GL.glGenVertexArrays(1)
vbo = GL.glGenBuffers(1)
uvbo = GL.glGenBuffers(1)
GL.glBindVertexArray(vao)
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo)
GL.glBufferData(GL.GL_ARRAY_BUFFER, v_pos.nbytes, v_pos, GL.GL_DYNAMIC_DRAW)
GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, False, 0, None)
GL.glEnableVertexAttribArray(0)
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, uvbo)
GL.glBufferData(GL.GL_ARRAY_BUFFER, uv_coord.nbytes, uv_coord, GL.GL_STATIC_DRAW)
GL.glVertexAttribPointer(1, 2, GL.GL_FLOAT, False, 0, None)
GL.glEnableVertexAttribArray(1)
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)
GL.glBindVertexArray(0)
return vao
def create_texture(imagePath: str):
image = Image.open(imagePath)
if image.mode != 'RGBA':
image = image.convert("RGBA")
image = image.transpose(Image.FLIP_TOP_BOTTOM)
image_data = image.tobytes()
width, height = image.size
GL.glEnable(GL.GL_TEXTURE_2D)
texture = GL.glGenTextures(1)
GL.glBindTexture(GL.GL_TEXTURE_2D, texture)
GL.glTexImage2D(
GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, width, height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, image_data
)
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_NEAREST)
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR)
GL.glGenerateMipmap(GL.GL_TEXTURE_2D)
GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
return texture
class Background:
def __init__(self, imagePath: str):
vertex_shader = """#version 330 core
layout(location = 0) in vec2 a_position;
layout(location = 1) in vec2 a_texCoord;
out vec2 v_texCoord;
void main() {
gl_Position = vec4(a_position, 0.0, 1.0);
v_texCoord = a_texCoord;
}
"""
frag_shader = """#version 330 core
in vec2 v_texCoord;
uniform sampler2D tex;
uniform float opacity;
void main() {
gl_FragColor = texture(tex, v_texCoord);
}
"""
self.program = create_program(vertex_shader, frag_shader)
self.texture = create_texture(imagePath)
vertices = np.array([
# 位置
-1, 1,
-1, -1,
1, -1,
-1, 1,
1, -1,
1, 1,
], dtype=np.float32)
uvs = np.array([
# 纹理坐标
0, 1,
0, 0,
1, 0,
0, 1,
1, 0,
1, 1
], dtype=np.float32)
self.vao = create_vao(vertices, uvs)
def Draw(self):
GL.glBindVertexArray(self.vao)
GL.glUseProgram(self.program)
GL.glActiveTexture(GL.GL_TEXTURE0)
GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
GL.glDrawArrays(GL.GL_TRIANGLES, 0, 6)
GL.glBindVertexArray(0)
def main():
pygame.init()
live2d.init()
display = (800, 600)
pygame.display.set_mode(display, pygame.DOUBLEBUF | pygame.OPENGL)
pygame.display.set_caption("pygame window")
live2d.glewInit()
model = live2d.LAppModel()
background = Background(
os.path.join(resources.RESOURCES_DIRECTORY, "RING.png")
)
if live2d.LIVE2D_VERSION == 3:
model.LoadModelJson(
os.path.join(resources.RESOURCES_DIRECTORY, "v3/Haru/Haru.model3.json")
)
else:
model.LoadModelJson(
os.path.join(resources.RESOURCES_DIRECTORY, "v2/kasumi2/kasumi2.model.json")
)
model.Resize(*display)
running = True
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
if event.type == pygame.MOUSEMOTION:
model.Drag(*pygame.mouse.get_pos())
if not running:
break
live2d.clearBuffer()
background.Draw()
model.Update()
model.Draw()
pygame.display.flip()
live2d.dispose()
pygame.quit()
if __name__ == "__main__":
main()