TextureViewerMain.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. }
  21. Error frameUpdate(Second prevUpdateTime, Second crntTime)
  22. {
  23. if(!m_textureView.isCreated())
  24. {
  25. m_textureView = m_textureResource->getGrTextureView();
  26. }
  27. return Error::NONE;
  28. }
  29. private:
  30. FontPtr m_font;
  31. TextureViewPtr m_textureView;
  32. U32 m_crntMip = 0;
  33. F32 m_zoom = 1.0f;
  34. void draw(CanvasPtr& canvas)
  35. {
  36. const Texture& grTex = *m_textureResource->getGrTexture().get();
  37. ImGui::Begin("Console", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoTitleBar);
  38. canvas->pushFont(m_font, 16);
  39. ImGui::SetWindowPos(Vec2(0.0f, 0.0f));
  40. ImGui::SetWindowSize(Vec2(F32(canvas->getWidth()), F32(canvas->getHeight())));
  41. ImGui::BeginChild("Tools", ImVec2(ImGui::GetWindowContentRegionWidth() * 0.25f, -1.0f), false, 0);
  42. // Info
  43. ImGui::TextWrapped("Size %ux%u Mips %u", grTex.getWidth(), grTex.getHeight(), grTex.getMipmapCount());
  44. // Zoom
  45. ImGui::DragFloat("Zoom", &m_zoom, 0.01f, 0.1f, 10.0f, "%.3f");
  46. // Mips combo
  47. {
  48. Array<CString, 8> mipTexts = {"0", "1", "2", "3", "4", "5", "6", "7"};
  49. CString comboLevel = mipTexts[m_crntMip];
  50. const U32 lastCrntMip = m_crntMip;
  51. if(ImGui::BeginCombo("Mipmap", comboLevel.cstr(), 0))
  52. {
  53. for(U32 mip = 0; mip < grTex.getMipmapCount(); ++mip)
  54. {
  55. const Bool isSelected = (m_crntMip == mip);
  56. if(ImGui::Selectable(mipTexts[mip].cstr(), isSelected))
  57. {
  58. m_crntMip = mip;
  59. }
  60. if(isSelected)
  61. {
  62. ImGui::SetItemDefaultFocus();
  63. }
  64. }
  65. ImGui::EndCombo();
  66. }
  67. if(lastCrntMip != m_crntMip)
  68. {
  69. // Re-create the image view
  70. TextureViewInitInfo viewInitInf(m_textureResource->getGrTexture());
  71. viewInitInf.m_firstMipmap = m_crntMip;
  72. m_textureView = getSceneGraph().getGrManager().newTextureView(viewInitInf);
  73. }
  74. }
  75. ImGui::EndChild();
  76. ImGui::SameLine();
  77. ImGui::BeginChild("Image", ImVec2(ImGui::GetWindowContentRegionWidth() * 0.75f, -1.0f), false,
  78. ImGuiWindowFlags_HorizontalScrollbar);
  79. ImGui::Image(const_cast<TextureView*>(m_textureView.get()),
  80. ImVec2(F32(grTex.getWidth()) * m_zoom, F32(grTex.getHeight()) * m_zoom), ImVec2(0.0f, 0.0f),
  81. ImVec2(1.0f, 1.0f), Vec4(1.0f), Vec4(0.0f, 0.0f, 0.0f, 1.0f));
  82. ImGui::EndChild();
  83. canvas->popFont();
  84. ImGui::End();
  85. }
  86. };
  87. class MyApp : public App
  88. {
  89. public:
  90. Error init(int argc, char** argv, CString appName)
  91. {
  92. HeapAllocator<U32> alloc(allocAligned, nullptr);
  93. StringAuto mainDataPath(alloc, ANKI_SOURCE_DIRECTORY);
  94. ConfigSet config = DefaultConfigSet::get();
  95. config.set("window_fullscreen", false);
  96. config.set("rsrc_dataPaths", mainDataPath);
  97. config.set("gr_validation", 0);
  98. ANKI_CHECK(config.setFromCommandLineArguments(argc - 1, argv + 1));
  99. ANKI_CHECK(App::init(config, allocAligned, nullptr));
  100. // Load the texture
  101. if(argc < 2)
  102. {
  103. ANKI_LOGE("Wrong number of arguments");
  104. return Error::USER_DATA;
  105. }
  106. TextureResourcePtr tex;
  107. ANKI_CHECK(getResourceManager().loadResource(argv[1], tex, false));
  108. // Create the node
  109. SceneGraph& scene = getSceneGraph();
  110. TextureViewerUiNode* node;
  111. ANKI_CHECK(scene.newSceneNode("TextureViewer", node));
  112. node->m_textureResource = tex;
  113. return Error::NONE;
  114. }
  115. Error userMainLoop(Bool& quit, Second elapsedTime) override
  116. {
  117. Input& input = getInput();
  118. if(input.getKey(KeyCode::ESCAPE))
  119. {
  120. quit = true;
  121. }
  122. return Error::NONE;
  123. }
  124. };
  125. int main(int argc, char* argv[])
  126. {
  127. Error err = Error::NONE;
  128. MyApp* app = new MyApp;
  129. err = app->init(argc, argv, "Texture Viewer");
  130. if(!err)
  131. {
  132. err = app->mainLoop();
  133. }
  134. delete app;
  135. if(err)
  136. {
  137. ANKI_LOGE("Error reported. Bye!!");
  138. }
  139. else
  140. {
  141. ANKI_LOGI("Bye!!");
  142. }
  143. return 0;
  144. }