When inheriting from QOpenGLWidget
and QOpenGLFunctions_4_5_Core
, if I call glActiveTexture(GL_TEXTURE1)
(or any other non-zero number) before calling update()
textures appear completely black on the screen.
Relevant code snippets:
void OpenGLWidget::paintGL() {
if (fp) {
// First paint seems to always happen before the first update
qDebug() << "First Paint";
fp = false;
}
glClear(GL_COLOR_BUFFER_BIT);
unsigned int tex;
glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
QImage img = QImage("textures/awesomeface.png").convertToFormat(QImage::Format_RGBA8888).mirrored();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width(), img.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img.bits());
glTextureParameteri(tex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(tex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTextureParameteri(tex, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTextureParameteri(tex, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glUseProgram(frame_shader.get_id());
glBindVertexArray(frame_vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
// Clean up
glDeleteTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);
// This doesn't cause the textures to be black
glActiveTexture(GL_TEXTURE1);
}
And the mainloop function:
void OpenGLWidget::main_loop() {
if (fu) {
qDebug() << "First Update";
fu = false;
}
// This causes the textures to completely black
// Change GL_TEXTURE1 to GL_TEXTURE0 (or get rid of it glActiveTexture
// completely) and textures will work once again
glActiveTexture(GL_TEXTURE1);
update();
}
The shaders can still draw to the screen; if I set a solid color in the fragment shader, the color will properly display to the screen. It seems only user-made textures are turning completely black.
I'm not getting any OpenGL Errors.
The full MRE can be found at: https://github.com/Luminic/MRE_QOpenGLWidget_glActiveTexture_GL_TEXTURE1_before_update
Does anyone know what is causing this odd behavior?