Own update and correction, I finally found the issue and make the gles1 version working! and see that the original version is "live stream" on rotating box and also live background !
The issue was that in gles1 you cannot glTexture2D a 400x240 texture, instead use 512x512 and the sub texture it will do the trick.
For people who want to use gles1 version
Code:
const int TextureSizeW = 512;
const int TextureSizeH = 512;
int GLWidget::buffer_width=400;
int GLWidget::buffer_height=240;
void GLWidget::initializeGL ()
{
glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &m_uiTexture);
// m_uiTexture = bindTexture(QImage(":/qt.png"));
glBindTexture(GL_TEXTURE_2D, m_uiTexture);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, TextureSizeW,TextureSizeH, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
m_fAngle = 0;
m_fScale = 1;
createGeometry();
createBubbles(bubbleNum - bubbles.count());
}
void GLWidget::refresh_texture()
{
if(CameraN900::scanning)
{
CameraN900::refresh_buffer(); //manually call appsink, not through the callback function
if(CameraN900::buffer!=NULL)
{
glBindTexture( GL_TEXTURE_2D, m_uiTexture );
unsigned char* data=(unsigned char *) GST_BUFFER_DATA (CameraN900::buffer);
//glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, buffer_width,buffer_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, buffer_width,buffer_height, GL_RGB, GL_UNSIGNED_BYTE, data);
}
gst_buffer_unref(CameraN900::buffer);
}
}
My working paintGL looks as
Code:
void GLWidget::paintGL()
{
createBubbles(bubbleNum - bubbles.count());
QPainter painter;
painter.begin(this);
glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
refresh_texture();
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
drawBackground(&painter);
//////////////////////////////////////////////////////////////////////////////
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMatrixMode(GL_TEXTURE);
glPushMatrix();
//Since OpenGL ES does not support glPush/PopAttrib(GL_ALL_ATTRIB_BITS)
//we have to take care of the states ourselves
glEnable(GL_TEXTURE_2D);
q_glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
q_glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_FLAT);
glFrontFace(GL_CW);
glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
q_glRotate(f2vt(m_fAngle), f2vt(0.0), f2vt(1.0), f2vt(0.0));
q_glRotate(f2vt(m_fAngle), f2vt(1.0), f2vt(0.0), f2vt(0.0));
q_glRotate(f2vt(m_fAngle), f2vt(0.0), f2vt(0.0), f2vt(1.0));
q_glScale(f2vt(m_fScale), f2vt(m_fScale),f2vt(m_fScale));
q_glTranslate(f2vt(0),f2vt(-0.2),f2vt(0));
q_vertexType matDiff[] = {f2vt(0.40), f2vt(1.0), f2vt(0.0), f2vt(1.0)};
q_glMaterialv(GL_FRONT_AND_BACK, GL_DIFFUSE, matDiff);
if (qtLogo)
paintQtLogo();
else
paintTexturedCube();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_TEXTURE);
glPopMatrix();
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
if (m_showBubbles)
foreach (Bubble *bubble, bubbles) {
bubble->drawBubble(&painter);
}
QString framesPerSecond;
framesPerSecond.setNum(frames /(time.elapsed() / 1000.0), 'f', 2);
painter.setPen(Qt::white);
painter.drawText(20, 40, framesPerSecond + " fps");
painter.drawRect(0,0,799,479);
painter.end();
swapBuffers();
QMutableListIterator<Bubble*> iter(bubbles);
while (iter.hasNext()) {
Bubble *bubble = iter.next();
bubble->move(rect());
}
if (!(frames % 100)) {
time.start();
frames = 0;
}
m_fAngle += 1.0f;
frames ++;
}
and finally the drawBackground function for gles1 using texture as background
Code:
void GLWidget::drawBackground(QPainter *painter)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrthof(0, 1, 0, 1, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glClear( GL_DEPTH_BUFFER_BIT);
glDisable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
q_vertexType afVertices[] = {
f2vt(0.0), f2vt(0.0), f2vt(1.0), f2vt(0.0),f2vt(0.0),f2vt(1.0),f2vt(1.0),f2vt(1.0)
};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2,q_vertexTypeEnum,0,afVertices);
GLfloat tw = GLfloat(buffer_width / TextureSizeW);
GLfloat th = GLfloat(buffer_height / TextureSizeH);
q_vertexType afTexCoord[] = {
// remember to scale your tex coord according to 400/512 240/512 value
f2vt(0.0f),f2vt(0.469f), f2vt(0.781f),f2vt(0.469f), f2vt(0.0f),f2vt(0.0f), f2vt(0.781f),f2vt(0.0f)
//f2vt(0.0f),f2vt(th), f2vt(tw),f2vt(th), f2vt(0.0f),f2vt(0.0f), f2vt(tw),f2vt(0.0f)
};
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2,q_vertexTypeEnum,0,afTexCoord);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glEnable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
in my case it runs at around 15 fps, already happy about it! all credit goes to Klen!
Enjoy!
Previous Post under:=================
One question for curiosity and confirmation of my understanding:
I notice in your project you have a drawBackground() and is by default disabled, this is what I need in AR study, unfortunately when I enable it the drawing is correct but frame rate drop to 2 or 3 fps.
when I tap "show video stream" in app, it update the texture of the rotating cube, but then keep rendering with that frame, it is no "live stream" on my cube, is it the expect result?
I try to use
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, buffer_width,buffer_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
as in refresh_texture and rend a quad without using QImage, it however does not work so far ( I use opengl es 1 version)
Have you correct this in similar way, meaning have you get these two effect: "live stream" on your rotating cube or use "rendering background as texture instead of paint QImage"
thanks for tip.
ZR_YAO