Skip to content

Instantly share code, notes, and snippets.

@fereria
Created October 27, 2020 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fereria/464c545b02d872b47fdec60ab16d2365 to your computer and use it in GitHub Desktop.
Save fereria/464c545b02d872b47fdec60ab16d2365 to your computer and use it in GitHub Desktop.
// dear imgui: standalone example application for GLFW + OpenGL2, using legacy fixed pipeline
// If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp.
// (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)
// **DO NOT USE THIS CODE IF YOUR CODE/ENGINE IS USING MODERN OPENGL (SHADERS, VBO, VAO, etc.)**
// **Prefer using the code in the example_glfw_opengl2/ folder**
// See imgui_impl_glfw.cpp for details.
#define NOMINMAX
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl2.h"
#include <stdio.h>
#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
#endif
#include <GLFW/glfw3.h>
#include "pxr/usd/usd/stage.h"
#include "pxr/usd/usdGeom/sphere.h"
#include "pxr/usd/sdf/path.h"
#include "pxr/usdImaging/usdImagingGL/engine.h"
#include "pxr/usdImaging/usdImagingGL/renderParams.h"
#include "pxr/base/gf/camera.h"
#include "pxr/base/gf/vec4d.h"
#include "pxr/base/gf/vec3d.h"
#include "pxr/base/gf/matrix4d.h"
#include "pxr/base/gf/rotation.h"
#include "pxr/imaging/glf/simpleLight.h"
#include "pxr/imaging/glf/simpleMaterial.h"
#include <iostream>
using namespace std;
using namespace pxr;
// [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to maximize ease of testing and compatibility with old VS compilers.
// To link with VS2010-era libraries, VS2015+ requires linking with legacy_stdio_definitions.lib, which we do using this pragma.
// Your own project should not be affected, as you are likely to link with a newer binary of GLFW that is adequate for your version of Visual Studio.
#if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS)
#pragma comment(lib, "legacy_stdio_definitions")
#endif
#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR) / sizeof(*(_ARR)))) // Size of a static C-style array. Don't use on pointers!
static void glfw_error_callback(int error, const char *description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
void traverse(UsdPrim &prim)
{
const char *name = prim.GetName().GetText();
if (ImGui::TreeNode(name))
{
for (auto &child : prim.GetChildren())
{
traverse(child);
}
ImGui::TreePop();
}
}
int main(int, char **)
{
// Setup window
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return 1;
GLFWwindow *window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL2 example", NULL, NULL);
if (window == NULL)
return 1;
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void)io;
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsClassic();
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL2_Init();
// Our state
bool show_demo_window = true;
bool show_another_window = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
// load Stage
//auto stage = UsdStage::CreateInMemory();
auto stage = UsdStage::Open("D:/Kitchen_set/Kitchen_set.usd");
//auto stage = UsdStage::Open("D:/SampleUSD.usd");
//auto sphere = UsdGeomSphere::Define(stage, SdfPath("/TestSphere"));
auto renderer = new UsdImagingGLEngine();
UsdImagingGLRenderParams renderParams;// = UsdImagingGLRenderParams();
renderParams.drawMode = UsdImagingGLDrawMode::DRAW_WIREFRAME_ON_SURFACE;
renderParams.enableLighting = true;
renderParams.enableIdRender = false;
renderParams.frame = 0;
renderParams.complexity = 1;
renderParams.cullStyle = UsdImagingGLCullStyle::CULL_STYLE_BACK_UNLESS_DOUBLE_SIDED;
renderParams.enableSceneMaterials = false;
renderParams.highlight = true;
float rotTheta_;
float rotPhi_;
float rotPsi_;
GfVec3d center_;
float dist_;
float offsetUpDown_;
float offsetLR_;
dist_ = 30.0;
rotTheta_ = 0;
rotPhi_ = 0;
rotPsi_ = 90;
offsetUpDown_ = 0;
offsetLR_ = 0;
// Main loop
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
// Start the Dear ImGui frame
ImGui_ImplOpenGL2_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
{
ImGui::SetNextWindowSize(ImVec2(520, 600), ImGuiCond_FirstUseEver);
ImGui::Begin("New Window");
if (ImGui::CollapsingHeader("Camera"))
{
// たためるMenu
static char str0[128] = "Hello, world!";
ImGui::InputText("Input Value", str0, IM_ARRAYSIZE(str0));
ImGui::TextWrapped(str0);
ImGui::SliderFloat("Distance", &dist_, 0.0, 10000.0, "%.6f");
ImGui::SliderFloat("Up/Down", &offsetUpDown_, -1000, 1000.0, "%.6f");
ImGui::SliderFloat("Left/Right", &offsetLR_, -1000, 1000.0, "%.6f");
ImGui::SliderFloat("X", &rotTheta_, 0.0, 360.0, "%.6f");
ImGui::SliderFloat("Y", &rotPhi_, 0.0, 360.0, "%.6f");
ImGui::SliderFloat("Z", &rotPsi_, 0.0, 360.0, "%.6f");
}
if (ImGui::TreeNode("Trees"))
{
ImGui::Indent();
ImGui::SmallButton("hoge");
ImGui::Indent();
ImGui::SmallButton("hoge2");
ImGui::TreePop();
}
ImGui::End();
}
traverse(stage->GetPrimAtPath(SdfPath("/")));
// Rendering
ImGui::Render();
GfCamera cam;
cam.SetFocusDistance((float)dist_);
GfMatrix4d viewMatrix;
viewMatrix.SetIdentity();
viewMatrix *= GfMatrix4d().SetTranslate(GfVec3d::ZAxis() * dist_);
viewMatrix *= GfMatrix4d().SetTranslate(GfVec3d::YAxis() * offsetUpDown_);
viewMatrix *= GfMatrix4d().SetTranslate(GfVec3d::XAxis() * offsetLR_);
viewMatrix *= GfMatrix4d().SetRotate(GfRotation(GfVec3d(1.0, 0.0, 0.0), rotPsi_));
viewMatrix *= GfMatrix4d().SetRotate(GfRotation(GfVec3d(0.0, 1.0, 0.0), rotPhi_));
viewMatrix *= GfMatrix4d().SetRotate(GfRotation(GfVec3d(0.0, 0.0, 1.0), rotTheta_));
cam.SetTransform(viewMatrix);
cam.SetFocusDistance((float)dist_);
auto frustum = cam.GetFrustum();
GfVec3d cam_pos = frustum.GetPosition();
GlfSimpleLightVector lights;
GlfSimpleMaterial material;
GfVec4f ambient(GfVec4f(.1f, .1f, .1f, 1.f));
GlfSimpleLight l;
l.SetAmbient(GfVec4f(0, 0, 0, 0));
l.SetPosition(GfVec4f(cam_pos[0], cam_pos[1], cam_pos[2], 1.f));
lights.push_back(l);
material.SetAmbient(GfVec4f(.1f, .1f, .1f, 1.f));
material.SetSpecular(GfVec4f(.6f, .6f, .6f, .6f));
material.SetShininess(16.0);
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
renderer->SetRenderViewport(GfVec4d(0, 0, display_w, display_h));
renderer->SetCameraState(frustum.ComputeViewMatrix(),
frustum.ComputeProjectionMatrix());
renderer->SetLightingState(lights, material, ambient);
UsdPrim root = stage->GetPseudoRoot();
renderer->Render(root, renderParams);
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
glfwMakeContextCurrent(window);
glfwSwapBuffers(window);
}
// Cleanup
ImGui_ImplOpenGL2_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment