0

我有一个对象沿 X、Y、Z 方向的 3D 向量行进……我需要根据每个轴(x 轴、y 轴和 z 轴)旋转对象。

我如何以度数获得这些测量值?

(我正在使用 OpenGL 并且只知道glRotatef(...)) [glRotatef(...)此处的文档] 看着这个问题,答案给出了

视图向量 =<x, y, z>

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

phi = arctan2(y, x)

theta = arccos(z / r)

但从这个wiki页面我了解到:

[伊格纳西奥·巴斯克斯-艾布拉姆斯编辑]

phi => 围绕 Z 轴的角度

theta => x/y 平面之间的角度

但是我如何找到 Y?还是我需要?

真正的问题是,我如何用 来表示这一点glRotatef(...)

4

2 回答 2

1

Theta 是 XY 平面上方的角度。Phi 是围绕 Z 轴的角度。一般来说,n维极坐标有n-1个角度分量和1个半径分量。

于 2012-04-04T04:56:03.350 回答
0

我已经花了 3 天多的时间来解决这个问题,我得出了这个结论:如果你有 vector3 feparticle_velocity 而不是你可以找到角度 phi 和 theta,为什么你需要这个角度 - 因为你可以沿着速度向量旋转顶点:

    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顶点着色器:

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;
}    
于 2021-03-18T20:30:30.833 回答