PDA

View Full Version : display framebuffer on screen



rafal
04-26-2012, 02:28 AM
a questions are :
1) how to paint framebuffer on a screen ?
2) why rendering the same picture directly on screen is about 10 times slower than renering to frambuffer ? (i have much slower g. card than the rest)
3) im using texture from that framebuffer to display it on screen. but it is not working why ? the simple code belows. frambuffer
context is good.


void oglPlotWidget::renderOffScreen() {

makeCurrent();

fb = new QGLFramebufferObject(this->width(), this->height(), GL_TEXTURE_2D);
fb->bind();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glViewport(0, 0, this->width(), this->height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (-0.01, 2.01, -0.01, 2.01, 0, 1);
glMatrixMode(GL_MODELVIEW);

foreach(plotCurve *curve, m_curves)
if (curve->style() == plotCurve::points)
{
QVector<SamplePointer> samples = curve->samples();
glVertexPointer(2, GL_FLOAT, sizeof(SamplePointer), samples.data() + curve->firstSample());
glColor3f(1, 0.1, 0.1);
glPointSize(10);
glDrawArrays(GL_POINTS, 0, samples.size() - curve->firstSample());
}

fb->release();
textureId = fb->texture();

delete fb;

}

and to paint just generated textureId i use :

void oglPlotWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

if(textureId)
{
glBindTexture(GL_TEXTURE_2D, textureId);

glBegin(GL_QUADS);

glTexCoord2f(0.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);

glTexCoord2f(0.0, 1.0);
glVertex3f(-1.0, 1.0, 0.0);

glTexCoord2f(1.0, 1.0);
glVertex3f(1.0, 1.0, 0.0);

glTexCoord2f(1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);

glEnd();

}
...

BionicBytes
04-26-2012, 06:02 AM
glOrtho (-0.01, 2.01, -0.01, 2.01, 0, 1);

Are you sure your ortho projection values are right? Usually you set up a 2D screen using the width & height values that you use for the viewport.


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glViewport(0, 0, this->width(), this->height());
glMatrixMode(GL_PROJECTION);

Not necessarily a problem...but your code is not clear if this is setting ModelView matrix to identity.



glVertex3f(-1.0, -1.0, 0.0);

glTexCoord2f(0.0, 1.0);
glVertex3f(-1.0, 1.0, 0.0);

glTexCoord2f(1.0, 1.0);
glVertex3f(1.0, 1.0, 0.0);

glTexCoord2f(1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);


The vertex coordinates of a 2D screen are usually the width and height values of the glOrtho & viewport parameters.
As you have not done that and rolled your own (which I don't understand) you need to check if these are the correct coordinates. Remeber, these coordinates will be transformed by the ModelView and Projection matricies, so the values you use here are very much linked to the projection matrix you setup earlier.

rafal
04-26-2012, 07:02 AM
new coord values are :
glTexCoord2f (0.0, 0.0);
glVertex2f (0.0, 0.0);

glTexCoord2f (0.0, 1.0);
glVertex2f (0.0, 2.0);

glTexCoord2f (1.0, 1.0);
glVertex2f (2.0, 2.0);

glTexCoord2f (1.0, 0.0);
glVertex2f (2.0, 0.0);


but it isnt problem at all. im interesting in that why i see solid polygon instead of texture.

Dan Bartlett
04-26-2012, 07:17 AM
Have you remembered to include
glEnable(GL_TEXTURE_2D); and if you have done this, do you have a mipmap complete texture? ie. all texture levels populated, or mipmaps disabled - see http://www.opengl.org/wiki/Common_Mistakes#Creating_a_Texture

rafal
04-26-2012, 07:44 AM
sure i have glEnable(GL_TEXTURE_2D); and any line with mipmap which means disabled i think. i use texture to display framebuffer as i pointed before. is it the best way to do that ?

aqnuep
04-26-2012, 07:55 AM
You can also use glBlitFramebuffer (http://www.opengl.org/sdk/docs/man4/xhtml/glBlitFramebuffer.xml) to perform the copy. It's more convenient and you can skip that deprecated immediate mode full screen quad drawing code.