trlsmax / imgui-vtk

test on how to integrate vtk into glfw + imgui project
MIT License
103 stars 26 forks source link

using image actor #11

Open pwm1234-sri opened 3 years ago

pwm1234-sri commented 3 years ago

Thanks for this excellent example using vtk with imgui+glfw. Unfortunately, I am a novice at both, and I am having problems adapting it to use a `vtkImageActor. In the snippet below, the reader and actor code (up to the dynamic_cast) came from the vtk example ImageReader2Factory. But when I use the actor returned by my SetupImagePipeline() instead of your SetupDemoPipeline(), I do not get the image shown in the imgui window. (I am able to run the standalone vtk example, so my vtk+glfw environment is working.)

Is there something special that is needed to make your example work with a vtkImageActor? I appreciate any help, and, of course, I will be glad to provide you a PR if I able to get this to work. (By the way, I am doing this on windows with a vcpkg build of imgui and vtk.)

static vtkSmartPointer<vtkActor> SetupImagePipeline()
{
    // Read a file
    std::string inputFilename = "/tmp/foo.bmp";
    vtkNew<vtkImageReader2Factory> readerFactory;
    vtkSmartPointer<vtkImageReader2> imageReader;
    imageReader.TakeReference(
        readerFactory->CreateImageReader2(inputFilename.c_str()));
    imageReader->SetFileName(inputFilename.c_str());
    imageReader->Update();

    // create actor
    static auto actor = vtkSmartPointer<vtkImageActor>::New();
    actor->GetMapper()->SetInputConnection(imageReader->GetOutputPort());
    vtkSmartPointer<vtkActor> actor2(dynamic_cast<vtkActor*>(actor.Get()));
    return actor2;
}

(Note: in the snippet above I have a hack where the initial actor is static. I do this because I am trying to focus on the main problem where the image actor is not showing the image in the imgui window. This is here to avoid the secondary problem to correct my ignorance of how to work with both vtkNew and vtkSmartPointer. I thought a vtkSmartPointer would automatcally convert to a vtkSmartPointer but that does not seem to compile. So the last two lines in the snippet are a hack to make the compiler happy and avoid crashing when the inital actor goes out of scope. I will fix that if/when I am able to get the vtkImageActor to work.)

trlsmax commented 1 year ago

It should work on current master

bumjuncho commented 1 year ago

Hello! I made some different using the imgui-vtk. Also, I have same problems. In my case, I got a nullpointException in SetupDemoPipeline(). [line no 106]

bumjuncho commented 1 year ago

Hello! I made some different using the imgui-vtk. Also, I have same problems. In my case, I got a nullpointException in SetupDemoPipeline(). [line no 106]

I solved it. It occurred vtk files[using the cmake]. (vtk version latest version 9.2)

bumjuncho commented 1 year ago

I'm faced with another problem as below. As you can see, it does not appear on the screen. So, I tried to vtk sample code on the vtk homepage.

==============================

include

define vtkRenderingCore_AUTOINIT 3(vtkRenderingOpenGL2,vtkInteractionStyle, vtkRenderingFreeType)

define vtkRenderingContext2D_AUTOINIT 1(vtkRenderingContextOpenGL2)

include

include

include

include

include

include

include

include

include

include

include

static vtkSmartPointer SetupDemoCylinder() { auto colors = vtkSmartPointer::New();

// Set the background color.
std::array<unsigned char, 4> bkg{ {26, 51, 102, 255} };
colors->SetColor("BkgColor", bkg.data());

// This creates a polygonal cylinder model with eight circumferential facets
// (i.e, in practice an octagonal prism).
auto cylinder = vtkSmartPointer<vtkCylinderSource>::New();
cylinder->SetResolution(8);

// The mapper is responsible for pushing the geometry into the graphics
// library. It may also do color mapping, if scalars or other attributes are
// defined.
auto cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());

// The actor is a grouping mechanism: besides the geometry (mapper), it
// also has a property, transformation matrix, and/or texture map.
// Here we set its color and rotate it around the X and Y axes.

auto cylinderActor = vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
//cylinderActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());
cylinderActor->RotateX(30.0);
cylinderActor->RotateY(-45.0);

return cylinderActor;

}

image

rajkundu commented 1 year ago

What system are you using? I can't seem to replicate your issue on my 16" Intel MacBook Pro (macOS 12.5), as the cylinder works fine for me: imgui-vtk demo with cylinder successfully rendered

GengGode commented 1 month ago

I encountered the same issue while running the demo. The viewer area does not display anything, and the button to adjust the background color does not respond. I am using Windows+vcpkg+vtk9.3. I am a beginner in VTK, is it due to platform differences?

image