(Solved) [QT 5.2 OpenGL] Unable to draw triangles
-
Hi everyone,
I'm just stuck trying to draw triangles using QT5.2 and OpenGL 4.3.
Simply nothing is drawn on screen, I get backgroundColor only.I have an OpenGLViewport class derived from QWindow, and in its constructor I do:
@
// Tell Qt we will use OpenGL for this window
setSurfaceType( OpenGLSurface );// Specify the format we wish to use QSurfaceFormat format; format.setDepthBufferSize( 24 ); format.setMajorVersion( 4 ); format.setMinorVersion( 3 ); format.setSamples( 4 ); format.setProfile( QSurfaceFormat::CoreProfile ); setFormat( format ); create(); // Create an OpenGL context pContext = new QOpenGLContext; pContext->setFormat( format ); pContext->create(); // Setup our scene pContext->makeCurrent( this );
//init OpenGL
initializeGL();// Make sure we tell OpenGL about new window sizes connect( this, SIGNAL( widthChanged( int ) ), this, SLOT( resizeGL() ) ); connect( this, SIGNAL( heightChanged( int ) ), this, SLOT( resizeGL() ) ); resizeGL();
@
My initializeGL method is the following:
@
this->pContext->makeCurrent( this );this->pOGLFunctions = pContext->versionFunctions<QOpenGLFunctions_4_3_Core>();
if ( !pOGLFunctions )
{
qFatal("Requires OpenGL >= 4.3");
exit( 1 );
}
this->pOGLFunctions->initializeOpenGLFunctions();//backgroundColor
this->pOGLFunctions->glClearColor( 0.25f, 0.25f, 0.25f, 1.0f );
// Enable depth testing
this->pOGLFunctions->glEnable( GL_DEPTH_TEST );
this->pOGLFunctions->glEnable( GL_CULL_FACE );
@this is what I'm doing to render triangles (it is a bit rough, I know, but it is just a test):
@
// Make the context current
this->pContext->makeCurrent( this );//test code
// Shader sources
const GLchar* vertexSource =
"#version 150 core\n"
"in vec2 position;"
"in vec3 color;"
"out vec3 Color;"
"void main() {"
" Color = color;"
" gl_Position = vec4(position, 0.0, 1.0);"
"}";
const GLchar* fragmentSource =
"#version 150 core\n"
"in vec3 Color;"
"out vec4 outColor;"
"void main() {"
" outColor = vec4(Color, 1.0);"
"}";// Create Vertex Array Object
GLuint vao;
this->pOGLFunctions->glGenVertexArrays(1, &vao);
this->pOGLFunctions->glBindVertexArray(vao);// Create a Vertex Buffer Object and copy the vertex data to it GLuint vbo; this->pOGLFunctions->glGenBuffers(1, &vbo); GLfloat vertices[] = { -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // Top-left 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Top-right 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // Bottom-right -0.5f, -0.5f, 1.0f, 1.0f, 1.0f // Bottom-left }; this->pOGLFunctions->glBindBuffer(GL_ARRAY_BUFFER, vbo); this->pOGLFunctions->glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
err += glGetError();
// Create an element array
GLuint ebo;
this->pOGLFunctions->glGenBuffers(1, &ebo);GLuint elements[] = { 0, 1, 2, 2, 3, 0 }; this->pOGLFunctions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); this->pOGLFunctions->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); // Create and compile the vertex shader GLuint vertexShader = this->pOGLFunctions->glCreateShader(GL_VERTEX_SHADER); this->pOGLFunctions->glShaderSource(vertexShader, 1, &vertexSource, NULL); this->pOGLFunctions->glCompileShader(vertexShader); // Create and compile the fragment shader GLuint fragmentShader = this->pOGLFunctions->glCreateShader(GL_FRAGMENT_SHADER); this->pOGLFunctions->glShaderSource(fragmentShader, 1, &fragmentSource, NULL); this->pOGLFunctions->glCompileShader(fragmentShader); // Link the vertex and fragment shader into a shader program GLuint shaderProgram = this->pOGLFunctions->glCreateProgram(); this->pOGLFunctions->glAttachShader(shaderProgram, vertexShader); this->pOGLFunctions->glAttachShader(shaderProgram, fragmentShader); this->pOGLFunctions->glBindFragDataLocation(shaderProgram, 0, "outColor"); this->pOGLFunctions->glLinkProgram(shaderProgram); this->pOGLFunctions->glUseProgram(shaderProgram); // Specify the layout of the vertex data GLint posAttrib = this->pOGLFunctions->glGetAttribLocation(shaderProgram, "position"); this->pOGLFunctions->glEnableVertexAttribArray(posAttrib); this->pOGLFunctions->glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0); GLint colAttrib = this->pOGLFunctions->glGetAttribLocation(shaderProgram, "color"); this->pOGLFunctions->glEnableVertexAttribArray(colAttrib); this->pOGLFunctions->glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat))); // Clear the screen to black glClearColor(1.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // Draw a rectangle from the 2 triangles using 6 indices glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//end test code
// Swap front/back buffers this->pContext->swapBuffers( this );
@
I was using DX11 before swapping to OpenGL and I still feel a bit confused. I'm sure I'm doing something wrong, but can't find the problem: what I got is only the backgroundColor (I tested animating it also, and it works...)
The only clue I have is that if I look at my pContext pointer during execution, its qGLContextHandle is 0x0000...0.Thanks!
-
Nevermind,
I found this working code and modified mine following it:
http://qt-project.org/wiki/Using-QOpenGLFunctions-and-QOpenGLContextthanks anyway