[Cube disappears when I declare the modelview matrix as a member variable in Qt6]

Hello Guys,

I hope you’re doing well.
I don’t really know wether this is question related to OpenGL or I should post it on a Qt forum but I suspect that it has something to do with some OpenGL transformations.

So in my paintGL() function, I declared modelview matrix as a local variable, applied some transformation using lookAt function, translate and rotate.

But while working on my application, I needed to declare the modelview matrix as a member variable; as an attribute of the class MainWidget. [ it is no longer a local variable in paintGL() function]

I didn’t change anything in my paintGL() function and compiled the app.

I expected to see the cube in my scene with its coordinate system; however All I got was an empty black scene; it seems like the cube has disappeared.

I’m so confused and I’m clueless about how to deal with this because I don’t really know if it’s an OpenGL problem or a Qt.

I really would appreciate it if someone can help me or guide me or tell me what I should do.
Thank you.

Based upon the limited information available, I suspect that you’re accumulating each frame’s transformation onto the existing transformation.

If you create the matrix as a local variable in the paintGL method, it will be initialised to an identity matrix each time. If you make it a member variable, it will be initialised once upon construction rather than each frame.

1 Like

Thank you for the quick reply.

Okay, but I still don’t get why the cube disappears from the scene when I declare the modelview matrix as a member variable.

I have a constructor in which I intialized the modelview matrix ( the member variable) ; I mean I set it to identity, although I know already the matrix is an identity matrix; it doesn’t need to be initialized.

But that initialization only happens once. When it was a local variable, it happened every time you redrew the scene. When it is a member variable, it stores its value, so every time you start to redraw the scene, it still has whatever value it had at the end of the previous scene redraw.

You need to reset it at the start of the scene redraw.

1 Like

Most matrix operations (lookAt, translate, rotate) are cumulative: they concatenate a transformation to the existing transformation. If you don’t reset the matrix to an identity matrix at the start of each frame, the transformations will accumulate.

1 Like

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.