0

X、Y、Zの観点から3Dベクトルに沿って移動するオブジェクトがあります...各軸(x軸、y軸、z軸)に従ってオブジェクトを回転させる必要があります。

これらの測定値を度で取得するにはどうすればよいですか?

(私はOpenGLを使用していて、知っているだけですglRotatef(...))[glRotatef(...)ドキュメントはこちら] この質問 を見ると、答えは次のようになります。

viewvector =<x, y, z>

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

phi = arctan2(y、x)

theta = arccos(z / r)

しかし、このwikiページから私はそれを理解しています:

[IgnacioVazquez-Abramsから編集]

phi=>Z軸周りの角度

シータ=>x/y平面間の角度

しかし、どうすればYを見つけることができますか?または私はする必要がありますか?

本当の問題は、これをどのように表現すればよいのglRotatef(...)かということです。

4

2 に答える 2

1

シータは、XY平面の上の角度です。PhiはZ軸の周りの角度です。一般に、n次元の極座標には、n-1個の角度成分と1個の半径成分があります。

于 2012-04-04T04:56:03.350 に答える
0

私はこの質問について3日以上過ごしましたが、これに到達しました:角度phiとthetaを見つけることができるよりもvector3 feparticle_velocityがある場合、なぜこの角度が必要なのですか?速度ベクトルに沿って頂点を回転できるためです:

    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 に答える