我搜索了很多,没有回答我的问题。我正在学习OpenGL,而且我正在使用Qt编写小程序,因为我想使用Raspberry PI 2。
我正在努力学习一个简单的程序:绘制一个矩形(带有索引的4个顶点)和一个纹理。正如我所说,我使用的是Qt,因此是QOpenGLWidget的子类。
当我为桌面编译这个程序时,它运行得很好。在这种情况下,我猜Qt使用OpenGL。当我打开ARM时,我想使用OpenGL ES 2.0进行Qt编译。 而且我没有看到直肠。哪里错了?我疯了。
头文件;
#ifndef MYGLWIDGET_H
#define MYGLWIDGET_H
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLBuffer>
#include <QOpenGLShader>
#include <QOpenGLTexture>
class MyGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit MyGLWidget(QWidget *parent = 0);
~MyGLWidget();
static const GLfloat vertices[];
static const GLuint indices[];
static const GLfloat textureVertex[];
protected:
void resizeGL(int w, int h) Q_DECL_OVERRIDE;
void paintGL() Q_DECL_OVERRIDE;
void initializeGL() Q_DECL_OVERRIDE;
private:
QOpenGLShader *m_vshader1;
QOpenGLShader *m_fshader1;
QOpenGLShaderProgram *m_program1;
QOpenGLBuffer m_vbo1;
int m_vertexAttr1;
int m_texAttr1;
QOpenGLTexture *_texture;
signals:
public slots:
};
#endif // MYGLWIDGET_H
CPP文件:
#include "myglwidget.h"
//Set up vertex data (and buffer(s)) and attribute pointers
const GLfloat MyGLWidget::vertices[] = {
0.5f, 0.5f, 0.0f, // Top Right
0.5f, -0.5f, 0.0f,// Bottom Right
-0.5f, -0.5f, 0.0f,// Bottom Left
-0.5f, 0.5f, 0.0f// Top Left
};
//setup the order of vertices (indexes)
const GLuint MyGLWidget::indices[] = { // Note that we start from 0!
0, 3, 2, // First Triangle
2, 1, 0 // Second Triangle 0, 1, 2
};
//Set up vertex for textures
const GLfloat MyGLWidget::textureVertex[] = {
1.0f, 1.0f, // Top Right
1.0f, 0.0f,// Bottom Right
0.0f, 0.0f,// Bottom Left
0.0f, 1.0f// Top Left
};
MyGLWidget::MyGLWidget(QWidget *parent):
QOpenGLWidget(parent)
{
}
MyGLWidget::~MyGLWidget()
{
// And now release all OpenGL resources.
makeCurrent();
delete m_vshader1;
delete m_fshader1;
delete _texture;
delete m_program1;
m_vbo1.destroy();
doneCurrent();
}
void MyGLWidget::initializeGL()
{
initializeOpenGLFunctions();
m_vshader1 = new QOpenGLShader(QOpenGLShader::Vertex);
const char *vsrc1 =
"attribute vec3 aPosition; \n"
"attribute vec2 aTexCoord; \n"
"varying vec2 vTexCoord; \n"
"void main() \n"
"{ \n"
" gl_Position = vec4(aPosition, 1.0); \n"
" vTexCoord = aTexCoord; \n"
"} \n";
bool r = m_vshader1->compileSourceCode(vsrc1);
qDebug()<<"Compilation shader = "<<r;
m_fshader1 = new QOpenGLShader(QOpenGLShader::Fragment);
const char *fsrc1 =
"varying vec2 vTexCoord; \n"
"uniform sampler2D ourTexture; \n"
"void main() \n"
"{ \n"
" gl_FragColor =texture2D(ourTexture, vTexCoord); \n"
"} \n";
r = m_fshader1->compileSourceCode(fsrc1);
qDebug()<<"Compilation shaders = "<<r;
m_program1 = new QOpenGLShaderProgram;
m_program1->addShader(m_vshader1);
m_program1->addShader(m_fshader1);
qDebug()<<"Link = "<<m_program1->link();
m_vertexAttr1 = m_program1->attributeLocation("aPosition");
qDebug()<<"Aposition index = "<<m_vertexAttr1;
m_texAttr1 = m_program1->attributeLocation("aTexCoord");
qDebug()<<"aTexCoord index = "<<m_texAttr1;
// Use a vertex buffer object. Client-side pointers are old-school and should be avoided.
m_vbo1.create();
m_vbo1.bind();
m_vbo1.allocate(sizeof(vertices) + sizeof(textureVertex));
//add vertices
m_vbo1.write(0, vertices, sizeof(vertices));
//add texture vertices
m_vbo1.write(sizeof(vertices), textureVertex, sizeof(textureVertex));
//texture
QImage image("./oneframe.png");
qDebug()<<"Image = "<<image.byteCount()<<" bytes";
_texture = new QOpenGLTexture(QImage("./oneframe.png").mirrored());
_texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
_texture->setMagnificationFilter(QOpenGLTexture::Linear);
m_vbo1.release();
}
void MyGLWidget::resizeGL(int w, int h)
{
}
void MyGLWidget::paintGL()
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
m_program1->bind();
m_program1->enableAttributeArray(m_vertexAttr1);
m_program1->enableAttributeArray(m_texAttr1);
m_vbo1.bind();
m_program1->setAttributeBuffer(m_vertexAttr1, GL_FLOAT, 0, 3);
m_program1->setAttributeBuffer(m_texAttr1, GL_FLOAT, sizeof(vertices), 2);
_texture->bind();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, indices);
m_vbo1.release();
m_program1->disableAttributeArray(m_vertexAttr1);
m_program1->disableAttributeArray(m_texAttr1);
m_program1->release();
}
在桌面上,我看到矩形中带有纹理的主窗口。
我在Raspberry下运行此程序时看到的只是具有背景颜色的主窗口...没有三角形可见。 PNG文件位于同一目录中。
感谢您的帮助。
克里斯蒂