Exemple #1
0
void RaycastCamera::init(u32 screenWidth, u32 screenHeight, f32 fov, f32 nearZ, f32 farZ)
{
    mScreenWidth = (f32)screenWidth;
    mScreenHeight = (f32)screenHeight;
    mFov = fov;
    mNearZ = nearZ;
    mFarZ = farZ;

    mAspectRatio = mScreenWidth / mScreenHeight;

    mProjection = perspective(radians(60.0f),
                              mAspectRatio,
                              mNearZ,
                              mFarZ);
    mProjectionInv = ~mProjection;

    move(vec3(0.0f, 0.0f, 10.0f), quat(0, vec3(0.0f, 0.0f, -1.0f)));

    vec3 rayBottomLeft  = normalize(vec3(mProjectionInv * vec4(-1.0f, -1.0f, 0.0f, 1.0f)));
    vec3 rayBottomRight = vec3(-rayBottomLeft.x,  rayBottomLeft.y, rayBottomLeft.z);
    vec3 rayTopLeft     = vec3( rayBottomLeft.x, -rayBottomLeft.y, rayBottomLeft.z);
    vec3 rayTopRight    = vec3(-rayBottomLeft.x, -rayBottomLeft.y, rayBottomLeft.z);

    f32 cosAngle = dot(rayBottomLeft, normalize(vec3(0.0f, 0.0f, mNearZ)));

    f32 nearLen = -mNearZ / cosAngle;
    f32 farLen = -mFarZ / cosAngle;

    // calculate each corner
    vec3 corBLN = rayBottomLeft * nearLen;
    vec3 corBLF = rayBottomLeft * farLen;

    mCornersInit[kCOR_BottomLeft].nearPos  = corBLN;
    mCornersInit[kCOR_BottomLeft].farPos   = corBLF;

    mCornersInit[kCOR_BottomRight].nearPos = vec3(-corBLN.x, corBLN.y, corBLN.z);
    mCornersInit[kCOR_BottomRight].farPos  = vec3(-corBLF.x, corBLF.y, corBLF.z);

    mCornersInit[kCOR_TopLeft].nearPos = vec3(corBLN.x, -corBLN.y, corBLN.z);
    mCornersInit[kCOR_TopLeft].farPos  = vec3(corBLF.x, -corBLF.y, corBLF.z);

    mCornersInit[kCOR_TopRight].nearPos = vec3(-corBLN.x, -corBLN.y, corBLN.z);
    mCornersInit[kCOR_TopRight].farPos  = vec3(-corBLF.x, -corBLF.y, corBLF.z);

    reset();
    calcPlanes();
}
void ViewFrustum::setCamParams(float _angle, float _ratio, float nearClipDist, float farClipDist, glm::vec3& pos, glm::vec3& lookAt, glm::vec3& up)
{
	ratio = _ratio;
	angle = _angle;
	nearClip = nearClipDist;
	farClip = farClipDist;

	//Vyska a sirka orezovych rovin
	float tang = (float)tan(angle * 0.5) ;
	nearHeight = nearClip * tang;
	nearWidth = nearHeight * ratio;
	farHeight = farClip  * tang;
	farWidth = farHeight * ratio;

	glm::vec3 X, Y, Z, nearClipCenter, farClipCenter;
	
	//Base vectors calculation
	//Z
	Z = glm::normalize(pos - lookAt);

	// X = UP x Z
	X = glm::normalize(glm::cross(up, Z));//upV * ZV;

	//Y = Z x X
	Y = glm::normalize(glm::cross(Z, X));

	//Vypocet stredov near/far plane
	nearClipCenter = pos - (Z * nearClip);
	farClipCenter = pos - (Z * farClip);

	//Vypocet rohov VF - near plane
	ntl = nearClipCenter + (Y * nearHeight) - (X * nearWidth);
	ntr = nearClipCenter + (Y * nearHeight) + (X * nearWidth);
	nbl = nearClipCenter - (Y * nearHeight) - (X * nearWidth);
	nbr = nearClipCenter - (Y * nearHeight) + (X * nearWidth);

	//Far plane
	ftl = farClipCenter + (Y * farHeight) - (X * farWidth);
	ftr = farClipCenter + (Y * farHeight) + (X * farWidth);
	fbl = farClipCenter - (Y * farHeight) - (X * farWidth);
	fbr = farClipCenter - (Y * farHeight) + (X * farWidth);

	//Vypocet rovin
	calcPlanes();
}
Exemple #3
0
void RaycastCamera::move(const vec3 & pos, const quat & dir)
{
    mPos = pos;
    mDir = dir;

    //mView = mat4::translation(-mPos) * mat4::transpose(mat4(mDir));
    mView = mat4(-mPos) * mat4(mDir);
    //mView = ~mView;
    //mView.setTranslation(-pos);

    // Reset points to our default unmoved positions, and
    // than transform each point.
    reset();

    for (u32 ct = 0; ct < kCOR_COUNT; ++ct)
    {
        mCorners[ct].nearPos = dir * mCorners[ct].nearPos;
        mCorners[ct].farPos = dir * mCorners[ct].farPos;
    }

    calcPlanes();
}