-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprite_renderer.py
71 lines (61 loc) · 2.53 KB
/
sprite_renderer.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
import glm
from OpenGL.GL import *
from texture2d import Texture2D
from shader import Shader
class SpriteRenderer:
def __init__(self, shader:Shader)->None:
self.shader = shader
self.initRenderData()
# Renders a defined quad textured with given sprite
def drawSprite(self, texture:Texture2D, position:glm.vec2, size:glm.vec2, rotate:float, color=glm.vec3(1.0))->None:
# prepare transformations
self.shader.use()
model = glm.mat4(1.0)
# first translate (transformations are: scale happens first, then rotation, and then final translation happens; reversed order)
model = glm.translate(model, glm.vec3(position, 0.0))
# move origin of rotation to center of quad
model = glm.translate(model, glm.vec3(0.5 * size.x, 0.5 * size.y, 0.0))
# then rotate
model = glm.rotate(model, glm.radians(rotate), glm.vec3(0.0, 0.0, 1.0))
# move origin back
model = glm.translate(model, glm.vec3(-0.5 * size.x, -0.5 * size.y, 0.0))
# last scale
model = glm.scale(model, glm.vec3(size, 1.0))
self.shader.setMatrix4("model", model)
# render textured quad
self.shader.setVec3("spriteColor", color)
texture.bind(0)
glBindVertexArray(self.vao)
glDrawArrays(GL_TRIANGLES, 0, 6)
# Initializes and configures the quad's buffer and vertex attributes
def initRenderData(self)->None:
# configure VAO/VBO
# vertices = np.array([
# # pos # tex
# 0.0, 1.0, 0.0, 1.0,
# 1.0, 0.0, 1.0, 0.0,
# 0.0, 0.0, 0.0, 0.0,
# 0.0, 1.0, 0.0, 1.0,
# 1.0, 1.0, 1.0, 1.0,
# 1.0, 0.0, 1.0, 0.0
# ], dtype=GLfloat)
vertices = [
# pos # tex
0.0, 1.0, 0.0, 1.0,
1.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 1.0,
1.0, 1.0, 1.0, 1.0,
1.0, 0.0, 1.0, 0.0
]
ctype_vertices = (GLfloat * len(vertices))(*vertices)
self.vao = GLuint()
glCreateVertexArrays(1, self.vao)
vbo = GLuint()
glCreateBuffers(1, vbo)
glNamedBufferStorage(vbo, ctypes.sizeof(ctype_vertices), ctype_vertices,
GL_DYNAMIC_STORAGE_BIT)
glVertexArrayVertexBuffer(self.vao, 0, vbo, 0, 4 * sizeof(GLfloat))
glVertexArrayAttribFormat(self.vao, 0, 4, GL_FLOAT, GL_FALSE, 0)
glVertexArrayAttribBinding(self.vao, 0, 0)
glEnableVertexArrayAttrib(self.vao, 0)