×
Namespaces

Variants
Actions

Archived:How to integrate OpenGLES2 graphics into a Qt application

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (using C++ for the Qt app UI code) is deprecated.

This article shows how to embed OpenGL ES2 code inside Qt using QGLWidget. Readers who are interested about OpenGL ES2 can learn more from this book.

Article Metadata
Tested with
Devices(s): N900
Compatibility
Platform(s): Maemo5
Symbian
Article
Keywords: Qt, OpenGL ES2, Graphics
Created: gnuton (18 Jun 2010)
Last edited: hamishwillee (11 Oct 2012)

What is OpenGL ES?

ES OpenGL ES is a royalty-free, cross-platform API for full-function 2D and 3D graphics on embedded systems. New devices, like N900, support only OpenGL ES2. OpenGL ES1 can be emulated, but with a significant loss of performance.

Qt Hello Triangle example

Hello Triangle is a classic OpenGL example that has been embedded in a minimal Qt application. This application simply draws a triangle using GLES2.

mywidget.h

  #ifndef MYGLWIDGET_H 
#define MYGLWIDGET_H
 
#include <QGLWidget>
#include <QMessageBox>
#include <QDebug>
 
#define INDEX 0 //ARRAY
 
class MyGLWidget : public QGLWidget
{
Q_OBJECT
 
public:
MyGLWidget(QWidget *parent = 0) : QGLWidget(parent) {
 
srcFragShader = "\
void main (void)\
{\
gl_FragColor = vec4(1.0, 0.0, 0.0 ,0.0);\
}"
;
 
srcVertShader = "\
attribute highp vec4 myVertex;\
void main(void)\
{\
gl_Position = myVertex;\
}"
;
}
 
protected:
 
void initializeGL()
{
GLuint fragmentShader;
GLuint vertexShader;
GLint linked;
 
// Set clear color
glClearColor(.3,.4,.6,1);
 
// Create the program object
programObject = glCreateProgram();
if(!programObject)
return;
 
// Load the shaders
fragmentShader = loadShader(qPrintable(srcFragShader), GL_FRAGMENT_SHADER);
vertexShader = loadShader(qPrintable(srcVertShader), GL_VERTEX_SHADER);
glAttachShader(programObject, vertexShader);
glAttachShader(programObject, fragmentShader);
 
// Bind myVertex to attribute INDEX
glBindAttribLocation(programObject, INDEX, "myVertex");
 
// Link the program
glLinkProgram(programObject);
 
// Check the link status
glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
if(!linked){
GLint infoLen = 0;
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1){
char* infoLog = (char*)malloc(sizeof(char) * infoLen);
glGetProgramInfoLog(programObject, infoLen, NULL, infoLog);
QMessageBox::warning(this,"qGLES2-1", QString("Link failed %1").arg(infoLog));
free(infoLog);
}
glDeleteProgram(programObject);
return;
}
}
 
void resizeGL(int w, int h)
{
// setup viewport, projection etc.:
glViewport(0, 0, (GLint)w, (GLint)h);
}
 
void paintGL()
{
//Clear background
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 
// Use programObject
glUseProgram(programObject);
 
// Define vertexs
GLfloat vertexs[] = {0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f};
 
// Load the vertex data
glVertexAttribPointer(INDEX, 3, GL_FLOAT, GL_FALSE, 0, vertexs);
glEnableVertexAttribArray(INDEX);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
 
private:
GLuint loadShader(const char* src, GLenum type)
{
GLuint shader;
GLint compiled;
 
// Create the shader object
shader = glCreateShader(type);
if(!shader)
return 0;
 
// Load the shader source
glShaderSource(shader, 1, &src, NULL);
 
// Compile the shader
glCompileShader(shader);
 
// Check the compile status
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if(!compiled){
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1){
char* infoLog = (char*)malloc(sizeof(char) * infoLen);
glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
QMessageBox::warning(this,"qGLES2-1", QString("Unable to compile the shader %1").arg(infoLog));
free(infoLog);
}
 
glDeleteShader(shader);
return 0;
}
 
return shader;
}
 
private:
GLuint programObject;
 
// Fragment and vertex shaders code
QString srcFragShader;
QString srcVertShader;
 
};
 
#endif // MYGLWIDGET_H

Links

This page was last modified on 11 October 2012, at 04:14.
100 page views in the last 30 days.