大白兔联盟

文章搜索
搜索
当前位置:首页 > code宝库 > c++ code > 文章详情

[C++]FPS游戏坐标转屏幕坐标的类库

大白兔  c++ code  2023-3-8  69  0评论

4*4的矩阵

头文件

#pragma once
#include <windows.h>

typedef struct _WORD_LOCATION_
{
    float x;
    float y;
    float z;
}WORD_LOCATION;

typedef struct _CUT_COOR_
{
    float x;
    float y;
    float z;
    float w;
}CUT_COOR;

typedef struct _NDC_COOR_
{
    float x;
    float y;
    float z;
}NDC_COOR;

typedef struct _SCREEN_COOR_
{
    float x;
    float y;
}SCREEN_COOR;

typedef struct _SCREEN_PARAM_
{
    float height;
    float width;
}SCREEN_PARAM;

class World2Screen
{
public:
    float PersonMatrix[4][4];
    WORD_LOCATION WordLocation;
    CUT_COOR CutCoor;
    NDC_COOR NDCCoor;
    SCREEN_COOR ScreenCoor;
    SCREEN_PARAM ScreenParam;
public:
    World2Screen(WORD_LOCATION Word, float* PersonMatrix);
    void Word2CutCoor();
    bool InVisualField();
    void Cut2NDCCoor();
    void NDC2ScreenCoor();

};

源文件

#include "World2Screen.h"

World2Screen::World2Screen(WORD_LOCATION Word, float* PersonMatrix)
{
    this->WordLocation.x = Word.x;
    this->WordLocation.y = Word.y;
    this->WordLocation.z = Word.z;
    memcpy_s(this->PersonMatrix,sizeof(this->PersonMatrix),PersonMatrix,sizeof(this->PersonMatrix));
}

void World2Screen::Word2CutCoor()
{   
    this->CutCoor.x = this->PersonMatrix[0][0] * this->WordLocation.x + this->PersonMatrix[0][1] * this->WordLocation.y + this->PersonMatrix[0][2] * this->WordLocation.z + this->PersonMatrix[0][3];
    this->CutCoor.y = this->PersonMatrix[1][0] * this->WordLocation.x + this->PersonMatrix[1][1] * this->WordLocation.y + this->PersonMatrix[1][2] * this->WordLocation.z + this->PersonMatrix[1][3];
    this->CutCoor.z = this->PersonMatrix[2][0] * this->WordLocation.x + this->PersonMatrix[2][1] * this->WordLocation.y + this->PersonMatrix[2][2] * this->WordLocation.z + this->PersonMatrix[2][3];
    this->CutCoor.w = this->PersonMatrix[3][0] * this->WordLocation.x + this->PersonMatrix[3][1] * this->WordLocation.y + this->PersonMatrix[3][2] * this->WordLocation.z + this->PersonMatrix[3][3];
}

bool World2Screen::InVisualField()
{
    if (this->CutCoor.w < 0.01) { return false; };
}

void World2Screen::Cut2NDCCoor()
{
    this->NDCCoor.x = this->CutCoor.x / this->CutCoor.w;
    this->NDCCoor.y = this->CutCoor.y / this->CutCoor.w;
    this->NDCCoor.z = this->CutCoor.z / this->CutCoor.w;
}

void World2Screen::NDC2ScreenCoor()
{
    this->ScreenCoor.x = this->ScreenParam.width / 2 + (this->ScreenParam.width / 2) * this->NDCCoor.x;
    this->ScreenCoor.y = this->ScreenParam.height / 2 - (this->ScreenParam.height / 2) * this->NDCCoor.y;
}

免责声明

本站提供的一切软件、教程和内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!

挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论