Sign in to follow this  

Build camera view matrix LH

This topic is 2041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If i got position and target point how to calculate "up" vector with this info?
[code]
// example
D3DXVECTOR3 pos = something;
D3DXVECTOR3 target = somethingElse;
D3DXVECTOR3 up = ???;
D3DXMatrixLookAtLH(&view, &pos, &target, &up);
[/code]

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, Edited by belfegor

Share this post


Link to post
Share on other sites
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°)

Share this post


Link to post
Share on other sites
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:
[img]http://www.dodaj.rs/f/34/EC/3xLMnKK5/volumedecals.jpg[/img]

Code:
[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);
[/code]

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?
[code]
decCamUP.xyz = triNormal.zxy;
[/code]
Is there some other similar "hack" that i might use? Edited by belfegor

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:
[img]http://www.dodaj.rs/f/21/4s/36qNZUqI/decals.jpg[/img]

Near clip is always smaller then radius and far clip is 2 * radius Edited by belfegor

Share this post


Link to post
Share on other sites
I still need help about this problem.

Let say that start vectors for camera is this:
[code]
pos 0 0 0
lookAt 0 0 1
up 0 1 0
[/code]

and later i assign new lookAt point
[code]
newLookAt = targetPos - pos
newLookAt.normalize()
[/code]

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

Thanks for your time. Edited by belfegor

Share this post


Link to post
Share on other sites

This topic is 2041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this