title: How to rotate objects in OpenGL relative to local or global axes
body: <p>I'm asking a question that has been asked a million times before, but I still haven't found a good answer after going through these and also resorting to other sites:</p>
  
  <p><a href="https://stackoverflow.com/questions/16246960/how-to-rotate-a-graphic-over-global-axes-and-not-to-local-axes">How to rotate a graphic over global axes, and not to local axes?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/9559333/rotating-objects-in-opengl">rotating objects in opengl</a></p>
  
  <p><a href="https://stackoverflow.com/questions/10374063/how-to-rotate-vertices-exactly-like-with-glrotatef-in-opengl">How to rotate vertices exactly like with glRotatef() in OpenGL?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/3596675/rotate-object-about-3-axes-in-opengl">Rotate object about 3 axes in OpenGL</a></p>
  
  <p><a href="https://stackoverflow.com/questions/26293055/rotate-an-object-on-its-own-axes-in-opengl">Rotate an object on its own axes in OpenGL</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7724840/is-it-possible-to-rotate-an-object-around-its-own-axis-and-not-around-the-base-c">Is it possible to rotate an object around its own axis and not around the base coordinate&#39;s axis?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/9702867/opengl-rotation-local-vs-global-axes">OpenGL Rotation - Local vs Global Axes</a></p>
  
  <p>Task is relatively simple, I am making an sensor module with accelerometer and gyro onboard, and I need to display rotation.</p>
  
  <p>I have a 3D model of the object in STL format, I've made an STL parser and I can visualise the object perfectly well.</p>
  
  <p>When I try to rotate it I get all kinds of strange results.</p>
  
  <p>I am using SharpGL (the Scene component).</p>
  
  <p>I tried to write an "effect" that (for now) is controlled with 3 separate trackbars, but this will be the data from the sensors.</p>
  
  <p>I wanted to do it as an effect because (in theory) I could have more objects in the scene and I only need to rotate a specific one (and also for educational purposes as I am relatively new to OpenGL).</p>
  
  <p>I tried to rotate the object using quaternions, multiplying them and generating a rotation matrix, then applying <code>gl.MultMatrix(transformMatrix.AsColumnMajorArray)</code>.</p>
  
  <p>I also tried <code>gl.Rotate(angleX, 1, 0, 0)</code> (and similar for Y and Z axes).</p>
  
  <p>Depending on the order of quaternion multiplication or <code>gl.Rotate(...)</code> statements, I would get the object to rotate about the local axis (first), something weird (second), and the "world" third. For example, if I did it with quaternions <code>qX*qY*qZ</code> and got the rotation matrix from that, I would get (I think) the rotations about a local X, "arbitrary" Y, and the world Z axes.</p>
  
  <p>The part I cannot understand is that when I apply <code>gl.Rotate(dx, 1, 0, 0)</code> (and others for Y and Z rotations) inside the trackbar's <code>Scroll</code> event, the entire scene rotates about the X, Y, and Z axes and in that order, and exactly as I expected the rotation to happen.</p>
  
  <p>My only issue with this approach is that the entire scene rotates. So if I (hypothetically) wanted to draw a room around the object, the whole room would rotate too, which is not what I want.</p>
  
  <p>I will post any code requested, but I feel there is too much and the aforementioned links have similar code to what I have.</p>
  
  <p>At the end of the day, I want my object (<code>Polygon</code> in SharpGL terms) to rotate about its own axes (or about the "world" axes, but be consistent).</p>
  
  <p>EDIT:</p>
  
  <p>I've placed the project in my dropbox: <a href="https://goo.gl/1LzNhn" rel="nofollow noreferrer">https://goo.gl/1LzNhn</a></p>
  
  <p>If someone wants to look at it, any help will be greatly appreciated.</p>
  
  <p>In the <code>MainForm</code>, the TbRotXYZScroll function has trackbar code and the Rotation class is where the problem resides (or so I think).</p>
  
  <p>Rotate X, Y, and Z by say 45 degrees in that order. All seems to work great. Now rotate X, Y, and Z further by another 30 or so degrees, try to visualise about which axis the rotation is happening as you rotate...<br/>
  X seems to be always world and Z seems to be always local, while Y is something in between (changing the order of rotation changes this).</p>
  
  <p>Going into TbRotXYZScroll function and changing method to 2 does what I want (except the rotation of the entire scene). Tried quaternions and they produce the exact same result, so <strong><em>I must be doing something wrong</em></strong>...<br/>
  Changing it to 1 produces similar result, but that is applying the rotation to the polygons alone (not what's being rendered as that includes local coordinate axes too).</p>