OpenGL load textures from QImage
-
Hi! I try to make a transition animation between 2 pages in OpenGL. I have 2 QImages objects, tex1 - the current page, tex2 - the next page. The mask is the image drawed in OpenGL.
if (!m_program) { initializeOpenGLFunctions (); m_program = new QOpenGLShaderProgram (); qDebug () << m_program->addShaderFromSourceCode (QOpenGLShader::Vertex, "attribute highp vec4 posAttr;\n" "attribute lowp vec4 colAttr;\n" "uniform highp mat4 matrix;\n" "varying highp vec2 coord;\n" "varying lowp vec4 col;\n" "void main() {\n" " col = colAttr;\n" " coord = vec2(gl_MultiTexCoord0.xy);\n" " gl_Position = matrix * posAttr;\n" "}" ); qDebug () << m_program->addShaderFromSourceCode (QOpenGLShader::Fragment, "varying highp vec2 coord;\n" "varying lowp vec4 col;\n" "uniform sampler2D texx1;\n" "uniform sampler2D texx2;\n" "void main() {\n" " mediump vec4 texture1Color = vec4(texture2D(texx1, coord).xyz, 1.0);\n" " mediump vec4 texture2Color = vec4(texture2D(texx2, coord).xyz, 1.0);\n" " gl_FragColor = mix(texture1Color, texture2Color, col);\n" // " gl_FragColor = col;" "}" ); qDebug () << m_program->link (); } qDebug () << m_program->bind (); int vert = m_program->attributeLocation ("posAttr"); int cols = m_program->attributeLocation ("colAttr"); int matx = m_program->uniformLocation ("matrix"); m_program->enableAttributeArray (0); m_program->enableAttributeArray (1); GLfloat vertex [40] = { center_x, current_rgb_top, center_x, current_mono_top, current_mono_left, center_y, current_rgb_left, center_y, current_rgb_left, center_y, current_mono_left, center_y, center_x, current_mono_bottom, center_x, current_rgb_bottom, center_x, current_rgb_bottom, center_x, current_mono_bottom, current_mono_right, center_y, current_rgb_right, center_y, current_rgb_right, center_y, current_mono_right, center_y, center_x, current_mono_top, center_x, current_rgb_top, center_x, current_mono_top, current_mono_right, center_y, center_x, current_mono_bottom, current_mono_left, center_y, }; QMatrix4x4 matrix; matrix.ortho (0., viewportSize.width (), viewportSize.height (), 0., -1, 1); glEnable (GL_TEXTURE_2D); GLuint m_tex1, m_tex2; glGenTextures (1, &m_tex1); glBindTexture (GL_TEXTURE_2D, m_tex1); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex1.width(), tex1.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, tex1.bits()); glBindTexture (GL_TEXTURE_2D, 0); qDebug() << "tex1 sizes" << tex1.width() << tex1.height(); glGenTextures (1, &m_tex2); glBindTexture (GL_TEXTURE_2D, m_tex2); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex2.width(), tex2.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, tex2.bits()); glBindTexture (GL_TEXTURE_2D, 0); qDebug() << "tex2 sizes" << tex2.width() << tex2.height(); // glGenerateMipmap (GL_TEXTURE_2D); m_program->setUniformValue (matx, matrix); glVertexAttribPointer (vert, 2, GL_FLOAT, GL_FALSE, 0, vertex); glVertexAttribPointer (cols, 3, GL_FLOAT, GL_FALSE, 0, colors); glViewport (0, 0, viewportSize.width (), viewportSize.height () ); glDisable (GL_DEPTH_TEST); glClearColor (0.5, 0.5, 0.5, 1); glClear (GL_COLOR_BUFFER_BIT); glActiveTexture (GL_TEXTURE0); glBindTexture (GL_TEXTURE_2D, m_tex1); glActiveTexture (GL_TEXTURE1); glBindTexture (GL_TEXTURE_2D, m_tex2); GLint m_tex1_loc, m_tex2_loc; m_tex1_loc = m_program->uniformLocation ("texx1"); m_tex2_loc = m_program->uniformLocation ("texx2"); m_program->setUniformValue (m_tex1_loc, m_tex1); m_program->setUniformValue (m_tex2_loc, m_tex2); glDrawArrays (GL_QUADS, 0, 20); m_program->disableAttributeArray (0); m_program->disableAttributeArray (1); m_program->release (); glBegin (GL_TRIANGLES); glVertex3f (0., 1, 0); glVertex3f (1., 0, 0); glVertex3f (1., 1, 0); glEnd (); glDisable (GL_TEXTURE_2D); qDebug () << m_program->log ();
The texture colors are always black, tex1 and and tex 2 are valid images. The code print in console:
tex1 sizes 390 520 tex2 sizes 390 520
Which is the problem?
-
There could be quite a few thing wrong. Could you try using power-of-two textures to see if the images start showing (resize the images to a 512x512 or 256x256) and ensure the image is completely defined?
https://www.opengl.org/wiki/Common_Mistakes#Creating_a_complete_textureIt would help if you provided code for the entire application.