1 | from gui.visual.loader import Loader
|
---|
2 | from gui.visual.worldShader import WorldShader
|
---|
3 | from gui.visual.staticShader import StaticShader
|
---|
4 | from gui.visual.camera import Camera
|
---|
5 | from gui.visual.objLoader import OBJLoader
|
---|
6 | import OpenGL.GL as gl
|
---|
7 | from typing import Tuple, List
|
---|
8 | import numpy as np
|
---|
9 | import re
|
---|
10 | import glm
|
---|
11 |
|
---|
12 | class WorldRenderer:
|
---|
13 | WORLD_Z_OFFSET = -0.2
|
---|
14 |
|
---|
15 | def __init__(self, loader: Loader, projectionMatrix, staticShader: StaticShader) -> None:
|
---|
16 | self.shader = WorldShader()
|
---|
17 | self._createWater(loader)
|
---|
18 | self.staticShader: StaticShader = staticShader
|
---|
19 |
|
---|
20 | self.fence = OBJLoader.loadOBJ("gui/res/obj/fence-element.obj", "gui/res/img/wood.png", loader)
|
---|
21 | self.teleport = OBJLoader.loadOBJ("gui/res/obj/teleport.obj", "gui/res/img/teleportglow.png", loader)
|
---|
22 |
|
---|
23 | self.boundary: List[Tuple[glm.vec3, float]] = []
|
---|
24 | self.worldType = 0
|
---|
25 | self.worldSize = 20
|
---|
26 | self.worldBoundaries = 0
|
---|
27 | self.z_offset = 0
|
---|
28 |
|
---|
29 | self.shader.start()
|
---|
30 | self.loadProjectionMatrix(projectionMatrix)
|
---|
31 | self.shader.stop()
|
---|
32 |
|
---|
33 | def render(self, camera: Camera):
|
---|
34 | if hasattr(self, "plane"):
|
---|
35 | self.shader.start()
|
---|
36 | self.shader.loadViewMatrix(camera)
|
---|
37 | self.shader.loadMode(0)
|
---|
38 | gl.glEnable(gl.GL_BLEND)
|
---|
39 | gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
|
---|
40 |
|
---|
41 | #render world plane
|
---|
42 | gl.glBindVertexArray(self.plane.vaoID)
|
---|
43 | gl.glEnableVertexAttribArray(0)
|
---|
44 | gl.glDrawArrays(gl.GL_TRIANGLES, 0, int(self.plane.vertexCount))
|
---|
45 | gl.glDisableVertexAttribArray(0)
|
---|
46 | gl.glBindVertexArray(0)
|
---|
47 |
|
---|
48 | #render water plane
|
---|
49 | gl.glBindVertexArray(self.water.vaoID)
|
---|
50 | gl.glEnableVertexAttribArray(0)
|
---|
51 | gl.glEnableVertexAttribArray(1)
|
---|
52 | self.shader.loadMode(1)
|
---|
53 | gl.glDrawArrays(gl.GL_TRIANGLES, 0, int(self.water.vertexCount))
|
---|
54 | gl.glDisableVertexAttribArray(0)
|
---|
55 | gl.glBindVertexArray(0)
|
---|
56 | gl.glBindVertexArray(1)
|
---|
57 |
|
---|
58 | gl.glDisable(gl.GL_BLEND)
|
---|
59 |
|
---|
60 | #render bounds
|
---|
61 | self.renderBounds()
|
---|
62 |
|
---|
63 | self.shader.stop()
|
---|
64 |
|
---|
65 | def loadProjectionMatrix(self, projectionMatrix):
|
---|
66 | self.shader.loadProjectionMatrix(projectionMatrix)
|
---|
67 |
|
---|
68 | def reloadWorld(self, loader: Loader, worldType: int, simType: int, worldSize: float, worldMap: str, worldBoundaries: int, worldWaterLevel: float):
|
---|
69 | self.worldType = worldType
|
---|
70 | self.worldSize = worldSize
|
---|
71 | self.worldBoundaries = worldBoundaries
|
---|
72 |
|
---|
73 | if simType == 0:
|
---|
74 | self.z_offset = self.WORLD_Z_OFFSET
|
---|
75 | else:
|
---|
76 | self.z_offset = 0
|
---|
77 |
|
---|
78 | v = self._loadQuads(worldMap)
|
---|
79 | self.plane = loader.loadToVao1(v, len(v) / 3)
|
---|
80 | self.shader.start()
|
---|
81 | self.shader.loadWorldSize(worldSize)
|
---|
82 | self.shader.loadWaterLevel(worldWaterLevel + self.z_offset)
|
---|
83 | self.shader.stop()
|
---|
84 |
|
---|
85 | if worldBoundaries == 1:
|
---|
86 | self._addFence()
|
---|
87 | elif worldBoundaries == 2:
|
---|
88 | self._addTeleport()
|
---|
89 |
|
---|
90 | def _createBlock(self, p1: Tuple[float, float, float], p2: Tuple[float, float, float],
|
---|
91 | p3: Tuple[float, float, float], p4: Tuple[float, float, float]) -> List[float]:
|
---|
92 | vertices = []
|
---|
93 | vertices.append(p1[0])
|
---|
94 | vertices.append(p1[1])
|
---|
95 | vertices.append(p1[2])
|
---|
96 |
|
---|
97 | vertices.append(p2[0])
|
---|
98 | vertices.append(p2[1])
|
---|
99 | vertices.append(p2[2])
|
---|
100 |
|
---|
101 | vertices.append(p3[0])
|
---|
102 | vertices.append(p3[1])
|
---|
103 | vertices.append(p3[2])
|
---|
104 |
|
---|
105 | vertices.append(p1[0])
|
---|
106 | vertices.append(p1[1])
|
---|
107 | vertices.append(p1[2])
|
---|
108 |
|
---|
109 | vertices.append(p3[0])
|
---|
110 | vertices.append(p3[1])
|
---|
111 | vertices.append(p3[2])
|
---|
112 |
|
---|
113 | vertices.append(p4[0])
|
---|
114 | vertices.append(p4[1])
|
---|
115 | vertices.append(p4[2])
|
---|
116 |
|
---|
117 | return vertices
|
---|
118 |
|
---|
119 | def _createColorsForBlock(self, color: Tuple[float, float, float]) -> List[float]:
|
---|
120 | return [color[0], color[1], color[2]] * 6
|
---|
121 |
|
---|
122 | def _createWater(self, loader: Loader):
|
---|
123 | vertices = self._createBlock((0,0,-0.01), (1,0,-0.01), (1,1,-0.01), (0,1,-0.01))
|
---|
124 | colors = self._createColorsForBlock((0,0,1))
|
---|
125 |
|
---|
126 | self.water = loader.loadToVao2(vertices, colors, len(vertices) / 3)
|
---|
127 |
|
---|
128 | def _loadQuads(self, a: str):
|
---|
129 | points = []
|
---|
130 | indices = []
|
---|
131 | lines = a.splitlines()
|
---|
132 | self.primitive = None
|
---|
133 |
|
---|
134 | for line in lines:
|
---|
135 | if line[0] == 'v':
|
---|
136 | v = line.split()
|
---|
137 | points.append((float(v[1]), float(v[2]), float(v[3]) + self.z_offset))
|
---|
138 | elif line[0] == 'f':
|
---|
139 | f = line.split()
|
---|
140 | if len(f) == 4:
|
---|
141 | indices.append(int(f[1]))
|
---|
142 | indices.append(int(f[2]))
|
---|
143 | indices.append(int(f[3]))
|
---|
144 | elif len(f) == 5:
|
---|
145 | indices.append(int(f[1]))
|
---|
146 | indices.append(int(f[2]))
|
---|
147 | indices.append(int(f[3]))
|
---|
148 |
|
---|
149 | indices.append(int(f[1]))
|
---|
150 | indices.append(int(f[3]))
|
---|
151 | indices.append(int(f[4]))
|
---|
152 |
|
---|
153 | vertices = []
|
---|
154 | for i in indices:
|
---|
155 | vertices.append(points[i-1][0])
|
---|
156 | vertices.append(points[i-1][1])
|
---|
157 | vertices.append(points[i-1][2])
|
---|
158 |
|
---|
159 | return vertices
|
---|
160 |
|
---|
161 | def renderBounds(self):
|
---|
162 | if self.worldBoundaries == 1:
|
---|
163 | self.staticShader.start()
|
---|
164 | gl.glBindVertexArray(self.fence.rawModel.vaoID)
|
---|
165 | gl.glEnableVertexAttribArray(0)
|
---|
166 | gl.glEnableVertexAttribArray(1)
|
---|
167 | gl.glActiveTexture(gl.GL_TEXTURE0)
|
---|
168 | gl.glBindTexture(gl.GL_TEXTURE_2D, self.fence.texture.id)
|
---|
169 | self.staticShader.loadTextureOn(True)
|
---|
170 |
|
---|
171 | for b in self.boundary:
|
---|
172 | matrix = glm.mat4(1)
|
---|
173 | matrix = glm.translate(matrix, b[0])
|
---|
174 | matrix = glm.rotate(matrix, 3.14/2, glm.vec3(1,0,0))
|
---|
175 | matrix = glm.rotate(matrix, b[1], glm.vec3(0,1,0))
|
---|
176 | self.staticShader.loadTransformationMatrix(matrix)
|
---|
177 | gl.glDrawArrays(gl.GL_TRIANGLES, 0, int(self.fence.rawModel.vertexCount))
|
---|
178 |
|
---|
179 | self.staticShader.stop()
|
---|
180 | elif self.worldBoundaries == 2:
|
---|
181 | self.staticShader.start()
|
---|
182 | gl.glBindVertexArray(self.teleport.rawModel.vaoID)
|
---|
183 | gl.glEnableVertexAttribArray(0)
|
---|
184 | gl.glEnableVertexAttribArray(1)
|
---|
185 | gl.glActiveTexture(gl.GL_TEXTURE0)
|
---|
186 | gl.glBindTexture(gl.GL_TEXTURE_2D, self.teleport.texture.id)
|
---|
187 |
|
---|
188 | for b in self.boundary:
|
---|
189 | matrix = glm.mat4(1)
|
---|
190 | matrix = glm.translate(matrix, b[0])
|
---|
191 | matrix = glm.rotate(matrix, 3.14/2, glm.vec3(1,0,0))
|
---|
192 | matrix = glm.rotate(matrix, b[1], glm.vec3(0,1,0))
|
---|
193 | self.staticShader.loadTransformationMatrix(matrix)
|
---|
194 | gl.glDrawArrays(gl.GL_TRIANGLES, 0, int(self.teleport.rawModel.vertexCount))
|
---|
195 |
|
---|
196 | self.staticShader.stop()
|
---|
197 |
|
---|
198 | gl.glDisableVertexAttribArray(0)
|
---|
199 | gl.glDisableVertexAttribArray(1)
|
---|
200 | gl.glBindVertexArray(0)
|
---|
201 |
|
---|
202 | def _addFence(self):
|
---|
203 | i = 0
|
---|
204 | self.boundary.clear()
|
---|
205 | while i < self.worldSize / 2:
|
---|
206 | self.boundary.append((glm.vec3(i * 2, 0, self.z_offset), 0))
|
---|
207 | self.boundary.append((glm.vec3(i * 2, self.worldSize, self.z_offset), 0))
|
---|
208 | self.boundary.append((glm.vec3(0, i * 2, self.z_offset), glm.radians(90)))
|
---|
209 | self.boundary.append((glm.vec3(self.worldSize, i * 2, self.z_offset), glm.radians(90)))
|
---|
210 | i += 1
|
---|
211 |
|
---|
212 | def _addTeleport(self):
|
---|
213 | i = 0
|
---|
214 | self.boundary.clear()
|
---|
215 | while i < self.worldSize / 3:
|
---|
216 | self.boundary.append((glm.vec3(i * 3, 0, self.z_offset), 0))
|
---|
217 | self.boundary.append((glm.vec3(i * 3, self.worldSize, self.z_offset), 0))
|
---|
218 | self.boundary.append((glm.vec3(0, i * 3, self.z_offset), glm.radians(-90)))
|
---|
219 | self.boundary.append((glm.vec3(self.worldSize, i * 3, self.z_offset), glm.radians(-90)))
|
---|
220 | i += 1
|
---|