From 981c193041084f359b74ff646a29daa0920059ce Mon Sep 17 00:00:00 2001 From: omar Date: Fri, 20 Sep 2019 18:28:33 +0200 Subject: [PATCH] DirectX9 example --- Image-Loading-and-Displaying-Examples.md | 44 +++++++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/Image-Loading-and-Displaying-Examples.md b/Image-Loading-and-Displaying-Examples.md index 5e6efea..52e936d 100644 --- a/Image-Loading-and-Displaying-Examples.md +++ b/Image-Loading-and-Displaying-Examples.md @@ -1,3 +1,5 @@ +Examples: [OpenGL](#Example-for-OpenGL-users) - [DirectX9](#Example-for-DirectX9-users) - [DirectX11](#Example-for-DirectX11-users) + ### TL;DR; Loading an image file into a GPU texture is outside of the scope of Dear ImGui and has more to do with your Graphics API. Because this is such a recurring issue for Dear ImGui users, we are providing a guide here. @@ -34,7 +36,6 @@ We will here use [stb_image.h](https://github.com/nothings/stb/blob/master/stb_i You may then include `` from multiple sources, but only `#define STB_IMAGE_IMPLEMENTATION` in one of them. Then, let's load a file from disk: - ``` int my_image_width = 0; int my_image_height = 0; @@ -63,20 +64,53 @@ glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, my_image_width, my_image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data); ``` -Now that we have an OpenGL texture, assuming our imgui rendering function (imgui_impl_xxx.cpp file) takes GLuint as ImTextureID, we can display it. Add in your main loop: +Now that we have an OpenGL texture and its dimension, we can add in our main loop: ``` ImGui::Begin("Test"); -ImGui::Text("my_image_texture = %p", my_image_texture); -ImGui::Text("my_image_size = %d x %d", my_image_width, my_image_height); +ImGui::Text("pointer = %p", my_image_texture); +ImGui::Text("size = %d x %d", my_image_width, my_image_height); ImGui::Image((void*)(intptr_t)my_image_texture, ImVec2(my_image_width, my_image_height)); ImGui::End(); ``` -![image](https://user-images.githubusercontent.com/8225057/65341917-d3d35100-dbd1-11e9-9f13-6d99a6f1a891.png) +![image](https://user-images.githubusercontent.com/8225057/65342485-26f9d380-dbd3-11e9-967e-b279b1ad8551.png) ### Example for DirectX9 users +Unlike the majority of modern graphics API, DirectX9 include helper functions to load image files from disk. We are going to use them here, instead of using `stb_image.h`. + +Add at the top of one of your source file: +``` +#include +#pragma comment(lib, "D3dx9") +``` + +Then, let's load a file from disk directly into a DirectX9 texture: +``` +// Load texture +PDIRECT3DTEXTURE9 my_image_texture; +HRESULT hr = D3DXCreateTextureFromFileA(g_pd3dDevice, "../../MyImage01.jpg", &my_image_texture); +IM_ASSERT(hr == S_OK); + +// Retrieve description of the texture surface so we can access its size +D3DSURFACE_DESC my_image_desc; +my_image_texture->GetLevelDesc(0, &my_image_desc); +int my_image_width = my_image_desc.Width; +int my_image_height = my_image_desc.Height; +``` + +Now that we have an DirectX9 texture and its dimensions, we can add in our main loop: +``` +ImGui::Begin("Test"); +ImGui::Text("pointer = %p", my_image_texture); +ImGui::Text("size = %d x %d", my_image_width, my_image_height); +ImGui::Image((void*)my_image_texture, ImVec2(my_image_width, my_image_height)); +ImGui::End(); +``` + +![image](https://user-images.githubusercontent.com/8225057/65342425-0467ba80-dbd3-11e9-833f-3feb5084e1da.png) + ### Example for DirectX11 users ### Technical Explanation