-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.h
More file actions
217 lines (177 loc) · 5.64 KB
/
util.h
File metadata and controls
217 lines (177 loc) · 5.64 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#pragma once
#ifndef UTIL_H
#define UTIL_H
#include <fstream>
#ifdef WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#define SAFE_DELETE(p) if (p) { delete p; p = NULL; }
#define ZERO_MEM(a) memset(a, 0, sizeof(a))
#define ZERO_MEM_VAR(var) memset(&var, 0, sizeof(var))
#define ARRAY_SIZE_IN_ELEMENTS(a) (sizeof(a)/sizeof(a[0]))
#ifdef WIN32
#define SNPRINTF _snprintf_s
#define VSNPRINTF vsnprintf_s
#define RANDOM rand
#define SRANDOM srand((unsigned)time(NULL))
#if (_MSC_VER == 1900)
#elif (_MSC_VER == 1800)
#else
float fmax(float a, float b);
#endif
#else
#define SNPRINTF snprintf
#define VSNPRINTF vsnprintf
#define RANDOM random
#define SRANDOM srandom(getpid())
#endif
#define INVALID_MATERIAL 0xFFFFFFFF
#define NUM_BONES_PER_VEREX 4
#define MAX_BONES 100
#define INVALID_MATERIAL 0xFFFFFFFF
//***************************************************************************************
// d3dUtil.h by Frank Luna (C) 2011 All Rights Reserved.
//***************************************************************************************
#ifndef D3DUTIL_H
#define D3DUTIL_H
#if defined(DEBUG) || defined(_DEBUG)
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#endif
#include <d3dx11.h>
#include "d3dx11Effect.h"
#include <xnamath.h>
#include <dxerr.h>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include "MathHelper.h"
#include "LightHelper.h"
//---------------------------------------------------------------------------------------
// Simple d3d error checker for book demos.
//---------------------------------------------------------------------------------------
#if defined(DEBUG) | defined(_DEBUG)
#ifndef HR
#define HR(x) \
{ \
HRESULT hr = (x); \
if(FAILED(hr)) \
{ \
DXTrace(__FILE__, (DWORD)__LINE__, hr, L#x, true); \
} \
}
#endif
#else
#ifndef HR
#define HR(x) (x)
#endif
#endif
//---------------------------------------------------------------------------------------
// Convenience macro for releasing COM objects.
//---------------------------------------------------------------------------------------
#define ReleaseCOM(x) { if(x){ x->Release(); x = 0; } }
//---------------------------------------------------------------------------------------
// Convenience macro for deleting objects.
//---------------------------------------------------------------------------------------
#define SafeDelete(x) { delete x; x = 0; }
//---------------------------------------------------------------------------------------
// Utility classes.
//---------------------------------------------------------------------------------------
class d3dHelper
{
public:
///<summary>
///
/// Does not work with compressed formats.
///</summary>
static ID3D11ShaderResourceView* CreateTexture2DArraySRV(
ID3D11Device* device, ID3D11DeviceContext* context,
std::vector<std::wstring>& filenames,
DXGI_FORMAT format = DXGI_FORMAT_FROM_FILE,
UINT filter = D3DX11_FILTER_NONE,
UINT mipFilter = D3DX11_FILTER_LINEAR);
static ID3D11ShaderResourceView* CreateRandomTexture1DSRV(ID3D11Device* device);
};
class TextHelper
{
public:
template<typename T>
static D3DX11INLINE std::wstring ToString(const T& s)
{
std::wostringstream oss;
oss << s;
return oss.str();
}
template<typename T>
static D3DX11INLINE T FromString(const std::wstring& s)
{
T x;
std::wistringstream iss(s);
iss >> x;
return x;
}
};
// Order: left, right, bottom, top, near, far.
void ExtractFrustumPlanes(XMFLOAT4 planes[6], CXMMATRIX M);
// #define XMGLOBALCONST extern CONST __declspec(selectany)
// 1. extern so there is only one copy of the variable, and not a separate
// private copy in each .obj.
// 2. __declspec(selectany) so that the compiler does not complain about
// multiple definitions in a .cpp file (it can pick anyone and discard
// the rest because they are constant--all the same).
namespace Colors
{
XMGLOBALCONST XMVECTORF32 White = { 1.0f, 1.0f, 1.0f, 1.0f };
XMGLOBALCONST XMVECTORF32 Black = { 0.0f, 0.0f, 0.0f, 1.0f };
XMGLOBALCONST XMVECTORF32 Red = { 1.0f, 0.0f, 0.0f, 1.0f };
XMGLOBALCONST XMVECTORF32 Green = { 0.0f, 1.0f, 0.0f, 1.0f };
XMGLOBALCONST XMVECTORF32 Blue = { 0.0f, 0.0f, 1.0f, 1.0f };
XMGLOBALCONST XMVECTORF32 Yellow = { 1.0f, 1.0f, 0.0f, 1.0f };
XMGLOBALCONST XMVECTORF32 Cyan = { 0.0f, 1.0f, 1.0f, 1.0f };
XMGLOBALCONST XMVECTORF32 Magenta = { 1.0f, 0.0f, 1.0f, 1.0f };
XMGLOBALCONST XMVECTORF32 Silver = { 0.75f, 0.75f, 0.75f, 1.0f };
XMGLOBALCONST XMVECTORF32 LightSteelBlue = { 0.69f, 0.77f, 0.87f, 1.0f };
}
///<summary>
/// Utility class for converting between types and formats.
///</summary>
class Convert
{
public:
///<summary>
/// Converts XMVECTOR to XMCOLOR, where XMVECTOR represents a color.
///</summary>
static D3DX11INLINE XMCOLOR ToXmColor(FXMVECTOR v)
{
XMCOLOR dest;
XMStoreColor(&dest, v);
return dest;
}
///<summary>
/// Converts XMVECTOR to XMFLOAT4, where XMVECTOR represents a color.
///</summary>
static D3DX11INLINE XMFLOAT4 ToXmFloat4(FXMVECTOR v)
{
XMFLOAT4 dest;
XMStoreFloat4(&dest, v);
return dest;
}
static D3DX11INLINE UINT ArgbToAbgr(UINT argb)
{
BYTE A = (argb >> 24) & 0xff;
BYTE R = (argb >> 16) & 0xff;
BYTE G = (argb >> 8) & 0xff;
BYTE B = (argb >> 0) & 0xff;
return (A << 24) | (B << 16) | (G << 8) | (R << 0);
}
};
#endif // D3DUTIL_H
#endif