2015-07-02 11:07:50 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2022-01-10 20:13:43 +00:00
|
|
|
Copyright (c) 2006-2022, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2015-07-02 11:07:50 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2021-07-29 11:28:51 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
2015-07-02 11:07:50 +00:00
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2021-07-29 11:28:51 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-07-02 11:07:50 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2021-07-29 11:28:51 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-07-02 11:07:50 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2021-07-29 11:28:51 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-07-02 11:07:50 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2021-07-29 11:28:51 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-07-02 11:07:50 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "assimp_view.h"
|
2018-01-07 05:00:11 +00:00
|
|
|
#include <assimp/StringUtils.h>
|
2015-07-02 11:07:50 +00:00
|
|
|
#include <map>
|
|
|
|
|
2018-05-07 22:28:53 +00:00
|
|
|
#ifdef __MINGW32__
|
2020-05-07 12:00:40 +00:00
|
|
|
#include <mmsystem.h>
|
2018-05-07 22:28:53 +00:00
|
|
|
#else
|
2020-05-07 12:00:40 +00:00
|
|
|
#include <timeapi.h>
|
2018-05-07 22:28:53 +00:00
|
|
|
#endif
|
|
|
|
|
2015-07-02 11:07:50 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace AssimpView {
|
|
|
|
|
|
|
|
extern std::string g_szNormalsShader;
|
|
|
|
extern std::string g_szDefaultShader;
|
|
|
|
extern std::string g_szPassThroughShader;
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
HINSTANCE g_hInstance = nullptr;
|
|
|
|
HWND g_hDlg = nullptr;
|
|
|
|
IDirect3D9 *g_piD3D = nullptr;
|
|
|
|
IDirect3DDevice9 *g_piDevice = nullptr;
|
|
|
|
IDirect3DVertexDeclaration9 *gDefaultVertexDecl = nullptr;
|
|
|
|
double g_fFPS = 0.0f;
|
2015-07-02 11:07:50 +00:00
|
|
|
char g_szFileName[MAX_PATH];
|
2020-05-07 12:00:40 +00:00
|
|
|
ID3DXEffect *g_piDefaultEffect = nullptr;
|
|
|
|
ID3DXEffect *g_piNormalsEffect = nullptr;
|
|
|
|
ID3DXEffect *g_piPassThroughEffect = nullptr;
|
|
|
|
ID3DXEffect *g_piPatternEffect = nullptr;
|
|
|
|
bool g_bMousePressed = false;
|
|
|
|
bool g_bMousePressedR = false;
|
|
|
|
bool g_bMousePressedM = false;
|
|
|
|
bool g_bMousePressedBoth = false;
|
|
|
|
float g_fElpasedTime = 0.0f;
|
2015-07-02 11:07:50 +00:00
|
|
|
D3DCAPS9 g_sCaps;
|
2020-05-07 12:00:40 +00:00
|
|
|
bool g_bLoadingFinished = false;
|
|
|
|
HANDLE g_hThreadHandle = nullptr;
|
|
|
|
float g_fWheelPos = -10.0f;
|
|
|
|
bool g_bLoadingCanceled = false;
|
|
|
|
IDirect3DTexture9 *g_pcTexture = nullptr;
|
|
|
|
bool g_bPlay = false;
|
2015-07-02 11:07:50 +00:00
|
|
|
double g_dCurrent = 0.;
|
|
|
|
|
|
|
|
// default pp steps
|
|
|
|
unsigned int ppsteps = aiProcess_CalcTangentSpace | // calculate tangents and bitangents if possible
|
2020-05-07 12:00:40 +00:00
|
|
|
aiProcess_JoinIdenticalVertices | // join identical vertices/ optimize indexing
|
|
|
|
aiProcess_ValidateDataStructure | // perform a full validation of the loader's output
|
|
|
|
aiProcess_ImproveCacheLocality | // improve the cache locality of the output vertices
|
|
|
|
aiProcess_RemoveRedundantMaterials | // remove redundant materials
|
|
|
|
aiProcess_FindDegenerates | // remove degenerated polygons from the import
|
|
|
|
aiProcess_FindInvalidData | // detect invalid model data, such as invalid normal vectors
|
|
|
|
aiProcess_GenUVCoords | // convert spherical, cylindrical, box and planar mapping to proper UVs
|
|
|
|
aiProcess_TransformUVCoords | // preprocess UV transformations (scaling, translation ...)
|
|
|
|
aiProcess_FindInstances | // search for instanced meshes and remove them by references to one master
|
|
|
|
aiProcess_LimitBoneWeights | // limit bone weights to 4 per vertex
|
|
|
|
aiProcess_OptimizeMeshes | // join small meshes, if possible;
|
|
|
|
aiProcess_SplitByBoneCount | // split meshes with too many bones. Necessary for our (limited) hardware skinning shader
|
|
|
|
0;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
unsigned int ppstepsdefault = ppsteps;
|
|
|
|
|
|
|
|
bool nopointslines = false;
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
extern bool g_bWasFlipped /*= false*/;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
aiMatrix4x4 g_mWorld;
|
|
|
|
aiMatrix4x4 g_mWorldRotate;
|
2020-05-07 12:00:40 +00:00
|
|
|
aiVector3D g_vRotateSpeed = aiVector3D(0.5f, 0.5f, 0.5f);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// NOTE: The second light direction is now computed from the first
|
2020-05-07 12:00:40 +00:00
|
|
|
aiVector3D g_avLightDirs[1] = { aiVector3D(-0.5f, 0.6f, 0.2f) };
|
|
|
|
|
|
|
|
D3DCOLOR g_avLightColors[3] = {
|
|
|
|
D3DCOLOR_ARGB(0xFF, 0xFF, 0xFF, 0xFF),
|
|
|
|
D3DCOLOR_ARGB(0xFF, 0xFF, 0x00, 0x00),
|
|
|
|
D3DCOLOR_ARGB(0xFF, 0x05, 0x05, 0x05),
|
2015-07-02 11:07:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
POINT g_mousePos;
|
|
|
|
POINT g_LastmousePos;
|
2020-05-07 12:00:40 +00:00
|
|
|
bool g_bFPSView = false;
|
|
|
|
bool g_bInvert = false;
|
|
|
|
EClickPos g_eClick = EClickPos_Circle;
|
|
|
|
unsigned int g_iCurrentColor = 0;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
float g_fLightIntensity = 1.0f;
|
|
|
|
float g_fLightColor = 1.0f;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
RenderOptions g_sOptions;
|
|
|
|
Camera g_sCamera;
|
2020-05-07 12:00:40 +00:00
|
|
|
AssetHelper *g_pcAsset = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
//
|
2020-05-07 12:00:40 +00:00
|
|
|
// Contains the mask image for the HUD
|
2015-07-02 11:07:50 +00:00
|
|
|
// (used to determine the position of a click)
|
|
|
|
//
|
2020-05-07 12:00:40 +00:00
|
|
|
unsigned char *g_szImageMask = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
float g_fLoadTime = 0.0f;
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// Entry point for the loader thread
|
2018-05-09 07:51:05 +00:00
|
|
|
// The loader thread loads the asset while the progress dialog displays the
|
2015-07-02 11:07:50 +00:00
|
|
|
// smart progress bar
|
|
|
|
//-------------------------------------------------------------------------------
|
2023-03-01 20:40:45 +00:00
|
|
|
DWORD WINAPI LoadThreadProc(LPVOID) {
|
2015-07-02 11:07:50 +00:00
|
|
|
// get current time
|
|
|
|
double fCur = (double)timeGetTime();
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
aiPropertyStore *props = aiCreatePropertyStore();
|
|
|
|
aiSetImportPropertyInteger(props, AI_CONFIG_IMPORT_TER_MAKE_UVS, 1);
|
|
|
|
aiSetImportPropertyFloat(props, AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE, g_smoothAngle);
|
|
|
|
aiSetImportPropertyInteger(props, AI_CONFIG_PP_SBP_REMOVE, nopointslines ? aiPrimitiveType_LINE | aiPrimitiveType_POINT : 0);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
aiSetImportPropertyInteger(props, AI_CONFIG_GLOB_MEASURE_TIME, 1);
|
2015-07-02 11:07:50 +00:00
|
|
|
//aiSetImportPropertyInteger(props,AI_CONFIG_PP_PTV_KEEP_HIERARCHY,1);
|
|
|
|
|
|
|
|
// Call ASSIMPs C-API to load the file
|
2020-05-07 12:00:40 +00:00
|
|
|
g_pcAsset->pcScene = (aiScene *)aiImportFileExWithProperties(g_szFileName,
|
|
|
|
ppsteps | /* configurable pp steps */
|
|
|
|
aiProcess_GenSmoothNormals | // generate smooth normal vectors if not existing
|
|
|
|
aiProcess_SplitLargeMeshes | // split large, unrenderable meshes into submeshes
|
|
|
|
aiProcess_Triangulate | // triangulate polygons with more than 3 edges
|
|
|
|
aiProcess_ConvertToLeftHanded | // convert everything to D3D left handed space
|
|
|
|
aiProcess_SortByPType | // make 'clean' meshes which consist of a single typ of primitives
|
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
props);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
aiReleasePropertyStore(props);
|
|
|
|
|
|
|
|
// get the end time of zje operation, calculate delta t
|
|
|
|
double fEnd = (double)timeGetTime();
|
|
|
|
g_fLoadTime = (float)((fEnd - fCur) / 1000);
|
|
|
|
g_bLoadingFinished = true;
|
|
|
|
|
|
|
|
// check whether the loading process has failed ...
|
2020-05-07 12:00:40 +00:00
|
|
|
if (nullptr == g_pcAsset->pcScene) {
|
2015-07-02 11:07:50 +00:00
|
|
|
CLogDisplay::Instance().AddEntry("[ERROR] Unable to load this asset:",
|
2020-05-07 12:00:40 +00:00
|
|
|
D3DCOLOR_ARGB(0xFF, 0xFF, 0, 0));
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// print ASSIMPs error string to the log display
|
|
|
|
CLogDisplay::Instance().AddEntry(aiGetErrorString(),
|
2020-05-07 12:00:40 +00:00
|
|
|
D3DCOLOR_ARGB(0xFF, 0xFF, 0, 0));
|
2015-07-02 11:07:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// load the current asset
|
|
|
|
// THe path to the asset is specified in the global path variable
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
int LoadAsset() {
|
2018-05-31 18:15:13 +00:00
|
|
|
// set the world and world rotation matrices to the identity
|
2015-07-02 11:07:50 +00:00
|
|
|
g_mWorldRotate = aiMatrix4x4();
|
|
|
|
g_mWorld = aiMatrix4x4();
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
// char szTemp[MAX_PATH+64];
|
|
|
|
// sprintf(szTemp,"Starting to load %s",g_szFileName);
|
2015-07-02 11:07:50 +00:00
|
|
|
CLogWindow::Instance().WriteLine(
|
2020-05-07 12:00:40 +00:00
|
|
|
"----------------------------------------------------------------------------");
|
|
|
|
// CLogWindow::Instance().WriteLine(szTemp);
|
|
|
|
// CLogWindow::Instance().WriteLine(
|
|
|
|
// "----------------------------------------------------------------------------");
|
2015-07-02 11:07:50 +00:00
|
|
|
CLogWindow::Instance().SetAutoUpdate(false);
|
|
|
|
|
|
|
|
// create a helper thread to load the asset
|
|
|
|
DWORD dwID;
|
|
|
|
g_bLoadingCanceled = false;
|
|
|
|
g_pcAsset = new AssetHelper();
|
2020-05-07 12:00:40 +00:00
|
|
|
g_hThreadHandle = CreateThread(nullptr, 0, &LoadThreadProc, nullptr, 0, &dwID);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
if (!g_hThreadHandle) {
|
2015-07-02 11:07:50 +00:00
|
|
|
CLogDisplay::Instance().AddEntry(
|
2020-05-07 12:00:40 +00:00
|
|
|
"[ERROR] Unable to create helper thread for loading",
|
|
|
|
D3DCOLOR_ARGB(0xFF, 0xFF, 0, 0));
|
2015-07-02 11:07:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// show the progress bar dialog
|
2020-05-07 12:00:40 +00:00
|
|
|
DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_LOADDIALOG),
|
|
|
|
g_hDlg, &ProgressMessageProc);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// update the log window
|
|
|
|
CLogWindow::Instance().SetAutoUpdate(true);
|
|
|
|
CLogWindow::Instance().Update();
|
|
|
|
|
|
|
|
// now we should have loaded the asset. Check this ...
|
|
|
|
g_bLoadingFinished = false;
|
2020-05-07 12:00:40 +00:00
|
|
|
if (!g_pcAsset || !g_pcAsset->pcScene) {
|
|
|
|
if (g_pcAsset) {
|
2015-07-02 11:07:50 +00:00
|
|
|
delete g_pcAsset;
|
2020-04-07 14:56:22 +00:00
|
|
|
g_pcAsset = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate a new MeshHelper array and build a new instance
|
|
|
|
// for each mesh in the original asset
|
2020-05-07 12:00:40 +00:00
|
|
|
g_pcAsset->apcMeshes = new AssetHelper::MeshHelper *[g_pcAsset->pcScene->mNumMeshes]();
|
|
|
|
for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes; ++i)
|
2015-07-02 11:07:50 +00:00
|
|
|
g_pcAsset->apcMeshes[i] = new AssetHelper::MeshHelper();
|
|
|
|
|
|
|
|
// create animator
|
2020-05-07 12:00:40 +00:00
|
|
|
g_pcAsset->mAnimator = new SceneAnimator(g_pcAsset->pcScene);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// build a new caption string for the viewer
|
2020-05-07 12:00:40 +00:00
|
|
|
static const size_t Size = MAX_PATH + 10;
|
|
|
|
char szOut[Size];
|
|
|
|
ai_snprintf(szOut, Size, AI_VIEW_CAPTION_BASE " [%s]", g_szFileName);
|
|
|
|
SetWindowText(g_hDlg, szOut);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// scale the asset vertices to fit into the viewer window
|
|
|
|
ScaleAsset();
|
|
|
|
|
|
|
|
// reset the camera view to the default position
|
2020-05-07 12:00:40 +00:00
|
|
|
g_sCamera.vPos = aiVector3D(0.0f, 0.0f, -10.0f);
|
|
|
|
g_sCamera.vLookAt = aiVector3D(0.0f, 0.0f, 1.0f);
|
|
|
|
g_sCamera.vUp = aiVector3D(0.0f, 1.0f, 0.0f);
|
|
|
|
g_sCamera.vRight = aiVector3D(0.0f, 1.0f, 0.0f);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// build native D3D vertex/index buffers, textures, materials
|
2020-05-07 12:00:40 +00:00
|
|
|
if (1 != CreateAssetData())
|
2015-07-02 11:07:50 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!g_pcAsset->pcScene->HasAnimations()) {
|
2020-05-07 12:00:40 +00:00
|
|
|
EnableWindow(GetDlgItem(g_hDlg, IDC_PLAY), FALSE);
|
|
|
|
EnableWindow(GetDlgItem(g_hDlg, IDC_SLIDERANIM), FALSE);
|
|
|
|
} else {
|
|
|
|
EnableWindow(GetDlgItem(g_hDlg, IDC_PLAY), TRUE);
|
|
|
|
EnableWindow(GetDlgItem(g_hDlg, IDC_SLIDERANIM), TRUE);
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CLogDisplay::Instance().AddEntry("[OK] The asset has been loaded successfully");
|
|
|
|
CDisplay::Instance().FillDisplayList();
|
|
|
|
CDisplay::Instance().FillAnimList();
|
|
|
|
|
|
|
|
CDisplay::Instance().FillDefaultStatistics();
|
2020-05-07 12:00:40 +00:00
|
|
|
|
2015-07-02 11:07:50 +00:00
|
|
|
// render the scene once
|
|
|
|
CDisplay::Instance().OnRender();
|
|
|
|
|
|
|
|
g_pcAsset->iNormalSet = AssetHelper::ORIGINAL;
|
|
|
|
g_bWasFlipped = false;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// Delete the loaded asset
|
2023-11-23 20:08:01 +00:00
|
|
|
// The function does nothing if no asset is loaded
|
2015-07-02 11:07:50 +00:00
|
|
|
//-------------------------------------------------------------------------------
|
2019-02-04 21:15:15 +00:00
|
|
|
int DeleteAsset(void) {
|
|
|
|
if (!g_pcAsset) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// don't anymore know why this was necessary ...
|
|
|
|
CDisplay::Instance().OnRender();
|
|
|
|
|
|
|
|
// delete everything
|
|
|
|
DeleteAssetData();
|
2020-05-07 12:00:40 +00:00
|
|
|
for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes; ++i) {
|
2015-07-02 11:07:50 +00:00
|
|
|
delete g_pcAsset->apcMeshes[i];
|
|
|
|
}
|
|
|
|
aiReleaseImport(g_pcAsset->pcScene);
|
|
|
|
delete[] g_pcAsset->apcMeshes;
|
|
|
|
delete g_pcAsset->mAnimator;
|
|
|
|
delete g_pcAsset;
|
2020-04-07 14:56:22 +00:00
|
|
|
g_pcAsset = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// reset the caption of the viewer window
|
2020-05-07 12:00:40 +00:00
|
|
|
SetWindowText(g_hDlg, AI_VIEW_CAPTION_BASE);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// clear UI
|
|
|
|
CDisplay::Instance().ClearAnimList();
|
|
|
|
CDisplay::Instance().ClearDisplayList();
|
|
|
|
|
|
|
|
CMaterialManager::Instance().Reset();
|
|
|
|
UpdateWindow(g_hDlg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// Calculate the boundaries of a given node and all of its children
|
|
|
|
// The boundaries are in Worldspace (AABB)
|
|
|
|
// piNode Input node
|
|
|
|
// p_avOut Receives the min/max boundaries. Must point to 2 vec3s
|
|
|
|
// piMatrix Transformation matrix of the graph at this position
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
int CalculateBounds(aiNode *piNode, aiVector3D *p_avOut, const aiMatrix4x4 &piMatrix) {
|
2020-04-07 14:56:22 +00:00
|
|
|
ai_assert(nullptr != piNode);
|
|
|
|
ai_assert(nullptr != p_avOut);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
aiMatrix4x4 mTemp = piNode->mTransformation;
|
|
|
|
mTemp.Transpose();
|
|
|
|
aiMatrix4x4 aiMe = mTemp * piMatrix;
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
for (unsigned int i = 0; i < piNode->mNumMeshes; ++i) {
|
|
|
|
for (unsigned int a = 0; a < g_pcAsset->pcScene->mMeshes[piNode->mMeshes[i]]->mNumVertices; ++a) {
|
|
|
|
aiVector3D pc = g_pcAsset->pcScene->mMeshes[piNode->mMeshes[i]]->mVertices[a];
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
aiVector3D pc1;
|
2020-05-07 12:00:40 +00:00
|
|
|
D3DXVec3TransformCoord((D3DXVECTOR3 *)&pc1, (D3DXVECTOR3 *)&pc,
|
|
|
|
(D3DXMATRIX *)&aiMe);
|
|
|
|
|
|
|
|
p_avOut[0].x = min(p_avOut[0].x, pc1.x);
|
|
|
|
p_avOut[0].y = min(p_avOut[0].y, pc1.y);
|
|
|
|
p_avOut[0].z = min(p_avOut[0].z, pc1.z);
|
|
|
|
p_avOut[1].x = max(p_avOut[1].x, pc1.x);
|
|
|
|
p_avOut[1].y = max(p_avOut[1].y, pc1.y);
|
|
|
|
p_avOut[1].z = max(p_avOut[1].z, pc1.z);
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
for (unsigned int i = 0; i < piNode->mNumChildren; ++i) {
|
|
|
|
CalculateBounds(piNode->mChildren[i], p_avOut, aiMe);
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// Scale the asset that it fits perfectly into the viewer window
|
|
|
|
// The function calculates the boundaries of the mesh and modifies the
|
|
|
|
// global world transformation matrix according to the aset AABB
|
|
|
|
//-------------------------------------------------------------------------------
|
2023-03-01 20:40:45 +00:00
|
|
|
int ScaleAsset() {
|
2020-05-07 12:00:40 +00:00
|
|
|
aiVector3D aiVecs[2] = { aiVector3D(1e10f, 1e10f, 1e10f),
|
|
|
|
aiVector3D(-1e10f, -1e10f, -1e10f) };
|
2015-07-02 11:07:50 +00:00
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_pcAsset->pcScene->mRootNode) {
|
2015-07-02 11:07:50 +00:00
|
|
|
aiMatrix4x4 m;
|
2020-05-07 12:00:40 +00:00
|
|
|
CalculateBounds(g_pcAsset->pcScene->mRootNode, aiVecs, m);
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
aiVector3D vDelta = aiVecs[1] - aiVecs[0];
|
|
|
|
aiVector3D vHalf = aiVecs[0] + (vDelta / 2.0f);
|
2015-07-02 11:07:50 +00:00
|
|
|
float fScale = 10.0f / vDelta.Length();
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
g_mWorld = aiMatrix4x4(
|
|
|
|
1.0f, 0.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
-vHalf.x, -vHalf.y, -vHalf.z, 1.0f) *
|
|
|
|
aiMatrix4x4(
|
|
|
|
fScale, 0.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, fScale, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, fScale, 0.0f,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
2015-07-02 11:07:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// Generate a vertex buffer which holds the normals of the asset as
|
|
|
|
// a list of unconnected lines
|
|
|
|
// pcMesh Input mesh
|
|
|
|
// pcSource Source mesh from ASSIMP
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
int GenerateNormalsAsLineList(AssetHelper::MeshHelper *pcMesh, const aiMesh *pcSource) {
|
2020-04-07 14:56:22 +00:00
|
|
|
ai_assert(nullptr != pcMesh);
|
|
|
|
ai_assert(nullptr != pcSource);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
if (!pcSource->mNormals) return 0;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// create vertex buffer
|
2020-05-07 12:00:40 +00:00
|
|
|
if (FAILED(g_piDevice->CreateVertexBuffer(sizeof(AssetHelper::LineVertex) *
|
|
|
|
pcSource->mNumVertices * 2,
|
|
|
|
D3DUSAGE_WRITEONLY,
|
|
|
|
AssetHelper::LineVertex::GetFVF(),
|
|
|
|
D3DPOOL_DEFAULT, &pcMesh->piVBNormals, nullptr))) {
|
2015-07-02 11:07:50 +00:00
|
|
|
CLogDisplay::Instance().AddEntry("Failed to create vertex buffer for the normal list",
|
2020-05-07 12:00:40 +00:00
|
|
|
D3DCOLOR_ARGB(0xFF, 0xFF, 0, 0));
|
2015-07-02 11:07:50 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now fill the vertex buffer with data
|
2020-05-07 12:00:40 +00:00
|
|
|
AssetHelper::LineVertex *pbData2;
|
|
|
|
pcMesh->piVBNormals->Lock(0, 0, (void **)&pbData2, 0);
|
|
|
|
for (unsigned int x = 0; x < pcSource->mNumVertices; ++x) {
|
2015-07-02 11:07:50 +00:00
|
|
|
pbData2->vPosition = pcSource->mVertices[x];
|
|
|
|
|
|
|
|
++pbData2;
|
|
|
|
|
|
|
|
aiVector3D vNormal = pcSource->mNormals[x];
|
|
|
|
vNormal.Normalize();
|
|
|
|
|
|
|
|
// scalo with the inverse of the world scaling to make sure
|
|
|
|
// the normals have equal length in each case
|
|
|
|
// TODO: Check whether this works in every case, I don't think so
|
2020-05-07 12:00:40 +00:00
|
|
|
vNormal.x /= g_mWorld.a1 * 4;
|
|
|
|
vNormal.y /= g_mWorld.b2 * 4;
|
|
|
|
vNormal.z /= g_mWorld.c3 * 4;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
pbData2->vPosition = pcSource->mVertices[x] + vNormal;
|
|
|
|
|
|
|
|
++pbData2;
|
|
|
|
}
|
|
|
|
pcMesh->piVBNormals->Unlock();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// Create the native D3D representation of the asset: vertex buffers,
|
|
|
|
// index buffers, materials ...
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
int CreateAssetData() {
|
|
|
|
if (!g_pcAsset) return 0;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// reset all subsystems
|
|
|
|
CMaterialManager::Instance().Reset();
|
|
|
|
CDisplay::Instance().Reset();
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes; ++i) {
|
|
|
|
const aiMesh *mesh = g_pcAsset->pcScene->mMeshes[i];
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// create the material for the mesh
|
2020-05-07 12:00:40 +00:00
|
|
|
if (!g_pcAsset->apcMeshes[i]->piEffect) {
|
2015-07-02 11:07:50 +00:00
|
|
|
CMaterialManager::Instance().CreateMaterial(
|
2020-05-07 12:00:40 +00:00
|
|
|
g_pcAsset->apcMeshes[i], mesh);
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create vertex buffer
|
2020-05-07 12:00:40 +00:00
|
|
|
if (FAILED(g_piDevice->CreateVertexBuffer(sizeof(AssetHelper::Vertex) *
|
|
|
|
mesh->mNumVertices,
|
|
|
|
D3DUSAGE_WRITEONLY,
|
|
|
|
0,
|
|
|
|
D3DPOOL_DEFAULT, &g_pcAsset->apcMeshes[i]->piVB, nullptr))) {
|
|
|
|
MessageBox(g_hDlg, "Failed to create vertex buffer",
|
|
|
|
"ASSIMP Viewer Utility", MB_OK);
|
2015-07-02 11:07:50 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD dwUsage = 0;
|
|
|
|
if (g_pcAsset->apcMeshes[i]->piOpacityTexture || 1.0f != g_pcAsset->apcMeshes[i]->fOpacity)
|
|
|
|
dwUsage |= D3DUSAGE_DYNAMIC;
|
|
|
|
|
2020-04-03 18:36:44 +00:00
|
|
|
unsigned int nidx = 0;
|
2015-07-02 11:07:50 +00:00
|
|
|
switch (mesh->mPrimitiveTypes) {
|
2020-05-07 12:00:40 +00:00
|
|
|
case aiPrimitiveType_POINT:
|
|
|
|
nidx = 1;
|
|
|
|
break;
|
|
|
|
case aiPrimitiveType_LINE:
|
|
|
|
nidx = 2;
|
|
|
|
break;
|
|
|
|
case aiPrimitiveType_TRIANGLE:
|
|
|
|
nidx = 3;
|
|
|
|
break;
|
|
|
|
default:
|
2021-10-05 08:59:43 +00:00
|
|
|
CLogWindow::Instance().WriteLine("Unknown primitive type");
|
2020-05-07 12:00:40 +00:00
|
|
|
break;
|
2015-07-02 11:07:50 +00:00
|
|
|
};
|
|
|
|
|
2019-02-18 20:43:45 +00:00
|
|
|
unsigned int numIndices = mesh->mNumFaces * 3;
|
|
|
|
if (0 == numIndices && nidx == 1) {
|
|
|
|
numIndices = mesh->mNumVertices;
|
|
|
|
}
|
2015-07-02 11:07:50 +00:00
|
|
|
// check whether we can use 16 bit indices
|
2020-05-07 12:00:40 +00:00
|
|
|
if (numIndices >= 65536) {
|
2015-07-02 11:07:50 +00:00
|
|
|
// create 32 bit index buffer
|
2021-05-30 19:54:04 +00:00
|
|
|
if (FAILED(g_piDevice->CreateIndexBuffer(4 * numIndices,
|
2020-05-07 12:00:40 +00:00
|
|
|
D3DUSAGE_WRITEONLY | dwUsage,
|
|
|
|
D3DFMT_INDEX32,
|
|
|
|
D3DPOOL_DEFAULT,
|
|
|
|
&g_pcAsset->apcMeshes[i]->piIB,
|
|
|
|
nullptr))) {
|
|
|
|
MessageBox(g_hDlg, "Failed to create 32 Bit index buffer",
|
|
|
|
"ASSIMP Viewer Utility", MB_OK);
|
2015-07-02 11:07:50 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now fill the index buffer
|
2020-05-07 12:00:40 +00:00
|
|
|
unsigned int *pbData;
|
|
|
|
g_pcAsset->apcMeshes[i]->piIB->Lock(0, 0, (void **)&pbData, 0);
|
|
|
|
for (unsigned int x = 0; x < mesh->mNumFaces; ++x) {
|
|
|
|
for (unsigned int a = 0; a < nidx; ++a) {
|
2015-07-02 11:07:50 +00:00
|
|
|
*pbData++ = mesh->mFaces[x].mIndices[a];
|
|
|
|
}
|
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
} else {
|
2015-07-02 11:07:50 +00:00
|
|
|
// create 16 bit index buffer
|
2023-03-01 20:40:45 +00:00
|
|
|
if (FAILED(g_piDevice->CreateIndexBuffer(2 * numIndices,
|
2020-05-07 12:00:40 +00:00
|
|
|
D3DUSAGE_WRITEONLY | dwUsage,
|
|
|
|
D3DFMT_INDEX16,
|
|
|
|
D3DPOOL_DEFAULT,
|
|
|
|
&g_pcAsset->apcMeshes[i]->piIB,
|
|
|
|
nullptr))) {
|
|
|
|
MessageBox(g_hDlg, "Failed to create 16 Bit index buffer",
|
|
|
|
"ASSIMP Viewer Utility", MB_OK);
|
2015-07-02 11:07:50 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now fill the index buffer
|
2020-05-07 12:00:40 +00:00
|
|
|
uint16_t *pbData;
|
|
|
|
g_pcAsset->apcMeshes[i]->piIB->Lock(0, 0, (void **)&pbData, 0);
|
|
|
|
for (unsigned int x = 0; x < mesh->mNumFaces; ++x) {
|
|
|
|
for (unsigned int a = 0; a < nidx; ++a) {
|
2015-07-02 11:07:50 +00:00
|
|
|
*pbData++ = (uint16_t)mesh->mFaces[x].mIndices[a];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_pcAsset->apcMeshes[i]->piIB->Unlock();
|
|
|
|
|
|
|
|
// collect weights on all vertices. Quick and careless
|
2020-05-07 12:00:40 +00:00
|
|
|
std::vector<std::vector<aiVertexWeight>> weightsPerVertex(mesh->mNumVertices);
|
|
|
|
for (unsigned int a = 0; a < mesh->mNumBones; a++) {
|
|
|
|
const aiBone *bone = mesh->mBones[a];
|
|
|
|
for (unsigned int b = 0; b < bone->mNumWeights; b++)
|
|
|
|
weightsPerVertex[bone->mWeights[b].mVertexId].push_back(aiVertexWeight(a, bone->mWeights[b].mWeight));
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now fill the vertex buffer
|
2020-05-07 12:00:40 +00:00
|
|
|
AssetHelper::Vertex *pbData2;
|
|
|
|
g_pcAsset->apcMeshes[i]->piVB->Lock(0, 0, (void **)&pbData2, 0);
|
|
|
|
for (unsigned int x = 0; x < mesh->mNumVertices; ++x) {
|
2015-07-02 11:07:50 +00:00
|
|
|
pbData2->vPosition = mesh->mVertices[x];
|
|
|
|
|
2020-04-07 14:56:22 +00:00
|
|
|
if (nullptr == mesh->mNormals)
|
2020-05-07 12:00:40 +00:00
|
|
|
pbData2->vNormal = aiVector3D(0.0f, 0.0f, 0.0f);
|
|
|
|
else
|
|
|
|
pbData2->vNormal = mesh->mNormals[x];
|
|
|
|
|
|
|
|
if (nullptr == mesh->mTangents) {
|
|
|
|
pbData2->vTangent = aiVector3D(0.0f, 0.0f, 0.0f);
|
|
|
|
pbData2->vBitangent = aiVector3D(0.0f, 0.0f, 0.0f);
|
|
|
|
} else {
|
2015-07-02 11:07:50 +00:00
|
|
|
pbData2->vTangent = mesh->mTangents[x];
|
|
|
|
pbData2->vBitangent = mesh->mBitangents[x];
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
if (mesh->HasVertexColors(0)) {
|
2015-07-02 11:07:50 +00:00
|
|
|
pbData2->dColorDiffuse = D3DCOLOR_ARGB(
|
2020-05-07 12:00:40 +00:00
|
|
|
((unsigned char)max(min(mesh->mColors[0][x].a * 255.0f, 255.0f), 0.0f)),
|
|
|
|
((unsigned char)max(min(mesh->mColors[0][x].r * 255.0f, 255.0f), 0.0f)),
|
|
|
|
((unsigned char)max(min(mesh->mColors[0][x].g * 255.0f, 255.0f), 0.0f)),
|
|
|
|
((unsigned char)max(min(mesh->mColors[0][x].b * 255.0f, 255.0f), 0.0f)));
|
|
|
|
} else
|
|
|
|
pbData2->dColorDiffuse = D3DCOLOR_ARGB(0xFF, 0xff, 0xff, 0xff);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// ignore a third texture coordinate component
|
2020-05-07 12:00:40 +00:00
|
|
|
if (mesh->HasTextureCoords(0)) {
|
2015-07-02 11:07:50 +00:00
|
|
|
pbData2->vTextureUV = aiVector2D(
|
2020-05-07 12:00:40 +00:00
|
|
|
mesh->mTextureCoords[0][x].x,
|
|
|
|
mesh->mTextureCoords[0][x].y);
|
|
|
|
} else
|
|
|
|
pbData2->vTextureUV = aiVector2D(0.5f, 0.5f);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
if (mesh->HasTextureCoords(1)) {
|
2015-07-02 11:07:50 +00:00
|
|
|
pbData2->vTextureUV2 = aiVector2D(
|
2020-05-07 12:00:40 +00:00
|
|
|
mesh->mTextureCoords[1][x].x,
|
|
|
|
mesh->mTextureCoords[1][x].y);
|
|
|
|
} else
|
|
|
|
pbData2->vTextureUV2 = aiVector2D(0.5f, 0.5f);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// Bone indices and weights
|
2020-05-07 12:00:40 +00:00
|
|
|
if (mesh->HasBones()) {
|
2015-07-02 11:07:50 +00:00
|
|
|
unsigned char boneIndices[4] = { 0, 0, 0, 0 };
|
|
|
|
unsigned char boneWeights[4] = { 0, 0, 0, 0 };
|
2020-05-07 12:00:40 +00:00
|
|
|
ai_assert(weightsPerVertex[x].size() <= 4);
|
|
|
|
for (unsigned int a = 0; a < weightsPerVertex[x].size(); a++) {
|
2020-04-03 18:36:44 +00:00
|
|
|
boneIndices[a] = static_cast<unsigned char>(weightsPerVertex[x][a].mVertexId);
|
2020-05-07 12:00:40 +00:00
|
|
|
boneWeights[a] = (unsigned char)(weightsPerVertex[x][a].mWeight * 255.0f);
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
memcpy(pbData2->mBoneIndices, boneIndices, sizeof(boneIndices));
|
|
|
|
memcpy(pbData2->mBoneWeights, boneWeights, sizeof(boneWeights));
|
|
|
|
} else {
|
|
|
|
memset(pbData2->mBoneIndices, 0, sizeof(pbData2->mBoneIndices));
|
|
|
|
memset(pbData2->mBoneWeights, 0, sizeof(pbData2->mBoneWeights));
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++pbData2;
|
|
|
|
}
|
|
|
|
g_pcAsset->apcMeshes[i]->piVB->Unlock();
|
|
|
|
|
|
|
|
// now generate the second vertex buffer, holding all normals
|
2020-05-07 12:00:40 +00:00
|
|
|
if (!g_pcAsset->apcMeshes[i]->piVBNormals) {
|
|
|
|
GenerateNormalsAsLineList(g_pcAsset->apcMeshes[i], mesh);
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// Delete all effects, textures, vertex buffers ... associated with
|
|
|
|
// an asset
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
int DeleteAssetData(bool bNoMaterials) {
|
|
|
|
if (!g_pcAsset) return 0;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// TODO: Move this to a proper destructor
|
2020-05-07 12:00:40 +00:00
|
|
|
for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes; ++i) {
|
|
|
|
if (g_pcAsset->apcMeshes[i]->piVB) {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piVB->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piVB = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_pcAsset->apcMeshes[i]->piVBNormals) {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piVBNormals->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piVBNormals = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_pcAsset->apcMeshes[i]->piIB) {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piIB->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piIB = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO ... unfixed memory leak
|
|
|
|
// delete storage eventually allocated to hold a copy
|
|
|
|
// of the original vertex normals
|
|
|
|
//if (g_pcAsset->apcMeshes[i]->pvOriginalNormals)
|
|
|
|
//{
|
|
|
|
// delete[] g_pcAsset->apcMeshes[i]->pvOriginalNormals;
|
|
|
|
//}
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
if (!bNoMaterials) {
|
|
|
|
if (g_pcAsset->apcMeshes[i]->piEffect) {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piEffect->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piEffect = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_pcAsset->apcMeshes[i]->piDiffuseTexture) {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piDiffuseTexture->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piDiffuseTexture = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_pcAsset->apcMeshes[i]->piNormalTexture) {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piNormalTexture->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piNormalTexture = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_pcAsset->apcMeshes[i]->piSpecularTexture) {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piSpecularTexture->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piSpecularTexture = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_pcAsset->apcMeshes[i]->piAmbientTexture) {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piAmbientTexture->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piAmbientTexture = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_pcAsset->apcMeshes[i]->piEmissiveTexture) {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piEmissiveTexture->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piEmissiveTexture = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_pcAsset->apcMeshes[i]->piOpacityTexture) {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piOpacityTexture->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piOpacityTexture = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_pcAsset->apcMeshes[i]->piShininessTexture) {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piShininessTexture->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
g_pcAsset->apcMeshes[i]->piShininessTexture = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
2019-02-04 21:15:15 +00:00
|
|
|
// Switch between zoom/rotate view and the standard FPS view
|
2015-07-02 11:07:50 +00:00
|
|
|
// g_bFPSView specifies the view mode to setup
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
int SetupFPSView() {
|
|
|
|
if (!g_bFPSView) {
|
|
|
|
g_sCamera.vPos = aiVector3D(0.0f, 0.0f, g_fWheelPos);
|
|
|
|
g_sCamera.vLookAt = aiVector3D(0.0f, 0.0f, 1.0f);
|
|
|
|
g_sCamera.vUp = aiVector3D(0.0f, 1.0f, 0.0f);
|
|
|
|
g_sCamera.vRight = aiVector3D(0.0f, 1.0f, 0.0f);
|
|
|
|
} else {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_fWheelPos = g_sCamera.vPos.z;
|
2020-05-07 12:00:40 +00:00
|
|
|
g_sCamera.vPos = aiVector3D(0.0f, 0.0f, -10.0f);
|
|
|
|
g_sCamera.vLookAt = aiVector3D(0.0f, 0.0f, 1.0f);
|
|
|
|
g_sCamera.vUp = aiVector3D(0.0f, 1.0f, 0.0f);
|
|
|
|
g_sCamera.vRight = aiVector3D(0.0f, 1.0f, 0.0f);
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// Initialize the IDIrect3D interface
|
|
|
|
// Called by the WinMain
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
int InitD3D(void) {
|
|
|
|
if (nullptr == g_piD3D) {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_piD3D = Direct3DCreate9(D3D_SDK_VERSION);
|
2020-05-07 12:00:40 +00:00
|
|
|
if (nullptr == g_piD3D) return 0;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// Release the IDirect3D interface.
|
|
|
|
// NOTE: Assumes that the device has already been deleted
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
int ShutdownD3D(void) {
|
2015-07-02 11:07:50 +00:00
|
|
|
ShutdownDevice();
|
2020-05-07 12:00:40 +00:00
|
|
|
if (nullptr != g_piD3D) {
|
2015-07-02 11:07:50 +00:00
|
|
|
g_piD3D->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
g_piD3D = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
template <class TComPtr>
|
|
|
|
inline void SafeRelease(TComPtr *&ptr) {
|
2020-04-03 18:36:44 +00:00
|
|
|
if (nullptr != ptr) {
|
|
|
|
ptr->Release();
|
|
|
|
ptr = nullptr;
|
2018-01-23 12:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
2018-01-23 12:02:13 +00:00
|
|
|
// Shutdown the D3D device object and all resources associated with it
|
2015-07-02 11:07:50 +00:00
|
|
|
// NOTE: Assumes that the asset has already been deleted
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
int ShutdownDevice(void) {
|
2015-07-02 11:07:50 +00:00
|
|
|
// release other subsystems
|
|
|
|
CBackgroundPainter::Instance().ReleaseNativeResource();
|
|
|
|
CLogDisplay::Instance().ReleaseNativeResource();
|
|
|
|
|
2018-01-23 12:02:13 +00:00
|
|
|
// release global shaders that have been allocated
|
|
|
|
SafeRelease(g_piDefaultEffect);
|
|
|
|
SafeRelease(g_piNormalsEffect);
|
|
|
|
SafeRelease(g_piPassThroughEffect);
|
|
|
|
SafeRelease(g_piPatternEffect);
|
|
|
|
SafeRelease(g_pcTexture);
|
|
|
|
SafeRelease(gDefaultVertexDecl);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// delete the main D3D device object
|
2018-01-23 12:02:13 +00:00
|
|
|
SafeRelease(g_piDevice);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// deleted the one channel image allocated to hold the HUD mask
|
|
|
|
delete[] g_szImageMask;
|
2018-01-23 12:02:13 +00:00
|
|
|
g_szImageMask = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
int CreateHUDTexture() {
|
2015-07-02 11:07:50 +00:00
|
|
|
// lock the memory resource ourselves
|
2020-05-07 12:00:40 +00:00
|
|
|
HRSRC res = FindResource(nullptr, MAKEINTRESOURCE(IDR_HUD), RT_RCDATA);
|
|
|
|
HGLOBAL hg = LoadResource(nullptr, res);
|
|
|
|
void *pData = LockResource(hg);
|
|
|
|
|
|
|
|
if (FAILED(D3DXCreateTextureFromFileInMemoryEx(g_piDevice,
|
|
|
|
pData, SizeofResource(nullptr, res),
|
|
|
|
D3DX_DEFAULT_NONPOW2,
|
|
|
|
D3DX_DEFAULT_NONPOW2,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
D3DFMT_A8R8G8B8,
|
|
|
|
D3DPOOL_MANAGED,
|
|
|
|
D3DX_DEFAULT,
|
|
|
|
D3DX_DEFAULT,
|
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
&g_pcTexture))) {
|
2015-07-02 11:07:50 +00:00
|
|
|
CLogDisplay::Instance().AddEntry("[ERROR] Unable to load HUD texture",
|
2020-05-07 12:00:40 +00:00
|
|
|
D3DCOLOR_ARGB(0xFF, 0xFF, 0, 0));
|
2015-07-02 11:07:50 +00:00
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
g_pcTexture = nullptr;
|
2020-04-07 14:56:22 +00:00
|
|
|
g_szImageMask = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
FreeResource(hg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeResource(hg);
|
|
|
|
|
|
|
|
D3DSURFACE_DESC sDesc;
|
2020-05-07 12:00:40 +00:00
|
|
|
g_pcTexture->GetLevelDesc(0, &sDesc);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// lock the memory resource ourselves
|
2020-05-07 12:00:40 +00:00
|
|
|
res = FindResource(nullptr, MAKEINTRESOURCE(IDR_HUDMASK), RT_RCDATA);
|
|
|
|
hg = LoadResource(nullptr, res);
|
2015-07-02 11:07:50 +00:00
|
|
|
pData = LockResource(hg);
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
IDirect3DTexture9 *pcTex;
|
|
|
|
if (FAILED(D3DXCreateTextureFromFileInMemoryEx(g_piDevice,
|
|
|
|
pData, SizeofResource(nullptr, res),
|
|
|
|
sDesc.Width,
|
|
|
|
sDesc.Height,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
D3DFMT_L8,
|
|
|
|
D3DPOOL_MANAGED, // unnecessary
|
|
|
|
D3DX_DEFAULT,
|
|
|
|
D3DX_DEFAULT,
|
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
&pcTex))) {
|
2015-07-02 11:07:50 +00:00
|
|
|
CLogDisplay::Instance().AddEntry("[ERROR] Unable to load HUD mask texture",
|
2020-05-07 12:00:40 +00:00
|
|
|
D3DCOLOR_ARGB(0xFF, 0xFF, 0, 0));
|
2020-04-07 14:56:22 +00:00
|
|
|
g_szImageMask = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
FreeResource(hg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeResource(hg);
|
|
|
|
|
|
|
|
// lock the texture and copy it to get a pointer
|
|
|
|
D3DLOCKED_RECT sRect;
|
2020-05-07 12:00:40 +00:00
|
|
|
pcTex->LockRect(0, &sRect, nullptr, D3DLOCK_READONLY);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
unsigned char *szOut = new unsigned char[sDesc.Width * sDesc.Height];
|
|
|
|
unsigned char *_szOut = szOut;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
unsigned char *szCur = (unsigned char *)sRect.pBits;
|
|
|
|
for (unsigned int y = 0; y < sDesc.Height; ++y) {
|
|
|
|
memcpy(_szOut, szCur, sDesc.Width);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
szCur += sRect.Pitch;
|
|
|
|
_szOut += sDesc.Width;
|
|
|
|
}
|
|
|
|
pcTex->UnlockRect(0);
|
|
|
|
pcTex->Release();
|
|
|
|
|
|
|
|
g_szImageMask = szOut;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
int CreateDevice(bool p_bMultiSample, bool p_bSuperSample, bool bHW /*= true*/) {
|
2015-07-02 11:07:50 +00:00
|
|
|
D3DDEVTYPE eType = bHW ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF;
|
|
|
|
|
|
|
|
// get the client rectangle of the window.
|
|
|
|
RECT sRect;
|
2020-05-07 12:00:40 +00:00
|
|
|
GetWindowRect(GetDlgItem(g_hDlg, IDC_RT), &sRect);
|
2015-07-02 11:07:50 +00:00
|
|
|
sRect.right -= sRect.left;
|
|
|
|
sRect.bottom -= sRect.top;
|
|
|
|
|
|
|
|
D3DPRESENT_PARAMETERS sParams;
|
2020-05-07 12:00:40 +00:00
|
|
|
memset(&sParams, 0, sizeof(D3DPRESENT_PARAMETERS));
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// get the current display mode
|
|
|
|
D3DDISPLAYMODE sMode;
|
2020-05-07 12:00:40 +00:00
|
|
|
g_piD3D->GetAdapterDisplayMode(0, &sMode);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// fill the presentation parameter structure
|
2020-05-07 12:00:40 +00:00
|
|
|
sParams.Windowed = TRUE;
|
|
|
|
sParams.hDeviceWindow = GetDlgItem(g_hDlg, IDC_RT);
|
|
|
|
sParams.EnableAutoDepthStencil = TRUE;
|
|
|
|
sParams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
|
|
|
|
sParams.BackBufferWidth = (UINT)sRect.right;
|
|
|
|
sParams.BackBufferHeight = (UINT)sRect.bottom;
|
|
|
|
sParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
|
|
|
|
sParams.BackBufferCount = 1;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// check whether we can use a D32 depth buffer format
|
2020-05-07 12:00:40 +00:00
|
|
|
if (SUCCEEDED(g_piD3D->CheckDepthStencilMatch(0, eType,
|
|
|
|
D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, D3DFMT_D32))) {
|
2015-07-02 11:07:50 +00:00
|
|
|
sParams.AutoDepthStencilFormat = D3DFMT_D32;
|
2020-05-07 12:00:40 +00:00
|
|
|
} else
|
|
|
|
sParams.AutoDepthStencilFormat = D3DFMT_D24X8;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// find the highest multisample type available on this device
|
|
|
|
D3DMULTISAMPLE_TYPE sMS = D3DMULTISAMPLE_2_SAMPLES;
|
|
|
|
D3DMULTISAMPLE_TYPE sMSOut = D3DMULTISAMPLE_NONE;
|
|
|
|
DWORD dwQuality = 0;
|
2020-05-07 12:00:40 +00:00
|
|
|
if (p_bMultiSample) {
|
|
|
|
while ((D3DMULTISAMPLE_TYPE)(D3DMULTISAMPLE_16_SAMPLES + 1) !=
|
|
|
|
(sMS = (D3DMULTISAMPLE_TYPE)(sMS + 1))) {
|
|
|
|
if (SUCCEEDED(g_piD3D->CheckDeviceMultiSampleType(0, eType,
|
|
|
|
sMode.Format, TRUE, sMS, &dwQuality))) {
|
2015-07-02 11:07:50 +00:00
|
|
|
sMSOut = sMS;
|
|
|
|
}
|
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
if (0 != dwQuality) dwQuality -= 1;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
sParams.MultiSampleQuality = dwQuality;
|
|
|
|
sParams.MultiSampleType = sMSOut;
|
|
|
|
}
|
|
|
|
|
2019-02-04 21:15:15 +00:00
|
|
|
// get the device capabilities. If the hardware vertex shader is too old, we prefer software vertex processing
|
2020-05-07 12:00:40 +00:00
|
|
|
g_piD3D->GetDeviceCaps(0, D3DDEVTYPE_HAL, &g_sCaps);
|
2015-07-02 11:07:50 +00:00
|
|
|
DWORD creationFlags = D3DCREATE_MULTITHREADED;
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_sCaps.VertexShaderVersion >= D3DVS_VERSION(2, 0))
|
2015-07-02 11:07:50 +00:00
|
|
|
creationFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
|
|
|
|
else
|
|
|
|
creationFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
|
|
|
|
|
|
|
|
// create the D3D9 device object. with software-vertexprocessing if VS2.0 isn`t supported in hardware
|
2020-05-07 12:00:40 +00:00
|
|
|
if (FAILED(g_piD3D->CreateDevice(0, eType, g_hDlg, creationFlags, &sParams, &g_piDevice))) {
|
2015-07-02 11:07:50 +00:00
|
|
|
// if hardware fails use software rendering instead
|
2020-05-07 12:00:40 +00:00
|
|
|
if (bHW) return CreateDevice(p_bMultiSample, p_bSuperSample, false);
|
2015-07-02 11:07:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a vertex declaration to match the vertex
|
2020-05-07 12:00:40 +00:00
|
|
|
D3DVERTEXELEMENT9 *vdecl = AssetHelper::Vertex::GetDeclarationElements();
|
|
|
|
if (FAILED(g_piDevice->CreateVertexDeclaration(vdecl, &gDefaultVertexDecl))) {
|
|
|
|
MessageBox(g_hDlg, "Failed to create vertex declaration", "Init", MB_OK);
|
2015-07-02 11:07:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
g_piDevice->SetVertexDeclaration(gDefaultVertexDecl);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// get the capabilities of the device object
|
|
|
|
g_piDevice->GetDeviceCaps(&g_sCaps);
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_sCaps.PixelShaderVersion < D3DPS_VERSION(3, 0)) {
|
|
|
|
EnableWindow(GetDlgItem(g_hDlg, IDC_LOWQUALITY), FALSE);
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// compile the default material shader (gray gouraud/phong)
|
2020-05-07 12:00:40 +00:00
|
|
|
ID3DXBuffer *piBuffer = nullptr;
|
|
|
|
if (FAILED(D3DXCreateEffect(g_piDevice,
|
|
|
|
g_szDefaultShader.c_str(),
|
|
|
|
(UINT)g_szDefaultShader.length(),
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
AI_SHADER_COMPILE_FLAGS,
|
|
|
|
nullptr,
|
|
|
|
&g_piDefaultEffect, &piBuffer))) {
|
|
|
|
if (piBuffer) {
|
|
|
|
MessageBox(g_hDlg, (LPCSTR)piBuffer->GetBufferPointer(), "HLSL", MB_OK);
|
2015-07-02 11:07:50 +00:00
|
|
|
piBuffer->Release();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
if (piBuffer) {
|
2015-07-02 11:07:50 +00:00
|
|
|
piBuffer->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
piBuffer = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// use Fixed Function effect when working with shaderless cards
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_sCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
|
|
|
|
g_piDefaultEffect->SetTechnique("DefaultFXSpecular_FF");
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// create the shader used to draw the HUD
|
2020-05-07 12:00:40 +00:00
|
|
|
if (FAILED(D3DXCreateEffect(g_piDevice,
|
|
|
|
g_szPassThroughShader.c_str(), (UINT)g_szPassThroughShader.length(),
|
|
|
|
nullptr, nullptr, AI_SHADER_COMPILE_FLAGS, nullptr, &g_piPassThroughEffect, &piBuffer))) {
|
|
|
|
if (piBuffer) {
|
|
|
|
MessageBox(g_hDlg, (LPCSTR)piBuffer->GetBufferPointer(), "HLSL", MB_OK);
|
2015-07-02 11:07:50 +00:00
|
|
|
piBuffer->Release();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
if (piBuffer) {
|
2015-07-02 11:07:50 +00:00
|
|
|
piBuffer->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
piBuffer = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// use Fixed Function effect when working with shaderless cards
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_sCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
|
|
|
|
g_piPassThroughEffect->SetTechnique("PassThrough_FF");
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// create the shader used to visualize normal vectors
|
2020-05-07 12:00:40 +00:00
|
|
|
if (FAILED(D3DXCreateEffect(g_piDevice,
|
|
|
|
g_szNormalsShader.c_str(), (UINT)g_szNormalsShader.length(),
|
|
|
|
nullptr, nullptr, AI_SHADER_COMPILE_FLAGS, nullptr, &g_piNormalsEffect, &piBuffer))) {
|
|
|
|
if (piBuffer) {
|
|
|
|
MessageBox(g_hDlg, (LPCSTR)piBuffer->GetBufferPointer(), "HLSL", MB_OK);
|
2015-07-02 11:07:50 +00:00
|
|
|
piBuffer->Release();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-07 12:00:40 +00:00
|
|
|
if (piBuffer) {
|
2015-07-02 11:07:50 +00:00
|
|
|
piBuffer->Release();
|
2020-04-07 14:56:22 +00:00
|
|
|
piBuffer = nullptr;
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//MessageBox( g_hDlg, "Failed to create vertex declaration", "Init", MB_OK);
|
|
|
|
|
|
|
|
// use Fixed Function effect when working with shaderless cards
|
2020-05-07 12:00:40 +00:00
|
|
|
if (g_sCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
|
|
|
|
g_piNormalsEffect->SetTechnique("RenderNormals_FF");
|
2015-07-02 11:07:50 +00:00
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
g_piDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
// create the texture for the HUD
|
|
|
|
CreateHUDTexture();
|
|
|
|
CBackgroundPainter::Instance().RecreateNativeResource();
|
|
|
|
CLogDisplay::Instance().RecreateNativeResource();
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
g_piPassThroughEffect->SetTexture("TEXTURE_2D", g_pcTexture);
|
2015-07-02 11:07:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
int CreateDevice() {
|
2015-07-02 11:07:50 +00:00
|
|
|
return CreateDevice(g_sOptions.bMultiSample,
|
2020-05-07 12:00:40 +00:00
|
|
|
g_sOptions.bSuperSample);
|
2015-07-02 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
int GetProjectionMatrix(aiMatrix4x4 &p_mOut) {
|
2015-07-02 11:07:50 +00:00
|
|
|
const float fFarPlane = 100.0f;
|
|
|
|
const float fNearPlane = 0.1f;
|
|
|
|
const float fFOV = (float)(45.0 * 0.0174532925);
|
|
|
|
|
|
|
|
const float s = 1.0f / tanf(fFOV * 0.5f);
|
|
|
|
const float Q = fFarPlane / (fFarPlane - fNearPlane);
|
|
|
|
|
|
|
|
RECT sRect;
|
2020-05-07 12:00:40 +00:00
|
|
|
GetWindowRect(GetDlgItem(g_hDlg, IDC_RT), &sRect);
|
2015-07-02 11:07:50 +00:00
|
|
|
sRect.right -= sRect.left;
|
|
|
|
sRect.bottom -= sRect.top;
|
|
|
|
const float fAspect = (float)sRect.right / (float)sRect.bottom;
|
|
|
|
|
|
|
|
p_mOut = aiMatrix4x4(
|
2020-05-07 12:00:40 +00:00
|
|
|
s / fAspect, 0.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, s, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, Q, 1.0f,
|
|
|
|
0.0f, 0.0f, -Q * fNearPlane, 0.0f);
|
2015-07-02 11:07:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
2020-05-07 12:00:40 +00:00
|
|
|
aiVector3D GetCameraMatrix(aiMatrix4x4 &p_mOut) {
|
2015-07-02 11:07:50 +00:00
|
|
|
D3DXMATRIX view;
|
2020-05-07 12:00:40 +00:00
|
|
|
D3DXMatrixIdentity(&view);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
D3DXVec3Normalize((D3DXVECTOR3 *)&g_sCamera.vLookAt, (D3DXVECTOR3 *)&g_sCamera.vLookAt);
|
|
|
|
D3DXVec3Cross((D3DXVECTOR3 *)&g_sCamera.vRight, (D3DXVECTOR3 *)&g_sCamera.vUp, (D3DXVECTOR3 *)&g_sCamera.vLookAt);
|
|
|
|
D3DXVec3Normalize((D3DXVECTOR3 *)&g_sCamera.vRight, (D3DXVECTOR3 *)&g_sCamera.vRight);
|
|
|
|
D3DXVec3Cross((D3DXVECTOR3 *)&g_sCamera.vUp, (D3DXVECTOR3 *)&g_sCamera.vLookAt, (D3DXVECTOR3 *)&g_sCamera.vRight);
|
|
|
|
D3DXVec3Normalize((D3DXVECTOR3 *)&g_sCamera.vUp, (D3DXVECTOR3 *)&g_sCamera.vUp);
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
view._11 = g_sCamera.vRight.x;
|
|
|
|
view._12 = g_sCamera.vUp.x;
|
|
|
|
view._13 = g_sCamera.vLookAt.x;
|
|
|
|
view._14 = 0.0f;
|
|
|
|
|
|
|
|
view._21 = g_sCamera.vRight.y;
|
|
|
|
view._22 = g_sCamera.vUp.y;
|
|
|
|
view._23 = g_sCamera.vLookAt.y;
|
|
|
|
view._24 = 0.0f;
|
|
|
|
|
|
|
|
view._31 = g_sCamera.vRight.z;
|
|
|
|
view._32 = g_sCamera.vUp.z;
|
|
|
|
view._33 = g_sCamera.vLookAt.z;
|
|
|
|
view._34 = 0.0f;
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
view._41 = -D3DXVec3Dot((D3DXVECTOR3 *)&g_sCamera.vPos, (D3DXVECTOR3 *)&g_sCamera.vRight);
|
|
|
|
view._42 = -D3DXVec3Dot((D3DXVECTOR3 *)&g_sCamera.vPos, (D3DXVECTOR3 *)&g_sCamera.vUp);
|
|
|
|
view._43 = -D3DXVec3Dot((D3DXVECTOR3 *)&g_sCamera.vPos, (D3DXVECTOR3 *)&g_sCamera.vLookAt);
|
|
|
|
view._44 = 1.0f;
|
2015-07-02 11:07:50 +00:00
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
memcpy(&p_mOut, &view, sizeof(aiMatrix4x4));
|
2015-07-02 11:07:50 +00:00
|
|
|
|
|
|
|
return g_sCamera.vPos;
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:00:40 +00:00
|
|
|
} // namespace AssimpView
|