Updated Image Loading and Displaying Examples (markdown)

omar 2026-04-23 22:30:31 +02:00
parent e4939e6abf
commit b5af846fc6

@ -14,7 +14,8 @@
- [Example for WebGPU users](#example-for-webgpu-users)
- [About Texture Coordinates](#about-texture-coordinates)
- [Modifying Render State](#modifying-render-state)
- Texture Sampling
- Other Render States
## TL;DR;
@ -1316,13 +1317,31 @@ ImGui::Image((ImTextureID)(intptr_t)my_image_texture, ImVec2(my_image_width*0.5f
## Modifying Render State
You can use draw callbacks to manipulate the render state, e.g. alter sampling. Since 1.91.3, backends are now exposing some of their state during the render loop, which should make it easier to leverage callbacks. Refer to the .h file of each backend to see if a `ImGui_ImplXXXX_RenderState` struct is exposed.
You can use draw callbacks to manipulate the render state, e.g. alter sampling.
#### Texture Sampling
Since 1.92.8, backends are now exposing generic callbacks to switch between Linear and Point/Nearest texture sampling. See [#9378](https://github.com/ocornut/imgui/issues/9378) for details.
```cpp
// Add an image using Point/Nearest sampling
draw_list->AddCallback(ImGui::GetPlatformIO().DrawCallback_SetSamplerNearest);
draw_list->AddImage(....);
draw_list->AddCallback(ImGui::GetPlatformIO().DrawCallback_SetSamplerLinear);
```
#### Other Render States
Since 1.91.3, backends are exposing some of their state during the render loop, which should make it easier to leverage callbacks. Refer to the .h file of each backend to see if a `ImGui_ImplXXXX_RenderState` struct is exposed.
<BR>Access them with e.g. `ImGui_ImplDX11_RenderState* state = (ImGui_ImplDX11_RenderState*)ImGui::GetPlatformIO().Renderer_RenderState;`.
Below is an example of how to manually create a DX11 sampler and bind it in a callback. HOWEVER, if you were to want do exactly that you can use the method outlined above. The example here is mostly here to showcase the general mechanism of using callback and RenderState.
```cpp
// For DX11 backend: Callback to modify current sampler
void ImDrawCallback_ImplDX11_SetSampler(const ImDrawList* parent_list, const ImDrawCmd* cmd)
{
// Access render state to obtain the device context
ImGui_ImplDX11_RenderState* state = (ImGui_ImplDX11_RenderState*)ImGui::GetPlatformIO().Renderer_RenderState;
ID3D11SamplerState* sampler = cmd->UserCallbackData ? (ID3D11SamplerState*)cmd->UserCallbackData : state->SamplerLinear;
state->DeviceContext->PSSetSamplers(0, 1, &sampler);
@ -1348,23 +1367,13 @@ void ImDrawCallback_ImplDX11_SetSampler(const ImDrawList* parent_list, const ImD
```cpp
// Usage
ImGui::GetWindowDrawList()->AddCallback(ImDrawCallback_ImplDX11_SetSampler, g_SamplerPoint); // Set custom sampler
draw__list->AddCallback(ImDrawCallback_ImplDX11_SetSampler, g_SamplerPoint); // Set custom sampler
ImGui::Image((ImTextureID)(intptr_t)my_texture, ImVec2((float)my_image_width * 4, (float)my_image_height * 4));
ImGui::GetWindowDrawList()->AddCallback(ImDrawCallback_ImplDX11_SetSampler, NULL); // Restore sampler
draw__list->AddCallback(ImDrawCallback_ImplDX11_SetSampler, NULL); // Restore sampler
```
This may be easier or harder to use for some backends. Don't hesitate to open issues to discuss your needs and so we can improve the feature.
```cpp
// For SDLGPU3 backend: Callback to modify current sampler
void ImDrawCallback_ImplSDLGPU3_SetSampler(const ImDrawList* parent_list, const ImDrawCmd* cmd)
{
ImGui_ImplSDLGPU3_RenderState* state = (ImGui_ImplSDLGPU3_RenderState*)ImGui::GetPlatformIO().Renderer_RenderState;
SDL_GPUSampler* sampler = cmd->UserCallbackData ? (SDL_GPUSampler*)cmd->UserCallbackData : state->SamplerDefault;
state->SamplerCurrent = sampler;
}
```
----
##### [Return to Index](#index)