Exemplo n.º 1
0
void Texture2D::Load(D3DRenderer* renderer, const wchar_t* filePath)
{
	ID3D11Resource* textureResource;
	ID3D11ShaderResourceView* textureSRV;

	DirectX::CreateDDSTextureFromFile(renderer->device(), NULL, filePath, &textureResource, &textureSRV);

	if (textureResource == NULL || textureSRV == NULL)
	{
		char errStr[256];
		std::wstring errorPathw(filePath);
		std::string errorPath(errorPathw.begin(), errorPathw.end());

		sprintf(errStr, "Failed to load texture %s.", errorPath.c_str());

		NE_CRITICAL(errStr, "Texture2D");
		return;
	}

	D3D11_RESOURCE_DIMENSION dimension;
	textureResource->GetType(&dimension);

	if (dimension != D3D11_RESOURCE_DIMENSION_TEXTURE2D)
	{
		ReleaseCOM(textureSRV);
		ReleaseCOM(textureResource);

		NE_CRITICAL("Invalid texture dimension.", "Texture2D");
	}

	mpTexture = static_cast<ID3D11Texture2D*>(textureResource);
	mpResourceView = textureSRV;

	D3D11_TEXTURE2D_DESC texDesc;

	mpTexture->GetDesc(&texDesc);//This should just be switched out for a get file data function when I get DirectXTex set up 

	mWidth = texDesc.Width;
	mHeight = texDesc.Height;
}
Exemplo n.º 2
0
 // ----------------------------------------------------------------------------------------------  
 Texture* TextureLib::LoadEmbeddedTexture(ID3D11Device* device, const wchar_t* name)
 {
     assert(s_Inst != NULL);
     Imple* pImple = s_Inst->m_pImple;

     typedef std::pair<std::wstring, Texture*> NameTexPair;
     const wchar_t* resType = L"Texture";
     HRESULT hr = E_FAIL;

     Texture* tex = NULL;
     ID3D11Resource* dxresource = NULL;
     ID3D11ShaderResourceView* dxTexView = NULL;
     uint32_t  resSize = 0;
     uint8_t* data = (uint8_t*)ResUtil::LoadResource(resType, name, &resSize);
     hr = CreateWICTextureFromMemory(device,
         NULL,
         data,
         resSize,
         &dxresource,
         &dxTexView);
     free(data);
     if (!Logger::IsFailureLog(hr, L"Error loading %s\n", name))
     {
         D3D11_RESOURCE_DIMENSION resDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
         dxresource->GetType(&resDim);
         assert(resDim == D3D11_RESOURCE_DIMENSION_TEXTURE2D);
         ID3D11Texture2D* dxTex = NULL;
         hr = dxresource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&dxTex);
         dxresource->Release();
         assert(dxTex);
         tex = new Texture(dxTex, dxTexView);
         auto insertResult = pImple->m_textures.insert(NameTexPair(name, tex));
         assert(insertResult.second);
     }
     return tex;
 }
