-
Автор темы
- #1
Что не так?
Как преобразовать двухмерные координаты?
Screen Position хуевый!
Как преобразовать двухмерные координаты?
Screen Position хуевый!
C++:
bool WorldToScreen(vec3 pos, vec3& screen, float matrix[16], float windowWidth, float windowHeight)
{
std::cout << "Matrix:" << std::endl;
for (int i = 0; i < 16; ++i) {
std::cout << "Matrix[" << i << "] = " << matrix[i] << std::endl;
}
std::cout << "Position: X: " << pos.x << " Y: " << pos.y << " Z: " << pos.z << std::endl;
std::cout << "Window Width: " << windowWidth << " Window Height: " << windowHeight << std::endl;
Vec4 clipCoords;
clipCoords.x = pos.x * matrix[0] + pos.y * matrix[4] + pos.z * matrix[8] + matrix[12];
clipCoords.y = pos.x * matrix[1] + pos.y * matrix[5] + pos.z * matrix[9] + matrix[13];
clipCoords.z = pos.x * matrix[2] + pos.y * matrix[6] + pos.z * matrix[10] + matrix[14];
clipCoords.w = pos.x * matrix[3] + pos.y * matrix[7] + pos.z * matrix[11] + matrix[15];
std::cout << "X: " << clipCoords.x << " Y: " << clipCoords.y << " Z: " << clipCoords.z << " W: " << clipCoords.w << std::endl;
if (clipCoords.w < 0.1f)
{
std::cout << "Behind the camera." << std::endl;
return false;
}
Vec3 NDC{ clipCoords.x / clipCoords.w, clipCoords.y / clipCoords.w, clipCoords.z / clipCoords.w };
std::cout << "NDC: " << NDC << std::endl;
screen.x = (NDC.x + 1.0f) * (windowWidth / 2.0f);
screen.y = (1.0f - NDC.y) * (windowHeight / 2.0f);
std::cout << "Screen Position: X: " << screen.x << " Y: " << screen.y << std::endl;
if (screen.x < 0 || screen.x > windowWidth || screen.y < 0 || screen.y > windowHeight) {
std::cout << "Behind the camera" << std::endl;
return false;
}
return true;
}