-
Notifications
You must be signed in to change notification settings - Fork 1
/
backGlass.cpp
304 lines (278 loc) · 14 KB
/
backGlass.cpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "stdafx.h"
#include "rapidxml/rapidxml_utils.hpp"
#include <iostream>
#include <fstream>
#include "backGlass.h"
#include "RenderDevice.h"
#include "Shader.h"
#include "captureExt.h"
//#define WRITE_BACKGLASS_IMAGES 1
//XML helpers
inline char nextChar(size_t &inPos, size_t inSize, const char* inChars, const char* outChars, const char* inData) {
char c = (inPos >= inSize) ? '=' : inData[inPos];
while (outChars[c] < 0) {
inPos++;
c = (inPos >= inSize) ? '=' : inData[inPos];
}
inPos++;
return c;
}
/*
returns actual data size if successful or -1 if something went wrong.
*/
int decode_base64(const char* inData, char* outData, size_t inSize, size_t outSize) {
static const char inChars[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static char* outChars = NULL;
//Create decode table from encode table
if (!outChars) {
outChars = (char*)malloc(256);
for (size_t i = 0;i < 256;++i) outChars[i] = 0;
for (char i = 0;i < 64;++i) outChars[inChars[i]] = i;
}
//Hack for fast skipping
outChars['&'] = -1;
outChars[10] = -1;
outChars[13] = -1;
size_t inPos = 0;
size_t outPos = 0;
bool done = false;
int padding = 0;
while ((inPos < inSize) && (outPos < outSize) && !done) {
char b1 = nextChar(inPos, inSize, inChars, outChars, inData);
char b2 = nextChar(inPos, inSize, inChars, outChars, inData);
char b3 = nextChar(inPos, inSize, inChars, outChars, inData);
char b4 = nextChar(inPos, inSize, inChars, outChars, inData);
done = (b1 == '=' || b2 == '=' || b3 == '=' || b4 == '=');
if (done) {
if (b1 == '=') padding += 3;
else if (b2 == '=') padding += 3;
else if (b3 == '=') padding += 2;
else if (b4 == '=') padding++;
}
b1 = outChars[b1];
b2 = outChars[b2];
b3 = outChars[b3];
b4 = outChars[b4];
outData[outPos] = (b1 << 2) | (b2 >> 4);
if (outPos + 1 < outSize) outData[outPos + 1] = (b2 << 4) | (b3 >> 2);
if (outPos + 2 < outSize) outData[outPos + 2] = (b3 << 6) | (b4);
outPos += 3;
}
return min(outPos - padding, outSize);
}
//Actual Backglass code
BackGlass::BackGlass(RenderDevice* const pd3dDevice,Texture * backgroundFallback) :
m_pd3dDevice(pd3dDevice), m_backgroundFallback(backgroundFallback)
{
#ifdef ENABLE_VR
//Check for a directb2s and try to use its backglass data
std::string b2sFileName = string(g_pplayer->m_ptable->m_szFileName);
b2sFileName = b2sFileName.substr(0, b2sFileName.find_last_of("."));
b2sFileName.append(".directb2s");
void* data = NULL;
size_t data_len = 0;
backglass_dmd_x = 0;
backglass_dmd_y = 0;
backglass_dmd_width = 0;
backglass_dmd_height = 0;
backglass_grill_height = 0;
backglass_width = 0;
backglass_height = 0;
backglass_scale = 1.2f;
try {
rapidxml::file<> b2sFile(b2sFileName.c_str());
rapidxml::xml_document<> b2sTree;
b2sTree.parse<0>(b2sFile.data());
auto rootNode = b2sTree.first_node("DirectB2SData");
if (!rootNode) {
return;
}
auto currentNode = rootNode->first_node();
while (currentNode) {//Iterate all Nodes within DirectB2SData
char* nodeName = currentNode->name();
if (strcmp(nodeName, "VRDMDLocation") == 0) {
auto attrib = currentNode->first_attribute("LocX");
if (attrib) backglass_dmd_x = atoi(attrib->value());
attrib = currentNode->first_attribute("LocY");
if (attrib) backglass_dmd_y = atoi(attrib->value());
attrib = currentNode->first_attribute("Width");
if (attrib) backglass_dmd_width = atoi(attrib->value());
attrib = currentNode->first_attribute("Height");
if (attrib) backglass_dmd_height = atoi(attrib->value());
}
else if (strcmp(nodeName, "GrillHeight") == 0) {
auto attrib = currentNode->first_attribute("Value");
if (attrib) backglass_grill_height = atoi(attrib->value());
}
else if (strcmp(nodeName, "Illumination") == 0) {
auto illuminationNode = currentNode->first_node();
int bulb = 1;
while (illuminationNode) {//Iterate all Nodes within Illumination
auto attrib = illuminationNode->first_attribute("Image");
if (attrib) {
if (data_len < attrib->value_size() * 3 / 4 + 1) {
if (data) free(data);
data_len = attrib->value_size() * 3 / 4 + 1;
data = malloc(data_len);
}
int size = decode_base64(attrib->value(), (char*)data, attrib->value_size(), data_len);
#ifdef WRITE_BACKGLASS_IMAGES
if (WRITE_BACKGLASS_IMAGES > 0 && size > 0) {//Write Image to disk. Also check if the base64 decoder is working...
std::string imageFileName = b2sFileName;
imageFileName.append(illuminationNode->name()).append(".bulb").append(std::to_string(bulb)).append(".png");//if it is not a png just rename it...
std::ofstream imageFile(imageFileName, std::ios::out | std::ios::binary | std::ios::trunc);
if (imageFile.is_open()) {
imageFile.write((char*)data, size);
imageFile.close();
}
}
#endif
if (size > 0) {
//Handle Bulb light images
}
}
illuminationNode = illuminationNode->next_sibling();
bulb++;
}
}
else if (strcmp(nodeName, "Images") == 0) {
auto imagesNode = currentNode->first_node();
while (imagesNode) {//Iterate all Nodes within Images
auto attrib = imagesNode->first_attribute("Value");
if (attrib) {
if (data_len < attrib->value_size() * 3 / 4 + 1) {
if (data) free(data);
data_len = attrib->value_size() * 3 / 4 + 1;
data = malloc(data_len);
}
int size = decode_base64(attrib->value(), (char*)data, attrib->value_size(), data_len);
if ((size > 0) && (strcmp(imagesNode->name(), "BackglassImage") == 0)) {
m_backgroundTexture = m_pd3dDevice->m_texMan.LoadTexture(BaseTexture::CreateFromData(data, size), true);
backglass_width = m_backgroundTexture->width;
backglass_height = m_backgroundTexture->height;
}
#ifdef WRITE_BACKGLASS_IMAGES
if (WRITE_BACKGLASS_IMAGES > 0 && size > 0) {//Write Image to disk. Also useful to check if the base64 decoder is working...
std::string imageFileName = b2sFileName;
imageFileName.append(imagesNode->name()).append(".png");//if it is not a png just rename it...
std::ofstream imageFile(imageFileName, std::ios::out | std::ios::binary | std::ios::trunc);
if (imageFile.is_open()) {
imageFile.write((char*)data, size);
imageFile.close();
}
}
#endif
}
imagesNode = imagesNode->next_sibling();
}
}
currentNode = currentNode->next_sibling();
}
}
catch (...) {//If file does not exist, or something else goes wrong just disable the Backglass. This is very experimental anyway.
m_backgroundTexture = NULL;
}
if (data) free(data);
float tableWidth, glassHeight;
g_pplayer->m_ptable->get_Width(&tableWidth);
g_pplayer->m_ptable->get_GlassHeight(&glassHeight);
if (backglass_width>0 && backglass_height>0)
m_pd3dDevice->DMDShader->SetVector("backBoxSize", tableWidth * (0.5f - backglass_scale / 2.0f), glassHeight, backglass_scale * tableWidth, backglass_scale * tableWidth / (float)backglass_width*(float)backglass_height);
else
m_pd3dDevice->DMDShader->SetVector("backBoxSize", tableWidth * (0.5f - backglass_scale / 2.0f), glassHeight, backglass_scale * tableWidth, backglass_scale * tableWidth / 16.0f*9.0f);
if (backglass_dmd_width > 0 && backglass_dmd_height > 0 && backglass_width > 0 && backglass_height > 0) {
dmd_width = (float)backglass_dmd_width / (float)backglass_width;
dmd_height = (float)backglass_dmd_height / (float)backglass_height;
dmd_x = tableWidth * backglass_scale * (float)backglass_dmd_x / (float)backglass_width;
dmd_y = tableWidth * backglass_scale * (1.0f- (float)backglass_dmd_y / (float)backglass_height - dmd_height);
}
#endif
}
BackGlass::~BackGlass()
{
}
void BackGlass::Render()
{
PinTable * const ptable = g_pplayer->m_ptable;
if (g_pplayer->m_capPUP && capturePUP())
{
m_backgroundTexture = m_pd3dDevice->m_texMan.LoadTexture(g_pplayer->m_texPUP, true);
backglass_width = m_backgroundTexture->width;
backglass_height = m_backgroundTexture->height;
float tableWidth, glassHeight;
g_pplayer->m_ptable->get_Width(&tableWidth);
g_pplayer->m_ptable->get_GlassHeight(&glassHeight);
if (backglass_width > 0 && backglass_height > 0)
m_pd3dDevice->DMDShader->SetVector("backBoxSize", tableWidth * (0.5f - backglass_scale / 2.0f), glassHeight, backglass_scale * tableWidth, backglass_scale * tableWidth / (float)backglass_width*(float)backglass_height);
else
m_pd3dDevice->DMDShader->SetVector("backBoxSize", tableWidth * (0.5f - backglass_scale / 2.0f), glassHeight, backglass_scale * tableWidth, backglass_scale * tableWidth / 16.0f*9.0f);
}
if (m_backgroundTexture)
m_pd3dDevice->DMDShader->SetTexture("Texture0", m_backgroundTexture, false);
else if (m_backgroundFallback)
m_pd3dDevice->DMDShader->SetTexture("Texture0", m_backgroundFallback, false);
else return;
m_pd3dDevice->SetRenderState(RenderDevice::ZWRITEENABLE, RenderDevice::RS_FALSE);
m_pd3dDevice->SetRenderState(RenderDevice::ZENABLE, FALSE);
m_pd3dDevice->SetRenderStateCulling(RenderDevice::CULL_NONE);
m_pd3dDevice->SetRenderState(RenderDevice::ALPHABLENDENABLE, RenderDevice::RS_FALSE);
m_pd3dDevice->DMDShader->SetTechnique("basic_noDMD");
m_pd3dDevice->DMDShader->SetVector("vColor_Intensity", 1.0, 1.0, 1.0, 1.0);
m_pd3dDevice->DMDShader->Begin(0);
m_pd3dDevice->DrawTexturedQuad();
m_pd3dDevice->DMDShader->End();
m_pd3dDevice->DMDShader->SetVector("quadOffsetScale", 0.0f, 0.0f, 1.0f, 1.0f);
m_pd3dDevice->SetRenderStateCulling(RenderDevice::CULL_CCW);
m_pd3dDevice->SetRenderState(RenderDevice::ZENABLE, TRUE);
m_pd3dDevice->SetRenderState(RenderDevice::ZWRITEENABLE, RenderDevice::RS_TRUE);
}
void BackGlass::DMDdraw(const float DMDposx, const float DMDposy, const float DMDwidth, const float DMDheight, const COLORREF DMDcolor, const float intensity)
{
if (g_pplayer->m_texdmd || (g_pplayer->m_capExtDMD && (FindWindowA(NULL, "Virtual DMD") != NULL || FindWindowA("pygame", NULL) != NULL))) // If DMD capture is enabled check if external DMD exists (for capturing UltraDMD+P-ROC DMD)
{
//const float width = g_pplayer->m_pin3d.m_useAA ? 2.0f*(float)m_width : (float)m_width;
m_pd3dDevice->DMDShader->SetTechnique("basic_DMD"); //!! DMD_UPSCALE ?? -> should just work
const vec4 c = convertColor(DMDcolor, intensity);
m_pd3dDevice->DMDShader->SetVector("vColor_Intensity", &c);
#ifdef DMD_UPSCALE
const vec4 r((float)(m_dmdx * 3), (float)(m_dmdy * 3), 1.f, 0.f);
#else
const vec4 r((float)g_pplayer->m_dmdx, (float)g_pplayer->m_dmdy, 1.f, 0.f);
#endif
m_pd3dDevice->DMDShader->SetVector("vRes_Alpha", &r);
// If we're capturing Freezy DMD switch to ext technique to avoid incorrect colorization
if (captureExternalDMD())
m_pd3dDevice->DMDShader->SetTechnique("basic_DMD_ext");
if (g_pplayer->m_texdmd != NULL)
m_pd3dDevice->DMDShader->SetTexture("Texture0", m_pd3dDevice->m_texMan.LoadTexture(g_pplayer->m_texdmd, false), false);
// m_pd3dPrimaryDevice->DMDShader->SetVector("quadOffsetScale", 0.0f, -1.0f, backglass_scale, backglass_scale*(float)backglass_height / (float)backglass_width);
bool zDisabled = false;
const float scale = 0.5f;// 0.5 => use 50% of the height of the grill.
m_pd3dDevice->SetRenderStateCulling(RenderDevice::CULL_NONE);
if (m_backgroundTexture) {
if (dmd_width == 0.0f || dmd_height == 0.0f) {//If file contains no valid VRDMD position
if (backglass_grill_height > 0.0f) {
//DMD is centered in the Grill of the backglass
float tableWidth;
g_pplayer->m_ptable->get_Width(&tableWidth);
tableWidth *= backglass_scale;
dmd_height = backglass_scale * scale * (float)backglass_grill_height / (float)backglass_width;
dmd_width = dmd_height / (float)(g_pplayer->m_texdmd->height()) * (float)(g_pplayer->m_texdmd->width());
dmd_x = tableWidth * (0.5f - dmd_width / 2.0f);
dmd_y = (tableWidth * (float)backglass_grill_height*(0.5f - scale / 2.0f) / (float)backglass_width);
}
}
m_pd3dDevice->DMDShader->SetVector("quadOffsetScale", dmd_x, dmd_y, dmd_width, dmd_height);
m_pd3dDevice->SetRenderState(RenderDevice::ZENABLE, FALSE);
zDisabled = true;
}
else//No VR, so place it where it was intended
m_pd3dDevice->DMDShader->SetVector("quadOffsetScale", DMDposx, DMDposy, DMDwidth, DMDheight);
m_pd3dDevice->DMDShader->Begin(0);
m_pd3dDevice->DrawTexturedQuad();
m_pd3dDevice->DMDShader->End();
if (zDisabled)
m_pd3dDevice->SetRenderState(RenderDevice::ZENABLE, TRUE);
m_pd3dDevice->DMDShader->SetVector("quadOffsetScale", 0.0f, 0.0f, 1.0f, 1.0f);
}
}