TextureViewerMain.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoTitleBar);
  47. canvas->pushFont(m_font, 16);
  48. ImGui::SetWindowPos(Vec2(0.0f, 0.0f));
  49. ImGui::SetWindowSize(Vec2(F32(canvas->getWidth()), F32(canvas->getHeight())));
  50. ImGui::BeginChild("Tools", ImVec2(ImGui::GetWindowContentRegionWidth() * 0.25f, -1.0f), true, 0);
  51. // Info
  52. ImGui::TextWrapped("Size %ux%u Mips %u", grTex.getWidth(), grTex.getHeight(), grTex.getMipmapCount());
  53. // Zoom
  54. ImGui::DragFloat("Zoom", &m_zoom, 0.01f, 0.1f, 20.0f, "%.3f");
  55. // Sampling
  56. ImGui::Checkbox("Point sampling", &m_pointSampling);
  57. // Colors
  58. ImGui::Checkbox("Red", &m_colorChannel[0]);
  59. ImGui::SameLine();
  60. ImGui::Checkbox("Green", &m_colorChannel[1]);
  61. ImGui::SameLine();
  62. ImGui::Checkbox("Blue", &m_colorChannel[2]);
  63. ImGui::SameLine();
  64. ImGui::Checkbox("Alpha", &m_colorChannel[3]);
  65. // Mips combo
  66. {
  67. Array<CString, 11> mipTexts = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
  68. CString comboLevel = mipTexts[m_crntMip];
  69. const U32 lastCrntMip = m_crntMip;
  70. if(ImGui::BeginCombo("Mipmap", comboLevel.cstr(), ImGuiComboFlags_HeightLarge))
  71. {
  72. for(U32 mip = 0; mip < grTex.getMipmapCount(); ++mip)
  73. {
  74. const Bool isSelected = (m_crntMip == mip);
  75. if(ImGui::Selectable(mipTexts[mip].cstr(), isSelected))
  76. {
  77. m_crntMip = mip;
  78. }
  79. if(isSelected)
  80. {
  81. ImGui::SetItemDefaultFocus();
  82. }
  83. }
  84. ImGui::EndCombo();
  85. }
  86. if(lastCrntMip != m_crntMip)
  87. {
  88. // Re-create the image view
  89. TextureViewInitInfo viewInitInf(m_textureResource->getGrTexture());
  90. viewInitInf.m_firstMipmap = m_crntMip;
  91. viewInitInf.m_mipmapCount = 1;
  92. m_textureView = getSceneGraph().getGrManager().newTextureView(viewInitInf);
  93. }
  94. }
  95. ImGui::EndChild();
  96. ImGui::SameLine();
  97. // Image
  98. ImGui::BeginChild("Image", ImVec2(-1.0f, -1.0f), true, ImGuiWindowFlags_HorizontalScrollbar);
  99. {
  100. class ExtraPushConstants
  101. {
  102. public:
  103. Vec4 m_colorScale;
  104. } pc;
  105. pc.m_colorScale.x() = F32(m_colorChannel[0]);
  106. pc.m_colorScale.y() = F32(m_colorChannel[1]);
  107. pc.m_colorScale.z() = F32(m_colorChannel[2]);
  108. pc.m_colorScale.w() = F32(m_colorChannel[3]);
  109. canvas->setShaderProgram(m_imageGrProgram, &pc, sizeof(pc));
  110. ImGui::Image(UiImageId(m_textureView, m_pointSampling),
  111. ImVec2(F32(grTex.getWidth()) * m_zoom, F32(grTex.getHeight()) * m_zoom), ImVec2(0.0f, 0.0f),
  112. ImVec2(1.0f, 1.0f), Vec4(1.0f), Vec4(0.0f, 0.0f, 0.0f, 1.0f));
  113. canvas->clearShaderProgram();
  114. }
  115. ImGui::EndChild();
  116. canvas->popFont();
  117. ImGui::End();
  118. }
  119. };
  120. class MyApp : public App
  121. {
  122. public:
  123. Error init(int argc, char** argv, CString appName)
  124. {
  125. HeapAllocator<U32> alloc(allocAligned, nullptr);
  126. StringAuto mainDataPath(alloc, ANKI_SOURCE_DIRECTORY);
  127. ConfigSet config = DefaultConfigSet::get();
  128. config.set("window_fullscreen", false);
  129. config.set("rsrc_dataPaths", mainDataPath);
  130. config.set("gr_validation", 0);
  131. ANKI_CHECK(config.setFromCommandLineArguments(argc - 1, argv + 1));
  132. ANKI_CHECK(App::init(config, allocAligned, nullptr));
  133. // Load the texture
  134. if(argc < 2)
  135. {
  136. ANKI_LOGE("Wrong number of arguments");
  137. return Error::USER_DATA;
  138. }
  139. TextureResourcePtr tex;
  140. ANKI_CHECK(getResourceManager().loadResource(argv[1], tex, false));
  141. // Create the node
  142. SceneGraph& scene = getSceneGraph();
  143. TextureViewerUiNode* node;
  144. ANKI_CHECK(scene.newSceneNode("TextureViewer", node));
  145. node->m_textureResource = tex;
  146. return Error::NONE;
  147. }
  148. Error userMainLoop(Bool& quit, Second elapsedTime) override
  149. {
  150. Input& input = getInput();
  151. if(input.getKey(KeyCode::ESCAPE))
  152. {
  153. quit = true;
  154. }
  155. return Error::NONE;
  156. }
  157. };
  158. int main(int argc, char* argv[])
  159. {
  160. Error err = Error::NONE;
  161. MyApp* app = new MyApp;
  162. err = app->init(argc, argv, "Texture Viewer");
  163. if(!err)
  164. {
  165. err = app->mainLoop();
  166. }
  167. delete app;
  168. if(err)
  169. {
  170. ANKI_LOGE("Error reported. Bye!!");
  171. }
  172. else
  173. {
  174. ANKI_LOGI("Bye!!");
  175. }
  176. return 0;
  177. }