Tyler Wall
Tyler Wall

Reputation: 3788

OpenGL direction of a vector in degrees (X, Y, Z)

I have an object traveling along a 3D vector in terms of X, Y, Z... I need to rotate the object according to each axis (x-axis, y-axis, and z-axis).

How do I get these measurements in terms of degrees?

(I am using OpenGL and only know of glRotatef(...)) [glRotatef(...) documentation here] Looking at this question, the answer gives

viewvector =<x, y, z>

r = sqrt(x² + y² + z²)

phi = arctan2(y, x)

theta = arccos(z / r)

but from this wiki page I understand that:

[Edit from Ignacio Vazquez-Abrams]

phi => angle around Z-axis

theta => angle between x/y plane

but how do I find Y? or do I need to?

The real question is, How do I represent this in terms of glRotatef(...)?

Upvotes: 0

Views: 2832

Answers (2)

Andrew
Andrew

Reputation: 231

i've spent 3+ days about this question and i came to this: if you have vector3 f.e. particle_velocity than you can find angle phi and theta, why you need this angles - because you can rotate vertixes along velocity vector:

    float gaPhi(vec3 v1) {
       float r = length(v1);
       float phi = atan(v1.y / v1.x);
       return phi;
    }
    float gaTheta(vec3 v1) {
       float r = length(v1);
       float theta = acos(v1.z / r);
       return theta;
    }

    float rot_x = 0.0;
    float rot_y = gaTheta(vel) - pi/2;
    if (vel.x >= 0 && vel.y >= 0 && vel.z >= 0) {
       //ok
       rot_y = -rot_y;
    }
    if (vel.x < 0 && vel.y >= 0 && vel.z >= 0) {
       //ok
       rot_y = rot_y + pi;
    }
    if (vel.x >= 0 && vel.y < 0 && vel.z >= 0) {
       //ok
       rot_y = -rot_y;
    }
    if (vel.x >= 0 && vel.y >= 0 && vel.z < 0) {
       //ok
       rot_y = -rot_y; 
    }
    if (vel.x < 0 && vel.y < 0 && vel.z >= 0) {
       //ok
       rot_y = rot_y + pi;
    }
    if (vel.x >= 0 && vel.y < 0 && vel.z < 0) {
       //ok
       rot_y = -rot_y;
    }
    if (vel.x < 0 && vel.y >= 0 && vel.z < 0) {
       //ok
       rot_y = rot_y + pi;
    }
    if (vel.x < 0 && vel.y < 0 && vel.z < 0) {
       //ok
       rot_y = rot_y + pi;
    }
    
    float rot_z = -gaPhi(vel)  ;

glsl vertex shader:

layout(location = 0) in vec3 vertex;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 texCoord;

uniform mat4 view; // camera view matrix
uniform mat4 proj; // camera projection matrix
uniform float time; // camera projection matrix

in float particle_radius; // particle radius
in float particle_mass; // particle mass
in vec3 particle_position; // particle position
in vec3 particle_velocity; // particle velocity

out vec2 frag_uv; // pass UV to fragment shader
out float frag_mass; // pass mass to fragment shader
out vec3 frag_velocity; // pass velocity to fragment shader
out vec4 texCoords; 


float gaPhi(vec3 v1) {
    float r = length(v1);
    float phi = atan(v1.y / v1.x);
    return phi;
}
float gaTheta(vec3 v1) {
    float r = length(v1);
    float theta = acos(v1.z / r);
    return theta;
}

void main() {
    frag_uv = texCoord; //sprite_uv;
    texCoords = vec4(texCoord, 0.0, 0.0);
    frag_mass = particle_mass;
    frag_velocity = particle_velocity;

    vec3 vert = vertex * max(10,min(100,particle_radius));
    vec3 vertn = normalize(vertex);
    float vertl = length(vert);
    vec3 vel =  particle_velocity;
    vec3 vel_dir =  normalize(particle_velocity);
    float pi = 3.14159265359;
    vec4 posnew = vec4(vert, 1.0);    

    float rot_x = 0.0;
    float rot_y = gaTheta(vel) - pi/2;
    if (vel.x >= 0 && vel.y >= 0 && vel.z >= 0) {
       //ok
       rot_y = -rot_y;
    }
    if (vel.x < 0 && vel.y >= 0 && vel.z >= 0) {
       //ok
       rot_y = rot_y + pi;
    }
    if (vel.x >= 0 && vel.y < 0 && vel.z >= 0) {
       //ok
       rot_y = -rot_y;
    }
    if (vel.x >= 0 && vel.y >= 0 && vel.z < 0) {
       //ok
       rot_y = -rot_y; 
    }
    if (vel.x < 0 && vel.y < 0 && vel.z >= 0) {
       //ok
       rot_y = rot_y + pi;
    }
    if (vel.x >= 0 && vel.y < 0 && vel.z < 0) {
       //ok
       rot_y = -rot_y;
    }
    if (vel.x < 0 && vel.y >= 0 && vel.z < 0) {
       //ok
       rot_y = rot_y + pi;
    }
    if (vel.x < 0 && vel.y < 0 && vel.z < 0) {
       //ok
       rot_y = rot_y + pi;
    }
    
    float rot_z = -gaPhi(vel)  ;

    mat4 rx = mat4( 1.0, 0.0, 0.0, 0.0,
                    0.0,cos(rot_x),sin(rot_x), 0.0,
                    0.0,-sin(rot_x),cos(rot_x), 0.0,
                    0.0, 0.0, 0.0, 1.0);

    mat4 ry = mat4( cos(rot_y),0.0, -sin(rot_y), 0.0,
                    0.0, 1.0, 0.0, 0.0,
                    sin(rot_y), 0.0, cos(rot_y), 0.0,
                    0.0,0.0,0.0,1.0);
                   
    mat4 rz = mat4( cos(rot_z), sin(rot_z), 0.0, 0.0,
                   -sin(rot_z), cos(rot_z), 0.0, 0.0,
                   0.0        , 0.0       ,1.0 ,0.0,
                   0.0        , 0.0       ,0.0 ,1.0);  
                    
    posnew = posnew * rx * ry * rz;     
    vec4 pos = proj * view * vec4(posnew.xyz + particle_position, 1.0);

    gl_Position = pos;
}    

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798686

Theta is the angle above the XY plane. Phi is the angle around the Z axis. In general, polar coordinates in n dimensions have n-1 angle components and 1 radius component.

Upvotes: 1

Related Questions