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

huabel hu.ds.abel at icloud.com
Fri Feb 10 17:29:48 EST 2017


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 = vtkInteractorStyleTrackballCamera::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






-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20170211/37709270/attachment-0004.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2017-02-11 at 12.01.45 AM.png
Type: image/png
Size: 47559 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20170211/37709270/attachment-0001.png>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20170211/37709270/attachment-0005.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CMakeLists.txt
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20170211/37709270/attachment-0001.txt>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20170211/37709270/attachment-0006.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: demo1.cpp
Type: application/octet-stream
Size: 3407 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20170211/37709270/attachment-0001.obj>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20170211/37709270/attachment-0007.html>


More information about the vtk-developers mailing list