# Using PyOpenGL, can I rotate raw data rather than the current matrix?

I'm trying to create a sort of PyOpenGL renderer for the Bullet physics engine, and at the moment I'm rotating my basic cubes with math in Python. However, if I do the math in Python that's going to take more time than doing the math with OpenGL.

OpenGL has the ability to rotate a matrix using glRotatef(), but it only rotates the "current matrix". I'm not really interested in convoluting my render loop with an individual check for each object's rotation and position if I don't have to.

So my question is, is there a way I can, for example, load a matrix of points into OpenGL, rotate this matrix and then read it back and give it to my render loop? Or should I instead look for an alternative 3D math library? (Or for that matter, is running the code in python really all that laggy?)

• It seems like you are doing things the old, fixed-function way with calls like glRotatef(). What you probably are looking for are Vertex Buffer Objects (VBOs). These would allow you to pass in an array of transformation (model-view) matrices. You could then use a shader program to take that attribute data and calculate the transforms on the GPU instead. – CodeSurgeon Jul 29 '18 at 3:26
• Actually, I'm not even using glRotatef(), I'm using math in Python that I found on the internet to rotate my cubes. Currently, I'm moving towards VBOs in order to speed up rendering my game terrain, is there a way to rotate parts of a VBO? – C1ff Jul 30 '18 at 16:20
• There are several approaches to rotating a part of a VBO. If you have the chance, you might want to take a look at this video for some ideas (it uses javascript and webgl, but it could give you some ideas to start with). I will try to write up a more thorough answer later today in the meantime. Also, what math library are you using? If you are doing this in python, I can vouch that it will be slow. I have written some math modules in cython that I wouldn't mind cleaning up and sharing, since numpy is not very convenient to use for 3d math. – CodeSurgeon Jul 31 '18 at 19:35
• I've actually been using just the math library and making matrices out of lists. – C1ff Jul 31 '18 at 20:13
• Wrote up an answer as a stream of consciousness. Please let me know if there are any parts that need to be clarified or are worded in a confusing manner! – CodeSurgeon Jul 31 '18 at 23:32

First of all, it is good that you appear to be on the right track with using VBOs! In general, it is a good idea when working with the GPU to pass as much data to the GPU at once as is possible. This means minimizing the number of draw calls (i.e. send a whole bunch of cubes in a single draw call rather than drawing one cube at a time with glDrawArrays or worse, glVertex).
• There is first the issue of Python's memory model. In python, everything, even primitives such as integers, are all bulky objects built from a basic PyObject struct rather than direct, native machine types. This can make accessing the data of the underlying objects slow as a result, with even apparently simple operations such as adding two numbers invoking lots of operations behind the scenes in the python interpreter. For fast math operations, this is not conducive to good performance, and there is generally little to gain from the safety features (such as avoiding overflow) that are offered from this additional layer.