-
Автор темы
- #1
C++:
bool WorldToScreen(const Vec3& worldPos, Vec3& screenPos, const Matrix44& proj, const Matrix44& view, float width, float height)
{
Vec4 out = Vec4{ worldPos, 1 } *view * proj;
if (out.w == 0.0f)
return false;
screenPos.x = (1 + out.x / out.w) * width * 0.5f;
screenPos.y = (1 - out.y / out.w) * height * 0.5f;
screenPos.z = out.z / out.w;
return screenPos.z > 0.0f && screenPos.z < 1.0f;
}
C++:
bool WorldToScreen(const Vec3& worldPos, Vec3& screenPos, const Matrix44& proj, const Matrix44& view, float width, float height)
{
Vec4 temp =
{
view.m00 * worldPos.x + view.m10 * worldPos.y + view.m20 * worldPos.z + view.m30,
view.m01 * worldPos.x + view.m11 * worldPos.y + view.m21 * worldPos.z + view.m31,
view.m02 * worldPos.x + view.m12 * worldPos.y + view.m22 * worldPos.z + view.m32,
view.m03 * worldPos.x + view.m13 * worldPos.y + view.m23 * worldPos.z + view.m33
};
Vec4 out =
{
proj.m00 * temp.x + proj.m10 * temp.y + proj.m20 * temp.z + proj.m30 * temp.w,
proj.m01 * temp.x + proj.m11 * temp.y + proj.m21 * temp.z + proj.m31 * temp.w,
proj.m02 * temp.x + proj.m12 * temp.y + proj.m22 * temp.z + proj.m32 * temp.w,
proj.m03 * temp.x + proj.m13 * temp.y + proj.m23 * temp.z + proj.m33 * temp.w
};
if (out.w == 0.0f)
return false;
screenPos.x = (1 + out.x / out.w) * width * 0.5f;
screenPos.y = (1 - out.y / out.w) * height * 0.5f;
screenPos.z = out.z / out.w;
return screenPos.z > 0.0f && screenPos.z < 1.0f;
}