Exemplo n.º 1
0
//-------------------------------------------------------------------------------
int CMaterialManager::LoadTexture(TextureID* p_ppiOut,aiString* szPath)
{	
	//???ai_assert(-1 != *p_ppiOut);
	ai_assert(0 != szPath);

	*p_ppiOut = -1;
#if 1

	LOG_PRINT("szPath=%s\n", szPath);
	// first get a valid path to the texture
	if( 5 == FindValidPath(szPath))
	{
		// embedded file. Find its index
		unsigned int iIndex = atoi(szPath->data+1);
		if (iIndex < mr->GetAsset()->pcScene->mNumTextures)
		{
/*			if (0 == mr->GetAsset()->pcScene->mTextures[iIndex]->mHeight)
			{
				// it is an embedded file ... don't need the file format hint,
				// simply let D3DXFROMWINE load the file
				if (FAILED(D3DXFROMWINECreateTextureFromFileInMemoryEx(mr->m_piDevice,
					mr->GetAsset()->pcScene->mTextures[iIndex]->pcData,
					mr->GetAsset()->pcScene->mTextures[iIndex]->mWidth,
					D3DXFROMWINE_DEFAULT,
					D3DXFROMWINE_DEFAULT,
					0,
					D3DUSAGE_AUTOGENMIPMAP,
					D3DFMT_UNKNOWN,
					D3DPOOL_MANAGED,
					D3DXFROMWINE_DEFAULT,
					D3DXFROMWINE_DEFAULT,
					0,
					0,
					0,
					p_ppiOut)))
				{
					std::string sz = "[ERROR] Unable to load embedded texture (#1): ";
					sz.append(szPath->data);
					//CLogDisplay::Instance().AddEntry(sz,D3DCOLOR_ARGB(0xFF,0xFF,0x0,0x0));

					this->SetDefaultTexture(p_ppiOut);
					return 1;
				}
			}
			else*/
			{
				// fill a new texture ...
		Image3 img;

		BYTE* dst=img.create(
			FORMAT_RGBA8,//???
			mr->GetAsset()->pcScene->mTextures[iIndex]->mWidth,
					mr->GetAsset()->pcScene->mTextures[iIndex]->mHeight,1,1);
		//BYTE* dst=img.getPixels();


				// now copy the data to it ... (assume non pow2 to be supported)

//D3DLOCKED_RECT sLock;

	//	sLock.pBits=IRenderer::GetRendererInstance()->LockTexture(*p_ppiOut, 0, sLock.Pitch);

				const aiTexel* pcData = mr->GetAsset()->pcScene->mTextures[iIndex]->pcData;

				for (unsigned int y = 0; y < mr->GetAsset()->pcScene->mTextures[iIndex]->mHeight;++y)
				{
					stx_memcpy(dst,pcData,mr->GetAsset()->pcScene->mTextures[iIndex]->
						mWidth *sizeof(aiTexel));
					dst = (BYTE *)dst + 4*mr->GetAsset()->pcScene->mTextures[iIndex]->mWidth;
					pcData += mr->GetAsset()->pcScene->mTextures[iIndex]->mWidth;
				}
  //  IRenderer::GetRendererInstance()->UnlockTexture(*p_ppiOut, 0);
//				(*p_ppiOut)->GenerateMipSubLevels();

		*p_ppiOut=IRenderer::GetRendererInstance()->addTexture(
			&img,
			false,
			IRenderer::GetRendererInstance()->Getlinear()
			,STATIC);

			}
			return 1;
		}
		else
		{
			std::string sz = "[ERROR] Invalid index for embedded texture: ";
			sz.append(szPath->data);
			//CLogDisplay::Instance().AddEntry(sz,D3DCOLOR_ARGB(0xFF,0xFF,0x0,0x0));

			SetDefaultTexture(p_ppiOut);
			return 1;
		}
	}

    	////LOG_PRINT("mr->m_piDevice=%x\n", mr->m_piDevice);
	//DBG_HALT;
	// then call D3DXFROMWINE to load the texture
	//D3DFMT_A8R8G8B8,
#endif
	std::string f1=szPath->data;
	LOG_FNLN;
	LOG_PRINT("Assimp Mesh Texture:%s\n", f1.c_str());
#if 1
		*p_ppiOut=//IRenderer::GetRendererInstance()->
			IRenderer::GetRendererInstance()->addImageLibTexture(
			f1.c_str(),
			false,
			IRenderer::GetRendererInstance()->Getlinear()
			,STATIC
			);
#else
	Image3 img;
	if (img.loadImageLibImage(szPath->data, false))
	{
		img.createMipMaps();
		*p_ppiOut=IRenderer::GetRendererInstance()->addTexture(&img,false, IRenderer::GetRendererInstance()->Getlinear(),STATIC,0.0f,false);
	}
#endif
	return 1;
}
Exemplo n.º 2
0
//-------------------------------------------------------------------------------
int CMaterialManager::LoadTexture(IDirect3DTexture9** p_ppiOut,aiString* szPath)
{
    ai_assert(NULL != p_ppiOut);
    ai_assert(NULL != szPath);

    *p_ppiOut = NULL;

    const std::string s = szPath->data;
    TextureCache::iterator ff;
    if ((ff = sCachedTextures.find(s)) != sCachedTextures.end()) {
        *p_ppiOut = (*ff).second;
        (*p_ppiOut)->AddRef();
        return 1;
    }

    // first get a valid path to the texture
    if( 5 == FindValidPath(szPath))
    {
        // embedded file. Find its index
        unsigned int iIndex = atoi(szPath->data+1);
        if (iIndex < g_pcAsset->pcScene->mNumTextures)
        {
            if (0 == g_pcAsset->pcScene->mTextures[iIndex]->mHeight)
            {
                // it is an embedded file ... don't need the file format hint,
                // simply let D3DX load the file
                D3DXIMAGE_INFO info;
                if (FAILED(D3DXCreateTextureFromFileInMemoryEx(g_piDevice,
                    g_pcAsset->pcScene->mTextures[iIndex]->pcData,
                    g_pcAsset->pcScene->mTextures[iIndex]->mWidth,
                    D3DX_DEFAULT,
                    D3DX_DEFAULT,
                    1,
                    D3DUSAGE_AUTOGENMIPMAP,
                    D3DFMT_UNKNOWN,
                    D3DPOOL_MANAGED,
                    D3DX_DEFAULT,
                    D3DX_DEFAULT,
                    0,
                    &info,
                    NULL,
                    p_ppiOut)))
                {
                    std::string sz = "[ERROR] Unable to load embedded texture (#1): ";
                    sz.append(szPath->data);
                    CLogDisplay::Instance().AddEntry(sz,D3DCOLOR_ARGB(0xFF,0xFF,0x0,0x0));

                    this->SetDefaultTexture(p_ppiOut);
                    return 1;
                }
            }
            else
            {
                // fill a new texture ...
                if(FAILED(g_piDevice->CreateTexture(
                    g_pcAsset->pcScene->mTextures[iIndex]->mWidth,
                    g_pcAsset->pcScene->mTextures[iIndex]->mHeight,
                    0,D3DUSAGE_AUTOGENMIPMAP,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,p_ppiOut,NULL)))
                {
                    std::string sz = "[ERROR] Unable to load embedded texture (#2): ";
                    sz.append(szPath->data);
                    CLogDisplay::Instance().AddEntry(sz,D3DCOLOR_ARGB(0xFF,0xFF,0x0,0x0));

                    this->SetDefaultTexture(p_ppiOut);
                    return 1;
                }

                // now copy the data to it ... (assume non pow2 to be supported)
                D3DLOCKED_RECT sLock;
                (*p_ppiOut)->LockRect(0,&sLock,NULL,0);

                const aiTexel* pcData = g_pcAsset->pcScene->mTextures[iIndex]->pcData;

                for (unsigned int y = 0; y < g_pcAsset->pcScene->mTextures[iIndex]->mHeight;++y)
                {
                    memcpy(sLock.pBits,pcData,g_pcAsset->pcScene->mTextures[iIndex]->
                        mWidth *sizeof(aiTexel));
                    sLock.pBits = (char*)sLock.pBits + sLock.Pitch;
                    pcData += g_pcAsset->pcScene->mTextures[iIndex]->mWidth;
                }
                (*p_ppiOut)->UnlockRect(0);
                (*p_ppiOut)->GenerateMipSubLevels();
            }
            sCachedTextures[s] = *p_ppiOut;
            (*p_ppiOut)->AddRef();
            return 1;
        }
        else
        {
            std::string sz = "[ERROR] Invalid index for embedded texture: ";
            sz.append(szPath->data);
            CLogDisplay::Instance().AddEntry(sz,D3DCOLOR_ARGB(0xFF,0xFF,0x0,0x0));

            SetDefaultTexture(p_ppiOut);
            return 1;
        }
    }

    // then call D3DX to load the texture
    if (FAILED(D3DXCreateTextureFromFileEx(
        g_piDevice,
        szPath->data,
        D3DX_DEFAULT,
        D3DX_DEFAULT,
        0,
        0,
        D3DFMT_A8R8G8B8,
        D3DPOOL_MANAGED,
        D3DX_DEFAULT,
        D3DX_DEFAULT,
        0,
        NULL,
        NULL,
        p_ppiOut)))
    {
        // error ... use the default texture instead
        std::string sz = "[ERROR] Unable to load texture: ";
        sz.append(szPath->data);
        CLogDisplay::Instance().AddEntry(sz,D3DCOLOR_ARGB(0xFF,0xFF,0x0,0x0));

        this->SetDefaultTexture(p_ppiOut);
    }
    sCachedTextures[s] = *p_ppiOut;
    (*p_ppiOut)->AddRef();

    return 1;
}