source: framspy/gui/visual/entityRenderer.py @ 1198

Last change on this file since 1198 was 1198, checked in by Maciej Komosinski, 15 months ago

Added simple Python GUI for Framsticks library/server

File size: 2.4 KB
Line 
1from gui.visual.staticShader import StaticShader
2from gui.visual.entity import Entity
3import OpenGL.GL as gl
4import glm
5
6class EntityRenderer:
7        def __init__(self, staticShader: StaticShader, projectionMatrix) -> None:
8                self.shader = staticShader
9                self.shader.start()
10                self.loadProjectionMatrix(projectionMatrix)
11
12        def render(self, entities: dict, textures: bool):
13                for item in entities.items():
14                        self.prepareModelData(item[0], textures)
15                        batch = item[1]
16                        for entity in batch:
17                                self.prepareInstance(entity)
18                                gl.glDrawElements(gl.GL_TRIANGLES, item[0].rawModel.vertexCount, gl.GL_UNSIGNED_INT, None)
19                        self.unbindModelData()
20
21        def prepareModelData(self, model, textures: bool):
22                rawModel = model.rawModel
23                gl.glBindVertexArray(rawModel.vaoID)
24                gl.glEnableVertexAttribArray(0)
25                gl.glEnableVertexAttribArray(1)
26                gl.glEnableVertexAttribArray(2)
27
28                self.shader.loadTextureOn(textures)
29
30                if textures:
31                        gl.glActiveTexture(gl.GL_TEXTURE0)
32                        gl.glBindTexture(gl.GL_TEXTURE_2D, model.texture.id)
33                else:
34                        gl.glBindTexture(gl.GL_TEXTURE_2D, 0)
35                        gl.glDisable(gl.GL_TEXTURE_2D)
36
37        def unbindModelData(self):
38                gl.glDisableVertexAttribArray(0)
39                gl.glDisableVertexAttribArray(1)
40                gl.glDisableVertexAttribArray(2)
41                gl.glBindVertexArray(0)
42
43        def createTransformationMatrix(self, entity: Entity):
44                matrix = glm.mat4(1)
45                matrix = glm.translate(matrix, entity.position)
46
47                if entity.angle is not None:
48                        matrix = glm.rotate(matrix, entity.angle, entity.vector)
49                elif entity.rotMatrix is not None:
50                        matrix = matrix * entity.rotMatrix
51                else:
52                        matrix = glm.rotate(matrix, entity.rotX, glm.vec3(1, 0, 0))
53                        matrix = glm.rotate(matrix, entity.rotY, glm.vec3(0, 1, 0))
54                        matrix = glm.rotate(matrix, entity.rotZ, glm.vec3(0, 0, 1))
55
56                if entity.scaleX is not None and entity.scaleY is not None and entity.scaleZ is not None:
57                        matrix = glm.scale(matrix, glm.vec3(entity.scaleX, entity.scaleY, entity.scaleZ))
58                else:
59                        matrix = glm.scale(matrix, glm.vec3(entity.scale))
60
61                return matrix
62
63        def prepareInstance(self, entity: Entity):
64                transformationMatrix = self.createTransformationMatrix(entity)
65                self.shader.loadTransformationMatrix(transformationMatrix)
66
67                if entity.color:
68                        self.shader.loadColor(entity.color)
69
70        def loadProjectionMatrix(self, projectionMatrix):
71                self.shader.loadProjectionMatrix(projectionMatrix)
Note: See TracBrowser for help on using the repository browser.