Beispiel #1
0
/**
 * Template method that sets the spot direction, spot exponent, and spot cutoff angle of this light
 * in the GL engine to the values of the globalForwardDirection, spotExponent and spotCutoffAngle
 * properties of this node, respectively. The direction and exponent are only valid if a cutoff has
 * been specified and less than 90 degrees, otherwise the light is treated as omnidirectional.
 * OpenGL ES only supports angles less than 90 degrees, so anything above is treated as omnidirectional.
 */
void CC3Light::applyDirectionWithVisitor( CC3NodeDrawingVisitor* visitor )
{
	CC3OpenGL* gl = visitor->getGL();
	if (m_spotCutoffAngle <= 90.0f) 
	{
		gl->setSpotlightDirection( getGlobalForwardDirection(), m_lightIndex );
		gl->setSpotlightCutoffAngle( m_spotCutoffAngle, m_lightIndex );
		gl->setSpotlightFadeExponent( m_spotExponent, m_lightIndex );
	} else {
		gl->setSpotlightCutoffAngle( kCC3SpotCutoffNone, m_lightIndex );
	}
}
Beispiel #2
0
CC3Ray CC3Camera::unprojectPoint( const CCPoint& cc2Point )
{
    // Scale from UI points to GL points
    CCPoint glPoint = ccpMult(cc2Point, CCDirector::sharedDirector()->getContentScaleFactor());

    // Express the glPoint X & Y as proportion of the viewport dimensions.
    CC3Viewport vp = getViewport();
    GLfloat xp = ((2.0f * glPoint.x) / vp.w) - 1;
    GLfloat yp = ((2.0f * glPoint.y) / vp.h) - 1;

    // Ensure that the camera's frustum is up to date, and then map the proportional point
    // on the viewport to its position on the near clipping rectangle. The Z-coordinate is
    // negative because the camera points down the negative Z axis in its local coordinates.
    buildProjection();
    CC3Vector pointLocNear = cc3v(_frustum->getRight() * xp, _frustum->getTop() * yp, -_frustum->getNear());

    CC3Ray ray;
    if ( isUsingParallelProjection() )
    {
        // The location on the near clipping plane is relative to the camera's
        // local coordinates. Convert it to global coordinates before returning.
        // The ray direction is straight out from that global location in the
        // camera's globalForwardDirection.
        ray.startLocation =  getGlobalTransformMatrix()->transformLocation( pointLocNear );
        ray.direction = getGlobalForwardDirection();
    }
    else
    {
        // The location on the near clipping plane is relative to the camera's local
        // coordinates. Since the camera's origin is zero in its local coordinates,
        // this point on the near clipping plane forms a directional vector from the
        // camera's origin. Rotate this directional vector with the camera's rotation
        // matrix to convert it to a global direction vector in global coordinates.
        // Thanks to Cocos3D forum user Rogs for suggesting the use of the globalRotationMatrix.
        ray.startLocation = getGlobalLocation();
        ray.direction = getGlobalRotationMatrix()->transformDirection( pointLocNear );
    }

    // Ensure the direction component is normalized before returning.
    ray.direction = ray.direction.normalize();

    //LogTrace(@"%@ unprojecting point %@ to near plane location %@ and to ray starting at %@ and pointing towards %@",
    //			  [self class], NSStringFromCGPoint(glPoint), NSStringFromCC3Vector(pointLocNear),
    //			  NSStringFromCC3Vector(ray.startLocation), NSStringFromCC3Vector(ray.direction));

    return ray;
}
Beispiel #3
0
CC3Vector CC3Camera::getProjectLocation( const CC3Vector& a3DLocation )
{
    // Convert specified location to a 4D homogeneous location vector
    // and transform it using the modelview and projection matrices.
    CC3Vector4 hLoc;
    hLoc.fromLocation(a3DLocation);
    hLoc = getViewMatrix()->transformHomogeneousVector( hLoc );
    hLoc = getProjectionMatrix()->transformHomogeneousVector( hLoc );

    // Convert projected 4D vector back to 3D.
    CC3Vector projectedLoc = hLoc.homogenizedCC3Vector();

    // The projected vector is in a projection coordinate space between -1 and +1 on all axes.
    // Normalize the vector so that each component is between 0 and 1 by calculating ( v = (v + 1) / 2 ).
    projectedLoc = projectedLoc.average( CC3Vector::kCC3VectorUnitCube );

    CCAssert(_viewport.h > 0 && _viewport.w > 0, "%CC3Camera does not have a valid viewport");

    // Map the X & Y components of the projected location (now between 0 and 1) to display coordinates.
    GLfloat g2p = 1.0f / CCDirector::sharedDirector()->getContentScaleFactor();
    projectedLoc.x *= ((GLfloat)_viewport.w * g2p);
    projectedLoc.y *= ((GLfloat)_viewport.h * g2p);

    // Using the vector from the camera to the 3D location, determine whether or not the
    // 3D location is in front of the camera by using the dot-product of that vector and
    // the direction the camera is pointing. Set the Z-component of the projected location
    // to be the signed distance from the camera to the 3D location, with a positive sign
    // indicating the location is in front of the camera, and a negative sign indicating
    // the location is behind the camera.
    CC3Vector camToLocVector = a3DLocation - getGlobalLocation();
    GLfloat camToLocDist = camToLocVector.length();
    GLfloat frontOrBack = (GLfloat)SIGN( camToLocVector.dot( getGlobalForwardDirection() ) );
    projectedLoc.z = frontOrBack * camToLocDist;

    //LogTrace(@"%@ projecting location %@ to %@ and orienting with device to %@ using viewport %@",
    //		 self, NSStringFromCC3Vector(a3DLocation), NSStringFromCC3Vector(projectedLoc),
    //		 NSStringFromCC3Vector(orientedLoc), NSStringFromCC3Viewport(_viewport));
    return projectedLoc;
}