forked from Schwarzbaer/RenderPipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
292 lines (221 loc) · 9.78 KB
/
main.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""
RenderPipeline testing file
If you are looking for Code Examples, look at Samples/. This file is for
testing purposes only, and also not very clean coded!
"""
# Don't generate .pyc files
import sys
import os
sys.dont_write_bytecode = True
import math
import struct
from direct.showbase.ShowBase import ShowBase
from panda3d.core import loadPrcFile, Vec3
from panda3d.core import Texture
from Code.MovementController import MovementController
from Code.RenderingPipeline import RenderingPipeline
from Code.PointLight import PointLight
from Code.DirectionalLight import DirectionalLight
from Code.BetterShader import BetterShader
from Code.DebugObject import DebugObject
from Code.FirstPersonController import FirstPersonCamera
from Code.GlobalIllumination import GlobalIllumination
class Main(ShowBase, DebugObject):
""" This is the render pipeline testing showbase """
def __init__(self):
DebugObject.__init__(self, "Main")
self.debug("Bit System =", 8 * struct.calcsize("P"))
# Load engine configuration
self.debug("Loading panda3d configuration from configuration.prc ..")
loadPrcFile("Config/configuration.prc")
# Init the showbase
ShowBase.__init__(self)
# Create the render pipeline
self.debug("Creating pipeline")
self.renderPipeline = RenderingPipeline(self)
# Uncomment to use temp directory
# writeDirectory = tempfile.mkdtemp(prefix='Shader-tmp')
# writeDirectory = "Temp/"
# Clear write directory when app exits
# atexit.register(os.remove, writeDirectory)
# Set a write directory, where the shader cache and so on is stored
# self.renderPipeline.getMountManager().setWritePath(writeDirectory)
self.renderPipeline.getMountManager().setBasePath(".")
####### END OF RENDER PIPELINE SETUP #######
# Load some demo source
# self.sceneSource = "Demoscene.ignore/sponza.egg.bam"
# self.sceneSource = "Demoscene.ignore/occlusionTest/Model.egg"
# self.sceneSource = "Demoscene.ignore/lost-empire/Model.egg"
# self.sceneSource = "Models/PSSMTest/Model.egg.bam"
# self.sceneSource = "Demoscene.ignore/GITest/Model.egg"
# self.sceneSource = "Demoscene.ignore/PSSMTest/Model.egg.bam"
# self.sceneSource = "Demoscene.ignore/Room/LivingRoom.egg"
# self.sceneSource = "Models/CornelBox/Model.egg"
# self.sceneSource = "Models/HouseSet/Model.egg"
self.sceneSource = "Toolkit/Blender Material Library/MaterialLibrary.egg"
self.renderPipeline.loadSettings("Config/pipeline.ini")
# Create the pipeline, and enable scattering
self.renderPipeline.create()
self.renderPipeline.enableDefaultEarthScattering()
# Load scene from disk
self.debug("Loading Scene '" + self.sceneSource + "'")
self.scene = self.loader.loadModel(self.sceneSource)
# Wheter to use a ground floor
self.usePlane = False
self.sceneWireframe = False
# Flatten scene?
self.scene.flattenStrong()
self.scene.analyze()
# Load ground plane if configured
if self.usePlane:
self.groundPlane = self.loader.loadModel(
"Models/Plane/Model.egg.bam")
self.groundPlane.setPos(0, 0, -0.01)
self.groundPlane.setScale(2.0)
self.groundPlane.setTwoSided(True)
self.groundPlane.flattenStrong()
self.groundPlane.reparentTo(self.scene)
# Some artists really don't know about backface culling
# self.scene.setTwoSided(True)
# Required for tesselation
# self.convertToPatches(self.scene)
self.scene.reparentTo(self.render)
# Prepare textures with SRGB format
self.prepareSRGB(self.scene)
# Create movement controller (Freecam)wwww
self.controller = MovementController(self)
self.controller.setInitialPosition(
Vec3(0, -5, 5.0), Vec3(0, 0, 5))
self.controller.setup()
# Hotkey for wireframe
self.accept("f3", self.toggleSceneWireframe)
# Hotkey to reload all shaders
self.accept("r", self.setShaders)
# for i in xrange(1):
# pointLight = PointLight()
# pointLight.setPos(Vec3( (i-1)*3, 0, 7))
# pointLight.setColor(Vec3(0.1))
# pointLight.setShadowMapResolution(1024)
# pointLight.setRadius(50)
# pointLight.setCastsShadows(True)
# # pointLight.attachDebugNode(render)
# self.renderPipeline.addLight(pointLight)
# Create a sun light
dPos = Vec3(60, 30, 100)
dirLight = DirectionalLight()
dirLight.setDirection(dPos)
dirLight.setShadowMapResolution(2048)
dirLight.setAmbientColor(Vec3(0.0, 0.0, 0.0))
dirLight.setPos(dPos)
dirLight.setColor(Vec3(3))
dirLight.setPssmTarget(base.cam, base.camLens)
dirLight.setCastsShadows(True)
self.renderPipeline.addLight(dirLight)
self.dirLight = dirLight
sunPos = Vec3(56.7587, -31.3601, 189.196)
self.dirLight.setPos(sunPos)
self.dirLight.setDirection(sunPos)
# Tell the GI which light casts the GI
self.renderPipeline.setGILightSource(dirLight)
# Slider to move the sun
if self.renderPipeline.settings.displayOnscreenDebugger:
self.renderPipeline.guiManager.demoSlider.node[
"command"] = self.setSunPos
self.renderPipeline.guiManager.demoSlider.node[
"value"] = 20
self.lastSliderValue = 0.0
# Load skyboxn
self.skybox = None
self.loadSkybox()
# Set default object shaders
self.setShaders(refreshPipeline=False)
# Show windows
# for window in base.graphicsEngine.getWindows():
# print window.getName(), window.getSort()
def setSunPos(self):
""" Sets the sun position based on the debug slider """
radial = True
rawValue = self.renderPipeline.guiManager.demoSlider.node["value"]
diff = self.lastSliderValue - rawValue
self.lastSliderValue = rawValue
if radial:
rawValue = rawValue / 100.0 * 2.0 * math.pi
dPos = Vec3(
math.sin(rawValue) * 100.0, math.cos(rawValue) * 100.0, 100)
# dPos = Vec3(100, 100, (rawValue - 50) * 10.0)
else:
dPos = Vec3(30, (rawValue - 50) * 1.5, 100)
if abs(diff) > 0.0001:
self.dirLight.setPos(dPos)
self.dirLight.setDirection(dPos)
def toggleSceneWireframe(self):
""" Toggles the scene rendermode """
self.sceneWireframe = not self.sceneWireframe
if self.sceneWireframe:
self.scene.setRenderModeWireframe()
else:
self.scene.clearRenderMode()
def prepareSRGB(self, np):
""" Sets the correct texture format for all textures found in <np> """
for tex in np.findAllTextures():
baseFormat = tex.getFormat()
# Only diffuse textures should be SRGB
if "diffuse" in tex.getName().lower():
print "Preparing texture", tex.getName()
if baseFormat == Texture.FRgb:
tex.setFormat(Texture.FSrgb)
elif baseFormat == Texture.FRgba:
tex.setFormat(Texture.FSrgbAlpha)
elif baseFormat == Texture.FSrgb or baseFormat == Texture.FSrgbAlpha:
# Format is okay already
pass
else:
print "Unkown texture format:", baseFormat
print "\tTexture:", tex
# All textures should have the correct filter modes
tex.setMinfilter(Texture.FTLinearMipmapLinear)
tex.setMagfilter(Texture.FTLinear)
tex.setAnisotropicDegree(16)
def loadLights(self, scene):
""" Loads lights from a .egg. Lights should be empty objects (blender) """
model = self.loader.loadModel(scene)
lights = model.findAllMatches("**/PointLight*")
for prefab in lights:
light = PointLight()
light.setRadius(prefab.getScale().x)
light.setColor(Vec3(2))
light.setPos(prefab.getPos())
light.setShadowMapResolution(2048)
light.setCastsShadows(False)
self.renderPipeline.addLight(light)
print "Adding Light:", prefab.getPos(), prefab.getScale()
self.lights.append(light)
self.initialLightPos.append(prefab.getPos())
self.test = light
def loadSkybox(self):
""" Loads the skybox """
self.skybox = self.loader.loadModel("Models/Skybox/Model.egg.bam")
self.skybox.setScale(40000)
self.skybox.reparentTo(self.render)
def setShaders(self, refreshPipeline=True):
""" Sets all shaders """
self.debug("Reloading Shaders ..")
if self.renderPipeline:
self.scene.setShader(
self.renderPipeline.getDefaultObjectShader(False))
if refreshPipeline:
self.renderPipeline.reloadShaders()
if self.skybox:
self.skybox.setShader(BetterShader.load(
"Shader/DefaultObjectShader/vertex.glsl", "Shader/Skybox/fragment.glsl"))
def convertToPatches(self, model):
""" Converts a model to patches. This is REQUIRED before beeing able
to use it with tesselation shaders """
self.debug("Converting model to patches ..")
for node in model.find_all_matches("**/+GeomNode"):
geom_node = node.node()
num_geoms = geom_node.get_num_geoms()
for i in range(num_geoms):
geom_node.modify_geom(i).make_patches_in_place()
app = Main()
app.run()