Exemplo n.º 3
0
//carrega uma textura 2d do disco
HRESULT ACD3D::LoadTexture(std::string path, ACTexture** ppOutTexturePtr)
{
	*ppOutTexturePtr = new ACTexture();
	ZeroMemory(*ppOutTexturePtr, sizeof ( ACTexture ));

	ID3D11Resource* pTexture = nullptr;

	//carrega a textura do arquivo
	HRESULT hr = D3DX11CreateTextureFromFileA(ACD3DGlobals::G_pD3dDevice,
									path.c_str(),
									nullptr,
									nullptr,
									&pTexture,
									nullptr);

	if(hr != AC_OK)
    {
		std::string message;
        switch(hr)
        {
			case D3D11_ERROR_FILE_NOT_FOUND:  message = "[ERROR] Erro ao carregar textura. File not found: " + path;  break;
			case D3DERR_INVALIDCALL:          message = "[ERROR] Erro ao carregar textura. Invalid call: "   + path; break;
			case E_INVALIDARG:                message = "[ERROR] Erro ao carregar textura. Invalid argument: " + path; break;
			case E_OUTOFMEMORY:               message = "[ERROR] Erro ao carregar textura. Out of memory: " + path; break;
			default:                          message = "[ERROR] Erro ao carregar textura. Unknown error: " + path;
		}

		MessageBoxA(nullptr, message.c_str(), "Error", MB_OK | MB_ICONERROR);
		Log(const_cast<char*>(message.c_str()));

		return hr;
    }

	//dimension type
	D3D11_RESOURCE_DIMENSION type;
	pTexture->GetType( &type );
	switch( type )
	{
		case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
		{
			D3D11_TEXTURE1D_DESC desc;
			ID3D11Texture1D *pTexture1D = (ID3D11Texture1D*)pTexture;
			pTexture1D->GetDesc( &desc );

			//popula o actexture
			(*ppOutTexturePtr)->Width = desc.Width;
			(*ppOutTexturePtr)->Height = 0;
			(*ppOutTexturePtr)->MipLevels = desc.MipLevels;
			(*ppOutTexturePtr)->TextureType = ACTT_Texture1D;
		}
		break;
		case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
		{
			D3D11_TEXTURE2D_DESC desc;
			ID3D11Texture2D *pTexture2D = (ID3D11Texture2D*)pTexture;
			pTexture2D->GetDesc( &desc );

			//popula o actexture
			(*ppOutTexturePtr)->Width = desc.Width;
			(*ppOutTexturePtr)->Height = desc.Height;
			(*ppOutTexturePtr)->MipLevels = desc.MipLevels;
			(*ppOutTexturePtr)->TextureType = ACTT_Texture2D;
		}
		break;
		case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
		{
			D3D11_TEXTURE3D_DESC desc;
			ID3D11Texture3D *pTexture3D = (ID3D11Texture3D*)pTexture;
			pTexture3D->GetDesc( &desc );

			//popula o actexture
			(*ppOutTexturePtr)->Width = desc.Width;
			(*ppOutTexturePtr)->Height = desc.Height;
			(*ppOutTexturePtr)->Depth = desc.Depth;
			(*ppOutTexturePtr)->MipLevels = desc.MipLevels;
			(*ppOutTexturePtr)->TextureType = ACTT_Texture3D;
		}
		break;
	}

	ID3D11ShaderResourceView* pSRV;
	hr = ACD3DGlobals::G_pD3dDevice->CreateShaderResourceView( pTexture, nullptr, &pSRV );

	if (FAILED ( hr ) )
	{
		MessageBoxA(nullptr, "[ERROR] CreateShaderResourceView. LoadTexture()", "Error", MB_OK | MB_ICONERROR);
	}

	//popula o actexture
	(*ppOutTexturePtr)->pData = pSRV;

	return AC_OK;
};
Exemplo n.º 4
0
void TextureLib::InitInstance(ID3D11Device* device)
{
    s_Inst = new TextureLib();
    Imple* pImple = s_Inst->m_pImple;
    
    // color format ABGR
    pImple->m_defaultTextures[TextureType::DIFFUSE] = CreateCheckerboardTexture2D(device, 128, 128, 0xFF404040, 0xFF808080);
    pImple->m_defaultTextures[TextureType::Cubemap] = CreateCheckerboardTexture2D(device, 128, 128, 0xff000040, 0xff000080, true);

    pImple->m_defaultTextures[TextureType::NORMAL] = CreateSolidTexture2D(device, 8, 8, 0xFFFF8080);
    pImple->m_defaultTextures[TextureType::LIGHT] = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF);
    pImple->m_defaultTextures[TextureType::SPEC] = CreateSolidTexture2D(device, 8, 8, 0xFF000000);    
    pImple->m_defaultTextures[TextureType::BlankMask] = CreateSolidTexture2D(device, 4, 4, 0x00);
    pImple->m_defaultTextures[TextureType::FullMask] = CreateSolidTexture2D(device, 4, 4, 0xFFFFFFFF);
    
    pImple->m_whiteTexture = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF);

    typedef std::pair<std::wstring, Texture*> NameTexPair;


    uint32_t resCount = ResUtil::ResourceCount();
    for(uint32_t i = 0; i < resCount; i++)
    {        
        const wchar_t* resName = ResUtil::GetResourceName(i);
        const std::wstring ext = FileUtils::GetExtensionLower(resName);
        HRESULT hr = E_FAIL;

        Texture* tex = NULL;
        ID3D11Resource* dxresource = NULL;
        ID3D11ShaderResourceView* dxTexView = NULL;
        uint32_t  resSize = 0;

        if(ext == L".dds")
        {            
            uint8_t* data = ResUtil::LoadResource(resName,&resSize);
            if(resSize == 0) continue;
            
            hr = CreateDDSTextureFromMemory( device,
                                             data,
                                             resSize,
                                             &dxresource,
                                             &dxTexView);   
            free(data);
        }
        else if(ext == L".png" || ext == L".bmp" || ext == L".jpeg")
        {
            uint8_t* data = ResUtil::LoadResource(resName,&resSize);
            if(resSize == 0) continue;
            
            hr = CreateWICTextureFromMemory( device,
                                             NULL,
                                             data,
                                             resSize,
                                             &dxresource,
                                             &dxTexView);
            free(data);

        }

        if (Logger::IsFailureLog(hr, L"Error loading %s\n", resName))
        {
            continue;
        }

        D3D11_RESOURCE_DIMENSION resType = D3D11_RESOURCE_DIMENSION_UNKNOWN;
        dxresource->GetType( &resType );
        assert( resType == D3D11_RESOURCE_DIMENSION_TEXTURE2D);
        ID3D11Texture2D* dxTex = NULL;
        hr = dxresource->QueryInterface( __uuidof(ID3D11Texture2D), (void**) &dxTex );
        dxresource->Release();
        assert(dxTex);
        tex = new Texture(dxTex,dxTexView);
        auto insertResult = pImple->m_textures.insert(NameTexPair(resName,tex));
        assert(insertResult.second);            
    }
}
Exemplo n.º 5
0
void TextureLib::InitInstance(ID3D11Device* device)
{
    s_Inst = new TextureLib();
    Imple* pImple = s_Inst->m_pImple;
    
    
    pImple->m_defaultTextures[TextureType::DIFFUSE] = CreateCheckerboardTexture2D(device, 128, 128, 0xFF404040, 0xFF808080, false,true);
    pImple->m_defaultTextures[TextureType::DIFFUSE]->SetTextureType(TextureType::DIFFUSE);

    pImple->m_defaultTextures[TextureType::Cubemap] = CreateCheckerboardTexture2D(device, 128, 128, 0xff000040, 0xff000080, true);
    pImple->m_defaultTextures[TextureType::Cubemap]->SetTextureType(TextureType::Cubemap);

    pImple->m_defaultTextures[TextureType::NORMAL] = CreateSolidTexture2D(device, 8, 8, 0xFFFF8080);
    pImple->m_defaultTextures[TextureType::NORMAL]->SetTextureType(TextureType::NORMAL);

    pImple->m_defaultTextures[TextureType::LIGHT] = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF);
    pImple->m_defaultTextures[TextureType::LIGHT]->SetTextureType(TextureType::LIGHT);

    pImple->m_defaultTextures[TextureType::SPEC] = CreateSolidTexture2D(device, 8, 8, 0xFF000000);    
    pImple->m_defaultTextures[TextureType::SPEC]->SetTextureType(TextureType::SPEC);

    pImple->m_defaultTextures[TextureType::BlankMask] = CreateSolidTexture2D(device, 4, 4, 0x00);
    pImple->m_defaultTextures[TextureType::BlankMask]->SetTextureType(TextureType::BlankMask);

    pImple->m_defaultTextures[TextureType::FullMask] = CreateSolidTexture2D(device, 4, 4, 0xFFFFFFFF);
    pImple->m_defaultTextures[TextureType::FullMask]->SetTextureType(TextureType::FullMask);
    
    pImple->m_whiteTexture = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF);

    typedef std::pair<std::wstring, Texture*> NameTexPair;

    const wchar_t* resName = L"Light.png";
    const wchar_t* resType = L"Texture";
    HRESULT hr = E_FAIL;

    Texture* tex = NULL;
    ID3D11Resource* dxresource = NULL;
    ID3D11ShaderResourceView* dxTexView = NULL;
    uint32_t  resSize = 0;
    uint8_t* data = (uint8_t*)ResUtil::LoadResource(resType, resName,&resSize);
    hr = CreateWICTextureFromMemory( device,
                                             NULL,
                                             data,
                                             resSize,
                                             &dxresource,
                                             &dxTexView);
   free(data);
   if (!Logger::IsFailureLog(hr, L"Error loading %s\n", resName))
   {
       D3D11_RESOURCE_DIMENSION resDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
       dxresource->GetType( &resDim );
       assert( resDim == D3D11_RESOURCE_DIMENSION_TEXTURE2D);
       ID3D11Texture2D* dxTex = NULL;
       hr = dxresource->QueryInterface( __uuidof(ID3D11Texture2D), (void**) &dxTex );
       dxresource->Release();
       assert(dxTex);
       tex = new Texture(dxTex,dxTexView);
       auto insertResult = pImple->m_textures.insert(NameTexPair(resName,tex));
       assert(insertResult.second);
   }
}