Render texture to QQuickFramebufferObject using OpenGL
-
I'm following QQuickFramebufferObject example and now I'm editing logorenderer class to render single texture that rotates in 3D and it only renders black square.
EDIT: I simplified code and I found out that if on line 62 i change image's size to 1x1 it works (it's solid blue) but if I change to anything else it stops working. What am I doing wrong?
My code:
logorenderer.cpp
@
#include "logorenderer.h"void LogoRenderer::initialize()
{
initializeGLFunctions();QOpenGLShader *vshader1 = new QOpenGLShader(QOpenGLShader::Vertex, &shaderProgram); const char *vsrc1 = "#version 120\n" "attribute vec4 vertex;\n" "uniform mat4 matrix;\n" "attribute vec2 texturePos;\n" "varying vec2 texcoord;\n" "void main(void)" "{" " texcoord = texturePos;" " gl_Position = matrix * vertex;" "}"; vshader1->compileSourceCode(vsrc1); QOpenGLShader *fshader1 = new QOpenGLShader(QOpenGLShader::Fragment, &shaderProgram); const char *fsrc1 = "#version 120\n" "uniform sampler2D tex;\n" "varying vec2 texcoord;" "void main()" "{" " gl_FragColor = texture2D(tex, texcoord);" "}";; fshader1->compileSourceCode(fsrc1); shaderProgram.addShader(vshader1); shaderProgram.addShader(fshader1); shaderProgram.link(); vertexAttr = shaderProgram.attributeLocation("vertex"); matrixUniform = shaderProgram.uniformLocation("matrix"); texturePositionAttr = shaderProgram.attributeLocation("texturePos"); textureUniform = shaderProgram.uniformLocation("tex"); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); m_fAngle = 0; createGeometry(); glEnable(GL_TEXTURE_2D); glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); // create checkerboard array GLfloat pixels[64*64*3]; for(int i=0;i<64*64;i++){ if((i/64%2==1)^(i%2==1)){ pixels[i*3] = 0; pixels[i*3+1] = 1; pixels[i*3+2] = 0; }else{ pixels[i*3] = 0; pixels[i*3+1] = 0; pixels[i*3+2] = 1; } } glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_FLOAT, &pixels[0]); glDisable(GL_TEXTURE_2D);
}
void LogoRenderer::render()
{
glDepthMask(true);glClearColor(1,1,1,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QMatrix4x4 modelview; modelview.rotate(m_fAngle, 0.0f, 1.0f, 0.0f); modelview.rotate(10, 1.0f, 0.0f, 0.0f); modelview.rotate(10, 0.0f, 1.0f, 0.0f); shaderProgram.bind(); shaderProgram.setUniformValue(matrixUniform, modelview); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, textureID); shaderProgram.enableAttributeArray(vertexAttr); shaderProgram.enableAttributeArray(texturePositionAttr); shaderProgram.setAttributeArray(vertexAttr, vertices.constData()); shaderProgram.setAttributeArray(texturePositionAttr, textureCoords.constData()); glDrawArrays(GL_TRIANGLES, 0, vertices.size()); shaderProgram.disableAttributeArray(vertexAttr); shaderProgram.disableAttributeArray(texturePositionAttr); shaderProgram.release(); m_fAngle += 2.0f;
}
void LogoRenderer::createGeometry()
{
vertices << QVector3D(-0.5, -0.5, 0.5) << QVector3D( 0.5, -0.5, 0.5) << QVector3D( 0.5, 0.5, 0.5)
<< QVector3D(-0.5, -0.5, 0.5) << QVector3D( 0.5, 0.5, 0.5) << QVector3D(-0.5, 0.5, 0.5);textureCoords << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) << QVector2D(1, 1) << QVector2D(0, 1) << QVector2D(0, 0);
}
@logorenderer.h
@
#ifndef LOGORENDERER_H
#define LOGORENDERER_H#include <QtGui/qvector3d.h>
#include <QtGui/qmatrix4x4.h>
#include <QtGui/qopenglshaderprogram.h>#include <QVector>
#include <QGLFunctions>class LogoRenderer: private QGLFunctions {
public:
void render();
void initialize();
void setContext(QGLContext* context);private:
qreal m_fAngle;void createGeometry(); QVector<QVector3D> vertices; QVector<QVector3D> textureCoords; QOpenGLShaderProgram shaderProgram; int vertexAttr; int texturePositionAttr; int matrixUniform; int textureUniform; GLuint textureID;
};
#endif
@