saman_artorious
18th September 2013, 13:46
I want to bind a QImage/QPixmap to texture and render it on screen. The step Ive been after is to
1. Grab a screen shot
2. Send pixmap bytes to another code segment
3. convert bytes to QImage
4. bind QImage to texture
I am sure that the rendering is correct. However, the program terminates when it reaches the bindTexture() function which takes QImage as input. This QImage as I noted is created from the pixmap byte array generated by screen shot.
/* save png directly to qbytearray */
QPixmap pixmap;
QByteArray bytes;
QBuffer buffer(&bytes);
pixmap = QPixmap::grabWidget(this);
buffer.open(QIODevice::WriteOnly);
pixmap.save(&buffer, "PNG"); // writes pixmap into bytes in PNG format
emit sendPixmapToMainWin(bytes);
Where program terminates:
void GlWidget::pixmapCatchFromForm(QByteArray bytes)
{
QImage image((const uchar* )bytes.constData(), glWidth, glHeight, QImage::Format_ARGB32);
texture = bindTexture(image); // something's wrong with image apparently
qDebug() << texture; // returns 1
}
Rendering is correct, I set the texture here to be rendered:
void GlWidget::paintGL()
{
//! [5]
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
QMatrix4x4 cameraTransformation;
cameraTransformation.rotate(alpha, 0, 1, 0);
cameraTransformation.rotate(beta, 1, 0, 0);
QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);
vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);
//! [6]
shaderProgram.bind();
shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
shaderProgram.setUniformValue("texture", 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(0);
shaderProgram.setAttributeArray("vertex", vertices.constData());
shaderProgram.enableAttributeArray("vertex");
shaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData());
shaderProgram.enableAttributeArray("textureCoordinate");
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
shaderProgram.disableAttributeArray("vertex");
shaderProgram.disableAttributeArray("textureCoordinate");
shaderProgram.release();
}
1. Grab a screen shot
2. Send pixmap bytes to another code segment
3. convert bytes to QImage
4. bind QImage to texture
I am sure that the rendering is correct. However, the program terminates when it reaches the bindTexture() function which takes QImage as input. This QImage as I noted is created from the pixmap byte array generated by screen shot.
/* save png directly to qbytearray */
QPixmap pixmap;
QByteArray bytes;
QBuffer buffer(&bytes);
pixmap = QPixmap::grabWidget(this);
buffer.open(QIODevice::WriteOnly);
pixmap.save(&buffer, "PNG"); // writes pixmap into bytes in PNG format
emit sendPixmapToMainWin(bytes);
Where program terminates:
void GlWidget::pixmapCatchFromForm(QByteArray bytes)
{
QImage image((const uchar* )bytes.constData(), glWidth, glHeight, QImage::Format_ARGB32);
texture = bindTexture(image); // something's wrong with image apparently
qDebug() << texture; // returns 1
}
Rendering is correct, I set the texture here to be rendered:
void GlWidget::paintGL()
{
//! [5]
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;
QMatrix4x4 cameraTransformation;
cameraTransformation.rotate(alpha, 0, 1, 0);
cameraTransformation.rotate(beta, 1, 0, 0);
QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);
vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);
//! [6]
shaderProgram.bind();
shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
shaderProgram.setUniformValue("texture", 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(0);
shaderProgram.setAttributeArray("vertex", vertices.constData());
shaderProgram.enableAttributeArray("vertex");
shaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData());
shaderProgram.enableAttributeArray("textureCoordinate");
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
shaderProgram.disableAttributeArray("vertex");
shaderProgram.disableAttributeArray("textureCoordinate");
shaderProgram.release();
}