Build camera view matrix LH

Started by
5 comments, last by belfegor 11 years, 11 months ago
If i got position and target point how to calculate "up" vector with this info?

// example
D3DXVECTOR3 pos = something;
D3DXVECTOR3 target = somethingElse;
D3DXVECTOR3 up = ???;
D3DXMatrixLookAtLH(&view, &pos, &target, &up);


Thanks for your time.

EDIT: I need to set correct UP vector because i am using this matrix to project texture, witch in some cases might look straight down or up,
Advertisement
If your cam never rolls just set it fixed to 0.0f, 1.0f, 0.0f, where up really is along the y axis when you're standing on the ground.
A turning airplane would roll.
It's only that easy if he also doesn't allow to pitch 90° or more.

The question as is has no answer. There is an infinite number of possible "up" vectors unless you either know something like "right" or just randomly determine that "up" is always a fixed direction (with the above limitations or no roll and don't pitch over 89.9999°)
f@dzhttp://festini.device-zero.de
I am using this to project decal texture on terrain and it fails if triangle normal is 0, 1, 0 so i thought its because of "camera" up dir?

Here, rightmost decal:
volumedecals.jpg

Code:

// Used to setup decal "camera" matrices
struct Decal
{
D3DXVECTOR3 pos;// point of intersection on terrain
D3DXVECTOR3 nrm;// triangle normal at intersection
};
std::vector<Decal> v;
...
...
D3DXVECTOR2 decTexDim(256.0f, 256.0f);
D3DXVECTOR3 decCamPos, decCamUp, decCamTarget, decNrm;
decCamPos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
decCamUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
decCamTarget = (*it).pos;
D3DXMATRIX decCamView, decCamProj, camViewInv;
float OffsetX = 0.5f + (0.5f / decTexDim.x);
float OffsetY = 0.5f + (0.5f / decTexDim.y);
D3DXMATRIX texScaleBiasMat(
0.5f, 0.0f, 0.0f, 0.0f,
0.0f, -0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
OffsetX, OffsetY, 0.0f, 1.0f );
D3DXMatrixPerspectiveFovLH(&decCamProj, D3DX_PI / 2.0f, decTexDim.x / decTexDim.y, 0.1f, volRadius * 2.0f);
D3DXVec3Normalize(&decNrm, &(*it).nrm);
decCamPos = decCamTarget + (decNrm * volRadius);
D3DXMatrixLookAtLH(&decCamView, &decCamPos, &decCamTarget, &decCamUp);
D3DXMatrixInverse(&camViewInv, 0, &cam->View);

D3DXMATRIX decCamViewProjScaleBias = decCamView * decCamProj * texScaleBiasMat;
D3DXMATRIX matViewToDecalViewProj = camViewInv * decCamViewProjScaleBias;

d3d9device->SetVertexShader(DECALvs);
d3d9device->SetPixelShader(DECALps);
d3d9device->SetPixelShaderConstantF(0, matViewToDecalViewProj, 4);
...
decalVolumeMesh->DrawSubset(0);


I tried to "swizzle" axis from tri normal and assign to UP witch apeirs to work but i don't know if it will work always?

decCamUP.xyz = triNormal.zxy;

Is there some other similar "hack" that i might use?
This looks more like clipping artefacts. You are using a procedurally generated projection here so it's something to do with your calculations. In a normal projection I'd tell you to increase the far clipping plane's value but that isn't applicable here.
Its not clipping, simple check is to manually set UP dir to 0, 0, 1 and it works on that triangle with normal 0,1,0.

I am setting a sphere at point of intersection and position camera like this:
decals.jpg

Near clip is always smaller then radius and far clip is 2 * radius
I still need help about this problem.

Let say that start vectors for camera is this:

pos 0 0 0
lookAt 0 0 1
up 0 1 0


and later i assign new lookAt point

newLookAt = targetPos - pos
newLookAt.normalize()


With what to transform old UP to get new UP vector?

Thanks for your time.

This topic is closed to new replies.

Advertisement