If QT_OPENGL_ES_CL is not defined we use the floating point functions.
This way we support OpenGL ES Common and Common Light with the same code and abstract the fact that we use either the floating point functions or otherwise the fixed point functions.
Porting OpenGL to OpenGL ES
Since OpenGL ES is missing the immediate mode and does not support quads, we have to create triangle arrays.
We create a quad by adding vertices to a QList of vertices. We create both sides of the quad and hardcode a distance of 0.05f. We also compute the correct normal for each face and store them in another QList.
void GLWidget::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4)
{
qreal nx, ny, nz;
vertices << x1 << y1 << -0.05f;
vertices << x2 << y2 << -0.05f;
vertices << x4 << y4 << -0.05f;
vertices << x3 << y3 << -0.05f;
vertices << x4 << y4 << -0.05f;
vertices << x2 << y2 << -0.05f;
CrossProduct(nx, ny, nz, x2 - x1, y2 - y1, 0, x4 - x1, y4 - y1, 0);
Normalize(nx, ny, nz);
normals << nx << ny << nz;
normals << nx << ny << nz;
normals << nx << ny << nz;
normals << nx << ny << nz;
normals << nx << ny << nz;
normals << nx << ny << nz;
vertices << x4 << y4 << 0.05f;
vertices << x2 << y2 << 0.05f;
vertices << x1 << y1 << 0.05f;
vertices << x2 << y2 << 0.05f;
vertices << x4 << y4 << 0.05f;
vertices << x3 << y3 << 0.05f;
CrossProduct(nx, ny, nz, x2 - x4, y2 - y4, 0, x1 - x4, y1 - y4, 0);
Normalize(nx, ny, nz);
normals << nx << ny << nz;
normals << nx << ny << nz;
normals << nx << ny << nz;
normals << nx << ny << nz;
normals << nx << ny << nz;
normals << nx << ny << nz;
}
And then we convert the complete list of vertexes and the list of normals into the native OpenGL ES format that we can use with the OpenGL ES API.
m_vertexNumber = vertices.size();
createdVertices = new q_vertexType[m_vertexNumber];
createdNormals = new q_vertexType[m_vertexNumber];
for (int i = 0;i < m_vertexNumber;i++) {
createdVertices[i] = f2vt(vertices.at(i) * 2);
createdNormals[i] = f2vt(normals.at(i));
}
vertices.clear();
normals.clear();
}
In paintQtLogo() we draw the triangle array using OpenGL ES. We use q_vertexTypeEnum to abstract the fact that our vertex and normal arrays are either in float or in fixed point format.
void GLWidget::paintQtLogo()
{
glDisable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,q_vertexTypeEnum,0, createdVertices);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(q_vertexTypeEnum,0,createdNormals);
glDrawArrays(GL_TRIANGLES, 0, m_vertexNumber / 3);
}
Using QGLPainter
Since the QGLPainter is slower for OpenGL ES we paint the bubbles with the rasterizer and cache them in a QImage. This happends only once during the initialiazation.
void Bubble::updateCache()
{
if (cache)
delete cache;
cache = new QImage(qRound(radius * 2 + 2), qRound(radius * 2 + 2), QImage::Format_ARGB32);
cache->fill(0x00000000);
QPainter p(cache);
p.setRenderHint(QPainter::HighQualityAntialiasing);
QPen pen(Qt::white);
pen.setWidth(2);
p.setPen(pen);
p.setBrush(brush);
p.drawEllipse(0, 0, int(2*radius), int(2*radius));
}
For each bubble this QImage is then drawn to the QGLWidget by using the according QPainter with transparency enabled.
void Bubble::drawBubble(QPainter *painter)
{
painter->save();
painter->translate(position.x() - radius, position.y() - radius);
painter->setOpacity(0.8);
painter->drawImage(0, 0, *cache);
painter->restore();
}
Another difference beetwen OpenGL and OpenGL ES is that OpenGL ES does not support glPushAttrib(GL_ALL_ATTRIB_BITS). So we have to restore all the OpenGL states ourselves, after we created the QPainter in GLWidget::paintGL().
QPainter painter;
painter.begin(this);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
Setting up up the model view matrix and setting the right OpenGL states is done in the same way as for standard OpenGL.
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMatrixMode(GL_TEXTURE);
glPushMatrix();
q_glClearColor(f2vt(0.1f), f2vt(0.1f), f2vt(0.2f), f2vt(1.0f));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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();
Now we have to restore the OpenGL state for the QPainter. This is not done automatically for OpenGL ES.
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);
Now we use the QPainter to draw the transparent bubbles.
if (m_showBubbles)
foreach (Bubble *bubble, bubbles) {
bubble->drawBubble(&painter);
}
In the end, we calculate the framerate and display it using the QPainter again.
QString framesPerSecond;
framesPerSecond.setNum(frames /(time.elapsed() / 1000.0), 'f', 2);
painter.setPen(Qt::white);
painter.drawText(20, 40, framesPerSecond + " fps");
painter.end();
After we finished all the drawing operations we swap the screen buffer.
swapBuffers();
Summary
Similar to the Hello GL Example, we subclass QGLWidget to render a 3D scene using OpenGL ES calls. QGLWidget is a subclass of QWidget. Hence, its QGLWidget's subclasses can be placed in layouts and provided with interactive features just like normal custom widgets.
QGLWidget allows pure OpenGL ES rendering to be mixed with QPainter calls, but care must be taken to maintain the state of the OpenGL ES implementation.