I am trying to transfer an OpenGL code from GLFW to Qt, in order to integrate it with a larger project. But not matter what I have tried, I have not been able to make a texture show up and all I get is a black screen.
In Qt, the program compiles with no errors and I have managed to draw a multi-color triangle before attempting to insert textures.
I have tried using the Qt OpenGL Objects for shaders, shader programs, textures, and re-write code based on their examples but still nothing.
Below is a simple Qt implementation that attempts to display a texture, which results in black screen. An exact GLFW equivalent works fine:
CustomViewGL::CustomViewGL(QWidget *parent): QOpenGLWidget(parent)
{
QSurfaceFormat format;
format.setMajorVersion(3);
format.setMinorVersion(3);
format.setProfile(QSurfaceFormat::CoreProfile);
setFormat(format);
}
void CustomViewGL::initializeGL()
{
glewExperimental=true;
if (glewInit() != GLEW_OK)
{
qDebug() << "Failed to initialize GLEW";
}
std::string vertexShaderSource =
" #version 330 core\n "
" "
" layout (location = 0) in vec3 aPos; "
" layout (location = 1) in vec2 aTexCoord; "
" "
" out vec2 TexCoord; "
" "
" void main() "
" { "
" "
" TexCoord = aTexCoord; "
" gl_Position = vec4(aPos, 1.0); "
" "
" }\0 ";
std::string fragmentShaderSource =
" #version 330 core\n "
" "
" in vec2 TexCoord; "
" out vec4 FragColor; "
" uniform sampler2D tex; "
" void main() "
" { "
" "
" FragColor = texture(tex, TexCoord); "
" "
" }\0 ";
// Variables
unsigned int shaderVertex, shaderFragment, shaderProgram;
....
....
// Compile Shaders //
...
...
glUseProgram(shaderProgram);
// Vetrices
float rectangle[] = {
// positions // texture coords
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top right
1.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, // bottom left
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
QImage texture = QImage("wall.jpg").convertToFormat(QImage::Format_RGB888);
// Variables
unsigned int VAO, VBO, EBO, tex;
// VAO, VBO, Attributes Configuration
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(rectangle), rectangle, GL_STATIC_DRAW);
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// Texture
glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
float borderColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture.width(), texture.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, texture.bits());
glGenerateMipmap(GL_TEXTURE_2D);
}
void CustomViewGL::paintGL()
{
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*) 0);
}