Rendering an OpenGL ES 2.0 triangle using QGLWidget in Qt5.3
-
Hi all.
Actually I am new to both OpenGL ES and Qt.So please don't mind if I go wrong anywhere.
I am trying to render a simple triangle with interpolated vertices.When I compile it shows following errors.@error: 'initializeGLFunctions' was not declared in this scope
error: 'glGenBuffers' was not declared in this scope
error: 'program' was not declared in this scope
positionID = program.attributeLocation ("s_vPosition");
error: 'glVertexAttribPointer' was not declared in this scope
glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0);
error: 'glEnableVertexAttribArray' was not declared in this scope
error: 'glBindBuffer' was not declared in this scope
error: 'glBufferData' was not declared in this scope@In one of the Forums I found that its because of exclusion of GLEW(not sure why it is used).So I went ahead and added #include<GL/glew.h>. But it made the thing worst,I got 121 errors.
This is my code written in Qt Creator. The code below is Geometry.cpp which inherits QGLWidget as Base Class.
@#include "geometry.h"
//#include<GL/glew.h>
#include <QVector3D>
#include <QtOpenGL/QGLFunctions>
#include <QGLShaderProgram>
#include <QtOpenGL/qgl.h>Geometry::Geometry(QWidget *parent) :
QGLWidget(parent)
{
}void Geometry::initializeGL()
{
initializeGLFunctions();
glGenBuffers(1, &vbo);
positionID = program.attributeLocation ("s_vPosition");// return the program id which is stored in positionID
colorID= program.attributeLocation("s_vColor");
initTriangleGeometry();
// glewInit();
}void Geometry::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}void Geometry::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(colorID, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(3*3*sizeof(GLfloat))); glEnableVertexAttribArray(positionID); glEnableVertexAttribArray(colorID); glDrawArrays(GL_TRIANGLES, 0, 3);
}
void Geometry::initShaders()
{
// Compile vertex shader
if (!program.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.glsl"))
close();// Compile fragment shader if (!program.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmen--tShader.glsl")) close(); // Link shader pipeline if (!program.link()) close(); // Bind shader pipeline for use if (!program.bind()) close();
}
void Geometry::initTriangleGeometry()
{
GLfloat vertices[]={-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f};GLfloat colors[]={1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f}; glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, 7*3*sizeof(GLfloat), NULL, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, 3*3*sizeof(GLfloat), vertices); glBufferSubData(GL_ARRAY_BUFFER, 3*3*sizeof(GLfloat),3*4*sizeof(GLfloat), colors);
}
@Main.cpp
@#include "mainwindow.h"
#include <QApplication>
#include <geometry.h>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setApplicationName("Triangle");
Geometry widget;
widget.showMaximized();return a.exec();
}@
Vertex Shader:
@attribute vec3 s_vPosition;
attribute vec4 s_vColor;
varying vec4 color;void main () {
color = s_vColor;
gl_Position = vec4(s_vPosition, 1.0);
}@Fragmnet Shader:
@varying vec4 color;void main () {
gl_FragColor = color;
}@Please Help.
-
codefy,
the following link is to a youtube video of someone doing exactly what you want
https://www.youtube.com/watch?v=1nzHSkY4K18however, I encourage you to look into QWindow. As I understand, the Qt project is trying to move away from the QGLWidget. They want to use QWindow only. Use createWindowContainer() on your MainWindow class to create a drawable OpenGL surface, the QWindow.
Hope this works.
-
Hi,
No they are not, QGLWidget will have a replacement called QOpenGLWidget.
-
Thank you both of you for your reply.
Ferobles,
Actually I have referred this video before it is to render a triangle using fixed rendering pipeline but I want to do it using programmable pipeline.What about the Error any idea?SGaist,
What does that mean can you please explain it precisely? -
A new, better class will be introduced in Qt 5.4 (alpha version will be available this week, or early next week): QOpenGLWidget. You can read some discussion about it here: "link":http://comments.gmane.org/gmane.comp.lib.qt.devel/17852.
-
-
According to Qt's roadmap, Qt 5.4 will have all QGL* classes and functions deprecated. Try using QOpenGL* classes instead.