Discussion:
[vtkusers] how to render VTK into user provided FBO
Max Chen
2018-12-04 03:35:13 UTC
Permalink
Hi all,

I want VTK render into a FBO I created and use the texture attached to this
FBO to render into a glfw window.

I tried this :
create a class from vtkExternalOpenGLRenderWindow so I can modify the
protected member.

class MyRW : public vtkExternalOpenGLRenderWindow
{
public:
void SetFBO(unsigned int fbo, unsigned int tex, unsigned int w, unsigned
int h);
};

void MyRW::SetFBO(unsigned int fbo, unsigned int tex, unsigned int w,
unsigned int h)
{

this->SetBackLeftBuffer(GL_COLOR_ATTACHMENT0);
this->SetFrontLeftBuffer(GL_COLOR_ATTACHMENT0);
this->SetBackBuffer(GL_COLOR_ATTACHMENT0);
this->SetFrontBuffer(GL_COLOR_ATTACHMENT0);

this->Size[0] = w;
this->Size[1] = h;
this->NumberOfFrameBuffers = 1;
this->DepthRenderBufferObject = 0;
this->FrameBufferObject = static_cast<unsigned int>(fbo);
this->TextureObjects[0] = static_cast<unsigned int>(tex);
this->OffScreenRendering = 1;
this->OffScreenUseFrameBuffer = 1;
this->Modified();
}

I also pass the glfw window pointer to vtkCommand::WindowMakeCurrentEvent.

In the glfw loop :

// render into FBO
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
myvtk->render();
glBindFramebuffer(GL_FRAMEBUFFER, 0);

// use the texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_tex);
unsigned int e = glGetError();
glColor4f(1, 1, 1, 1);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 0);
glTexCoord2f(1.0, 0.0); glVertex3f(1.0, -1.0, 0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);

// draw imgui
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());

but it did not work, window is flickering and imgui just disappear.

I setup a cmake test project in github, please help.
https://github.com/trlsmax/imgui-vtk <https://github.com/trlsmax/imgui-vtk>



--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers
Ken Martin via vtkusers
2018-12-04 12:56:56 UTC
Permalink
Try binding your FBO and context and then use
InitializeFromCurrentContext. That has some logic to record the current
FBO and use it for subsequent operations. That might do what you want.

- Ken
Post by Max Chen
Hi all,
I want VTK render into a FBO I created and use the texture attached to this
FBO to render into a glfw window.
create a class from vtkExternalOpenGLRenderWindow so I can modify the
protected member.
class MyRW : public vtkExternalOpenGLRenderWindow
{
void SetFBO(unsigned int fbo, unsigned int tex, unsigned int w, unsigned
int h);
};
void MyRW::SetFBO(unsigned int fbo, unsigned int tex, unsigned int w,
unsigned int h)
{
this->SetBackLeftBuffer(GL_COLOR_ATTACHMENT0);
this->SetFrontLeftBuffer(GL_COLOR_ATTACHMENT0);
this->SetBackBuffer(GL_COLOR_ATTACHMENT0);
this->SetFrontBuffer(GL_COLOR_ATTACHMENT0);
this->Size[0] = w;
this->Size[1] = h;
this->NumberOfFrameBuffers = 1;
this->DepthRenderBufferObject = 0;
this->FrameBufferObject = static_cast<unsigned int>(fbo);
this->TextureObjects[0] = static_cast<unsigned int>(tex);
this->OffScreenRendering = 1;
this->OffScreenUseFrameBuffer = 1;
this->Modified();
}
I also pass the glfw window pointer to vtkCommand::WindowMakeCurrentEvent.
// render into FBO
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
myvtk->render();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// use the texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_tex);
unsigned int e = glGetError();
glColor4f(1, 1, 1, 1);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 0);
glTexCoord2f(1.0, 0.0); glVertex3f(1.0, -1.0, 0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
// draw imgui
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
but it did not work, window is flickering and imgui just disappear.
I setup a cmake test project in github, please help.
https://github.com/trlsmax/imgui-vtk <https://github.com/trlsmax/imgui-vtk>
--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
_______________________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
http://www.vtk.org/Wiki/VTK_FAQ
Search the list archives at: http://markmail.org/search/?q=vtkusers
https://public.kitware.com/mailman/listinfo/vtkusers
--
Ken Martin PhD
Distinguished Engineer
Kitware Inc.
101 East Weaver Street
Carrboro, North Carolina
27510 USA

This communication, including all attachments, contains confidential and
legally privileged information, and it is intended only for the use of the
addressee. Access to this email by anyone else is unauthorized. If you are
not the intended recipient, any disclosure, copying, distribution or any
action taken in reliance on it is prohibited and may be unlawful. If you
received this communication in error please notify us immediately and
destroy the original message. Thank you.
Max Chen
2018-12-05 04:04:25 UTC
Permalink
Thank you. Now I have a working prototype.
But I get a strangle issue, I can only resize the offscreen
vtkGenericOpenGLRenderWindow 3 times, otherwise I get error like :
ERROR: In E:\projects\VTK-8.1.2\Rendering\OpenGL2\vtkOpenGLActor.cxx, line
107
vtkOpenGLActor (0B2C4C10): failed after Render 1 OpenGL errors detected
0:(1285)out of memory

the resize code is :
void MyVTKRenderer::UpdateSize(unsigned int w, unsigned int h)
{
if (w == m_Width && h == m_Height)
return;

if (w == 0 || h == 0)
return;

m_Width = w;
m_Height = h;

// resize the render window
m_vtkRenderWindow->SetSize(m_Width, m_Height);
m_vtkRenderWindow->FullScreenOn();
m_vtkRenderWindow->OffScreenRenderingOn();
//m_vtkRenderWindow->SetMultiSamples(0);
m_vtkRenderWindow->Modified();
m_vtkRenderWindowInteractor->UpdateSize(m_Width, m_Height);
m_vtkRenderWindowInteractor->Modified();
m_IsInited = false;

// delete old fbo
glDeleteBuffers(1, &m_fbo);
glDeleteBuffers(1, &m_rbo);
glDeleteTextures(1, &m_tex);

// create a texture object
glGenTextures(1, &m_tex);
glBindTexture(GL_TEXTURE_2D, m_tex);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_Width, m_Height, 0, GL_RGB,
GL_UNSIGNED_BYTE, 0);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, 0);
// create a renderbuffer object to store depth info
glGenRenderbuffers(1, &m_rbo);
glBindRenderbuffer(GL_RENDERBUFFER, m_rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, m_Width,
m_Height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);

// create a framebuffer object
glGenFramebuffers(1, &m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
m_tex, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER, m_rbo);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

Another issue is , these code can't work on VTK-8.2 rc1



--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers
Max Chen
2018-12-05 08:26:39 UTC
Permalink
Everything works now. Code is update to github repo.

I have to remove the actor before resize the vtkGenericOpenGLRenderWindow
and add it back.



--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers

Loading...