[vtk-developers] Use GLFW as VTK window manager ?

Utkarsh Ayachit utkarsh.ayachit at kitware.com
Sat Feb 11 11:39:10 EST 2017


Abel,

You may want to look into using vtkGenericOpenGLRenderWindow. See the newly
added QVTKOpenGLWidget [1] in VTK/master as an example. There's too we're
letting QOpenGLWidget create the OpenGL context and VTK manage the
rendering.

Utkarsh

[1]
https://gitlab.kitware.com/vtk/vtk/blob/master/GUISupport/Qt/QVTKOpenGLWidget.cxx


On Fri, Feb 10, 2017 at 5:29 PM, huabel <hu.ds.abel at icloud.com> wrote:

> Hi,
> I am try to use GLFW as window manager, get this result:
>
>
>
> It create two independent window, how can I let the vtk renderwindow show
> in glfw window?
>
> The code demo1.cpp:
>
> #include "vtkConeSource.h"
> #include "vtkPolyDataMapper.h"
> #include "vtkRenderWindow.h"
> #include "vtkRenderWindowInteractor.h"
> #include "vtkCamera.h"
> #include "vtkActor.h"
> #include "vtkRenderer.h"
> #include "vtkCommand.h"
> #include "vtkBoxWidget.h"
> #include "vtkTransform.h"
> #include "vtkInteractorStyleTrackballCamera.h"
> #include <GLFW/glfw3.h>
>
> #define SCREEN_WIDTH 800
> #define SCREEN_HEIGHT 600
>
> class vtkMyCallback : public vtkCommand
> {
> public:
> static vtkMyCallback *New()
>   { return new vtkMyCallback; }
> void Execute(vtkObject *caller, unsigned long, void*) VTK_OVERRIDE
> {
>     vtkTransform *t = vtkTransform::New();
>     vtkBoxWidget *widget = reinterpret_cast<vtkBoxWidget*>(caller);
>     widget->GetTransform(t);
>     widget->GetProp3D()->SetUserTransform(t);
>     t->Delete();
> }
> };
>
> static void error_cb(int error, const char* description) {
> fprintf(stderr, "Error: %s\n", description);
> }
>
> void drawScene(void) {
> glClear(GL_COLOR_BUFFER_BIT);
> vtkConeSource *cone = vtkConeSource::New();
> cone->SetHeight( 3.0 );
> cone->SetRadius( 1.0 );
> cone->SetResolution( 10 );
>
> vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
> coneMapper->SetInputConnection( cone->GetOutputPort() );
>
> vtkActor *coneActor = vtkActor::New();
> coneActor->SetMapper( coneMapper );
>
> vtkRenderer *ren1= vtkRenderer::New();
> ren1->AddActor( coneActor );
> ren1->SetBackground( 0.1, 0.2, 0.4 );
>
> vtkRenderWindow *renWin = vtkRenderWindow::New();
> renWin->AddRenderer( ren1 );
> renWin->SetSize( 300, 300 );
>
> vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
> iren->SetRenderWindow(renWin);
>
> vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCam
> era::New();
> iren->SetInteractorStyle(style);
>
> vtkBoxWidget *boxWidget = vtkBoxWidget::New();
> boxWidget->SetInteractor(iren);
> boxWidget->SetPlaceFactor(1.25);
>
> boxWidget->SetProp3D(coneActor);
> boxWidget->PlaceWidget();
> vtkMyCallback *callback = vtkMyCallback::New();
> boxWidget->AddObserver(vtkCommand::InteractionEvent, callback);
>
> boxWidget->On();
>
> iren->Initialize();
> iren->Start();
>
> cone->Delete();
> coneMapper->Delete();
> coneActor->Delete();
> callback->Delete();
> boxWidget->Delete();
> ren1->Delete();
> renWin->Delete();
> iren->Delete();
> style->Delete();
> }
>
> void setup(void) {
> glClearColor(0.2, 0.2, 0.2, 0.0);
>
> glShadeModel(GL_FLAT);
> }
>
> void reshape(GLFWwindow* window) {
> glViewport(0.0f, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT);
> glMatrixMode(GL_PROJECTION);
> glLoadIdentity();
> glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1);
> glMatrixMode(GL_MODELVIEW);
> glLoadIdentity();
> }
>
> static void key_cb(GLFWwindow* window, int key, int s, int action, int
> mods) {
> if (action != GLFW_PRESS) return;
>
> switch (key) {
>   case GLFW_KEY_ESCAPE:
>     glfwSetWindowShouldClose(window, 1);
>     break;
> }
> }
> int main()
> {
> GLFWwindow* window;
>
> glfwSetErrorCallback(error_cb);
>
> if (!glfwInit()) exit(EXIT_FAILURE);
>
> window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "demo1", NULL,
> NULL);
> if (!window) {
>   glfwTerminate();
>   exit(EXIT_FAILURE);
> }
>
> glfwMakeContextCurrent(window);
> glfwSwapInterval(1);
>
> glfwSetKeyCallback(window, key_cb);
>
> setup();
>
> while (!glfwWindowShouldClose(window)) {
>   reshape(window);
>   drawScene();
>   glfwSwapBuffers(window);
>   glfwWaitEvents();
> }
>
> glfwTerminate();
> exit(EXIT_SUCCESS);
> return 0;
> }
>
> The CMakeLists.txt
>
> cmake_minimum_required(VERSION 3.4)
>
> project(examples)
>
> include_directories(${CMAKE_CURRENT_BINARY_DIR}
> ${CMAKE_CURRENT_SOURCE_DIR})
>
> find_package(VTK REQUIRED)
> include(${VTK_USE_FILE})
>
> #set(glfw3_DIR "/opt/glfw/lib/cmake/glfw3")
> find_package(glfw3 REQUIRED)
> find_package(OpenGL REQUIRED)
> find_package(GLEW REQUIRED)
>
> include_directories(${OPENGL_INCLUDE_DIR})
> include_directories(${GLEW_INCLUDE_DIR})
> include_directories(${GLFW_INCLUDE_DIR})
>
> if(CMAKE_HOST_SYSTEM MATCHES Linux)
> message("---> ${CMAKE_HOST_SYSTEM}")
> set(libs ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} glfw m)
> endif()
>
> if(CMAKE_HOST_SYSTEM MATCHES Darwin)
> message("---> ${CMAKE_HOST_SYSTEM}")
> set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -framework OpenGL")
> set(libs ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} glfw m)
> endif()
>
> set(lib ${VTK_LIBRARIES} ${ITK_LIBRARIES})
>
> set(EXAMPLES_SRCS
> demo1
> )
>
> foreach(name ${EXAMPLES_SRCS})
> add_executable(${name} ${name}.cpp)
> target_link_libraries(${name} ${VTK_LIBRARIES} ${libs})
> #target_link_libraries(${name} ${VTK_LIBRARIES})
> endforeach()
>
>
>
> Thanks!
> Abel
>
>
>
>
>
>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/
> opensource/opensource.html
>
> Search the list archives at: http://markmail.org/search/?q=vtk-developers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtk-developers
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20170211/40047e4b/attachment.html>


More information about the vtk-developers mailing list