1 | from gui.visual.player import Player
|
---|
2 | from gui.visual.entity import Entity
|
---|
3 | import glm
|
---|
4 | import OpenGL.GL as gl
|
---|
5 | from gui.visual.camera import Camera
|
---|
6 | from gui.visual.staticShader import StaticShader
|
---|
7 | from gui.visual.entityRenderer import EntityRenderer
|
---|
8 | from gui.visual.skyboxRenderer import SkyboxRenderer
|
---|
9 | from gui.visual.worldRenderer import WorldRenderer
|
---|
10 |
|
---|
11 | class MasterRenderer:
|
---|
12 | FOV = 70
|
---|
13 | NEAR_PLANE = 0.1
|
---|
14 | FAR_PLANE = 17500 # max world size + some offset
|
---|
15 | INITIAL_WIDTH = 800
|
---|
16 | INITIAL_HEIGHT = 600
|
---|
17 |
|
---|
18 | def __init__(self, loader) -> None:
|
---|
19 | self._width = self.INITIAL_WIDTH
|
---|
20 | self._height = self.INITIAL_HEIGHT
|
---|
21 | self._createProjectionMatrix()
|
---|
22 | self._shader = StaticShader()
|
---|
23 | self._renderer = EntityRenderer(self._shader, self.projectionMatrix)
|
---|
24 | self._skyboxRenderer = SkyboxRenderer(loader, self.projectionMatrix)
|
---|
25 | self.worldRenderer = WorldRenderer(loader, self.projectionMatrix, self._shader)
|
---|
26 | self._entities = {}
|
---|
27 | self._isTexturesOn = True
|
---|
28 |
|
---|
29 | def renderScene(self, player: Player, entities: list, camera: Camera):
|
---|
30 | for entity in entities:
|
---|
31 | self.processEntity(entity)
|
---|
32 |
|
---|
33 | self.render(camera)
|
---|
34 |
|
---|
35 | def render(self, camera: Camera):
|
---|
36 | self.__prepare()
|
---|
37 | self._shader.start()
|
---|
38 | self._shader.loadViewMatrix(camera)
|
---|
39 | self._renderer.render(self._entities, self._isTexturesOn)
|
---|
40 | self.worldRenderer.render(camera)
|
---|
41 | self._skyboxRenderer.render(camera)
|
---|
42 | self._shader.stop()
|
---|
43 | self._entities.clear()
|
---|
44 |
|
---|
45 | def processEntity(self, entity: Entity):
|
---|
46 | entityModel = entity.modelData
|
---|
47 | batch = self._entities.get(entityModel)
|
---|
48 | if batch:
|
---|
49 | batch.append(entity)
|
---|
50 | else:
|
---|
51 | newBatch = [entity]
|
---|
52 | self._entities[entityModel] = newBatch
|
---|
53 |
|
---|
54 | def toggleTextures(self):
|
---|
55 | self._isTexturesOn = not self.isTexturesOn
|
---|
56 |
|
---|
57 | def cleanUp(self):
|
---|
58 | self._shader.cleanUp()
|
---|
59 |
|
---|
60 | def resize(self, width, height):
|
---|
61 | self._width = width
|
---|
62 | self._height = height
|
---|
63 | self._createProjectionMatrix()
|
---|
64 | self._shader.start()
|
---|
65 | self._renderer.loadProjectionMatrix(self.projectionMatrix)
|
---|
66 | self._shader.stop()
|
---|
67 | self._skyboxRenderer.shader.start()
|
---|
68 | self._skyboxRenderer.loadProjectionMatrix(self.projectionMatrix)
|
---|
69 | self._skyboxRenderer.shader.stop()
|
---|
70 | self.worldRenderer.shader.start()
|
---|
71 | self.worldRenderer.loadProjectionMatrix(self.projectionMatrix)
|
---|
72 | self.worldRenderer.shader.stop()
|
---|
73 |
|
---|
74 | def __prepare(self):
|
---|
75 | gl.glEnable(gl.GL_DEPTH_TEST)
|
---|
76 | gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
|
---|
77 | gl.glClearColor(0.0, 0.0, 0.0, 1)
|
---|
78 |
|
---|
79 | def _createProjectionMatrix(self):
|
---|
80 | self.projectionMatrix = glm.perspective(self.FOV, self._width / self._height, self.NEAR_PLANE, self.FAR_PLANE)
|
---|
81 |
|
---|
82 | def reloadWorld(self, loader, worldType: int, simType: int, worldSize: float, worldMap: str, worldBoundaries: int, worldWaterLevel: float) -> None:
|
---|
83 | self.worldRenderer.reloadWorld(loader, worldType, simType, worldSize, worldMap, worldBoundaries, worldWaterLevel) |
---|