ImageViewerMain.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/AnKi.h>
  6. using namespace anki;
  7. class TextureViewerUiNode : public SceneNode
  8. {
  9. public:
  10. ImageResourcePtr m_imageResource;
  11. TextureViewerUiNode(SceneGraph* scene, CString name)
  12. : SceneNode(scene, name)
  13. {
  14. SpatialComponent* spatialc = newComponent<SpatialComponent>();
  15. spatialc->setAlwaysVisible(true);
  16. UiComponent* uic = newComponent<UiComponent>();
  17. uic->init(
  18. [](CanvasPtr& canvas, void* ud) {
  19. static_cast<TextureViewerUiNode*>(ud)->draw(canvas);
  20. },
  21. this);
  22. ANKI_CHECK_AND_IGNORE(getSceneGraph().getUiManager().newInstance(m_font, "EngineAssets/UbuntuMonoRegular.ttf",
  23. Array<U32, 1>{16}));
  24. ANKI_CHECK_AND_IGNORE(getSceneGraph().getResourceManager().loadResource(
  25. "AnKi/Shaders/UiVisualizeImage.ankiprog", m_imageProgram));
  26. }
  27. Error frameUpdate(Second prevUpdateTime, Second crntTime)
  28. {
  29. if(!m_textureView.isCreated())
  30. {
  31. m_textureView = m_imageResource->getTextureView();
  32. }
  33. return Error::NONE;
  34. }
  35. private:
  36. FontPtr m_font;
  37. ShaderProgramResourcePtr m_imageProgram;
  38. ShaderProgramPtr m_imageGrProgram;
  39. TextureViewPtr m_textureView;
  40. U32 m_crntMip = 0;
  41. F32 m_zoom = 1.0f;
  42. F32 m_depth = 0.0f;
  43. Bool m_pointSampling = true;
  44. Array<Bool, 4> m_colorChannel = {true, true, true, true};
  45. void draw(CanvasPtr& canvas)
  46. {
  47. const Texture& grTex = *m_imageResource->getTexture().get();
  48. const U32 colorComponentCount = getFormatInfo(grTex.getFormat()).m_componentCount;
  49. ANKI_ASSERT(grTex.getTextureType() == TextureType::_2D || grTex.getTextureType() == TextureType::_3D);
  50. if(!m_imageGrProgram.isCreated())
  51. {
  52. ShaderProgramResourceVariantInitInfo variantInit(m_imageProgram);
  53. variantInit.addMutation("TEXTURE_TYPE", (grTex.getTextureType() == TextureType::_2D) ? 0 : 1);
  54. const ShaderProgramResourceVariant* variant;
  55. m_imageProgram->getOrCreateVariant(variantInit, variant);
  56. m_imageGrProgram = variant->getProgram();
  57. }
  58. ImGui::Begin("Console", nullptr,
  59. ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove);
  60. canvas->pushFont(m_font, 16);
  61. ImGui::SetWindowPos(Vec2(0.0f, 0.0f));
  62. ImGui::SetWindowSize(Vec2(F32(canvas->getWidth()), F32(canvas->getHeight())));
  63. ImGui::BeginChild("Tools", Vec2(-1.0f, 30.0f), false, ImGuiWindowFlags_AlwaysAutoResize);
  64. // Zoom
  65. if(ImGui::Button("-"))
  66. {
  67. m_zoom -= 0.1f;
  68. }
  69. ImGui::SameLine();
  70. ImGui::DragFloat("", &m_zoom, 0.01f, 0.1f, 20.0f, "Zoom %.3f");
  71. ImGui::SameLine();
  72. if(ImGui::Button("+"))
  73. {
  74. m_zoom += 0.1f;
  75. }
  76. ImGui::SameLine();
  77. ImGui::Spacing();
  78. ImGui::SameLine();
  79. // Sampling
  80. ImGui::Checkbox("Point sampling", &m_pointSampling);
  81. ImGui::SameLine();
  82. ImGui::Spacing();
  83. ImGui::SameLine();
  84. // Colors
  85. ImGui::Checkbox("Red", &m_colorChannel[0]);
  86. ImGui::SameLine();
  87. ImGui::Checkbox("Green", &m_colorChannel[1]);
  88. ImGui::SameLine();
  89. ImGui::Checkbox("Blue", &m_colorChannel[2]);
  90. ImGui::SameLine();
  91. if(colorComponentCount == 4)
  92. {
  93. ImGui::Checkbox("Alpha", &m_colorChannel[3]);
  94. ImGui::SameLine();
  95. }
  96. ImGui::Spacing();
  97. ImGui::SameLine();
  98. // Mips combo
  99. {
  100. StringListAuto mipLabels(getFrameAllocator());
  101. for(U32 mip = 0; mip < grTex.getMipmapCount(); ++mip)
  102. {
  103. mipLabels.pushBackSprintf("Mip %u (%llu x %llu)", mip, grTex.getWidth() >> mip,
  104. grTex.getHeight() >> mip);
  105. }
  106. const U32 lastCrntMip = m_crntMip;
  107. if(ImGui::BeginCombo("##Mipmap", (mipLabels.getBegin() + m_crntMip)->cstr(), ImGuiComboFlags_HeightLarge))
  108. {
  109. for(U32 mip = 0; mip < grTex.getMipmapCount(); ++mip)
  110. {
  111. const Bool isSelected = (m_crntMip == mip);
  112. if(ImGui::Selectable((mipLabels.getBegin() + mip)->cstr(), isSelected))
  113. {
  114. m_crntMip = mip;
  115. }
  116. if(isSelected)
  117. {
  118. ImGui::SetItemDefaultFocus();
  119. }
  120. }
  121. ImGui::EndCombo();
  122. }
  123. if(lastCrntMip != m_crntMip)
  124. {
  125. // Re-create the image view
  126. TextureViewInitInfo viewInitInf(m_imageResource->getTexture());
  127. viewInitInf.m_firstMipmap = m_crntMip;
  128. viewInitInf.m_mipmapCount = 1;
  129. m_textureView = getSceneGraph().getGrManager().newTextureView(viewInitInf);
  130. }
  131. ImGui::SameLine();
  132. }
  133. // Depth
  134. if(grTex.getTextureType() == TextureType::_3D)
  135. {
  136. StringListAuto labels(getFrameAllocator());
  137. for(U32 d = 0; d < grTex.getDepth(); ++d)
  138. {
  139. labels.pushBackSprintf("Depth %u", d);
  140. }
  141. if(ImGui::BeginCombo("##Depth", (labels.getBegin() + U32(m_depth))->cstr(), ImGuiComboFlags_HeightLarge))
  142. {
  143. for(U32 d = 0; d < grTex.getDepth(); ++d)
  144. {
  145. const Bool isSelected = (m_depth == F32(d));
  146. if(ImGui::Selectable((labels.getBegin() + d)->cstr(), isSelected))
  147. {
  148. m_depth = F32(d);
  149. }
  150. if(isSelected)
  151. {
  152. ImGui::SetItemDefaultFocus();
  153. }
  154. }
  155. ImGui::EndCombo();
  156. }
  157. ImGui::SameLine();
  158. }
  159. ImGui::EndChild();
  160. ImGui::BeginChild("Image", Vec2(-1.0f, -1.0f), false,
  161. ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_HorizontalScrollbar);
  162. // Image
  163. {
  164. // Center image
  165. const Vec2 imageSize = Vec2(F32(grTex.getWidth()), F32(grTex.getHeight())) * m_zoom;
  166. class ExtraPushConstants
  167. {
  168. public:
  169. Vec4 m_colorScale;
  170. Vec4 m_depth;
  171. } pc;
  172. pc.m_colorScale.x() = F32(m_colorChannel[0]);
  173. pc.m_colorScale.y() = F32(m_colorChannel[1]);
  174. pc.m_colorScale.z() = F32(m_colorChannel[2]);
  175. pc.m_colorScale.w() = F32(m_colorChannel[3]);
  176. pc.m_depth = Vec4((m_depth + 0.5f) / F32(grTex.getDepth()));
  177. canvas->setShaderProgram(m_imageGrProgram, &pc, sizeof(pc));
  178. ImGui::Image(UiImageId(m_textureView, m_pointSampling), imageSize, Vec2(0.0f, 1.0f), Vec2(1.0f, 0.0f),
  179. Vec4(1.0f), Vec4(0.0f, 0.0f, 0.0f, 1.0f));
  180. canvas->clearShaderProgram();
  181. if(ImGui::IsItemHovered())
  182. {
  183. if(ImGui::GetIO().KeyCtrl)
  184. {
  185. // Zoom
  186. const F32 zoomSpeed = 0.05f;
  187. if(ImGui::GetIO().MouseWheel > 0.0f)
  188. {
  189. m_zoom *= 1.0f + zoomSpeed;
  190. }
  191. else if(ImGui::GetIO().MouseWheel < 0.0f)
  192. {
  193. m_zoom *= 1.0f - zoomSpeed;
  194. }
  195. // Pan
  196. if(ImGui::GetIO().MouseDown[0])
  197. {
  198. const Vec2 velocity = toAnki(ImGui::GetIO().MouseDelta);
  199. if(velocity.x() != 0.0f)
  200. {
  201. ImGui::SetScrollX(ImGui::GetScrollX() - velocity.x());
  202. }
  203. if(velocity.y() != 0.0f)
  204. {
  205. ImGui::SetScrollY(ImGui::GetScrollY() - velocity.y());
  206. }
  207. }
  208. }
  209. }
  210. }
  211. ImGui::EndChild();
  212. canvas->popFont();
  213. ImGui::End();
  214. }
  215. };
  216. class MyApp : public App
  217. {
  218. public:
  219. Error init(int argc, char** argv, CString appName)
  220. {
  221. if(argc < 2)
  222. {
  223. ANKI_LOGE("Wrong number of arguments");
  224. return Error::USER_DATA;
  225. }
  226. HeapAllocator<U32> alloc(allocAligned, nullptr);
  227. StringAuto mainDataPath(alloc, ANKI_SOURCE_DIRECTORY);
  228. ConfigSet config = DefaultConfigSet::get();
  229. config.set("window_fullscreen", false);
  230. config.set("rsrc_dataPaths", mainDataPath);
  231. config.set("gr_validation", 0);
  232. ANKI_CHECK(config.setFromCommandLineArguments(argc - 2, argv + 2));
  233. ANKI_CHECK(App::init(config, allocAligned, nullptr));
  234. // Load the texture
  235. ImageResourcePtr image;
  236. ANKI_CHECK(getResourceManager().loadResource(argv[1], image, false));
  237. // Change window name
  238. StringAuto title(alloc);
  239. title.sprintf("%s %llu x %llu Mips %u Format %s", argv[1], image->getWidth(), image->getHeight(),
  240. image->getTexture()->getMipmapCount(), getFormatInfo(image->getTexture()->getFormat()).m_name);
  241. getWindow().setWindowTitle(title);
  242. // Create the node
  243. SceneGraph& scene = getSceneGraph();
  244. TextureViewerUiNode* node;
  245. ANKI_CHECK(scene.newSceneNode("TextureViewer", node));
  246. node->m_imageResource = image;
  247. return Error::NONE;
  248. }
  249. Error userMainLoop(Bool& quit, Second elapsedTime) override
  250. {
  251. Input& input = getInput();
  252. if(input.getKey(KeyCode::ESCAPE))
  253. {
  254. quit = true;
  255. }
  256. return Error::NONE;
  257. }
  258. };
  259. int main(int argc, char* argv[])
  260. {
  261. Error err = Error::NONE;
  262. MyApp* app = new MyApp;
  263. err = app->init(argc, argv, "Texture Viewer");
  264. if(!err)
  265. {
  266. err = app->mainLoop();
  267. }
  268. delete app;
  269. if(err)
  270. {
  271. ANKI_LOGE("Error reported. Bye!!");
  272. }
  273. else
  274. {
  275. ANKI_LOGI("Bye!!");
  276. }
  277. return 0;
  278. }