1
\$\begingroup\$

I've been working on an OpenGL program that simply renders a square that rotates in 3D space. The square also has a texture applied to both sides. Here you can see an example screenshot of the program running:

enter image description here

I used very little abstraction in that project and most of the code was in main.cpp (apart from the shader loading and usage).

After finishing this project, I wanted to improve it as much as I could with my current knowledge. I started abstracting away functionality. The final result was a very clean main.cpp file with multiple classes (Mesh, Shader, Resource Manager and Window classes).

Apart from the texture, everything should functionally be the same, but I experienced the following after using the rotate function; left is the abstracted version of the first project, right is the first project:

enter image description here

The square in the new project rotates at a much higher speed than in the previous project. The implementation is the same, as in, the degrees that the square should rotate around the y-axis are the same in both projects - the time the program has been running, which is gotten through GLFW's glfwGetTime().

I checked nearly everything. The .lib files are linked the same way between the projects. There are no differences between the vertex data. Both projects are running in DEBUG mode.

The only difference I could think of is the texture not being applied in the second project, but I recall that the textureless version of the first project was still running with the same speed as it is currently. But rendering a 64x64 texture on a square shouldn't be that expensive to run.

NOTE: I'm using GLM for the matrices and for the rotation transformation.

Does abstracting away all of OpenGL's initializing and buffer code improve performance by that much?

I'm using GLFW for window and context creation, and thus using (float)glfwGetTime() for the angle of rotation along the y-axis.

The second project basically moves over all functionality into categorized classes. So everything to do with meshes was moved to a Mesh class, loading of GLSL shaders and textures was moved over to a ResourceManager class etc. So all functionality should be the same.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Could you elaborate on "abstraction" ? I mean, I know what it means, but if you do it properly, the result should be the same, since in your case it is not, obviously there's something wrong with the code or the idea behind it. Without more information I doubt anyone can answer this accurately. \$\endgroup\$
    – TomTsagk
    Sep 17, 2018 at 15:40
  • \$\begingroup\$ @TomTsagk I will elaborate as soon as I can, but basically all functionality should be the same. Even cleaning up buffers, window, context etc. is the same. I'll provide much more detail and if I can, source code on GitHub. \$\endgroup\$
    – user119544
    Sep 17, 2018 at 15:45
  • 2
    \$\begingroup\$ sounds like a bug on your end created during refactor. Most likely not resetting a transformation matrix to identity. \$\endgroup\$ Sep 17, 2018 at 16:01

1 Answer 1

3
\$\begingroup\$

As @ratchetfreak commented, I hadn't reset the model matrix to the identity matrix in the render loop. This was indeed omitted from the remade project.

The matrix needs to be reset each frame to the identity matrix because each frame it stores a new transformation (in this situation a rotation) in the matrix, so when the next frame a new rotation transformation is applied, it's applied to all the previous frames' rotations. So for example, on frame #5 the matrix is in a form that stores the rotations of frames 1-4 and the 5th rotation is applied on top of all those.

The resulting effect is a sort of 'exponential' increase in the speed of the rotation, before slowing down for a second and speeding back up again.

Again, thank you @ratchetfreak :)

(I'll accept this as the answer when the site allows me.)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I notice a down-vote on this answer, but I'm not sure why. While it's short, it clearly identifies the source of the issue with enough specificity that a user experiencing a similar problem could find the analogous error in their own implementation. The answer could be even better if it described why failing to reset the model matrix to identity causes the excessively fast rotation observed, but I don't think it's "not useful" in its current form. \$\endgroup\$
    – DMGregory
    Sep 17, 2018 at 17:01
  • \$\begingroup\$ @DMGregory I've explained what I think is happening in the solution now. Hopefully it helps others understand how the problem occurred. \$\endgroup\$
    – user119544
    Sep 17, 2018 at 18:46

You must log in to answer this question.