Ejemplo n.º 1
0
def lookAt(pos, at):
    """
    Takes 2 LVector3f vectors,
    Returns the lookAt matrix Mat3d
    """
    
    # forward
    yaxis = at - pos
    print("forward ", yaxis)
    yaxis.normalize()
    
    # right
    xaxis = LVector3f.up().cross(yaxis)
    xaxis.normalize()
    print("right ", xaxis)
    
    # up
    zaxis = xaxis.cross(yaxis)
    zaxis.normalize()
    print("up ", zaxis)
    
    rmat = LMatrix4f()
    rmat.setRow(0, xaxis)
    rmat.setRow(1, zaxis)
    rmat.setRow(2, yaxis)
    
    tmat = LMatrix4f.translateMat(pos)
    
    print(rmat*tmat)
    
    return rmat * tmat
Ejemplo n.º 2
0
    def getLightsForModel(self, modelId):
        lights = []
        if modelId in self.supportedModelIds:

            for n, lightData in enumerate(self.data[modelId]):

                attenuation = LVector3f(*lightData['attenuation'])

                #TODO: implement light power
                #power = float(lightData['power'])

                positionYup = LVector3f(*lightData['position'])
                yupTozupMat = LMatrix4f.convertMat(CS_yup_right, CS_zup_right)
                position = yupTozupMat.xformVec(positionYup)

                colorHtml = lightData['color']
                color = LVector3f(*[
                    int('0x' + colorHtml[i:i + 2], 16)
                    for i in range(1, len(colorHtml), 2)
                ]) / 255.0

                direction = None
                lightType = lightData['type']
                lightName = modelId + '-light-' + str(n)
                if lightType == 'SpotLight':
                    light = Spotlight(lightName)
                    light.setAttenuation(attenuation)
                    light.setColor(color)

                    cutoffAngle = float(lightData['cutoffAngle'])
                    lens = PerspectiveLens()
                    lens.setFov(cutoffAngle / np.pi * 180.0)
                    light.setLens(lens)

                    # NOTE: unused attributes
                    #dropoffRate = float(lightData['dropoffRate'])

                    directionYup = LVector3f(*lightData['direction'])
                    direction = yupTozupMat.xformVec(directionYup)

                elif lightType == 'PointLight':
                    light = PointLight(lightName)
                    light.setAttenuation(attenuation)
                    light.setColor(color)

                elif lightType == 'LineLight':
                    #XXX: we may wish to use RectangleLight from the devel branch of Panda3D
                    light = PointLight(lightName)
                    light.setAttenuation(attenuation)
                    light.setColor(color)

                    # NOTE: unused attributes
                    #dropoffRate = float(lightData['dropoffRate'])
                    #cutoffAngle = float(lightData['cutoffAngle'])

                    #position2Yup = LVector3f(*lightData['position2'])
                    #position2 = yupTozupMat.xformVec(position2Yup)

                    #directionYup = LVector3f(*lightData['direction'])
                    #direction = yupTozupMat.xformVec(directionYup)

                else:
                    raise Exception('Unsupported light type: %s' % (lightType))

                lightNp = NodePath(light)

                # Set position and direction of light
                lightNp.setPos(position)
                if direction is not None:
                    targetPos = position + direction
                    lightNp.look_at(targetPos, LVector3f.up())

                lights.append(lightNp)

        return lights