TextureViewerMain.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. TextureResourcePtr m_textureResource;
  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([](CanvasPtr& canvas, void* ud) { static_cast<TextureViewerUiNode*>(ud)->draw(canvas); }, this);
  18. ANKI_CHECK_AND_IGNORE(getSceneGraph().getUiManager().newInstance(m_font, "EngineAssets/UbuntuMonoRegular.ttf",
  19. Array<U32, 1>{16}));
  20. ANKI_CHECK_AND_IGNORE(getSceneGraph().getResourceManager().loadResource(
  21. "AnKi/Shaders/UiVisualizeImage.ankiprog", m_imageProgram));
  22. const ShaderProgramResourceVariant* variant;
  23. m_imageProgram->getOrCreateVariant(variant);
  24. m_imageGrProgram = variant->getProgram();
  25. }
  26. Error frameUpdate(Second prevUpdateTime, Second crntTime)
  27. {
  28. if(!m_textureView.isCreated())
  29. {
  30. m_textureView = m_textureResource->getGrTextureView();
  31. }
  32. return Error::NONE;
  33. }
  34. private:
  35. FontPtr m_font;
  36. ShaderProgramResourcePtr m_imageProgram;
  37. ShaderProgramPtr m_imageGrProgram;
  38. TextureViewPtr m_textureView;
  39. U32 m_crntMip = 0;
  40. F32 m_zoom = 1.0f;
  41. Bool m_pointSampling = true;
  42. Array<Bool, 4> m_colorChannel = {true, true, true, true};
  43. void draw(CanvasPtr& canvas)
  44. {
  45. const Texture& grTex = *m_textureResource->getGrTexture().get();
  46. ImGui::Begin("Console", nullptr,
  47. ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove);
  48. canvas->pushFont(m_font, 16);
  49. ImGui::SetWindowPos(Vec2(0.0f, 0.0f));
  50. ImGui::SetWindowSize(Vec2(F32(canvas->getWidth()), F32(canvas->getHeight())));
  51. ImGui::BeginChild("Tools", Vec2(300.0f, -1.0f), true, 0);
  52. // Info
  53. ImGui::TextWrapped("Size %ux%u Mips %u", grTex.getWidth(), grTex.getHeight(), grTex.getMipmapCount());
  54. ImGui::NewLine();
  55. // Zoom
  56. ImGui::DragFloat("Zoom", &m_zoom, 0.01f, 0.1f, 20.0f, "%.3f");
  57. // Sampling
  58. ImGui::Checkbox("Point sampling", &m_pointSampling);
  59. // Colors
  60. ImGui::Checkbox("Red", &m_colorChannel[0]);
  61. ImGui::SameLine();
  62. ImGui::Checkbox("Green", &m_colorChannel[1]);
  63. ImGui::SameLine();
  64. ImGui::Checkbox("Blue", &m_colorChannel[2]);
  65. ImGui::SameLine();
  66. ImGui::Checkbox("Alpha", &m_colorChannel[3]);
  67. // Mips combo
  68. {
  69. StringListAuto mipLabels(getFrameAllocator());
  70. for(U32 mip = 0; mip < grTex.getMipmapCount(); ++mip)
  71. {
  72. mipLabels.pushBackSprintf("%u (%llux%llu)", mip, grTex.getWidth() >> mip, grTex.getHeight() >> mip);
  73. }
  74. const U32 lastCrntMip = m_crntMip;
  75. if(ImGui::BeginCombo("Mipmap", (mipLabels.getBegin() + m_crntMip)->cstr(), ImGuiComboFlags_HeightLarge))
  76. {
  77. for(U32 mip = 0; mip < grTex.getMipmapCount(); ++mip)
  78. {
  79. const Bool isSelected = (m_crntMip == mip);
  80. if(ImGui::Selectable((mipLabels.getBegin() + mip)->cstr(), isSelected))
  81. {
  82. m_crntMip = mip;
  83. }
  84. if(isSelected)
  85. {
  86. ImGui::SetItemDefaultFocus();
  87. }
  88. }
  89. ImGui::EndCombo();
  90. }
  91. if(lastCrntMip != m_crntMip)
  92. {
  93. // Re-create the image view
  94. TextureViewInitInfo viewInitInf(m_textureResource->getGrTexture());
  95. viewInitInf.m_firstMipmap = m_crntMip;
  96. viewInitInf.m_mipmapCount = 1;
  97. m_textureView = getSceneGraph().getGrManager().newTextureView(viewInitInf);
  98. }
  99. }
  100. ImGui::EndChild();
  101. ImGui::SameLine();
  102. // Image
  103. ImGui::BeginChild("Image", Vec2(-1.0f, -1.0f), true, ImGuiWindowFlags_HorizontalScrollbar);
  104. {
  105. // Center image
  106. const Vec2 imageSize = Vec2(F32(grTex.getWidth()), F32(grTex.getHeight())) * m_zoom;
  107. // ImGui::SetCursorPos((toAnki(ImGui::GetContentRegionAvail()) - imageSize) * 0.5f);
  108. class ExtraPushConstants
  109. {
  110. public:
  111. Vec4 m_colorScale;
  112. } pc;
  113. pc.m_colorScale.x() = F32(m_colorChannel[0]);
  114. pc.m_colorScale.y() = F32(m_colorChannel[1]);
  115. pc.m_colorScale.z() = F32(m_colorChannel[2]);
  116. pc.m_colorScale.w() = F32(m_colorChannel[3]);
  117. canvas->setShaderProgram(m_imageGrProgram, &pc, sizeof(pc));
  118. ImGui::Image(UiImageId(m_textureView, m_pointSampling), imageSize, Vec2(0.0f), Vec2(1.0f), Vec4(1.0f),
  119. Vec4(0.0f, 0.0f, 0.0f, 1.0f));
  120. canvas->clearShaderProgram();
  121. if(ImGui::IsItemHovered())
  122. {
  123. if(ImGui::GetIO().KeyCtrl)
  124. {
  125. // Zoom
  126. const F32 zoomSpeed = 0.05f;
  127. if(ImGui::GetIO().MouseWheel > 0.0f)
  128. {
  129. m_zoom *= 1.0f + zoomSpeed;
  130. }
  131. else if(ImGui::GetIO().MouseWheel < 0.0f)
  132. {
  133. m_zoom *= 1.0f - zoomSpeed;
  134. }
  135. // Pan
  136. if(ImGui::GetIO().MouseDown[0])
  137. {
  138. const Vec2 velocity = toAnki(ImGui::GetIO().MouseDelta);
  139. if(velocity.x() != 0.0f)
  140. {
  141. ImGui::SetScrollX(ImGui::GetScrollX() - velocity.x());
  142. }
  143. if(velocity.y() != 0.0f)
  144. {
  145. ImGui::SetScrollY(ImGui::GetScrollY() - velocity.y());
  146. }
  147. }
  148. }
  149. }
  150. }
  151. ImGui::EndChild();
  152. canvas->popFont();
  153. ImGui::End();
  154. }
  155. };
  156. class MyApp : public App
  157. {
  158. public:
  159. Error init(int argc, char** argv, CString appName)
  160. {
  161. if(argc < 2)
  162. {
  163. ANKI_LOGE("Wrong number of arguments");
  164. return Error::USER_DATA;
  165. }
  166. HeapAllocator<U32> alloc(allocAligned, nullptr);
  167. StringAuto mainDataPath(alloc, ANKI_SOURCE_DIRECTORY);
  168. ConfigSet config = DefaultConfigSet::get();
  169. config.set("window_fullscreen", false);
  170. config.set("rsrc_dataPaths", mainDataPath);
  171. config.set("gr_validation", 0);
  172. ANKI_CHECK(config.setFromCommandLineArguments(argc - 2, argv + 2));
  173. ANKI_CHECK(App::init(config, allocAligned, nullptr));
  174. // Load the texture
  175. TextureResourcePtr tex;
  176. ANKI_CHECK(getResourceManager().loadResource(argv[1], tex, false));
  177. // Create the node
  178. SceneGraph& scene = getSceneGraph();
  179. TextureViewerUiNode* node;
  180. ANKI_CHECK(scene.newSceneNode("TextureViewer", node));
  181. node->m_textureResource = tex;
  182. return Error::NONE;
  183. }
  184. Error userMainLoop(Bool& quit, Second elapsedTime) override
  185. {
  186. Input& input = getInput();
  187. if(input.getKey(KeyCode::ESCAPE))
  188. {
  189. quit = true;
  190. }
  191. return Error::NONE;
  192. }
  193. };
  194. int main(int argc, char* argv[])
  195. {
  196. Error err = Error::NONE;
  197. MyApp* app = new MyApp;
  198. err = app->init(argc, argv, "Texture Viewer");
  199. if(!err)
  200. {
  201. err = app->mainLoop();
  202. }
  203. delete app;
  204. if(err)
  205. {
  206. ANKI_LOGE("Error reported. Bye!!");
  207. }
  208. else
  209. {
  210. ANKI_LOGI("Bye!!");
  211. }
  212. return 0;
  213. }