PDA

View Full Version : render to texture under Qt



qlizhi
09-18-2012, 12:29 AM
I rendered some graphics in QGLWidget, it works fine. And i want to store a frame to an QImage, so i add a button and the button code is below:


void VolRenCore::RendrToFBO()
{
GLuint fb;
glGenFramebuffersEXT(1, &fb);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);

glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_ViewPort[0], m_ViewPort[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);

bool status = checkFramebufferStatus();

glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
int w = m_ViewPort[0];
int h = m_ViewPort[1];
QImage img(w,h, QImage::Format_ARGB32);
glReadPixels(0,0,w,h,GL_RGBA, GL_UNSIGNED_BYTE, img.bits());
mDisplayDialog fbo(img);
fbo.exec();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

}

I want the frame image show in the mDisplayDialog, but what i get is a white dialog.

Rosario Leonardi
09-18-2012, 03:27 PM
If you use the QGlFramBufferObject (Qt4.8) you alread have the toImage method that return a QImage.

http://qt-project.org/doc/qt-4.8/qglframebufferobject.html

In your code you are using GL_TEXTURE_2D instead of GL_TEXTURE_RECT
Also I don't see any render/clear code, you create a FBO, bind it and the read it back.

qlizhi
09-18-2012, 05:48 PM
You mean that i did not render the frame to texture?
I know i do not clear FBO and texture.

I use the transfer QImage to QPixmap, and show it in QLabel.


void VolRenCore::RendrToFBO()
{
glEnable(GL_TEXTURE_2D);
GLuint fb;
glGenFramebuffersEXT(1, &fb);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);

glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_ViewPort[0], m_ViewPort[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);

bool status = checkFramebufferStatus();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
glEnd();
glFlush();

glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
int w = m_ViewPort[0];
int h = m_ViewPort[1];
QImage img(w,h, QImage::Format_ARGB32);
glReadPixels(0,0,w,h,GL_RGBA, GL_UNSIGNED_BYTE, img.bits());
mDisplayDialog fbo(img);
fbo.exec();

glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glDeleteTexturesEXT(1, &tex);
tex = 0;
glDeleteFramebuffersEXT(1, &fb);
}