[Interest] Qt5 QML OpenGL and "black" texture problem

Sletta Gunnar Gunnar.Sletta at digia.com
Thu May 2 14:46:57 CEST 2013


You are at least not setting the required texture parameters which will make the texture incomplete. You typically need to set the textures wrap and min/mag filters at least once: 

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

btw, I would think the "Scene Graph - Rendering FBOs" example I added for 5.1 is a good starting place for implementing what you are trying to do here: http://doc-snapshot.qt-project.org/qt5-stable/qtquick/scenegraph-textureinsgnode.html

cheers,
Gunnar

 
On May 2, 2013, at 12:31 PM, BOUCARD Olivier <boucard_olivier at yahoo.fr> wrote:

> Hi guys,
> 
> I'm trying to display some "native" OpenGL inside a custom QML item using 
> Qt 5.0.2 on a Linux x86_64 with a Nvidia 9600M GT (driver v295.40).
> I have a custom QQuickPaintedItem with a overloaded paint where I use painter->beginNativePainting() and a shader program:
> 
> Vertex shader:
> 
> uniform highp mat4 projectionMatrix;
> uniform highp mat4 modelViewMatrix;
> 
> void main(void)
> {
>   gl_Position = projectionMatrix * modelViewMatrix * gl_Vertex;
>   gl_FrontColor = gl_Color;
>   gl_TexCoord[0] = gl_MultiTexCoord0;
> }
> 
> 
> 
> Fragment shader:
> 
> uniform highp sampler2D diffuseTexture;
> 
> void main(void)
> {
>   gl_FragColor = texture2D(diffuseTexture, gl_TexCoord[0].st);
>   //gl_FragColor = gl_Color; // Display the mesh in white -> OK
> 
>   //gl_FragColor = vec4(gl_TexCoord[0].st, 0.0, 1.0); // Display the correct coord -> OK
> }
> 
> 
> 
> I have a mesh class used to load a mesh using Assimp. At the moment I only load a simple cube.obj.
> The rendering code is the following:
> 
> void Mesh::render()
> {
>   if(mData->hasDiffuse)
>   {
>     glActiveTexture(GL_TEXTURE0);
>     glBindTexture(GL_TEXTURE_2D, mData->diffuse);
>   }
> 
>   glEnableVertexAttribArray(SimpleGameEngine::sAttrLocVertex);
>   glEnableVertexAttribArray(SimpleGameEngine::sAttrLocTex0);
>   glBindBuffer(GL_ARRAY_BUFFER, mData->vertexBuffer);
>   glVertexAttribPointer(SimpleGameEngine::sAttrLocVertex, 3, GL_FLOAT, GL_FALSE, 0, NULL);
> 
>   glBindBuffer(GL_ARRAY_BUFFER, mData->texBuffer);
>   glVertexAttribPointer(SimpleGameEngine::sAttrLocTex0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
> 
>   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mData->faceBuffer);
>   glDrawElements(GL_TRIANGLES, mData->faceNum * 3,GL_UNSIGNED_INT, 0);
> 
>   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
>   glBindBuffer(GL_ARRAY_BUFFER, 0);
>   glDisableVertexAttribArray(SimpleGameEngine::sAttrLocTex0);
>   glDisableVertexAttribArray(SimpleGameEngine::sAttrLocVertex);
> 
>   if(mData->hasDiffuse)
>   {
>     glBindTexture(GL_TEXTURE_2D, 0);
>     glActiveTexture(GL_TEXTURE0);
>   }
> }
> 
> My texture is loaded using the following code:
> 
> void Mesh::setDiffuseTexture(const QImage &image)
> {
>   if(image.isNull()) qFatal("Invalid image");
> 
>   QImage texture = QGLWidget::convertToGLFormat(image);
> 
>   if(texture.isNull()) qFatal("Invalid texture");
> 
>   glGenTextures(1, &mData->diffuse);
> 
>   glBindTexture(GL_TEXTURE_2D, mData->diffuse);
> 
>   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texture.width(), texture.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.bits());
> 
>   glBindTexture(GL_TEXTURE_2D, 0);
> 
>   mData->hasDiffuse = true;
> }
> 
> Everything works. But as soon as I add the texture my cube turns black.
> I already try with a procedural 4x4 checker texture and it didn't work either.
> 
> 
> Any help will be appreciated.
> Olivier. 
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest




More information about the Interest mailing list