cModel::cModel(const LPCTSTR filename) { cFile file; const WCHAR* tag; const WCHAR* value; IXmlReader* pReader; UINT iAtributeCount; int t1, t2; int nVertexFields, nTris, nVerts, nColors, nTex; queue<wstring> m_tokens; vector<D3DXVECTOR3> vertices, normals; vector<D3DXVECTOR2> textures; vector<D3DXCOLOR> colors; vector<WORD> tris; file.OpenXML(filename); pReader = file.GetXml(); file.ReadXML(); file.GetXmlPrefix(&tag); file.GetXmlValue(&value); if(value != L"tgcScene") { //Break formato incorrecto } file.ReadXML(); //<name> file.ReadXML(); //Text_Node file.GetXmlValue(&value); //Guardar name de la scene file.ReadXML(); //</name> file.ReadXML(); //<texturesExport /> pReader->GetLocalName(&value, NULL); pReader->GetAttributeCount(&iAtributeCount); file.ReadXML(); //<lightmapExport /> pReader->GetAttributeCount(&iAtributeCount); file.ReadXML(); //<sceneBoundingBox /> pReader->GetAttributeCount(&iAtributeCount); file.ReadXML(); //<materials> pReader->GetAttributeCount(&iAtributeCount); pReader->MoveToAttributeByName(L"count", NULL); //se busca el count pReader->GetValue(&value, NULL); t1 = _wtoi(value); for(int i = 0; i < t1; i++) { file.ReadXML(); //<m> file.ReadXML(); //<ambient> file.ReadXML(); //Text_node file.ReadXML(); //</ambient> file.ReadXML(); //<diffuse> file.ReadXML(); //Text_node file.ReadXML(); //</diffuse> file.ReadXML(); //<specular> file.ReadXML(); //Text_node file.ReadXML(); //</specular> file.ReadXML(); //<opacity> file.ReadXML(); //Text_node file.ReadXML(); //</opacity> file.ReadXML(); //<bitmap> file.ReadXML(); //Text_node pReader->GetValue(&value, NULL); m_texture.append(Application()->m_dAppMedia+L"Textures\\"); m_texture.append(value); file.ReadXML(); //</bitmap> file.ReadXML(); //</m> } file.ReadXML(); //</Materials> file.ReadXML(); //<Meshes> pReader->GetAttributeCount(&iAtributeCount); pReader->MoveToAttributeByName(L"count", NULL); //se busca el count pReader->GetValue(&value, NULL); t1 = _wtoi(value); for(int i = 0; i < t1; i++) { file.ReadXML(); //<mesh> pReader->MoveToNextAttribute();//name pReader->MoveToNextAttribute();//pos pReader->MoveToNextAttribute();//matId pReader->MoveToNextAttribute();//color pReader->MoveToNextAttribute();//visibility pReader->MoveToNextAttribute();//lightmap file.ReadXML(); //<boundingBox /> pReader->GetAttributeCount(&iAtributeCount); file.ReadXML(); //<coordinatesIdx> pReader->MoveToAttributeByName(L"count", NULL); //se busca el count pReader->GetValue(&value, NULL); //Se guarda la cantidad de triangulos nTris = _wtoi(value) / 3; file.ReadXML(); //Text_Node pReader->GetValue(&value, NULL); //String file.TokenizeXmlValue(&m_tokens, ' '); m_tris.reserve(nTris); for(int j = 0; j < nTris; j++) { sTri tri; tri.v[0] = _wtoi(m_tokens.front().c_str()); m_tokens.pop(); tri.v[1] = _wtoi(m_tokens.front().c_str()); m_tokens.pop(); tri.v[2] = _wtoi(m_tokens.front().c_str()); m_tokens.pop(); m_tris.push_back(tri); } file.ReadXML(); //</coordinatesIdx> file.ReadXML(); //<textCoordsIdx> pReader->MoveToAttributeByName(L"count", NULL); //se busca el count pReader->GetValue(&value, NULL); nTex = _wtoi(value) / 3; file.ReadXML(); //Text_Node pReader->GetValue(&value, NULL); //String file.TokenizeXmlValue(&m_tokens, ' '); m_textures.reserve(nTex); for(int j = 0; j < nTex; j++) { sTri tri; tri.v[0] = _wtoi(m_tokens.front().c_str()); m_tokens.pop(); tri.v[1] = _wtoi(m_tokens.front().c_str()); m_tokens.pop(); tri.v[2] = _wtoi(m_tokens.front().c_str()); m_tokens.pop(); m_textures.push_back(tri); } file.ReadXML(); //</textCoordsIdx> file.ReadXML(); //<colorsIdx> pReader->MoveToAttributeByName(L"count", NULL); //se busca el count pReader->GetValue(&value, NULL); nColors = _wtoi(value) / 3; file.ReadXML(); //Text_Node pReader->GetValue(&value, NULL); //String file.TokenizeXmlValue(&m_tokens, ' '); m_colors.reserve(nColors); for(int j = 0; j < nColors; j++) { sTri tri; tri.v[0] = _wtoi(m_tokens.front().c_str()); m_tokens.pop(); tri.v[1] = _wtoi(m_tokens.front().c_str()); m_tokens.pop(); tri.v[2] = _wtoi(m_tokens.front().c_str()); m_tokens.pop(); m_colors.push_back(tri); } file.ReadXML(); //</colorsIdx> file.ReadXML(); //<matIds> file.ReadXML(); //Text_Node file.ReadXML(); //</matIds> file.ReadXML(); //<vertices> pReader->MoveToAttributeByName(L"count", NULL); //se busca el count pReader->GetValue(&value, NULL); //Se guarda la cantidad de vertices nVerts = _wtoi(value) / 3; file.ReadXML(); //Text_Node pReader->GetValue(&value, NULL); //String file.TokenizeXmlValue(&m_tokens, ' '); vertices.reserve(nVerts); for(int j = 0; j < nVerts; j++) { D3DXVECTOR3 v; v.x = (float)_wtof(m_tokens.front().c_str()); m_tokens.pop(); v.y = (float)_wtof(m_tokens.front().c_str()); m_tokens.pop(); v.z = (float)_wtof(m_tokens.front().c_str()); m_tokens.pop(); vertices.push_back(v); } file.ReadXML(); //</vertices> file.ReadXML(); //<normals> pReader->MoveToAttributeByName(L"count", NULL); //se busca el count pReader->GetValue(&value, NULL); //Se guarda la cantidad de normales normals.reserve(_wtoi(value) / 3); file.ReadXML(); //Text_Node file.TokenizeXmlValue(&m_tokens, ' '); for(int j = 0; j < normals.capacity(); j++) { D3DXVECTOR3 v; v.x = (float)_wtof(m_tokens.front().c_str()); m_tokens.pop(); v.y = (float)_wtof(m_tokens.front().c_str()); m_tokens.pop(); v.z = (float)_wtof(m_tokens.front().c_str()); m_tokens.pop(); normals.push_back(v); } file.ReadXML(); //</normals> file.ReadXML(); //<texCoords> pReader->MoveToAttributeByName(L"count", NULL); //se busca el count pReader->GetValue(&value, NULL); //Se guarda la cantidad de normales textures.reserve(_wtoi(value) / 2); file.ReadXML(); //Text_Node file.TokenizeXmlValue(&m_tokens, ' '); for(int j = 0; j < textures.capacity(); j++) { D3DXVECTOR2 v; v.x = (float)_wtof(m_tokens.front().c_str()); m_tokens.pop(); v.y = (float)_wtof(m_tokens.front().c_str()); m_tokens.pop(); textures.push_back(v); } file.ReadXML(); //</texCoords> file.ReadXML(); //<colors> pReader->MoveToAttributeByName(L"count", NULL); //se busca el count pReader->GetValue(&value, NULL); colors.reserve(_wtoi(value) / 3); file.ReadXML(); //Text_Node file.TokenizeXmlValue(&m_tokens, ' '); for(int j = 0; j < colors.capacity();j++) { D3DXCOLOR c; c.r = (float)_wtof(m_tokens.front().c_str()); m_tokens.pop(); c.g = (float)_wtof(m_tokens.front().c_str()); m_tokens.pop(); c.b = (float)_wtof(m_tokens.front().c_str()); m_tokens.pop(); c.a = 1.0f; colors.push_back(c); } file.ReadXML(); //</colors> file.ReadXML(); //</mesh> nVerts = nTris * 3; m_verts.reserve(nVerts); for(int j = 0; j < nTris; j++) { cGraphicsLayer::cDefaultVertex v1, v2, v3; v1.m_vPosition = vertices[m_tris[j].v[0]]; v1.m_vColor = colors[m_colors[j].v[0]]; v1.m_vNormal = normals[m_tris[j].v[0]]; v1.m_TexCoords = textures[m_textures[j].v[0]]; m_verts.push_back(v1); tris.push_back(m_verts.size() - 1); v2.m_vPosition = vertices[m_tris[j].v[1]]; v2.m_vColor = colors[m_colors[j].v[1]]; v2.m_vNormal = normals[m_tris[j].v[1]]; v2.m_TexCoords = textures[m_textures[j].v[1]]; m_verts.push_back(v2); tris.push_back(m_verts.size() - 1); v3.m_vPosition = vertices[m_tris[j].v[2]]; v3.m_vColor = colors[m_colors[j].v[2]]; v3.m_vNormal = normals[m_tris[j].v[2]]; v3.m_TexCoords = textures[m_textures[j].v[2]]; m_verts.push_back(v3); tris.push_back(m_verts.size() - 1); } } file.ReadXML(); //</meshes> file.ReadXML(); //</tgcScene> // Setup vertex and index buffers m_pVertexBuffer = NULL; m_pIndexBuffer = NULL; D3D10_BUFFER_DESC descBuffer; //Se crea la estructura que describe el vertexBuffer memset(&descBuffer, 0, sizeof(descBuffer)); descBuffer.Usage = D3D10_USAGE_DEFAULT; descBuffer.ByteWidth = sizeof(cGraphicsLayer::cDefaultVertex) * NumVerts(); descBuffer.BindFlags = D3D10_BIND_VERTEX_BUFFER; descBuffer.CPUAccessFlags = 0; descBuffer.MiscFlags = 0; D3D10_SUBRESOURCE_DATA resData; memset(&resData, 0, sizeof(resData)); resData.pSysMem = &m_verts[0]; Graphics()->GetDevice()->CreateBuffer(&descBuffer, &resData, &m_pVertexBuffer); //Creacion del VertexBuffer descBuffer.Usage = D3D10_USAGE_DEFAULT; //Se crea la estructura que describe el IndexBuffer descBuffer.ByteWidth = sizeof(WORD) * NumTris() * 3; descBuffer.BindFlags = D3D10_BIND_INDEX_BUFFER; descBuffer.CPUAccessFlags = 0; descBuffer.MiscFlags = 0; resData.pSysMem = &tris[0]; Graphics()->GetDevice()->CreateBuffer(&descBuffer, &resData, &m_pIndexBuffer); //Creacion del IndexBuffer D3DX10_IMAGE_LOAD_INFO loadInfo; ZeroMemory( &loadInfo, sizeof(D3DX10_IMAGE_LOAD_INFO) ); loadInfo.BindFlags = D3D10_BIND_SHADER_RESOURCE; loadInfo.Format = DXGI_FORMAT_BC1_UNORM; ID3D10ShaderResourceView *pSRView = NULL; D3DX10CreateShaderResourceViewFromFile(Graphics()->GetDevice(), m_texture.c_str(), &loadInfo, NULL, &m_pSRView, NULL ); vTranslation = D3DXVECTOR3(0.0f, 0.0f, 0.0f); vRotation = D3DXVECTOR3(0.0f, 0.0f, 0.0f); vScale = D3DXVECTOR3(1.0f, 1.0f, 1.0f); D3DXMatrixIdentity(&mTransform); }
HRESULT ReadList(LPCWSTR path, LPCWSTR section, APPDATAXMLLIST &list) { HRESULT hr; IXmlReader *pReader = NULL; IStream *pFileStream = NULL; XmlNodeType nodeType; LPCWSTR pwszLocalName; LPCWSTR pwszAttributeName; LPCWSTR pwszAttributeValue; int sequence = 0; APPDATAXMLATTR attr; APPDATAXMLROW row; hr = CreateStreamReader(path, &pReader, &pFileStream); EXIT_NOT_S_OK(hr); while(pReader->Read(&nodeType) == S_OK) { switch(nodeType) { case XmlNodeType_Element: hr = pReader->GetLocalName(&pwszLocalName, NULL); EXIT_NOT_S_OK(hr); switch(sequence) { case 0: if(wcscmp(TagRoot, pwszLocalName) == 0) { sequence = 1; } break; case 1: if(wcscmp(TagSection, pwszLocalName) == 0) { sequence = 2; } break; case 3: if(wcscmp(TagList, pwszLocalName) == 0) { sequence = 4; } break; case 4: if(wcscmp(TagRow, pwszLocalName) == 0) { sequence = 5; row.clear(); } break; default: break; } for(hr = pReader->MoveToFirstAttribute(); hr == S_OK; hr = pReader->MoveToNextAttribute()) { hr = pReader->GetLocalName(&pwszAttributeName, NULL); EXIT_NOT_S_OK(hr); hr = pReader->GetValue(&pwszAttributeValue, NULL); EXIT_NOT_S_OK(hr); switch(sequence) { case 2: if(wcscmp(TagSection, pwszLocalName) == 0 && wcscmp(AttributeName, pwszAttributeName) == 0 && wcscmp(section, pwszAttributeValue) == 0) { sequence = 3; } break; case 5: if(wcscmp(TagRow, pwszLocalName) == 0) { attr.first = pwszAttributeName; attr.second = pwszAttributeValue; row.push_back(attr); } break; default: break; } } switch(sequence) { case 2: sequence = 1; break; case 5: list.push_back(row); row.clear(); break; default: break; } break; case XmlNodeType_EndElement: hr = pReader->GetLocalName(&pwszLocalName, NULL); EXIT_NOT_S_OK(hr); switch(sequence) { case 1: if(wcscmp(TagRoot, pwszLocalName) == 0) { goto exit; } break; case 2: if(wcscmp(TagSection, pwszLocalName) == 0) { sequence = 1; } break; case 3: if(wcscmp(TagSection, pwszLocalName) == 0) { goto exit; } break; case 4: if(wcscmp(TagSection, pwszLocalName) == 0 || wcscmp(TagList, pwszLocalName) == 0) { goto exit; } break; case 5: if(wcscmp(TagList, pwszLocalName) == 0) { goto exit; } break; default: break; } break; default: break; } } NOT_S_OK: exit: CloseStreamReader(pReader, pFileStream); return hr; }
HRESULT ReadValue(LPCWSTR path, LPCWSTR section, LPCWSTR key, std::wstring &strxmlval, LPCWSTR defval) { IXmlReader *pReader = NULL; IStream *pFileStream = NULL; HRESULT hr; XmlNodeType nodeType; LPCWSTR pwszLocalName; LPCWSTR pwszAttributeName; LPCWSTR pwszAttributeValue; int sequence = 0; strxmlval = defval; hr = CreateStreamReader(path, &pReader, &pFileStream); EXIT_NOT_S_OK(hr); while(pReader->Read(&nodeType) == S_OK) { switch(nodeType) { case XmlNodeType_Element: hr = pReader->GetLocalName(&pwszLocalName, NULL); EXIT_NOT_S_OK(hr); switch(sequence) { case 0: if(wcscmp(TagRoot, pwszLocalName) == 0) { sequence = 1; } break; case 1: if(wcscmp(TagSection, pwszLocalName) == 0) { sequence = 2; } break; case 3: if(wcscmp(TagKey, pwszLocalName) == 0) { sequence = 4; } break; default: break; } for(hr = pReader->MoveToFirstAttribute(); hr == S_OK; hr = pReader->MoveToNextAttribute()) { hr = pReader->GetLocalName(&pwszAttributeName, NULL); EXIT_NOT_S_OK(hr); hr = pReader->GetValue(&pwszAttributeValue, NULL); EXIT_NOT_S_OK(hr); switch(sequence) { case 2: if(wcscmp(AttributeName, pwszAttributeName) == 0 && wcscmp(section, pwszAttributeValue) == 0) { sequence = 3; } break; case 4: if(wcscmp(AttributeName, pwszAttributeName) == 0 && wcscmp(key, pwszAttributeValue) == 0) { sequence = 5; } break; case 5: if(wcscmp(AttributeValue, pwszAttributeName) == 0) { strxmlval.assign(pwszAttributeValue); goto exit; } break; default: break; } } break; case XmlNodeType_EndElement: hr = pReader->GetLocalName(&pwszLocalName, NULL); EXIT_NOT_S_OK(hr); switch(sequence) { case 1: if(wcscmp(TagRoot, pwszLocalName) == 0) { goto exit; } break; case 2: if(wcscmp(TagSection, pwszLocalName) == 0) { sequence = 1; } break; case 3: case 4: case 5: if(wcscmp(TagSection, pwszLocalName) == 0) { goto exit; } break; default: break; } break; default: break; } } NOT_S_OK: exit: CloseStreamReader(pReader, pFileStream); return hr; }
/* Modify this to avoid temp file I/O */ bool CXMLParser::extractData( wstring filename) { bool bRet = false; HRESULT hr = S_OK; IXmlReader *pReader = nullptr; IStream *pStream = nullptr; XmlNodeType nodeType; const TCHAR* pwszName; const TCHAR* pwszValue; if (FAILED(hr = SHCreateStreamOnFile(filename.c_str(), STGM_READ, &pStream))) { return bRet; } if (!GetDataContainer()) { if (FAILED(hr = CreateXmlReader(__uuidof(IXmlReader), (void**)&pReader, nullptr))) { return bRet; } if (FAILED(hr = pReader->SetInput(pStream))) { return bRet; } bool inKey = false; wstring csKeyFoundStr; while (!pReader->IsEOF()) { pReader->Read(&nodeType); switch (nodeType) { case XmlNodeType_Element: if (S_OK == pReader->GetLocalName(&pwszName, nullptr)) { if (true == isNodeInKeys(pwszName)) { inKey = true; csKeyFoundStr = pwszName; } } break; case XmlNodeType_CDATA: case XmlNodeType_Text: { if (true == inKey) { if (S_OK == pReader->GetValue(&pwszValue, nullptr)) { GetDataContainer()->setkeyValue(csKeyFoundStr, pwszValue); inKey = false; bRet = true; } } } break; default: break; } } } return bRet; }