Wrote this code to visualize the affects of point light attenuation coefficients.
#Lesson on Point Light Attenuation
#brianinsuwon, brianinsuwon@yahoo.com, July 1, 2008
import direct.directbase.DirectStart
import sys, math, random
from direct.showbase.DirectObject import DirectObject
from direct.gui.DirectGui import *
from pandac.PandaModules import *
from direct.task.Task import Task
from direct.interval.IntervalGlobal import *
from direct.gui.OnscreenText import OnscreenText
# Function to put title on the screen.
def addTitle(text):
return OnscreenText(text=text, style=1, fg=(1,1,1,1),
pos=(1.0,0.8), align=TextNode.ARight, scale = .07)
# Function to put instructions on the screen.
def addInstructions(pos, msg):
return OnscreenText(text=msg, style=1, fg=(1,1,1,1),
pos=(-1.3, pos), align=TextNode.ALeft, scale = .06)
class World (DirectObject):
def __init__(self):
base.win.setClearColor(Vec4(0,0,0,1))
# Post the instructions
self.title = addTitle("Point Light Attenuation")
self.inst1 = addInstructions(0.85, "[ESC]: Quit")
self.inst2 = addInstructions(0.75, "Up Arrow: Increase plight height")
self.inst3 = addInstructions(0.69, "Down Arrow: Decrease plight height")
self.inst4 = addInstructions(0.63, "W: Move plight forward")
self.inst6 = addInstructions(0.57, "S: Move plight backward")
base.setBackgroundColor(0,0,0,1)
#set up my floor
#floor was created in Blender using a plane
#subdivided 5 times,scaleX=16, scaleY=16
#no material on it
self.floor = loader.loadModel('floorGrid')
self.floor.reparentTo(render)
#set the 'camera'
#base.oobe()
base.disableMouse()
base.camera.setPos(0,-10,3)
base.camera.setHpr(0,-5,0)
#setup objects
#cube was created in Blender, no material on it
self.cube=[]
for i in range(8):
acube = loader.loadModelCopy('1cube')
acube.reparentTo(render)
acube.setPos(-14+(4*i), 14, 0)
acube.setColor(1,1,1,1)
self.cube.append(acube)
#set up point light
#pointLight.setAttenuation(Point3(A, B, C))
self.A=0
self.B=0
self.C=0
self.plight = PointLight('plight')
self.plight.setColor(VBase4(1, 1, 1, 1))
self.plight.setAttenuation(Point3(self.A,self.B, self.C))
self.plnp = render.attachNewNode(self.plight)
self.plnp.setPos(0,0,1)
render.setLight(self.plnp)
#label for sliders
self.aLabel = OnscreenText(text = "A", pos = (0.060,-0.56), scale = 0.09, fg=(1,0,0,1),mayChange=1)
self.bLabel = OnscreenText(text = "B", pos = (-0.24,-0.65), scale = 0.09, fg=(0,1.0,0,1), mayChange=1)
self.cLabel = OnscreenText(text = "C", pos = (0.06,-0.740), scale = 0.09, fg=(0,0,1,1), mayChange=1)
#set up sliders
self.Aslider=DirectSlider(range=(0,1), value=0, pageSize=0.01, command=self.setAF, scale = 0.55, pos=Point3(0.683333, 0, -0.54))
self.Bslider=DirectSlider(range=(0,1), value=0, pageSize=0.01, command=self.setBF, scale = 0.55, pos=Point3(0.363333, 0, -0.623333))
self.Cslider=DirectSlider(range=(0,1), value=0, pageSize=0.01, command=self.setCF, scale = 0.55, pos=Point3(0.693333, 0, -0.706667))
#setup output box
self.ABCValuesLabel = DirectLabel(text = " ", pos = (0, 0, -0.9), scale = 0.07, text_fg=(1,0.0,0.0,1))
self.ABCValuesLabel ['text'] = "plight.setAttenuation(%.2f, %.2f, %.2f)" % (self.A, self.B, self.C)
self.plnpZValuesLabel = DirectLabel(text = " ", pos = (0, 0, 0.9), scale = 0.07, text_fg=(1,0.0,0.0,1))
self.plnpZValuesLabel ['text'] = "plnp.setZ(%.2f)" %self.plnp.getZ()
self.plnpYValuesLabel = DirectLabel(text = " ", pos = (0, 0, 0.8), scale = 0.07, text_fg=(1,0.0,0.0,1))
self.plnpYValuesLabel ['text'] = "plnp.setY(%.2f)" %self.plnp.getY()
#key input
self.accept("arrow_up", self.plnpZF, [1])
self.accept("arrow_down", self.plnpZF, [-1])
self.accept("w", self.plnpYF, [1])
self.accept("s", self.plnpYF, [-1])
def plnpZF (self, arg):
self.plnp.setZ(self.plnp.getZ()+arg)
self.plnpZValuesLabel ['text'] = "plnp.setZ(%.2f)" %self.plnp.getZ()
def plnpYF (self, arg):
self.plnp.setY(self.plnp.getY()+arg)
self.plnpYValuesLabel ['text'] = "plnp.setY(%.2f)" %self.plnp.getY()
def setAF(self):
self.A = self.Aslider['value']
self.ABCValuesLabel ['text'] = "plight.setAttenuation(%.2f, %.2f, %.2f)" % (self.A, self.B, self.C)
self.plight.setAttenuation(Point3(self.A, self.B, self.C))
def setBF(self):
self.B = self.Bslider['value']
self.ABCValuesLabel ['text'] = "plight.setAttenuation(%.2f, %.2f, %.2f)" % (self.A, self.B, self.C)
self.plight.setAttenuation(Point3(self.A, self.B, self.C))
def setCF(self):
self.C = self.Cslider['value']
self.ABCValuesLabel ['text'] = "plight.setAttenuation(%.2f, %.2f, %.2f)" % (self.A, self.B, self.C)
self.plight.setAttenuation(Point3(self.A, self.B, self.C))
w = World()
run()