본문 바로가기

카테고리 없음

화면 좌표계와 NDC 좌표계간 변환

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <utility>
 
float width{ 1920.f }; 
float height{ 1080.f }; 
 
std::pair<floatfloat> ScreenCoordToNDC(float x, float y) {
    const float XTransform = (2./ width);
    const float YTransform = -(2./ height);
    x = x * XTransform - 1.f;
    y = y * YTransform + 1.f;
    return std::pair{ x,y };
};
 
std::pair<floatfloat> NDCToScreenCoord(float x, float y) {
    x = x * (width / 2+ (width / 2);
    y = y * -(height / 2+ (height / 2);
    return std::pair{ x,y };
};
 
int main() {
    for (int i = 0; i <= width; ++i) {
        auto [x, y] = ScreenCoordToNDC(i, 0);
        std::cout << "Screen pos x :  " << i << "    width : " << x << std::endl;
        std::cout << "Screen pos y :  " << 0 << "    height : " << y << std::endl;
    };
    for (int i = 0; i <= height; ++i) {
        auto [x, y] = ScreenCoordToNDC(0, i);
        std::cout << "Screen pos x :  " << 0 << "    width : " << x << std::endl;
        std::cout << "Screen pos y :  " << i << "    height : " << y << std::endl;
    };
}
cs