首页 > 其他 > 详细

DirectX11笔记2:时间,数学库,渲染管线

时间:2015-11-09 20:44:33      阅读:203      评论:0      收藏:0      [点我收藏+]

一个游戏程序当然需要各种计时,书中代码给予了GameTimer类:

#ifndef GAMETIMER_H
#define GAMETIMER_H

class GameTimer
{
public:
    GameTimer();

    float TotalTime()const;  // in seconds        返回程序运行(当然不包括暂停)的总时间
    float DeltaTime()const; // in seconds        返回mDeltaTime,当前帧与上一帧的时间差

    void Reset(); // Call before message loop.
    void Start(); // Call when unpaused.
    void Stop();  // Call when paused.
    void Tick();  // Call every frame.

private:
    double mSecondsPerCount;    //存储cpu 1秒有多少count,用于将获得的count计算为秒
    double mDeltaTime;

    __int64 mBaseTime;                //记录Reset函数调用时的计时count
    __int64 mPausedTime;            //暂停的总时间
    __int64 mStopTime;                //最近一次暂停时(Stop被调用)的计时Count
    __int64 mPrevTime;                //记录上一帧的计时count,以此来计算2帧的count差,当start时会被设为start的count来保证游戏的时间连贯性,reset时会为0
    __int64 mCurrTime;                //Trick调用时,会用于记录这一帧的计时count

    bool mStopped;                    //是否暂停
};

#endif // GAMETIMER_H

以及时间类在消息循环中的基本运用

int D3DApp::Run()
{
    MSG msg = {0};
  
    mTimer.Reset();
 
    while(msg.message != WM_QUIT)
    {
        // 如果接收到Window消息,则处理这些消息
        if(PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        // 否则,则运行动画/游戏
        else
        {  
            mTimer.Tick();
 
            if( !mAppPaused )
            {
                CalculateFrameStats();
                UpdateScene(mTimer.DeltaTime());   
                DrawScene();
            }
            else
            {
                Sleep(100);
            }
        }
    }
 
    return (int)msg.wParam;
}

 

 

XNA数学库:

在我的环境下(2013+win10)无法像原先一样直接#include <xnamath.h>调用XNA中的数学库,

需要使用

#include <DirectXMath.h>
#include <DirectXPackedVector.h>

(lib已经在上一节包含进来了)

具体内容参见:https://msdn.microsoft.com/en-us/library/windows/desktop/ee418730.aspx

XNA 介绍参见:https://msdn.microsoft.com/en-us/library/windows/desktop/ee415675.aspx

然后就可以使用书中的代码了,

这里可能会有一个小问题,反正我遇到了:

书中的矩阵输出重载代码:

ostream& operator<<(ostream& os, CXMMATRIX m)
{
    for(int i = 0; i < 4; ++i)
    {
        for(int j = 0; j < 4; ++j)
            os << m(i, j) << "\t";
        os << endl;
    }
    return os;
}

可能是新版的矩阵格式改变了,参照msdn资料

——————————————————————————————————————————————————————————————-————————————————

struct XMMATRIX {
  XMVECTOR r[4];
};

Members

r

Array of four vectors, representing the rows of the matrix.

Remarks

In the DirectXMath.h header file, the system uses an alias to the XMMATRIX object, specifically CXMMATRIX. The header uses the alias to comply with the optimal in-line calling conventions of different compilers. For most projects using DirectXMath it is sufficient to simply treat this as an exact alias to XMMATRIX.

Effectively:

typedef const XMMATRIX CXMMATRIX;

———————————————————————————————————————————————————————————————————————————————

我将输出矩阵的重载改成了:

ostream& operator<<(ostream& os, DirectX::FXMVECTOR v)
{
    DirectX::XMFLOAT4 dest;
    DirectX::XMStoreFloat4(&dest, v);

    os << "(" << dest.x << ", " << dest.y << ", " << dest.z << ", " << dest.w << ")";
    return os;
}

ostream& operator<<(ostream& os, DirectX::CXMMATRIX m)
{
    for (int i = 0; i < 4; ++i)
    {
        os << m.r[i]<< "\t";
        os << endl;
    }
    return os;
}

测试代码:

DirectX::XMMATRIX v
(1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 2.0f, 0.0f, 0.0f,
0.0f, 0.0f, 4.0f, 0.0f,
1.0f, 2.0f, 3.0f, 1.0f);


std::cout << "1" << std::endl << v << std::endl;

 

 

渲染管线:

这个暂时比较复杂,等待我将第一个工程完成,有的更好的理解再更新。

 

DirectX11笔记2:时间,数学库,渲染管线

原文:http://www.cnblogs.com/Windogs/p/4943548.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!