xtocmod.cpp
来自「celestia源代码」· C++ 代码 · 共 896 行 · 第 1/2 页
CPP
896 行
} unsigned int outputOffset = 0; for (i = 0; i < MAX_FVF_DECL_SIZE && declElements[i].Stream != 255; i++) { int attr = VertexAttribute::InvalidAttribute; VertexAttribute::Format format = VertexAttribute::InvalidFormat; unsigned int formatSize = 0; if (declElements[i].Stream == 0) { switch (declElements[i].Type) { case D3DDECLTYPE_FLOAT1: format = VertexAttribute::Float1; formatSize = 4; break; case D3DDECLTYPE_FLOAT2: format = VertexAttribute::Float2; formatSize = 8; break; case D3DDECLTYPE_FLOAT3: format = VertexAttribute::Float3; formatSize = 12; break; case D3DDECLTYPE_FLOAT4: format = VertexAttribute::Float4; formatSize = 16; break; case D3DDECLTYPE_UBYTE4: case D3DDECLTYPE_UBYTE4N: format = VertexAttribute::UByte4; formatSize = 4; break; } switch (declElements[i].Usage) { case D3DDECLUSAGE_POSITION: if (declElements[i].UsageIndex == 0) { attr = VertexAttribute::Position; } break; case D3DDECLUSAGE_NORMAL: if (declElements[i].UsageIndex == 0) { attr = VertexAttribute::Normal; } break; case D3DDECLUSAGE_TEXCOORD: if (declElements[i].UsageIndex < 4) { if (declElements[i].UsageIndex == 0) attr = VertexAttribute::Texture0; else if (declElements[i].UsageIndex == 1) attr = VertexAttribute::Texture1; else if (declElements[i].UsageIndex == 2) attr = VertexAttribute::Texture2; else attr = VertexAttribute::Texture3; } break; case D3DDECLUSAGE_COLOR: if (declElements[i].UsageIndex < 2) { if (declElements[i].UsageIndex == 0) attr = VertexAttribute::Color0; else attr = VertexAttribute::Color1; } break; case D3DDECLUSAGE_TANGENT: if (declElements[i].UsageIndex == 0) { attr = VertexAttribute::Tangent; } break; } if (attr != VertexAttribute::InvalidAttribute) { vertexMap[attr].offset = declElements[i].Offset; vertexMap[attr].format = format; } } } return true;}static LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){ switch (uMsg) { case WM_CREATE: break; case WM_DESTROY: PostQuitMessage(0); break; case WM_PAINT: if (g_d3dDev != NULL) { //render(); //g_d3dDev->Present(NULL, NULL, NULL, NULL); } break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return 0;}int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc = (WNDPROC) MainWindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)); wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = "xtocmod"; if (RegisterClass(&wc) == 0) { MessageBox(NULL, "Failed to register the window class.", "Fatal Error", MB_OK | MB_ICONERROR); return NULL; } DWORD windowStyle = (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX); g_mainWindow = CreateWindow("xtocmod", "xtocmod", windowStyle, CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, NULL, NULL, hInstance, NULL); if (g_mainWindow == NULL) { MessageBox(NULL, "Error creating application window.", "Fatal Error", MB_OK | MB_ICONERROR); } //ShowWindow(g_mainWindow, SW_SHOW); SetForegroundWindow(g_mainWindow); SetFocus(g_mainWindow); // Initialize D3D g_d3d = Direct3DCreate9(D3D_SDK_VERSION); if (g_d3d == NULL) { ShowD3DErrorMessage("Initializing D3D", 0); return 1; } D3DPRESENT_PARAMETERS presentParams; ZeroMemory(&presentParams, sizeof(presentParams)); presentParams.Windowed = TRUE; presentParams.SwapEffect = D3DSWAPEFFECT_COPY;#if 0 presentParams.BackBufferWidth = 300; presentParams.BackBufferHeight = 300; presentParams.BackBufferCount = 1; presentParams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; presentParams.Windowed = TRUE;#endif HRESULT hr = g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_mainWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &presentParams, &g_d3dDev); if (FAILED(hr)) { ShowD3DErrorMessage("Creating D3D device", hr); //return 1; } string inputFilename(lpCmdLine); string outputFilename(inputFilename, 0, inputFilename.rfind('.')); outputFilename += ".cmod"; ID3DXMesh* mesh = NULL; ID3DXBuffer* adjacency = NULL; ID3DXBuffer* materialBuf = NULL; ID3DXBuffer* effects = NULL; DWORD numMaterials; hr = D3DXLoadMeshFromX(inputFilename.c_str(), 0, g_d3dDev, &adjacency, &materialBuf, &effects, &numMaterials, &mesh); if (FAILED(hr)) { ShowD3DErrorMessage("Loading mesh from X file", hr); return 1; } DWORD numVertices = mesh->GetNumVertices(); DWORD numFaces = mesh->GetNumFaces(); cout << "vertices: " << numVertices << '\n'; cout << "faces: " << numFaces << '\n'; cout << "adjacency buffer size: " << adjacency->GetBufferSize() << '\n'; ofstream meshfile(outputFilename.c_str()); // Output the header meshfile << "#celmodel__ascii\n\n"; cout << "numMaterials=" << numMaterials << '\n'; D3DXMATERIAL* materials = reinterpret_cast<D3DXMATERIAL*>(materialBuf->GetBufferPointer()); for (DWORD mat = 0; mat < numMaterials; mat++) { meshfile << "material\n"; meshfile << "diffuse " << materials[mat].MatD3D.Diffuse << '\n'; //meshfile << "emissive " << materials[mat].MatD3D.Emissive << '\n'; meshfile << "specular " << materials[mat].MatD3D.Specular << '\n'; meshfile << "specpower " << materials[mat].MatD3D.Power << '\n'; meshfile << "opacity " << materials[mat].MatD3D.Diffuse.a << '\n'; meshfile << "end_material\n\n"; } // Vertex format D3DVERTEXELEMENT9 declElements[MAX_FVF_DECL_SIZE]; hr = mesh->GetDeclaration(declElements); if (FAILED(hr)) { ShowD3DErrorMessage("Checking vertex declaration", hr); return 1; } DWORD stride = D3DXGetDeclVertexSize(declElements, 0); VertexAttribute vertexMap[VertexAttribute::MaxAttribute]; CreateVertexAttributeMap(declElements, vertexMap); meshfile << "mesh\n\n"; DumpVertexDescription(vertexMap, meshfile); ID3DXMesh* optMesh = NULL; ID3DXBuffer* vertexRemap = NULL; DWORD* faceRemap = new DWORD[numFaces]; DWORD* optAdjacency = new DWORD[numFaces * 3]; hr = mesh->Optimize(D3DXMESHOPT_COMPACT | D3DXMESHOPT_STRIPREORDER, //D3DXMESHOPT_VERTEXCACHE | reinterpret_cast<DWORD*>(adjacency->GetBufferPointer()), optAdjacency, faceRemap, &vertexRemap, &optMesh); if (FAILED(hr)) { ShowD3DErrorMessage("Optimize failed: ", hr); return 1; } // Attribute table DWORD attribTableSize = 0; hr = optMesh->GetAttributeTable(NULL, &attribTableSize); if (FAILED(hr)) { ShowD3DErrorMessage("Querying attribute table size", hr); return 1; } D3DXATTRIBUTERANGE* attribTable = NULL; if (attribTableSize > 0) { attribTable = new D3DXATTRIBUTERANGE[attribTableSize]; hr = optMesh->GetAttributeTable(attribTable, &attribTableSize); if (FAILED(hr)) { ShowD3DErrorMessage("Getting attribute table", hr); return 1; } } cout << "Attribute table size: " << attribTableSize << '\n'; if (attribTableSize == 1) { cout << "Attribute id: " << attribTable[0].AttribId << '\n'; } if (!DumpMeshVertices(optMesh, vertexMap, stride, meshfile)) return 1; // output the indices for (DWORD attr = 0; attr < attribTableSize; attr++) { StripifyMeshSubset(optMesh, attr, meshfile); } meshfile << "\nend_mesh\n";#if 0 IDirect3DIndexBuffer9* indices = NULL; hr = mesh->GetIndexBuffer(&indices);#endif#if 0 // No message loop required for this app MSG msg; GetMessage(&msg, NULL, 0u, 0u); while (msg.message != WM_QUIT) { GetMessage(&msg, NULL, 0u, 0u); TranslateMessage(&msg); DispatchMessage(&msg); }#endif return 0;}int main(int argc, char* argv[]){ if (argc != 2) { cerr << "Usage: xtocmod <meshfile.x>\n"; return 1; } ID3DXMesh* mesh = NULL; ID3DXBuffer* adjacency = NULL; ID3DXBuffer* materials = NULL; ID3DXBuffer* effects = NULL; return 0;}char* D3DErrorString(HRESULT hr){ switch (hr) { case D3DERR_WRONGTEXTUREFORMAT: return "D3DERR_WRONGTEXTUREFORMAT"; case D3DERR_UNSUPPORTEDCOLOROPERATION: return "D3DERR_UNSUPPORTEDCOLOROPERATION"; case D3DERR_UNSUPPORTEDCOLORARG: return "D3DERR_UNSUPPORTEDCOLORARG"; case D3DERR_UNSUPPORTEDALPHAOPERATION: return "D3DERR_UNSUPPORTEDALPHAOPERATION"; case D3DERR_UNSUPPORTEDALPHAARG: return "D3DERR_UNSUPPORTEDALPHAARG"; case D3DERR_TOOMANYOPERATIONS: return "D3DERR_TOOMANYOPERATIONS"; case D3DERR_CONFLICTINGTEXTUREFILTER: return "D3DERR_CONFLICTINGTEXTUREFILTER"; case D3DERR_UNSUPPORTEDFACTORVALUE: return "D3DERR_UNSUPPORTEDFACTORVALUE"; case D3DERR_CONFLICTINGRENDERSTATE: return "D3DERR_CONFLICTINGRENDERSTATE"; case D3DERR_UNSUPPORTEDTEXTUREFILTER: return "D3DERR_UNSUPPORTEDTEXTUREFILTER"; case D3DERR_CONFLICTINGTEXTUREPALETTE: return "D3DERR_CONFLICTINGTEXTUREPALETTE"; case D3DERR_DRIVERINTERNALERROR: return "D3DERR_DRIVERINTERNALERROR"; case D3DERR_NOTFOUND: return "D3DERR_NOTFOUND"; case D3DERR_MOREDATA: return "D3DERR_MOREDATA"; case D3DERR_DEVICELOST: return "D3DERR_DEVICELOST"; case D3DERR_DEVICENOTRESET: return "D3DERR_DEVICENOTRESET"; case D3DERR_NOTAVAILABLE: return "D3DERR_NOTAVAILABLE"; case D3DERR_OUTOFVIDEOMEMORY: return "D3DERR_OUTOFVIDEOMEMORY"; case D3DERR_INVALIDDEVICE: return "D3DERR_INVALIDDEVICE"; case D3DERR_INVALIDCALL: return "D3DERR_INVALIDCALL"; case D3DERR_DRIVERINVALIDCALL: return "D3DERR_DRIVERINVALIDCALL"; case D3DERR_WASSTILLDRAWING: return "D3DERR_WASSTILLDRAWING"; case D3DOK_NOAUTOGEN: return "D3DOK_NOAUTOGEN"; case D3DXERR_CANNOTMODIFYINDEXBUFFER: return "D3DXERR_CANNOTMODIFYINDEXBUFFER"; case D3DXERR_INVALIDMESH: return "D3DXERR_INVALIDMESH"; case D3DXERR_CANNOTATTRSORT: return "D3DXERR_CANNOTATTRSORT"; case D3DXERR_SKINNINGNOTSUPPORTED: return "D3DXERR_SKINNINGNOTSUPPORTED"; case D3DXERR_TOOMANYINFLUENCES: return "D3DXERR_TOOMANYINFLUENCES"; case D3DXERR_INVALIDDATA: return "D3DXERR_INVALIDDATA"; case D3DXERR_LOADEDMESHASNODATA: return "D3DXERR_LOADEDMESHASNODATA"; case D3DXERR_DUPLICATENAMEDFRAGMENT: return "D3DXERR_DUPLICATENAMEDFRAGMENT"; default: return "Unkown D3D Error"; }}void ShowD3DErrorMessage(char* info, HRESULT hr){ char buf[1024]; sprintf(buf, "%s - %s", info, D3DErrorString(hr)); MessageBox(g_mainWindow, buf, "Fatal Error", MB_OK | MB_ICONERROR);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?