Sign in to follow this  
HermanssoN

OpenGL rotating stuff in OpenGL using quaternions

Recommended Posts

Hello. I'm trying to rotate stuff in OpenGL using quaternions. I made a camera that works nice, but when I'm trying to rotate using glRotatef I'm getting wrong results. I was not sure if I should put this in the GL forum or the math forum. anyways, what I do is: on every frame. Quaternion Q; Q = Quaternion.FromAxisAngle(Vector3.UnitY, 0.005f); m_Rotation = Quaternion.Mult(Q, m_Rotation ); Normaly I use my own quaternion class but now I use OpenTKs quaternions to be sure that the math is correct when it is time to render. float Angle; Vector3 Axis; m_Rotation .ToAxisAngle(out Axis, out Angle); Gl.glRotatef(Angle, Axis.X, Axis.Y, Axis.Z); The result is not correct, the cube in this case, starts to rotate correctly but after a litle while, the Axis vector flipps from Y= 1 to Y = -1. And i don't know why :/ The most anoying thing is that my camera use tha same code for rotation. and it works like a charm. the difference is that i use gluLookAt so i don't have to use glRotatef. I have functions for extracting Right,At and Up from a quatenrion. I't would be nice if there where an example somewhere, like the spinning trinagle, that used quaternions.

Share this post


Link to post
Share on other sites
Here are a few things you might double-check:

1. Make sure you're not missing a radians<->degrees conversion.

2. If you're not doing this already, you should normalize m_Rotation after it's updated to compensate for numerical drift.

3. If the *observed* behavior is correct, but if you're just concerned because you're getting an axis with y = -1 rather than y = 1, it's probably just an aliasing issue (remember that [angle, 0, 1, 0] and [-angle, 0, -1, 0] represent the same rotation).

Not all of these would explain the behavior you're seeing, but it's probably worth double-checking these things anyway.

Also, although the axis-angle method should work, you could instead use glMultMatrixf() and see if that makes any difference (that is, build a matrix using the object position and the direction vectors as extracted from the quaternion, and submit it to OpenGL directly).

Share this post


Link to post
Share on other sites
Thnx for reply.
I just can't find the problem!

So i'm going to post the whole code snippet, I removed all but the vital code:


public class ECRenderer
{

private Quaternion m_Rot;
public ECRenderer()
{

m_Rot = Quaternion.Identity;
}


public void Render()
{
RotateY();
Vector3 Axis;
float Angle;

m_Rot.ToAxisAngle( out Axis, out Angle );
Gl.glRotatef(Angle, Axis.X, Axis.Y, Axis.Z);

Gl.glBegin(Gl.GL_TRIANGLES);

Gl.glColor3f(1, 0, 0);
Gl.glVertex3f(0, 20, 20);
Gl.glColor3f(0, 1, 0);
Gl.glVertex3f(8, 0, 20);
Gl.glColor3f(0, 0, 1);
Gl.glVertex3f(-8, 0, 20);

Gl.glEnd();


}
public void RotateY()
{
//Will be unit length, OpenTK return normalized
Quaternion Q2 = Quaternion.FromAxisAngle(Vector3.UnitY, 0.005f);
//Don't realy need to make a copy when pasing by ref, but just to rule it out
Quaternion Q1 = m_Rot;
Quaternion.Mult(ref Q1, ref Q2, out m_Rot);

m_Rot = Quaternion.Normalize(m_Rot);
}

}


This should, In my opinion, rotate the Triangle. The result is like before, it rotates like it should for a short while until that Axis flip. Worth mentioning is that Angle goes up to around 5 and than down again.

It feels like there is something I'm missing, or rather there must be :/

Share this post


Link to post
Share on other sites
Did you check all of the things I mentioned in my previous post? Have you confirmed that none of them is the problem?

Share this post


Link to post
Share on other sites
What you see there is all that happens.

there are no conversion from degrees to radians.

Quaternion.FromAxisAngle(Vector3.UnitY, 0.005f) return a normalized Quaternion

all that happens is

ECRenderer R = new ECRenderer();
while( !quit )
{
Update();//Check is 'Q' is pressed, if so quit
R.Render();
}


Share this post


Link to post
Share on other sites
radians

source from OpenTK:

public void ToAxisAngle(out Vector3 axis, out float angle)
{
Vector4 result = ToAxisAngle();
axis = result.Xyz;
angle = result.W;
}

