Rise
The Vieneo Province
Loading...
Searching...
No Matches
Graphics.cpp
Go to the documentation of this file.
1#include "Graphics.h"
2#include "GameClass.h"
3#include "gui.h"
4
5#include <WinSock2.h>
6#include <WS2tcpip.h>
7#include <Windows.h>
8
10#include "UI/Controls.h"
11
12#include "resource.h"
13#include "polyobj.h"
14#include <vector>
15
17{
18 gui = g;
19 logger = g->game->logger.get();
20
22 g_pEffect = nullptr;
24 for (short t = 0; t < humantextureC; t++)
25 m_pddsAvatar[t] = nullptr;
26
28}
29
34
36{
37 logger->Log("Adding font resources...");
38
39 struct FontEntry
40 {
41 const wchar_t* path;
42 const char* name;
43 };
44
45 const FontEntry fonts[] = {
46 { L"Fonts\\euroswh.ttf", "euroswh.ttf" },
47 { L"Fonts\\DejaVuSansMono.ttf", "DejaVuSansMono.ttf" },
48 { L"Fonts\\lhandw.ttf", "lhandw.ttf" },
49 { L"Fonts\\ARIALN.ttf", "ARIALN.ttf" }
50 };
51
52 for (const auto& font : fonts)
53 {
54 if (AddFontResourceEx(font.path, FR_PRIVATE, nullptr) == 0)
55 {
56 wchar_t box[128];
57 swprintf_s(box, L"Error loading %hs in Fonts\\...", font.name);
58 MessageBoxW(nullptr, box, L"Error loading font!", MB_ICONERROR | MB_OK);
59
60 char logmsg[128];
61 sprintf_s(logmsg, sizeof logmsg, "Error loading \"%s\" in Fonts\\...", font.name);
63 }
64 }
65}
66
68{
69 logger->Log("Removing font resources...");
70
71 const wchar_t* fonts[] = {
72 L"Fonts\\euroswh.ttf",
73 L"Fonts\\DejaVuSansMono.ttf",
74 L"Fonts\\lhandw.ttf",
75 L"Fonts\\ARIALN.ttf"
76 };
77
78 for (const auto& font : fonts)
79 RemoveFontResourceEx(font, FR_PRIVATE, nullptr);
80
81 // Viewscreen.cpp
82 RemoveFontResourceEx(L"Fonts\\cour.ttf", FR_PRIVATE, nullptr);
83 RemoveFontResourceEx(L"Fonts\\LicensePlate.ttf", FR_PRIVATE, nullptr);
84 RemoveFontResourceEx(L"Fonts\\pala.ttf", FR_PRIVATE, nullptr);
85 RemoveFontResourceEx(L"Fonts\\rcaf6-GIMP.ttf", FR_PRIVATE, nullptr);
86}
87
88//--------------------------------------------------------------------------------------
89// This function loads the mesh and ensures the mesh has normals; it also optimizes the
90// mesh for the graphics card's vertex cache, which improves performance by organizing
91// the internal triangle list for less cache misses.
92//--------------------------------------------------------------------------------------
93/*
94HRESULT Graphics::LoadMesh(Rendering::RenderDevice* pd3dDevice, WCHAR* strFileName, ID3DXMesh** ppMesh)
95{
96 ID3DXMesh* pMesh = nullptr;
97 WCHAR str[MAX_PATH];
98 HRESULT hr;
99
100 // ChangeCockpit the mesh with D3DX and get back a ID3DXMesh*. For this
101 // sample we'll ignore the X file's embedded materials since we know
102 // exactly the model we're loading. See the mesh samples such as
103 // "OptimizedMesh" for a more generic mesh loading example.
104 V_RETURN(DXUTFindDXSDKMediaFileCch(str, MAX_PATH, strFileName));
105
106 V_RETURN(D3DXLoadMeshFromX(str, D3DXMESH_MANAGED, pd3dDevice, NULL, NULL, NULL, NULL, &pMesh));
107
108 // Make sure there are normals which are required for lighting
109 if (!(pMesh->GetFVF() & Rendering::VertexFormat::NORMAL))
110 {
111 ID3DXMesh* pTempMesh;
112 V(pMesh->CloneMeshFVF(pMesh->GetOptions(),
113 pMesh->GetFVF() | Rendering::VertexFormat::NORMAL,
114 pd3dDevice, &pTempMesh));
115 V(D3DXComputeNormals(pTempMesh, NULL));
116
117 SAFE_RELEASE(pMesh);
118 pMesh = pTempMesh;
119 }
120
121 // Optimize the mesh for this graphics card's vertex cache
122 // so when rendering the mesh's triangle list the vertices will
123 // cache hit more often so it won't have to re-execute the vertex shader
124 // on those vertices so it will improve perf.
125 std::vector<DWORD> adjacency(pMesh->GetNumFaces() * 3);
126 if (adjacency.empty())
127 return E_OUTOFMEMORY;
128 V(pMesh->GenerateAdjacency(1e-6f, adjacency.data()));
129 V(pMesh->OptimizeInplace(D3DXMESHOPT_VERTEXCACHE, adjacency.data(), NULL, NULL, NULL));
130
131 *ppMesh = pMesh;
132
133 return S_OK;
134}
135*/
136
138{
139 HRESULT hr;
140 if (g_pEffect)
141 {
143 gui->game->UpdateTrackedResource("g_pEffect", 2);
144 }
145 return S_OK;
146}
147
149{
151 gui->game->UpdateTrackedResource("g_pEffect", 3);
152
153 char msg[99];
154
155 // These are released once we get to STATUS_LOADING
157 {
158 for (short t = 0; t < 3; t++)
159 {
161 sprintf_s(msg, sizeof msg, "m_pddsLightningAndBackground[%i]", t);
163 }
164
166 gui->game->UpdateTrackedResource("m_pddsCloudBaseMapAndNetwork", 3);
167 }
168
169 for (short t = 1; t < humantextureC; t++)
170 {
171 if (m_pddsAvatar[t])
172 {
174 sprintf_s(msg, sizeof msg, "m_pddsAvatar[%i]", t);
176 }
177 }
178}
179
180void Graphics::DrawImmigration(Rendering::RenderDevice* pd3dDevice, float fElapsedTime)
181{
182 logger->AddToCallStack("Graphics::DrawImmigration");
183
184 RiseMath::Matrix matScale;
185 RiseMath::MatrixScaling(&matScale, gui->game->displayscale, gui->game->displayscale, 1.0f); // both use Y due to locked 7x5 aspect
186 gui->g_pTextSprite->SetTransform(&matScale);
187
189
190 // background
191 gui->g_pTextSprite->Draw(gui->m_pddsGUIInterlace, nullptr, nullptr, nullptr, 0xFFFFFFFF);
192
193 // lights top left
195
198
200
201 if (gui->tempobject.VB)
202 {
203 logger->AddToCallStack("Graphics::DrawImmigration HUMAN");
204
205#pragma region Human
209
210 // Turn off D3D lighting, since we are providing our own vertex colors
212
213 // ReSharper disable once CppEntityAssignedButNoRead
214 HRESULT hr;
215 Rendering::Viewport mvp, oldmvp;
216 // 49,271 and 128x128
217 // all of these use displayscaley because the aspect is locked
218 mvp.X = static_cast<DWORD>(49.0f * gui->game->displayscale);
219 mvp.Y = static_cast<DWORD>(271.0f * gui->game->displayscale);
220 mvp.Width = static_cast<DWORD>(128.0f * gui->game->displayscale);
221 mvp.Height = static_cast<DWORD>(128.0f * gui->game->displayscale);
222 mvp.MinZ = 0.0001f; mvp.MaxZ = 0.01f;
223 V(pd3dDevice->GetViewport(&oldmvp));
224 V(pd3dDevice->SetViewport(&mvp));
225
226 V(pd3dDevice->Clear(0, NULL, Rendering::ClearFlags::TARGET | Rendering::ClearFlags::ZBUFFER, 0xFFC0B19C, 1.0f, 0));
227
228 RiseMath::Matrix matWorld, matrixBase;
230
231 // Set up the rotation matrix to generate 1 full rotation (2*PI radians)
232 // every 1000 ms. To avoid the loss of precision inherent in very high
233 // floating point numbers, the system time is modulated by the rotation
234 // period before conversion to a radian angle.
235
236 // Set up our view matrix. A view matrix can be defined given an eye point,
237 // a point to lookat, and a direction for which way is up. Here, we set the
238 // eye five units back along the z-axis and up three units, look at the
239 // origin, and define "up" to be in the y-direction.
240 RiseMath::Vector3 vEyePt(0.0f, 0.0f, 0.0005f);
241 RiseMath::Vector3 vLookatPt(0.0f, 0.0f, 0.0f);
242 RiseMath::Vector3 vUpVec(0.0f, 1.0f, 0.0f);
243 RiseMath::Matrix matView;
244 RiseMath::MatrixLookAtRH(&matView, &vEyePt, &vLookatPt, &vUpVec);
245
246
247 // For the projection matrix, we set up a perspective transform (which
248 // transforms geometry from 3D view space to 2D viewport space, with
249 // a perspective divide making objects smaller in the distance). To build
250 // a perpsective transform, we need the field of view (1/4 pi is common),
251 // the aspect ratio, and the near and far clipping planes (which define at
252 // what distances geometry should be no longer be rendered).
253 RiseMath::Matrix matProj;
254 RiseMath::MatrixPerspectiveFovRH(&matProj, RiseMath::Pi / 4, 1.0f, 0.0001f, 0.01f);
255
256
261
262 // Render the vertex buffer contents
263 pd3dDevice->SetStreamSource(0, gui->tempobject.VB, 0, sizeof(RiseVertexVertex));
264 pd3dDevice->SetFVF(RISE_FVF_VERTEX);
265 bool firstone = true;
266
267 // Keep the captured pose until the snapshot is cleared.
268 if (!gui->pBuffer)
269 immigrationPanAngle = sinf(static_cast<float>(Rendering::ClientTime()) * 1.0472f) * RiseMath::Pi * 0.125f;
270
271 for (short t = 0; t < gui->polyobj.GetComponents(); t++)
272 {
273 const s_mesh_component tempcomponent = gui->polyobj.GetComponent(t);
274 if (tempcomponent.type == 100 || // torso
275 tempcomponent.type == 101 || // head
276 tempcomponent.type == 103 || // UL
277 tempcomponent.type == 104) // UR
278 {
280 matWorld = matrixBase * matWorld;
281 RiseMath::Matrix matTemp;
282 if (tempcomponent.type == 100 && !firstone && gui->g_HUD.GetRadioButton(IDC_MALE)->GetChecked())
283 RiseMath::MatrixTranslation(&matTemp, tempcomponent.nominalxyz.x, tempcomponent.nominalxyz.y + 0.00009f, tempcomponent.nominalxyz.z - 0.00165f);
284 else
285 {
286 RiseMath::MatrixTranslation(&matTemp, tempcomponent.nominalxyz.x, tempcomponent.nominalxyz.y, tempcomponent.nominalxyz.z - 0.00165f);
287 if (tempcomponent.type == 100 && gui->g_HUD.GetRadioButton(IDC_MALE)->GetChecked())
288 firstone = false;
289 }
290 matWorld = matTemp * matWorld;
292 matWorld = matWorld * matTemp;
293
294 if (tempcomponent.attachto == 0 || // unassigned
295 (tempcomponent.type == 100 && tempcomponent.attachto == 250) || // bare torso
296 tempcomponent.attachto == gui->g_HUD.GetSlider(IDC_NOSE)->GetValue() ||
297 tempcomponent.attachto == gui->g_HUD.GetSlider(IDC_EARS)->GetValue() ||
298 tempcomponent.attachto == gui->g_HUD.GetSlider(IDC_JAWLINE)->GetValue() ||
299 tempcomponent.attachto == gui->g_HUD.GetSlider(IDC_HAIRSTYLE)->GetValue() ||
300 tempcomponent.attachto == gui->g_HUD.GetSlider(IDC_HEAD)->GetValue() ||
301 tempcomponent.attachto == gui->g_HUD.GetSlider(IDC_FEATURE)->GetValue())
302 {
303 RiseMath::Matrix mWorldViewProj = matWorld * matView * matProj;
304 V(g_pEffect->SetMatrix("matWorldViewProj", &mWorldViewProj));
305 V(g_pEffect->SetTexture("ColorMap", m_pddsAvatar[tempcomponent.texture]));
306 RiseMath::Vector4 tempvec4;
307 // age 18-88 is 0-1,
308 float powage = powf(gui->age, 2.0f);
309 tempvec4.x = gui->haircolor.r * (1.0f - powage) + 1.0f * powage; // hair/lips
310 tempvec4.y = gui->haircolor.g * (1.0f - powage) + 1.0f * powage;
311 tempvec4.z = gui->haircolor.b * (1.0f - powage) + 1.0f * powage;
312 tempvec4.w = gui->stubbleFreckle;
313 V(g_pEffect->SetVector("Radiosity", &tempvec4));
314 tempvec4.x = gui->skintone.r; // skin
315 tempvec4.y = gui->skintone.g;
316 tempvec4.z = gui->skintone.b;
317 tempvec4.w = gui->age;
318 V(g_pEffect->SetVector("shiplight", &tempvec4));
319 tempvec4.x = gui->eyecolor.r; // eyes
320 tempvec4.y = gui->eyecolor.g;
321 tempvec4.z = gui->eyecolor.b;
322 tempvec4.w = 0.0f; // unused but used in Makeship to signal whether to use alpha as transparency or colorization
323 V(g_pEffect->SetVector("ambient", &tempvec4));
324 g_pEffect->Begin(nullptr, 0);
326 pd3dDevice->DrawPrimitive(Rendering::Primitive::TriangleList, tempcomponent.startvertex, tempcomponent.primitives);
328 g_pEffect->End();
329 }
330 else if (tempcomponent.attachto == gui->g_HUD.GetSlider(IDC_FRAMETYPE)->GetValue() &&
332 {
333 // eye glasses
337
338 pd3dDevice->SetTransform(Rendering::TransformSlot::WORLD, &matWorld);
339 pd3dDevice->SetTransform(Rendering::TransformSlot::VIEW, &matView);
341
342 pd3dDevice->SetTexture(0, m_pddsAvatar[tempcomponent.texture]);
343
344 pd3dDevice->DrawPrimitive(Rendering::Primitive::TriangleList, tempcomponent.startvertex, tempcomponent.primitives);
345 }
346 }
347 }
348
349 V(pd3dDevice->SetViewport(&oldmvp)); // restore
350
355
356#pragma endregion
357
358 logger->AddToCallStack("Graphics::DrawImmigration DONE");
359 }
360
361 logger->AddToCallStack("Graphics::DrawImmigration HUD");
362
363 gui->g_HUD.OnRender(fElapsedTime);
364}
365
367{
368 auto* renderer=Rendering::ClientRenderer();if(!renderer || !renderer->Native())return E_UNEXPECTED;
372 logger->Log("Native D3D11 scene materials initialized from embedded SM5 shaders");
373 return S_OK;
374}
#define V_RETURN(expression)
#define SAFE_RELEASE(value)
#define V(expression)
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
@ STATUS_LOADING
Definition GameState.h:8
#define humantextureC
Definition Graphics.h:11
GameState displaystage
Definition GameClass.h:118
void AddTrackedResource(const char *name, Rendering::ResourcePool pool=Rendering::ResourcePool::Managed)
void UpdateTrackedResource(const char *name, int status)
float displayscale
Definition GameClass.h:130
std::unique_ptr< Logger > logger
Definition GameClass.h:109
void RemoveFontsFromSystem(void) const
Definition Graphics.cpp:67
Rendering::RenderTexture * m_pddsCloudBaseMapAndNetwork
Definition Graphics.h:31
void AddFontsToSystem(void) const
Definition Graphics.cpp:35
HMI * gui
Definition Graphics.h:37
HRESULT OnResetDevice(void) const
Definition Graphics.cpp:137
Graphics(HMI *g)
Definition Graphics.cpp:16
HRESULT LoadShaderFile(void)
Definition Graphics.cpp:366
Logger * logger
Definition Graphics.h:38
void OnDestroyDevice(void)
Definition Graphics.cpp:148
float immigrationPanAngle
Definition Graphics.h:36
void DrawImmigration(Rendering::RenderDevice *pd3dDevice, float fElapsedTime)
Definition Graphics.cpp:180
Rendering::RenderMaterial * g_pEffect
Definition Graphics.h:30
Rendering::RenderTexture * m_pddsAvatar[humantextureC]
Definition Graphics.h:33
Rendering::RenderTexture * m_pddsLightningAndBackground[3]
Definition Graphics.h:32
Definition gui.h:1012
bool immigrationPassed
Definition gui.h:1107
GameClass * game
Definition gui.h:1213
float age
Definition gui.h:1082
Rendering::RenderTexture * m_pddsGUIInterlace
Definition gui.h:1061
s_polygon_object tempobject
Definition gui.h:1105
RiseDialog g_HUD
Definition gui.h:1031
void DrawErrorBoxes(bool draw=true)
Definition gui.cpp:4848
PolyObj polyobj
Definition gui.h:1106
RiseMath::Color haircolor
Definition gui.h:1081
void DrawRXTXLights(Rendering::RenderTexture **tex) const
Definition gui.cpp:4770
Rendering::ImageBuffer * pBuffer
Definition gui.h:1089
float stubbleFreckle
Definition gui.h:1082
RiseMath::Color skintone
Definition gui.h:1081
RiseMath::Color eyecolor
Definition gui.h:1081
float weight
Definition gui.h:1082
float height
Definition gui.h:1082
Rendering::RenderSprite * g_pTextSprite
Definition gui.h:1051
void Log(const char *msg, Level level=Info, int errorCode=0)
Definition Logger.cpp:283
void AddToCallStack(const char *msg)
Definition Logger.cpp:355
@ Error
Definition Logger.h:28
s_mesh_component GetComponent(short i) const
Definition polyobj.cpp:192
unsigned short GetComponents() const
Definition polyobj.cpp:187
HRESULT DrawPrimitive(Primitive type, UINT first, UINT count)
HRESULT GetViewport(Viewport *value) const
HRESULT SetStreamSource(UINT stream, RenderBuffer *buffer, UINT offset, UINT stride)
HRESULT SetRenderState(DWORD state, DWORD value)
HRESULT SetFVF(DWORD value)
HRESULT SetViewport(const Viewport *value)
HRESULT SetTransform(DWORD slot, const RiseMath::Matrix *value)
HRESULT Clear(DWORD count, const RECT *rectangles, DWORD flags, PackedColor color, float depth, DWORD stencil)
HRESULT SetTextureStageState(DWORD stage, DWORD state, DWORD value)
HRESULT SetTexture(DWORD stage, RenderTexture *texture)
HRESULT SetSamplerState(DWORD sampler, DWORD state, DWORD value)
HRESULT SetVector(const char *name, const RiseMath::Vector4 *value)
HRESULT Begin(UINT *count, DWORD flags)
HRESULT BeginPass(UINT pass)
HRESULT SetTexture(const char *name, RenderTexture *value)
HRESULT SetMatrix(const char *name, const RiseMath::Matrix *value)
HRESULT Begin(DWORD flags)
HRESULT SetTransform(const RiseMath::Matrix *value)
HRESULT Draw(RenderTexture *image, const RECT *rectangle, const RiseMath::Vector3 *center, const RiseMath::Vector3 *position, PackedColor color)
bool GetChecked()
Definition Controls.h:766
RiseRadioButton * GetRadioButton(int ID)
Definition Controls.h:184
HRESULT OnRender(float fElapsedTime)
Definition Controls.cpp:526
RiseSlider * GetSlider(int ID)
Definition Controls.h:192
int GetValue() const
Definition Controls.h:1106
#define RISE_FVF_VERTEX
Definition globals.h:118
@ IDC_HAIRSTYLE
Definition gui.h:725
@ IDC_HEAD
Definition gui.h:721
@ IDC_FRAMES
Definition gui.h:717
@ IDC_MALE
Definition gui.h:686
@ IDC_FRAMETYPE
Definition gui.h:729
@ IDC_EARS
Definition gui.h:733
@ IDC_NOSE
Definition gui.h:723
@ IDC_FEATURE
Definition gui.h:735
@ IDC_JAWLINE
Definition gui.h:731
constexpr uint32_t WRAP
constexpr uint32_t INVSRCALPHA
constexpr uint32_t SRCALPHA
constexpr uint32_t TARGET
constexpr uint32_t ZBUFFER
constexpr uint32_t LESS
constexpr uint32_t ALWAYS
constexpr uint32_t CW
constexpr uint32_t SRCBLEND
constexpr uint32_t CULLMODE
constexpr uint32_t ALPHABLENDENABLE
constexpr uint32_t DESTBLEND
constexpr uint32_t TEXTUREFACTOR
constexpr uint32_t LIGHTING
constexpr uint32_t ZFUNC
constexpr uint32_t ADDRESSV
constexpr DWORD AlphaBlend
constexpr uint32_t TFACTOR
constexpr uint32_t TEXTURE
constexpr uint32_t MODULATE
constexpr uint32_t COLOROP
constexpr uint32_t COLORARG1
constexpr uint32_t COLORARG2
constexpr uint32_t VIEW
constexpr uint32_t PROJECTION
constexpr uint32_t WORLD
double ClientTime()
RenderDevice * ClientRenderer()
constexpr float Pi
Definition ClientMath.h:12
Matrix * MatrixLookAtRH(Matrix *out, const Vector3 *eye, const Vector3 *at, const Vector3 *up)
Definition ClientMath.h:141
Matrix * MatrixRotationX(Matrix *out, float value)
Definition ClientMath.h:126
Matrix * MatrixRotationY(Matrix *out, float value)
Definition ClientMath.h:127
Matrix * MatrixPerspectiveFovRH(Matrix *out, float fov, float aspect, float nearZ, float farZ)
Definition ClientMath.h:151
Matrix * MatrixScaling(Matrix *out, float x, float y, float z)
Definition ClientMath.h:124
Matrix * MatrixTranslation(Matrix *out, float x, float y, float z)
Definition ClientMath.h:125
constexpr float HalfPi
Definition ClientMath.h:12
RiseMath::Vector3 nominalxyz
Definition globals.h:413
unsigned char type
Definition globals.h:403
Rendering::RenderBuffer * VB
Definition globals.h:360