Jump to content
  • Advertisement
Sign in to follow this  

Build camera view matrix LH

This topic is 2350 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?

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

Share this post

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

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:


// 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->SetPixelShaderConstantF(0, matViewToDecalViewProj, 4);

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? 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:

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:

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

and later i assign new lookAt point

newLookAt = targetPos - pos

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!