/// <summary>
/// Convert this instance to an axis-angle representation.
/// </summary>
/// <returns>A Vector4 that is the axis-angle representation of this quaternion.</returns>
public Vector4 ToAxisAngle()
{
Quaternion q = this;
if (q.W > 1.0f)
q.Normalize();

Vector4 result = new Vector4();

result.W = 2.0f * (float)System.Math.Acos(q.W); // angle
float den = (float)System.Math.Sqrt(1.0 - q.W * q.W);
if (den > 0.0001f)
{
result.Xyz = q.Xyz / den;
}
else
{
// This occurs when the angle is zero.
// Not a problem: just set an arbitrary normalized axis.
result.Xyz = Vector3.UnitX;
}

return result;
}



Share this post


Link to post
Share on other sites
Holy hell, this is awkward.

just checked up glRotatef, and yes..it's in degrees..

Nice to know i got my math right, to bad I'm stupid :)

Thnx for your help!!

[Edited by - HermanssoN on March 15, 2009 4:16:32 PM]

Share this post


Link to post
Share on other sites
Quote:
It's the normal glRotatef, so i gues radians.
All OpenGL implementations that I'm aware of use degrees.
Quote:
But eaven if it was degrees, the only thing that would happe is that the rotation would be very slow, right?
Not necessarily - depending on how periodicity and so on is handled by OpenGL and by your application, leaving the conversion out could lead to fairly unpredictable results.

Anyway, I'd try adding the conversion - it probably needs to be in there anyway, and who knows, it might fix the problem :)

Share this post


Link to post
Share on other sites
Quote:
Original post by HermanssoN
It's the normal glRotatef, so i gues radians.

So, basically, you're using a function without knowing what parameters it takes? Knowledge is better than guesses, so check some documentation.

Share this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob
Quote:
Original post by HermanssoN
It's the normal glRotatef, so i gues radians.

So, basically, you're using a function without knowing what parameters it takes? Knowledge is better than guesses, so check some documentation.


Well yes...I just asumed radians was a standard.

I also asumed it after reading this:
Quaternion to Axis Angle

To change a quaternion to a rotation around an arbitrary axis in 3D space, we do the following:

If the axis of rotation is (ax, ay, az)
and the angle is theta (radians)
then the angle= 2 * acos(w)

ax= x / scale
ay= y / scale
az= z / scale

where scale = sqrt (x2 + y2 + z2)

from a GD article, Quaternion powers, http://www.gamedev.net/reference/articles/article1095.asp

But again thanks for your time

Share this post


Link to post
Share on other sites
I't sure did.

I learnt that quaternions can't be rotated several revolutions. That's why I got the messed up rotation instead of a very slow one.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Forum Statistics

    • Total Topics
      627638
    • Total Posts
      2978330
  • Similar Content

    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
    • By cebugdev
      hi guys, 
      are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
      Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic 
      let me know if you guys have recommendations.
      Thank you in advance!
    • By dud3
      How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below? 
      Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
      Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.
       
      References:
      Code: https://pastebin.com/Hcshj3FQ
      The video shows the difference between blender and my rotation:
       
    • By Defend
      I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
      My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
      * make lots of VAO/VBO pairs and flip through them to render different objects, or
      * make one big VBO and jump around its memory to render different objects. 
      I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
      If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?
       
    • By test opty
      Hello all,
       
      On my Windows 7 x64 machine I wrote the code below on VS 2017 and ran it.
      #include <glad/glad.h>  #include <GLFW/glfw3.h> #include <std_lib_facilities_4.h> using namespace std; void framebuffer_size_callback(GLFWwindow* window , int width, int height) {     glViewport(0, 0, width, height); } //****************************** void processInput(GLFWwindow* window) {     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)         glfwSetWindowShouldClose(window, true); } //********************************* int main() {     glfwInit();     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);     //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);     GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);     if (window == nullptr)     {         cout << "Failed to create GLFW window" << endl;         glfwTerminate();         return -1;     }     glfwMakeContextCurrent(window);     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))     {         cout << "Failed to initialize GLAD" << endl;         return -1;     }     glViewport(0, 0, 600, 480);     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);     glClear(GL_COLOR_BUFFER_BIT);     while (!glfwWindowShouldClose(window))     {         processInput(window);         glfwSwapBuffers(window);         glfwPollEvents();     }     glfwTerminate();     return 0; }  
      The result should be a fixed dark green-blueish color as the end of here. But the color of my window turns from black to green-blueish repeatedly in high speed! I thought it might be a problem with my Graphics card driver but I've updated it and it's: NVIDIA GeForce GTX 750 Ti.
      What is the problem and how to solve it please?
  • Popular Now