HRESULT GetXFileDataName( tstring& name,LPD3DXFILEDATA pXofData ) { HRESULT hr = S_OK; DWORD cchName; hr = pXofData->GetName(NULL,&cchName); if( FAILED(hr) ) { return hr; } if( cchName > 0 ) { char* szName = NULL; if( NULL == (szName = new char[cchName]) ) { hr = E_OUTOFMEMORY; return hr; } hr = pXofData->GetName( szName,&cchName ); if( FAILED(hr) ) { delete[] szName; return hr; } name = to_tstring(szName); delete[] szName; } return hr; }
bool Shader::CreateFromMemory( LPCVOID buffer,size_t size ) { Graphics* graphics = Graphics::GetInstance(); if( m_pEffect ) { m_pEffect.Release(); graphics->DelResource( this ); } LPD3DXBUFFER pBuffer = NULL; HRESULT hr = D3DXCreateEffect( graphics->GetDirect3DDevice(), buffer, size, NULL,NULL,0,NULL,&m_pEffect,&pBuffer ); if( pBuffer ) { tstring error = to_tstring( (char*)pBuffer->GetBufferPointer() ); OutputDebugStringFormat( _T("effect compiled\n%s"),error.c_str() ); pBuffer->Release(); } if( hr!=D3D_OK ) { return false; } graphics->AddResource( this ); return true; }
bool Shader::CreateFromFile( const tstring& filePath ) { if( m_pEffect ) { m_pEffect.Release(); Graphics::GetInstance()->DelResource( this ); } LPD3DXBUFFER pBuffer = NULL; HRESULT hr = D3DXCreateEffectFromFile( Graphics::GetInstance()->GetDirect3DDevice(), filePath.c_str(), NULL,NULL,0,NULL,&m_pEffect,&pBuffer ); if( pBuffer ) { tstring error = to_tstring( (char*)pBuffer->GetBufferPointer() ); OutputDebugStringFormat( _T("effect compiled\n%s"),error.c_str() ); pBuffer->Release(); } if( hr!=D3D_OK ) { return false; } Graphics::GetInstance()->AddResource( this ); return true; }
UpDown::UpDown(int x, int y, int width, int height, int minval, int maxval, unsigned long style /* = */ ): Controller(x, y, Controller::UPDOWN), width(width), height(height), style(style), range(minval, maxval), readyForChange(false), buddy(new EditBox(x, y, width, height, to_tstring(minval), WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER | ES_LEFT)), OnFocus(buddy->OnFocus), OnFocusLost(buddy->OnFocusLost) { }
ConsoleStream& operator||(ConsoleStream& os, const int& number) { tstring* temp = new tstring(to_tstring(number)); os.numbers.push_back(temp); int start = static_cast<int>(SendMessage(os.display, EM_LINEINDEX, -1, 0)); int length = static_cast<int>(SendMessage(os.display, EM_LINELENGTH, start, 0)); int end = start + length; SendMessage(os.display, EM_SETSEL, end, end); // int idx = GetWindowTextLength(os.display); // SendMessage(os.display, EM_SETSEL, static_cast<WPARAM>(idx), static_cast<LPARAM>(idx)); SendMessage(os.display, EM_REPLACESEL, 0, reinterpret_cast<LPARAM>(&(*temp)[0])); InvalidateRect(os.display, nullptr, false); return os; }
ConsoleStream& operator<<(ConsoleStream& os, const int& number) { //os.output << number; tstring* temp = new tstring(to_tstring(number)); os.numbers.push_back(temp); size_t ndx = GetWindowTextLength(os.display); SetFocus(os.display); SendMessage(os.display, EM_SETSEL, static_cast<WPARAM>(ndx), static_cast<LPARAM>(ndx));//put the cursor in the end SendMessage(os.display, EM_REPLACESEL, 0, reinterpret_cast<LPARAM>(&(*temp)[0])); /* SetWindowText(os.display, os.output.str().c_str()); SendMessage(os.display, EM_SETSEL, 0, -1); SendMessage(os.display, EM_SETSEL, -1, -1); SendMessage(os.display, EM_SCROLLCARET, 0, 0); */ InvalidateRect(os.display, nullptr, false); return os; }
CMBR::CMBR(const TCHAR* deviceName, HANDLE hDevice) { m_pPhysRawDisk = NULL; VOLUME_DISK_EXTENTS extents; memset(&extents, 0, sizeof(extents)); DWORD byteswritten = 0; if (!hDevice) { CRawDisk tempdisk(deviceName); hDevice = tempdisk.getHandle(); if (!DeviceIoControl(hDevice, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0, &extents, sizeof(extents), &byteswritten, NULL)) { cout << "Error getting Physical Device (" << GetLastError() << "), aborting...\n"; throw "Error in MBR (" __FILE__ ") attaining physcial device"; } } else { if (!DeviceIoControl(hDevice, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0, &extents, sizeof(extents), &byteswritten, NULL)) { cout << "Error getting Physical Device (" << GetLastError() << "), aborting...\n"; throw "Error in MBR (" __FILE__ ") attaining physcial device"; } } m_hDevice = hDevice; if (!DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geometry, sizeof(geometry), &byteswritten, NULL)) { cout << "Error getting Physical Device (" << GetLastError() << "), aborting...\n"; throw "Error in MBR (" __FILE__ ") attaining physcial device"; } int nPhysDrive = extents.Extents[0].DiskNumber; tstring path = tstring(_T("\\\\.\\PhysicalDrive") + to_tstring(nPhysDrive)); m_physicalpath = path; m_pPhysRawDisk = new CRawDisk(path.c_str()); m_pPhysRawDisk->setSectorSize(geometry.BytesPerSector); PARTITION_INFORMATION_EX partinfo; DWORD read = 0; if (!DeviceIoControl(hDevice, IOCTL_DISK_GET_PARTITION_INFO_EX, NULL, 0, &partinfo, sizeof(partinfo), &read, NULL)) throw "Cannot get partititon information"; m_partsize = partinfo.PartitionLength.QuadPart / geometry.BytesPerSector; }
void GameScene::DrawScene(const tt::GameContext& context) { auto pGfxService = MyServiceLocator::GetInstance()->GetService<IGraphicsService>(); pGfxService->PrepareShadowGeneration(); for(auto pObj : m_Objects) pObj->GenerateShadows(context); pGfxService->GetGraphicsDevice()->ResetRenderTarget(); pGfxService->PrepareDeferredShading(); for(auto pObj : m_Objects){ pObj->Draw(context); //pObj->DrawObject(context); } pGfxService->CompositeDeferredShading(context); //Render FPS m_pDefaultFont->DrawText(std::tstring(_T("FPS: ")) + to_tstring(context.FramesPerSecond) + _T("\nSPF: ") + to_tstring(context.GameTimer.GetElapsedSeconds() ), tt::Vector2(5,0), tt::Vector4(1,1,0,1) ); pGfxService->GetSpriteBatch()->Flush(context); auto particlesSprite = ParticleEmitterComponent::RenderDeferred(context); //pGfxService->GetSpriteBatch()->Draw(particlesSprite); //pGfxService->GetSpriteBatch()->Flush(context); if(!m_PostProEffects.empty() ){ auto postProSprite = pGfxService->RenderPostProcessing(context, m_PostProEffects); pGfxService->GetSpriteBatch()->Draw(postProSprite); pGfxService->GetSpriteBatch()->Flush(context); } }
void CALLBACK load_texture_cb(ID3D11Device* device, char* texture_file, ID3D11ShaderResourceView** srv, void* context) { tstring* base_path = reinterpret_cast<tstring*>(context); load_texture(device, (*base_path + to_tstring(texture_file)), srv); }
MeshContainer* XFileLoader::CreateMeshContainer() { HRESULT hr; MeshContainer* meshContainer = NULL; Graphics* graphics = Graphics::GetInstance(); IDirect3DDevice9Ptr pD3DDevice = graphics->GetDirect3DDevice(); if( !(m_pD3DMesh->GetFVF() & D3DFVF_NORMAL) ) { LPD3DXMESH tmpMesh = NULL; // 柔軟な頂点フォーマット (FVF) コードを使ってメッシュのコピーを作成する hr = m_pD3DMesh->CloneMeshFVF( m_pD3DMesh->GetOptions(), m_pD3DMesh->GetFVF() | D3DFVF_NORMAL, pD3DDevice, &tmpMesh); // ←ここにコピー if(FAILED(hr)) { goto exit; } // メッシュに含まれる各頂点の法線を計算して、設定する //D3DXComputeNormals( tmpMesh, reinterpret_cast<DWORD*>(pAdjacencyBuf->GetBufferPointer()) ); D3DXComputeNormals( tmpMesh, NULL ); m_pD3DMesh->Release(); m_pD3DMesh = tmpMesh; } D3DVERTEXELEMENT9 pDecl[MAX_FVF_DECL_SIZE]; hr = m_pD3DMesh->GetDeclaration(pDecl); if( FAILED(hr) ) { goto exit; } DWORD vertexNum = m_pD3DMesh->GetNumVertices(); DWORD faceNum = m_pD3DMesh->GetNumFaces(); DWORD attrNum = 0; m_pD3DMesh->GetAttributeTable(NULL, &attrNum); DWORD size = m_pD3DMesh->GetNumBytesPerVertex(); BYTE* pD3DVertice = NULL; m_pD3DMesh->LockVertexBuffer( 0,(LPVOID*)&pD3DVertice ); sVertex* vertices = new sVertex[vertexNum]; for( DWORD vertIdx = 0;vertIdx<vertexNum;vertIdx++ ) { sVertex* vertex = &vertices[vertIdx]; vertex->uv = D3DXVECTOR2(0.0f,0.0f); vertex->color = 0xFFFFFFFF; for( DWORD i=0;i<MAX_FVF_DECL_SIZE;i++ ) { D3DVERTEXELEMENT9& decl = pDecl[i]; if( decl.Stream==0xFF ) { break; } switch( decl.Usage ) { case D3DDECLUSAGE_POSITION: vertex->position = *(D3DXVECTOR3*)(pD3DVertice+vertIdx*size+decl.Offset); vertex->position = vertex->position * m_scale; break; case D3DDECLUSAGE_NORMAL: vertex->normal = *(D3DXVECTOR3*)(pD3DVertice+vertIdx*size+decl.Offset); break; case D3DDECLUSAGE_TEXCOORD: vertex->uv = *(D3DXVECTOR2*)(pD3DVertice+vertIdx*size+decl.Offset); break; case D3DDECLUSAGE_COLOR: vertex->color = *(DWORD*)(pD3DVertice+vertIdx*size+decl.Offset); break; } } } m_pD3DMesh->UnlockVertexBuffer(); LPDIRECT3DINDEXBUFFER9 pIndexBuffer = NULL; m_pD3DMesh->GetIndexBuffer( &pIndexBuffer ); D3DINDEXBUFFER_DESC desc; pIndexBuffer->GetDesc( &desc ); pIndexBuffer->Release(); DWORD* indices = new DWORD[faceNum*3]; if( desc.Format==D3DFMT_INDEX16 ) { WORD* pD3DIndices = NULL; m_pD3DMesh->LockIndexBuffer(0,(LPVOID*)&pD3DIndices ); for( DWORD i=0;i<faceNum*3;i++ ) { indices[i] = pD3DIndices[i]; } m_pD3DMesh->UnlockIndexBuffer(); } else { DWORD* pD3DIndices =NULL; m_pD3DMesh->LockIndexBuffer(0,(LPVOID*)&pD3DIndices ); memcpy( indices,pD3DIndices,sizeof(DWORD)*faceNum*3 ); m_pD3DMesh->UnlockIndexBuffer(); } D3DXATTRIBUTERANGE *attrList = new D3DXATTRIBUTERANGE[attrNum]; m_pD3DMesh->GetAttributeTable(attrList, &attrNum); meshContainer = new MeshContainer; meshContainer->pMesh = new Mesh; meshContainer->pMesh->Create( vertexNum,faceNum,attrNum ); meshContainer->pMesh->SetVertices( vertices ); meshContainer->pMesh->SetIndices( indices ); meshContainer->pMesh->SetAttributeRanges( attrList ); delete[] vertices; delete[] indices; delete[] attrList; meshContainer->materialNum = m_Materials; meshContainer->pMaterials = new sMaterial[m_Materials]; D3DXMATERIAL* pD3DMaterials = (D3DXMATERIAL*)m_pMaterialBuf->GetBufferPointer(); for( DWORD i=0;i<m_Materials;i++ ) { sMaterial* pMaterial = &meshContainer->pMaterials[i]; D3DXMATERIAL* pD3DMaterial = &pD3DMaterials[i]; pMaterial->colorDiffuse = pD3DMaterial->MatD3D.Diffuse; pMaterial->colorSpecular.r = pD3DMaterial->MatD3D.Specular.r; pMaterial->colorSpecular.g = pD3DMaterial->MatD3D.Specular.g; pMaterial->colorSpecular.b = pD3DMaterial->MatD3D.Specular.b; pMaterial->colorSpecular.a = 0.0f; pMaterial->colorAmbient.r = pD3DMaterial->MatD3D.Diffuse.r; pMaterial->colorAmbient.g = pD3DMaterial->MatD3D.Diffuse.g; pMaterial->colorAmbient.b = pD3DMaterial->MatD3D.Diffuse.b; pMaterial->colorAmbient.a = 0.0f; pMaterial->colorEmissive.r = pD3DMaterial->MatD3D.Emissive.r; pMaterial->colorEmissive.g = pD3DMaterial->MatD3D.Emissive.g; pMaterial->colorEmissive.b = pD3DMaterial->MatD3D.Emissive.b; pMaterial->colorEmissive.a = 0.0f; pMaterial->specularPower = pD3DMaterial->MatD3D.Power; TCHAR path[MAX_PATH]; _tcscpy_s( path,m_path.c_str() ); tstring texFileName; tstring sphereFileName; if( pD3DMaterial->pTextureFilename && strlen(pD3DMaterial->pTextureFilename)>0 ) { tstring filename = to_tstring(pD3DMaterial->pTextureFilename); tstring::size_type index = filename.find( _T("*") ); if( index != tstring::npos ) { sphereFileName = filename.substr( index+1 ); PathAppend( path,sphereFileName.c_str() ); sphereFileName = path; PathRemoveFileSpec( path ); texFileName = filename.erase( index ); PathAppend( path,texFileName.c_str() ); texFileName = path; PathRemoveFileSpec( path ); } else { texFileName = filename; PathAppend( path,texFileName.c_str() ); texFileName = path; PathRemoveFileSpec( path ); } tstring ext = PathFindExtension( texFileName.c_str() ); if( ext == _T(".sph" ) || ext == _T(".spa") ) { sphereFileName = texFileName; texFileName = _T(""); } } if( !texFileName.empty() ) { TexturePtr pTex = ResourceManager::GetInstance().GetResource<Texture>( texFileName ); if( !pTex ) { pTex = TexturePtr(new Texture); if( pTex->CreateFromFile( texFileName ) ) { ResourceManager::GetInstance().AddResource( texFileName,pTex ); } else { pTex.reset(); } } if( pTex ) { pMaterial->textureDiffuse = pTex; } } if( !sphereFileName.empty() ) { TexturePtr pTex = ResourceManager::GetInstance().GetResource<Texture>( sphereFileName ); if( !pTex ) { pTex = TexturePtr(new Texture); if( pTex->CreateFromFile( sphereFileName ) ) { ResourceManager::GetInstance().AddResource( sphereFileName,pTex ); } else { pTex.reset(); } } if( pTex ) { pMaterial->textureSphere = pTex; } tstring ext = PathFindExtension( sphereFileName.c_str() ); if( ext == _T(".sph" ) ) { pMaterial->spheremap = eSPHEREMAP_MUL; } else if( ext == _T(".spa") ) { pMaterial->spheremap = eSPHEREMAP_ADD; } } } exit: if( m_pMaterialBuf ) { m_pMaterialBuf->Release(); m_pMaterialBuf = NULL; } if( m_pEffectInstancesBuf ) { m_pEffectInstancesBuf->Release(); m_pEffectInstancesBuf = NULL; } if( m_pAdjacencyBuf ) { m_pAdjacencyBuf->Release(); m_pAdjacencyBuf = NULL; } if( m_pD3DMesh ) { m_pD3DMesh->Release(); m_pD3DMesh = NULL; } return meshContainer; }
DWORD WINAPI SocketThread_FileRecv(PVOID para) { PSOCK_THREAD_PARA pPara = (PSOCK_THREAD_PARA)para; char* recvBuff = new char[1024]; tstring filename = TEXT("unknown"); tstring dirname = TEXT("unknown"); tstring dirpath; char* filedata; int datalen = 0; bool first = true; FILE* ff = NULL; while(1) { memset(recvBuff, 0, 1024); int recv_size = recv(pPara->sock, recvBuff, 1024, 0); if (recv_size == 0 || recv_size == SOCKET_ERROR) break; if (first) // 应该包含文件名等信息 { first = false; PFILEPACK fp = (PFILEPACK)recvBuff; if (fp->dirLen + sizeof(FILEPACK) > (uint32_t)recv_size) { glogger.error(TEXT("error file packet")); break; } filename = to_tstring(fp->time); if (fp->type == 'H') filename += TEXT(".html"); else if (fp->type == 'L') filename += TEXT(".log"); else filename += TEXT(".txt"); dirname = gcommon::StringToTString(string(fp->data, fp->dirLen)); filedata = fp->data + fp->dirLen; datalen = recv_size - sizeof(FILEPACK) - fp->dirLen; // 信息 // 获取崩溃位置作为目录名 if (fp->type == 'H') { tstring remoteIP = inet_ltot(pPara->remoteIP); glogger.setDefaultColor(gcommon::PRINT_COLOR::BRIGHT_RED); glogger.insertCurrentTime(TEXT(" [yyyy-MM-dd HH:mm:ss] ")); glogger.screen(TEXT("[") + remoteIP + TEXT("] ") + dirname + TEXT("\n")); glogger.logfile(TEXT("[") + remoteIP + TEXT("] ") + dirname + TEXT("\n")); glogger.setDefaultColor(); } } else { filedata = recvBuff; datalen = recv_size; } // 打开文件 if (ff == NULL) { dirpath = pPara->para->outPath + inet_ltot(pPara->remoteIP) + TEXT("\\"); CreateDirectory(dirpath.c_str(), NULL); dirpath += dirname + TEXT("\\"); CreateDirectory(dirpath.c_str(), NULL); _tfopen_s(&ff, (dirpath+filename).c_str(),TEXT("a")); if (ff == NULL) { glogger.error(TEXT("can not create file: %s"), filename.c_str()); dirpath = pPara->para->outPath + inet_ltot(pPara->remoteIP) + TEXT("\\unknown\\"); _tfopen_s(&ff, (dirpath + filename).c_str(), TEXT("a")); if(ff == NULL) break; } } // 写文件 fwrite(filedata, 1, datalen, ff); } if (ff)fclose(ff); closesocket(pPara->sock); delete[] recvBuff; return 0; }
BOOL CSocketObj::ConnectSer(CCustomIOCPBaseList &IOCPList, LPCTSTR SerAddr, int Port, int IncRefNumber) { BOOL resu = FALSE; ADDRINFOT _Hints; int _Retval; PADDRINFOT _ResultAddInfo; PADDRINFOT _NextAddInfo; DWORD _AddrStringLen; LPTSTR _AddrString; assert(mIsSerSocket == FALSE); ZeroMemory(&_Hints, sizeof(_Hints)); _Hints.ai_family = AF_UNSPEC; _Hints.ai_socktype = SOCK_STREAM; _Hints.ai_protocol = IPPROTO_TCP; _Retval = GetAddrInfo(SerAddr, to_tstring(Port).c_str(), &_Hints, &_ResultAddInfo); if (_Retval != 0) { return FALSE; } _NextAddInfo = _ResultAddInfo; // 申请缓冲区 _AddrString = new TCHAR[ADDR_STRING_MAX_LEN]; while (_NextAddInfo != NULL) { _AddrStringLen = ADDR_STRING_MAX_LEN; // 获取 #ifdef _DEBUG if (WSAAddressToString(_NextAddInfo->ai_addr, (DWORD)_NextAddInfo->ai_addrlen, NULL, _AddrString, &_AddrStringLen) == 0) { // 改为真实长度,这里的_AddrStringLen包含了末尾的字符#0,所以要减去这个#0的长度 _AddrStringLen--; OutputDebugStr(_T("ai_addr:%s,ai_flags:%d,ai_canonname=%s\n"), _AddrString, _NextAddInfo->ai_flags, _NextAddInfo->ai_canonname); } else { OutputDebugStr(_T("WSAAddressToString Error:%d\n"), WSAGetLastError()); } #endif mSock = WSASocket(_NextAddInfo->ai_family, _NextAddInfo->ai_socktype, _NextAddInfo->ai_protocol, NULL, 0, WSA_FLAG_OVERLAPPED); if (mSock != INVALID_SOCKET) { if (connect(mSock, _NextAddInfo->ai_addr, (INT)_NextAddInfo->ai_addrlen) == SOCKET_ERROR) { DWORD LastError = WSAGetLastError(); OutputDebugStr(_T("连接%s失败:%d\n"), _AddrString, LastError); closesocket(mSock); WSASetLastError(LastError); mSock = INVALID_SOCKET; } else { mOwner = &IOCPList; // 增加引用 IncRefCount(IncRefNumber); resu = IOCPList.AddSockBase(this); if (!resu) { DWORD LastError = WSAGetLastError(); OutputDebugStr(_T("添加%s到列表中失败:%d\n"), _AddrString, LastError); closesocket(mSock); WSASetLastError(LastError); mSock = INVALID_SOCKET; // 减少引用 DecRefCount(IncRefNumber); } break; } } _NextAddInfo = _NextAddInfo->ai_next; } delete[] _AddrString; FreeAddrInfo(_ResultAddInfo); return resu; }
PMDModelPtr PMDFileLoader::Open( const tstring& filePath ) { m_fileName = filePath; TCHAR path[MAX_PATH]; _tcscpy_s( path,MAX_PATH,filePath.c_str() ); PathRemoveFileSpec( path ); PathAddBackslash( path ); m_path = path; FILE* fp=NULL; if( _tfopen_s(&fp,m_fileName.c_str(),_T("rb"))!=0 ) { return PMDModelPtr(); } fpos_t fsize = 0; fseek(fp,0,SEEK_END); fgetpos(fp,&fsize); fseek(fp,0,SEEK_SET); size_t sz=(size_t)fsize; unsigned char* buffer=new unsigned char[sz]; fread(buffer,1,sz,fp); fclose(fp); sPMD* pmd = new sPMD; bool ret = PMDLoad(buffer,sz,pmd); delete[] buffer; if( !ret ) { delete pmd; return PMDModelPtr(); } sPMD_Skin* skinBase = NULL; for( DWORD skinIdx=0;skinIdx<pmd->skin_list.skin_count;skinIdx++ ) { sPMD_Skin* skin = &pmd->skin_list.skin[skinIdx]; if( skin->skin_type == ePMD_SkinType_Base ) { skinBase = skin; for( DWORD vertIdx = 0; vertIdx < skin->skin_vert_count ; vertIdx++ ) { DWORD targetIndex = skin->skin_vert[vertIdx].index; memcpy( pmd->vertex_list.vertex[ targetIndex ].pos, skin->skin_vert[vertIdx].pos,sizeof(float)*3 ); } break; } } for( DWORD skinIdx=0;skinIdx<pmd->skin_list.skin_count;skinIdx++ ) { sPMD_Skin* skin = &pmd->skin_list.skin[skinIdx]; if( skin->skin_type != ePMD_SkinType_Base ) { for( DWORD vertIdx = 0; vertIdx < skin->skin_vert_count ; vertIdx++ ) { DWORD targetIndex = skin->skin_vert[vertIdx].index; skin->skin_vert[vertIdx].index = skinBase->skin_vert[ targetIndex ].index; } } } Graphics* graphics = Graphics::GetInstance(); sMaterial* pMaterials = new sMaterial[pmd->material_list.material_count]; for( DWORD i=0;i<pmd->material_list.material_count;i++ ) { sPMD_Material* pmdMat = &pmd->material_list.material[i]; sMaterial* pMaterial = &pMaterials[i]; pMaterial->colorDiffuse.r = 0.0f; pMaterial->colorDiffuse.g = 0.0f; pMaterial->colorDiffuse.b = 0.0f; pMaterial->colorDiffuse.a = pmdMat->alpha; pMaterial->colorAmbient.r = pmdMat->diffuse_color[0]; pMaterial->colorAmbient.g = pmdMat->diffuse_color[1]; pMaterial->colorAmbient.b = pmdMat->diffuse_color[2]; pMaterial->colorAmbient.a = 0.0f; pMaterial->colorSpecular.r = pmdMat->specular_color[0]; pMaterial->colorSpecular.g = pmdMat->specular_color[1]; pMaterial->colorSpecular.b = pmdMat->specular_color[2]; pMaterial->colorSpecular.a = 0.0f; pMaterial->colorEmissive.r = pmdMat->ambient_color[0]; pMaterial->colorEmissive.g = pmdMat->ambient_color[1]; pMaterial->colorEmissive.b = pmdMat->ambient_color[2]; pMaterial->colorEmissive.a = 0.0f; pMaterial->specularPower = pmdMat->specularity; pMaterial->spheremap = eSPHEREMAP_MUL; pMaterial->edge = pmdMat->edge_flag!=0; TCHAR path[MAX_PATH]; _tcscpy_s( path,m_path.c_str() ); tstring texFileName; tstring sphereFileName; if( strlen(pmdMat->texture_file_name)>0 ) { tstring filename = to_tstring(pmdMat->texture_file_name); tstring::size_type index = filename.find( _T("*") ); if( index != tstring::npos ) { sphereFileName = filename.substr( index+1 ); PathAppend( path,sphereFileName.c_str() ); sphereFileName = path; PathRemoveFileSpec( path ); texFileName = filename.erase( index ); PathAppend( path,texFileName.c_str() ); texFileName = path; PathRemoveFileSpec( path ); } else { texFileName = filename; PathAppend( path,texFileName.c_str() ); texFileName = path; PathRemoveFileSpec( path ); } tstring ext = PathFindExtension( texFileName.c_str() ); if( ext == _T(".sph" ) || ext == _T(".spa") ) { sphereFileName = texFileName; texFileName = _T(""); } } if( !texFileName.empty() ) { TexturePtr pTex = ResourceManager::GetInstance().GetResource<Texture>( texFileName ); if( !pTex ) { pTex = TexturePtr(new Texture); pTex->CreateFromFile( texFileName ); ResourceManager::GetInstance().AddResource( texFileName,pTex ); } pMaterial->textureDiffuse = pTex; } if( !sphereFileName.empty() ) { TexturePtr pTex = ResourceManager::GetInstance().GetResource<Texture>( sphereFileName ); if( !pTex ) { pTex = TexturePtr(new Texture); pTex->CreateFromFile( sphereFileName ); ResourceManager::GetInstance().AddResource( sphereFileName,pTex ); } pMaterial->textureSphere = pTex; tstring ext = PathFindExtension( sphereFileName.c_str() ); if( ext == _T(".sph" ) ) { pMaterial->spheremap = eSPHEREMAP_MUL; } else if( ext == _T(".spa") ) { pMaterial->spheremap = eSPHEREMAP_ADD; } } pMaterial->colorToon = D3DXCOLOR( 1.0f,1.0f,1.0f,1.0f ); tstring toonTexName = _T(""); tstring toonTexPath = _T(""); if( 0<=pmdMat->toon_index && pmdMat->toon_index<10 ) { // TODO:デフォルトのtoonファイルは固定パスか・・・ toonTexName = to_tstring( pmd->toon_list.toon_file_name[pmdMat->toon_index] ); } TexturePtr pTex; if( !toonTexName.empty() ) { PathAppend( path,toonTexName.c_str() ); toonTexPath = path; PathRemoveFileSpec( path ); pTex = ResourceManager::GetInstance().GetResource<Texture>( toonTexPath ); if( !pTex ) { pTex = TexturePtr(new Texture); if( !pTex->CreateFromFile( toonTexPath ) ) { pTex.reset(); } } } if( !pTex ) { pTex = graphics->GetDefaultToonTexture( pmdMat->toon_index ); if( !pTex ) { toonTexPath = _T("<FFFFFFFF>"); pTex = ResourceManager::GetInstance().GetResource<Texture>( toonTexPath ); if( !pTex ) { pTex = TexturePtr(new Texture); if( !pTex->CreateDotColor( 0xFFFFFFFF ) ) { pTex.reset(); } } } } if( pTex ) { pMaterial->textureToon = pTex; ResourceManager::GetInstance().AddResource( toonTexPath,pTex ); IDirect3DTexture9Ptr pD3DTexture = pTex->GetTexture(); D3DSURFACE_DESC desc; pD3DTexture->GetLevelDesc( 0,&desc ); D3DLOCKED_RECT lockRect; pD3DTexture->LockRect(0, &lockRect, NULL, 0); int x = 0; int y = desc.Height-1; DWORD color; memcpy(&color,(BYTE*)lockRect.pBits + lockRect.Pitch*y + 4*x, sizeof(DWORD) ); pD3DTexture->UnlockRect(0); pMaterial->colorToon = D3DXCOLOR( color ); } } return PMDModelPtr( new PMDModel(pmd,pmd->material_list.material_count,pMaterials) ); }
void stb_to_srv(ID3D11Device* device, LPCSTR filename, ID3D11ShaderResourceView** srv) { if (!srv) throw exception(L"No SRV"); *srv = nullptr; bool is_hdr = std::string(filename).find(".hdr") != std::string::npos; bool is_jpg = std::string(filename).find(".jpg") != std::string::npos; D3D11_TEXTURE2D_DESC tex_desc; ZeroMemory(&tex_desc, sizeof(tex_desc)); D3D11_SUBRESOURCE_DATA subdata; ZeroMemory(&subdata, sizeof(subdata)); int width, height, nc; unsigned char* ldr_data = nullptr; float* hdr_data = nullptr; if (!is_hdr) { ldr_data = stbi_load(filename, &width, &height, &nc, 4); subdata.SysMemPitch = width * 4; subdata.pSysMem = ldr_data; if (is_jpg) tex_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; else // TODO: Remap to SRGB later when throwing out D3DX tex_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // TODO: Need to create mipmaps } else { hdr_data = stbi_loadf(filename, &width, &height, &nc, 4); subdata.SysMemPitch = width * 4 * 4; subdata.pSysMem = hdr_data; tex_desc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; } if (!ldr_data && !hdr_data) { tstring error = L"can't load"; if (stbi_failure_reason()) error = to_tstring(std::string(stbi_failure_reason())); throw exception(tstring(L"stbi: ") + error); } tex_desc.Width = width; tex_desc.Height = height; tex_desc.MipLevels = 1; tex_desc.ArraySize = 1; tex_desc.SampleDesc.Count = 1; tex_desc.SampleDesc.Quality = 0; tex_desc.Usage = D3D11_USAGE_DEFAULT; tex_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; tex_desc.CPUAccessFlags = 0; tex_desc.MiscFlags = 0; ID3D11Texture2D* texture; assert_hr(device->CreateTexture2D(&tex_desc, &subdata, &texture)); assert_hr(device->CreateShaderResourceView(texture, nullptr, srv)); safe_release(texture); if (is_hdr) stbi_image_free(hdr_data); else stbi_image_free(ldr_data); }