开发者

calculate camera up vector after glulookat()?

I'm just starting out teaching myself openGL and now adding openAL to the mix.

I have some planets scattered around in 3D space and when I touch the screen, I'm assigning a sound to a random planet and then slowly and smoothly flying the "camera" over to look at it and listen to it. The animation/tweening part is working perfectly, but the openAL piece isn't quiet right. I move the camera around by doing 开发者_如何学运维a tiny translate() and gluLookAt() for every frame to keep things smooth (tweening the camera position and lookAt coords). The trouble seems to be with the stereo image I'm getting out of the headphones.. it seems like the left/right/up/down is mixed up sometimes after the camera rolls or spins. I am pretty sure the trouble is here:

    ALfloat listenerPos[]={camera->currentX,camera->currentY,camera->currentZ};

    ALfloat listenerOri[]={camera->currentLookX,
                           camera->currentLookY,
                           camera->currentLookZ,
                           0.0,//Camera Up X  <--- here
                           0.1,//Camera Up Y  <--- here
                           0.0}//Camera Up Z  <--- and here

   alListenerfv(AL_POSITION,listenerPos);
   alListenerfv(AL_ORIENTATION,listenerOri);

I'm wondering if I need to recompute the UP vector for the camera after each gluLookAt() to straighten out the audio positioning problem.. That seems like it could be the missing ingredient, but the math involved seems so advanced I'm not even sure where to begin..

1) Is it correct that I'll need to recalculate the up vector after each gluLookAt()?

2) Could someone teach me how to calculate an up vector?


So would this be a correct way to get the up vector of the camera after the gluLookAt()?

gluLookAt(cam->currentX,
          cam->currentY, 
          cam->currentZ, 
          cam->currentLookX, 
          cam->currentLookY, 
          cam->currentLookZ, 
          cam->upX, 
          cam->upY, 
          cam->upZ);

//Get the up vector
glGetFloatv( GL_MODELVIEW_MATRIX, cam->modelViewMatrix);

cam->upX = cam->modelViewMatrix[4];
cam->upY = cam->modelViewMatrix[5];
cam->upZ = cam->modelViewMatrix[6];


In an Y-up world, the global "up" vector is the local "Y" vector. Or, put another way, if you put in "0, 1, 0" into the transform, it will come out pointing "up."

In an OpenGL matrix, this means that the second column is your "up" vector. You can extract it as follows:

float *myMatrix = ...;
myUpVector = Vector3(myMatrix[4], myMatrix[5], myMatrix[6]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