Reputation: 1582
I was following a tutorial on youtube on how to use OpenGL within Qt. I have two classes:
glWidget -> Inherits from QGLWidget; GLWindow -> Inherits from QMainWindow and has a form.
Within GLWindow's form I place a Widget and promote it to glWidget so that whatever is rendered on glWidget is displayed through Widget on GLWindow.
I implement the following inherited functions in glWidget:
void glWidget::initializeGL()
{
glClearColor(0.1, 0.1, 0.1, 1.0);
}
void glWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(0.2, 0.8, 0.1);
glVertex3f(-0.5, -0.5, 0.0);
glColor3f(0.8, 0.3, 0.9);
glVertex3f(0.5, -0.5, 0.0);
glColor3f(0.1, 0.1, 0.5);
glVertex3f(0.0, 0.5, 0.0);
glEnd();
}
I plan on using shaders and buffers later, but I'm puzzled as to why my rendered primitive appears like this:
It appears the triangle has been transformed in world space, rather than local space. If it were in local space then the triangle would appear at the centre of the screen. The tutorial I'm referring to can be found here: https://www.youtube.com/watch?v=1nzHSkY4K18
I don't know what I'm doing wrong here. At first I thought it had something to do with my vertices, and I know this is immediate mode and should probably try with buffers/shaders instead; but I'd still like some insight as to what I'm doing wrong here. Why isn't the triangle being rendered as it appears in the tutorial? Should I be passing something in the constructor of glWidget or something?
Upvotes: 0
Views: 180
Reputation: 4763
When extending QGLWidget keep in mind that resizeGL() will get called automatically :
resizeGL() - Sets up the OpenGL viewport, projection, etc. Gets called whenever the widget has been resized (and also when it is shown for the first time because all newly created widgets get a resize event automatically).
It seems that in your case the Widget gets resized automatically, so a solution would be to just override it and let it empty.
When extending QGLWidget is usually a good practice to also override the resizeGL function.
Upvotes: 2