forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·188 lines (162 loc) · 6.92 KB
/
main.py
File metadata and controls
executable file
·188 lines (162 loc) · 6.92 KB
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
#!/usr/bin/env python
#
# Bump mapping is a way of making polygonal surfaces look
# less flat. This sample uses normal mapping for all
# surfaces, and also parallax mapping for the column.
#
# This is a tutorial to show how to do normal mapping
# in panda3d using the Shader Generator.
from direct.showbase.ShowBase import ShowBase
from panda3d.core import loadPrcFileData
from panda3d.core import WindowProperties
from panda3d.core import Filename, Shader
from panda3d.core import AmbientLight, PointLight
from panda3d.core import TextNode
from panda3d.core import LPoint3, LVector3
from direct.task.Task import Task
from direct.actor.Actor import Actor
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from direct.filter.CommonFilters import *
import sys
import os
# Function to put instructions on the screen.
def addInstructions(pos, msg):
return OnscreenText(text=msg, style=1, fg=(1, 1, 1, 1), scale=.05,
shadow=(0, 0, 0, 1), parent=base.a2dTopLeft,
pos=(0.08, -pos - 0.04), align=TextNode.ALeft)
# Function to put title on the screen.
def addTitle(text):
return OnscreenText(text=text, style=1, fg=(1, 1, 1, 1), scale=.08,
parent=base.a2dBottomRight, align=TextNode.ARight,
pos=(-0.1, 0.09), shadow=(0, 0, 0, 1))
class BumpMapDemo(ShowBase):
def __init__(self):
# Configure the parallax mapping settings (these are just the defaults)
loadPrcFileData("", "parallax-mapping-samples 3\n"
"parallax-mapping-scale 0.1")
# Initialize the ShowBase class from which we inherit, which will
# create a window and set up everything we need for rendering into it.
ShowBase.__init__(self)
# Check video card capabilities.
if not self.win.getGsg().getSupportsBasicShaders():
addTitle("Bump Mapping: "
"Video driver reports that Cg shaders are not supported.")
return
# Post the instructions
self.title = addTitle("Panda3D: Tutorial - Bump Mapping")
self.inst1 = addInstructions(0.06, "Press ESC to exit")
self.inst2 = addInstructions(0.12, "Move mouse to rotate camera")
self.inst3 = addInstructions(0.18, "Left mouse button: Move forwards")
self.inst4 = addInstructions(0.24, "Right mouse button: Move backwards")
self.inst5 = addInstructions(0.30, "Enter: Turn bump maps Off")
# Load the 'abstract room' model. This is a model of an
# empty room containing a pillar, a pyramid, and a bunch
# of exaggeratedly bumpy textures.
self.room = loader.loadModel("models/abstractroom")
self.room.reparentTo(render)
# Make the mouse invisible, turn off normal mouse controls
self.disableMouse()
props = WindowProperties()
props.setCursorHidden(True)
self.win.requestProperties(props)
self.camLens.setFov(60)
# Set the current viewing target
self.focus = LVector3(55, -55, 20)
self.heading = 180
self.pitch = 0
self.mousex = 0
self.mousey = 0
self.last = 0
self.mousebtn = [0, 0, 0]
# Start the camera control task:
taskMgr.add(self.controlCamera, "camera-task")
self.accept("escape", sys.exit, [0])
self.accept("mouse1", self.setMouseBtn, [0, 1])
self.accept("mouse1-up", self.setMouseBtn, [0, 0])
self.accept("mouse2", self.setMouseBtn, [1, 1])
self.accept("mouse2-up", self.setMouseBtn, [1, 0])
self.accept("mouse3", self.setMouseBtn, [2, 1])
self.accept("mouse3-up", self.setMouseBtn, [2, 0])
self.accept("enter", self.toggleShader)
self.accept("j", self.rotateLight, [-1])
self.accept("k", self.rotateLight, [1])
self.accept("arrow_left", self.rotateCam, [-1])
self.accept("arrow_right", self.rotateCam, [1])
# Add a light to the scene.
self.lightpivot = render.attachNewNode("lightpivot")
self.lightpivot.setPos(0, 0, 25)
self.lightpivot.hprInterval(10, LPoint3(360, 0, 0)).loop()
plight = PointLight('plight')
plight.setColor((1, 1, 1, 1))
plight.setAttenuation(LVector3(0.7, 0.05, 0))
plnp = self.lightpivot.attachNewNode(plight)
plnp.setPos(45, 0, 0)
self.room.setLight(plnp)
# Add an ambient light
alight = AmbientLight('alight')
alight.setColor((0.2, 0.2, 0.2, 1))
alnp = render.attachNewNode(alight)
self.room.setLight(alnp)
# Create a sphere to denote the light
sphere = loader.loadModel("models/icosphere")
sphere.reparentTo(plnp)
# Tell Panda that it should generate shaders performing per-pixel
# lighting for the room.
self.room.setShaderAuto()
self.shaderenable = 1
def setMouseBtn(self, btn, value):
self.mousebtn[btn] = value
def rotateLight(self, offset):
self.lightpivot.setH(self.lightpivot.getH() + offset * 20)
def rotateCam(self, offset):
self.heading = self.heading - offset * 10
def toggleShader(self):
self.inst5.destroy()
if (self.shaderenable):
self.inst5 = addInstructions(0.30, "Enter: Turn bump maps On")
self.shaderenable = 0
self.room.setShaderOff()
else:
self.inst5 = addInstructions(0.30, "Enter: Turn bump maps Off")
self.shaderenable = 1
self.room.setShaderAuto()
def controlCamera(self, task):
# figure out how much the mouse has moved (in pixels)
md = self.win.getPointer(0)
x = md.getX()
y = md.getY()
if self.win.movePointer(0, 100, 100):
self.heading = self.heading - (x - 100) * 0.2
self.pitch = self.pitch - (y - 100) * 0.2
if self.pitch < -45:
self.pitch = -45
if self.pitch > 45:
self.pitch = 45
self.camera.setHpr(self.heading, self.pitch, 0)
dir = self.camera.getMat().getRow3(1)
elapsed = task.time - self.last
if self.last == 0:
elapsed = 0
if self.mousebtn[0]:
self.focus = self.focus + dir * elapsed * 30
if self.mousebtn[1] or self.mousebtn[2]:
self.focus = self.focus - dir * elapsed * 30
self.camera.setPos(self.focus - (dir * 5))
if self.camera.getX() < -59.0:
self.camera.setX(-59)
if self.camera.getX() > 59.0:
self.camera.setX(59)
if self.camera.getY() < -59.0:
self.camera.setY(-59)
if self.camera.getY() > 59.0:
self.camera.setY(59)
if self.camera.getZ() < 5.0:
self.camera.setZ(5)
if self.camera.getZ() > 45.0:
self.camera.setZ(45)
self.focus = self.camera.getPos() + (dir * 5)
self.last = task.time
return Task.cont
demo = BumpMapDemo()
demo.run()