Q.setFromAxisAngle(new Vector4f(0,0,1,(float)Math.PI*.25f)) I only use it with pure axes to repeat yaw, pitch and roll note: I'm not sure how well Quaternion included works with various arbitrary axes x,y,z,w : x,y,z = axis normal, w = angle in radians There aren't currently any tutorials for 'qpq*' or ones that take the stuff in Wikipedia and make it practical for a computer science type.Ĭode: // create a quaternion to rotate 45 degrees on the z-axis (visually, rolling the points around the origin)
Java lwjgl 2 rotate about axis how to#
You could also do this outside the shader, effectively meaning you wouldn't need a shader, but in my view you want to make the card do the work it is specialized at doing (matrix math!)īy the way, I think I know how to modify the Quaternion code to actually be, um, useful for what Quaternions are supposed to be used for. Note that once you have the rotation matrix, you can simply multiply the vertex by it to rotate it in the shader. But yes, at the moment, you do have to write your own code. When I get home I'll look over what I have and share some. When I came to this problem, I wrote some code to create 4x4 matricies which could be used to translate and rotate points in the vertex shader. There is quaternion code in the pack, but it's not quite set up to be used to code rotation code. Opengl calls are in the reverse order as they appear : My definition of the order of the three common transform operations Scaling, Rotation, Translation is likewise : S x T x R x v. (otherwise it's linear algebra : things are a bit different, because it must feature the orthonormal basis change)
This supposes that the object is defined with vertices around the origin, such as the triangle -1,-1,0 1,-1,0 0,1,0. If the current modelview/transform matrix is C and the object has the vertices v0,v1,v2,v3 then multiply C with the rotation transform glRotate(), then translate with gltranslate() replace C with C x Translate x Rotate before to send v* vertices. Thereby one would rotate an object in the z-axis by FIRST rotating the z THEN by translating to the location where he wants the object to stand. According the glmultmatrix() specs, the current transform matrix C is replaced with the matrix C x M where M is the multiplied transformation matrix. That is with opengl like a simple matrix multiplication. To do correct transformations, such as a scaling, rotation, translation, you have to know how the modelview is modified by each of the gl* calls. Its not advisable to use the push and pop and to instead pre work out your final positions or to have a shader calculate the final position no the fly.
This is of course using opengl 1.0 type architecture. This effectively saves the point and rotation you are at lets you do something then go back to the point for further rendering. To get round this I can then push and then pop the matrix. If we make it more complicate and we want to render two triangles we have the same problem (remember opengl is state based). If I run glLoadIndentity() at the beginning of each render cycle I reset my starting point back to (0,0,0) with no rotation and I draw exactly what I drew before. I am just moving from the place where I finished the previous frame. In the first frame everything will look good, but in the second frame my starting point is now not (0,0,0) with no rotation it (0,10,0) with 90 degrees rotation.
The triangle I want to move 10 places up in the y direction and rotate 90 degrees in the z axis Here is a simple example, I have a triangle to render.