Imgui: Load Image

Created on 20 May 2016  路  10Comments  路  Source: ocornut/imgui

Hello, Omar.

A few days trying to upload the image to the form. Read previous questions on this topic, but was not able to implement it independently. If you don't mind, could you write a small example of loading third-party images(or several images) using them in ImageButton() (DirectX9).

sincerely, Louise.

Most helpful comment

Nice! Probably had a mistake somewhere in the old one. Hope some of the information above can be useful to others in the future.

All 10 comments

Hello,
Please specify which backend example code you are using and show us your code.

The extreme version:

ImTextureID        myImage    = NULL;
LPDIRECT3DTEXTURE9 newTexture = NULL;
void cSprite(IDirect3DDevice9* m_pD3Ddev, LPCSTR szFilePath)
{
    ZeroMemory(&pInfo, sizeof(D3DXIMAGE_INFO));

    if (FAILED(D3DXGetImageInfoFromFile(szFilePath, &pInfo)))
        MessageBox(NULL, "Cannot get image info", "Error", MB_ICONERROR);

    if (FAILED(D3DXCreateTextureFromFileEx(m_pD3Ddev, szFilePath, pInfo.Width, pInfo.Height,
        pInfo.MipLevels, NULL, pInfo.Format, D3DPOOL_MANAGED, D3DX_DEFAULT, NULL, NULL,
        NULL, NULL, &newTexture)))
        MessageBox(NULL, "Cannot create image texture", "Error", MB_ICONERROR);

}

Initialization:

if (!myImage)
        {
            cSprite(g_pd3dDevice, "X://Image.png");
            myImage = newTexture;
        }

Use:
ImGui::ImageButton(myImage, ImVec2( 200.f,200.f ));

The result:
img

Hi,

Can you update the formatting of the code? like this

``` C++

  • ```
    ``````

Also where is pTexture coming from?

updated the message.
Made a mistake, sorry. pTexture = newTexture

Can't see anything obvious wrong (except the code is a bit strange be having newTexture outside the function but that is another issue. Does the texture work correct if you render it directly with D3D (not using imgui)

Using DX9 in the bypass ImGUI everything works.
img1

Use cSprite class:
http://pastebin.com/3TgfDmjd (cSprite .cpp)
http://pastebin.com/X29Mbe2Q (cSprite .h)

 cSprite *Image; //provided globally

//Init
Image = new cSprite(m_pDevice, "C://Image.png");

Rendering sprite:

 if (this->m_pDevice->BeginScene() >= 0)
      {
          this->m_pStateBlock->Capture();
          this->m_pDevice->Clear(0, NULL, D3DCLEAR_TARGET /*| D3DCLEAR_ZBUFFER*/, 0, 1.0f, 0);

          if (FAILED(Image->Begin())) GetLastError();
          else
          {
              Image->Draw(450, 150, 150);
              if (FAILED(Image->End())) GetLastError();
          }

          ImGui::Render();

          this->m_pStateBlock->Apply();
          this->m_pDevice->EndScene();   
      }

Rendering ImageButton:

ImGui::ImageButton(Image->GetTexture(), ImVec2(200.f, 200.f));

Alright. That do look correct. I'm wondering if there is some state that could cause this (i.e. alpha blending with alpha in the texture) I would try to use something like RenderDoc to help figure out the issue but I'm not sure if it support DX9 or not (Might be DX11+ only)

You can use ShowMetricsWindow() to debug the draw call and see what is happening. That code is all pretty trivial and just passing down a void* to the renderer which you have visibility on.

You haven't answered my question which backend example code you are using so while I assume you are using imgui_impl_dx9.cpp are you using the latest and an unmodified version of it?

The thing is that I have no idea what all this D3DX code is doing, what's behind the LPD3DXSPRITE system, what underlying texture format the code is creating? Perhaps you can enforce that with a parameter and see if it has an effect.

The code is imgui_impl_dx9.cpp is creating a texture in ImGui_ImplDX9_CreateFontsTexture and that may be used as reference to figure out where your issue lies. The rendering code sets up a few blending state a most-typical alpha blending case. See the Setup render state: fixed-pipeline, alpha-blending, no face culling, no depth testing block in imgui_impl_dx9.cpp:

    // Setup render state: fixed-pipeline, alpha-blending, no face culling, no depth testing
    g_pd3dDevice->SetPixelShader(NULL);
    g_pd3dDevice->SetVertexShader(NULL);
    g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, false);
    g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
    g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
    g_pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, false);
    g_pd3dDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
    g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
    g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, true);
    g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
    g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
    g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
    g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
    g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
    g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
    g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
    g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);

It may be that your texture are in a different format where this blending state I use won't work. In this case you way want to consider changing ImTextureId to not be a LPDIRECT3DTEXTURE9 but rather a pointer to a structure that would contain the texture and some blending state information, and apply that in the renderer.

I don't even know much about DX9 myself so tell what sort of other pitfall to cause your image from not rendering.

I do not know what happened, but rebuild a new project it started working)))

44

ImGui::ImageButton(Image->GetTexture(), ImVec2(200.f, 200.f));

Nice! Probably had a mistake somewhere in the old one. Hope some of the information above can be useful to others in the future.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

namuda picture namuda  路  3Comments

ghost picture ghost  路  3Comments

KaungZawHtet picture KaungZawHtet  路  3Comments

bizehao picture bizehao  路  3Comments

inflex picture inflex  路  3Comments