| left_leg | 
			01-14-2013 06:18 PM | 
		 
		 
		 
		
			no fog in outer space - trouble getting viz.fog to work   
		
		
		hey guys, 
I programmed a starfield environment as action class. since the field size is limited I wanted to use viz.fog to smoothly fade in distant stars while moving through the field. however, the no matter what I do the fog is not appearing in the starfield. checked stuff like color of the fog, different densities, linear or exponential fog and even added a lighting.
 
my alternative is using a modest gaussian blur postprocessing, but the fog would definitely be the better option...
 
here is my code, first the starfield action class and then a little demo:
 
	Code: 
	
 import vizshape 
import viz 
import random 
import viztask 
 
 
class Starfield(viz.ActionClass): 
        """ 
        action class providing a starfield that can be used as optical flow stimulus 
        """ 
         
        def begin(self,object): 
                """ 
                constructor: places the stars randomly columns around a column 
                center (black cube). columns are stored in a list 
                 
                """ 
                self.data            = self._actiondata_.data 
                self.nStars    = self.data[0] 
                self.starSize  = self.data[1] 
                self.colDimXYZ = self.data[2] 
                self.nColsXYZ  = self.data[3] 
                 
                # internal parameters 
                self.starsPerColumn = int(self.nStars / (self.nColsXYZ[0] *  
                                                          self.nColsXYZ[1] * self.nColsXYZ[2])) 
                self.xDeltaMax = self.colDimXYZ[0] * self.nColsXYZ[0] / 2 
                self.yDeltaMax = self.colDimXYZ[1] * self.nColsXYZ[1] / 2 
                self.zDeltaMax = self.colDimXYZ[2] * self.nColsXYZ[2] / 2 
                self.countStart = 0 
                self.time = 0 
 
                # create a master star 
                protoStar = viz.addTexQuad(size = self.starSize) 
                protoStar.setPosition([0,1,1]) 
                diff = viz.add('star.png') 
                norm = viz.add('normal1.tif') 
                protoStar.texture(diff,'',1) 
                protoStar.bumpmap(norm,'',0) 
                protoStar.billboard(mode=viz.BILLBOARD_VIEW) 
                 
                # list with all stars 
                self.columns = [] 
                for idx_x in range(self.nColsXYZ[0]): 
                        for idx_y in range(self.nColsXYZ[1]): 
                                for idx_z in range(self.nColsXYZ[2]): 
                                        # create center node as parent for all stars in that column 
                                        columnCenter = vizshape.addCube(size = 0.001) 
                                        columnCenter.color(viz.BLACK) 
                                        columnCenter.setPosition([idx_x * self.colDimXYZ[0], 
                                                                                          idx_y * self.colDimXYZ[1], 
                                                                                          idx_z * self.colDimXYZ[2]]) 
                                        self.columns.append(columnCenter) 
                                        # randomly create stars within the limits of one column 
                                        for y in range(self.starsPerColumn): 
                                                # positioning 
                                                x = self.colDimXYZ[0] * (random.random() - 0.5) 
                                                y = self.colDimXYZ[1] * (random.random() - 0.5) 
                                                z = self.colDimXYZ[2] * (random.random() - 0.5) 
                                                star = protoStar.clone(columnCenter) 
                                                star.setPosition([x,y,z], mode = viz.ABS_PARENT) 
                                                star.billboard(mode=viz.BILLBOARD_VIEW) 
                 
                protoStar.remove() 
 
 
 
        def update(self,elapsed,object): 
                """ 
                checks every frame if a column center exceeded the volume 
                if yes, it reenters the volume on the oppsite side (like in snake) 
                """ 
 
                # get position of the avatar (center of the field) 
                [aX, aY, aZ] = object.getPosition(mode = viz.ABS_GLOBAL) 
                # for every star: check if within limits of field, if not, reposition using respawn function 
                for column in self.columns[self.countStart::4]: 
                        # get position of the star 
                        pos = column.getPosition(mode = viz.ABS_GLOBAL) 
                        # check every coordinate 
                        x = pos[0] if abs(pos[0] - aX) <= self.xDeltaMax else self.respawn(aX, pos[0], self.xDeltaMax) 
                        y = pos[1] if abs(pos[1] - aY) <= self.yDeltaMax else self.respawn(aY, pos[1], self.yDeltaMax) 
                        z = pos[2] if abs(pos[2] - aZ) <= self.zDeltaMax else self.respawn(aZ, pos[2], self.zDeltaMax) 
 
                        self.time = elapsed + self.time 
                         
                        # eventually reposition 
                        column.setPosition([x,y,z], mode = viz.ABS_GLOBAL) 
                         
                self.countStart = (self.countStart + 1) % 4 
                 
                if self.time >= 200.00 and self.time <= 200.1: 
                        object.setPosition([0,0,0], viz.ABS_GLOBAL) 
                         
                 
        def end(self,object): 
                print 'starfield ended'                 
                 
                 
        def respawn(self, cAvatar, cPoint, deltaMax): 
                """ if point exceeded displayed field, this calculates the reentry coordinates """ 
                # transform into coordinate system with avatar as (0,0) 
                delta  = cPoint - cAvatar 
                # how far the point exceeded the limit 
                rest   = abs(delta) % deltaMax 
                # check on which side 
                side   = -1 if cPoint < cAvatar else 1 
                # calculate new coordinates in global sys after entering on the other side 
                new    = cAvatar - side * (deltaMax - rest) 
                return new 
 
 
def addStarfield(nStars = 10000, starSize = 0.18, colDimXYZ = [5, 30, 5], nColsXYZ = [20, 1, 20], blur = True): 
        """ 
        creates a starfield around the object it is assigned to as action 
        object is center and the cuboid has the edge lenghts dimX, dimY and dimZ 
        every frame every star is checked if it exceeds the cuboid, if yes it reenters 
        the cuboid on the other side (like playing good old snkae) 
        """         
         
        if blur: 
                import vizfx.postprocess 
                from vizfx.postprocess.blur import GaussianBlurEffect 
                effect = GaussianBlurEffect(blurRadius = 10, blurScale = 0.001, downsample = 0.7) 
                vizfx.postprocess.addEffect(effect) 
                 
        bla                         = viz.ActionData() 
        bla.data                 = [nStars, starSize, colDimXYZ, nColsXYZ] 
        bla.actionclass        = Starfield 
        return bla 
 
	Code: 
	
 import iStarfield 
import viz 
 
# create an invisible avatar (needed to add the starfield action to it) 
avatar = viz.add('wheelbarrow.ive') 
avatar.visible(viz.OFF) 
 
# add the main view to it 
viz.link(viz.MainView, avatar) 
 
 
# add starfield action to it's OWN ACTION POOL 
avatar.addAction(iStarfield.addStarfield(nStars = 10000, starSize = 0.18, colDimXYZ = [5, 30, 5],  
                                                                                 nColsXYZ = [20, 1, 20], blur = False                     ), pool = 1) 
 
viz.go() 
 
# adding the fog 
viz.fog(0.9) 
viz.fogcolor(0.5,0.5,0.5) 
  
	 |