Reputation: 761
So I've been toying around with OpenGL under QML and have been looking at the supplied example file of the same name. I kind of understand how it's working but here's the thing: I tried to replace the OpenGL Shader Program that was in the paint() function of the example with my own very basic Open GL stuff. However I was unable to get anything visible on the screen. The only thing I was able to change was the color of the background. So I'm wondering how do I set up the viewport, the camera, and whatever is needed to have something on the screen. I have some (very rusty) experience on OpenGL but in the past there's always been things like freeglut that makes life a bit easier. Any pointers or examples (something I can put in the paint() method to observe and learn from) to the right direction would be much appreciated...
Edit: So here's what I have in the paint() method:
void QtOpenGLViewRenderer::paint()
{
// The following two lines fixed the problem
QOpenGLFunctions glFuncs(QOpenGLContext::currentContext());
glFuncs.glUseProgram(0);
glViewport(0, 0, m_viewportSize.width(), m_viewportSize.height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.2, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
GLfloat triangle[] = {
0.25f, 0.25f, 0.0f,
0.75f, 0.25f, 0.0f,
0.25f, 0.75f, 0.0f
};
GLfloat colors[] = {
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f
};
glVertexPointer(3, GL_FLOAT, 0, triangle);
glColorPointer(4, GL_FLOAT, 0, colors);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glShadeModel(GL_SMOOTH);
glDrawArrays(GL_TRIANGLES, 0, 3);
glFlush();
}
All I see is the dark reddish background but no sign of the triangle. Why is this?
Upvotes: 0
Views: 1002
Reputation: 761
Answering my own question for the record. The problem was that Qt by default is expecting new OpenGL Shading Language (GLSL) style instructions. To get old style OpenGL instructions to work you need to tell Qt that you're going to use them instead of a program defined by shaders. This is done by issuing the following commands:
QOpenGLFunctions glFuncs(QOpenGLContext::currentContext());
glFuncs.glUseProgram(0);
For these to work you also need to include the following headers:
#include <QOpenGLFunctions>
#include <QOpenGLContext>
Upvotes: 1