PDA

View Full Version : Convert a GL texture (GLuint) to QImage or QOpenGLTexture



miyoku157
9th June 2017, 16:27
Hi,

I find a lot of way to pass a QImage to GL but in fact i retrieve a gl texture from an API. So i'm trying to convert it either in a QImage or in a QOpenGLTexture to display it. I didn't see any function that could do it. I'm still trying by retrieving the data with glteximage and trying to fill a QImage but i don't know if it will work.

This is my code to display :

void GLWidget::paintGL()
{
glClearColor(clearColor.redF(), clearColor.greenF(), clearColor.blueF(), clearColor.alphaF());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

QMatrix4x4 m;
m.ortho(-1, 1, +1.f, -01.f, 1.0f, 20.0f);
m.translate(0.0f, 0.0f, -10.0f);
nv.StartCapture();
program->setUniformValue("matrix", m);
program->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
program->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE);
program->setAttributeBuffer(PROGRAM_VERTEX_ATTRIBUTE, GL_FLOAT, 0, 3, 5 * sizeof(GLfloat));
program->setAttributeBuffer(PROGRAM_TEXCOORD_ATTRIBUTE, GL_FLOAT, 3 * sizeof(GLfloat), 2, 5 * sizeof(GLfloat));
textures[0]->bind();
glDrawArrays(GL_TRIANGLE_FAN, 0 * 4, 4);
}

If you have any idea.
Thanks

wysota
12th June 2017, 08:16
There is no direct way to do it since the texture resides in your graphics card's memory. The most direct way I can think of is to render a rectangle with the texture to a frame buffer object and that can easily be converted to a QImage. As for QOpenGLTexture I see nothing in its API that could be used to bind it to an externally defined texture.

miyoku157
12th June 2017, 10:54
Hi,
Thanks for your answer
Currently, i retrieve the Gltexture from an opengl extension and i was thinking that the best way to draw it was to convert it in QImage. but if i can directly draw it in my glwidget, that's better. I've tried to set the GLTexture in uniform param but i have a black screen for the moment. I'm going to look at the framebuffer.


void GLWidget::initializeGL()
{
initializeOpenGLFunctions();
GLenum te=glewInit();
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);


nvc = NVConfig();
nvc.GlobalInit();
nv= NVGL(nvc.l_vioConfig,GetDC((HWND)this->winId()));
makeObject();

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);

#define PROGRAM_VERTEX_ATTRIBUTE 0
#define PROGRAM_TEXCOORD_ATTRIBUTE 1

QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
const char *vsrc =
"attribute highp vec4 vertex;\n"
"attribute mediump vec4 texCoord;\n"
"varying mediump vec4 texc;\n"
"uniform mediump mat4 matrix;\n"
"void main(void)\n"
"{\n"
" gl_Position = matrix * vertex;\n"
" texc = texCoord;\n"
"}\n";
vshader->compileSourceCode(vsrc);

QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
const char *fsrc =
"uniform sampler2D texture;\n"
"varying mediump vec4 texc;\n"
"void main(void)\n"
"{\n"
" gl_FragColor = texture2D(texture, texc.st);\n"
"}\n";
fshader->compileSourceCode(fsrc);

program = new QOpenGLShaderProgram;
program->addShader(vshader);
program->addShader(fshader);
program->bindAttributeLocation("vertex", PROGRAM_VERTEX_ATTRIBUTE);
program->bindAttributeLocation("texCoord", PROGRAM_TEXCOORD_ATTRIBUTE);
program->link();
program->bind();
program->setUniformValue("texture", nv.m_videoTexture);
}

void GLWidget::paintGL()
{
glClearColor(clearColor.redF(), clearColor.greenF(), clearColor.blueF(), clearColor.alphaF());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

QMatrix4x4 m;
m.ortho(-1, 1, +1.f, -01.f, 1.0f, 20.0f);
m.translate(0.0f, 0.0f, -10.0f);
nv.StartCapture(); //This function retrieve the GLTexture
program->setUniformValue("matrix", m);
program->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
program->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE);
program->setAttributeBuffer(PROGRAM_VERTEX_ATTRIBUTE, GL_FLOAT, 0, 3, 5 * sizeof(GLfloat));
program->setAttributeBuffer(PROGRAM_TEXCOORD_ATTRIBUTE, GL_FLOAT, 3 * sizeof(GLfloat), 2, 5 * sizeof(GLfloat));
glDrawArrays(GL_TRIANGLE_FAN, 0 * 4, 4);
}

EDIT***
I still have a black screen, so to be sure i have a correct texture, i saved my texture in .tiff file. And i got a correct result. So my problem is really to draw my texture on the widget

miyoku157
13th June 2017, 10:34
I finally found my mistake, i was just misbinding my texture. Work great now.