• Create Account

## Build camera view matrix LH

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1belfegor  Members

2833
Like
0Likes
Like

Posted 13 May 2012 - 04:04 PM

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);


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, 13 May 2012 - 04:42 PM.

### #2Fredericvo  Members

1566
Like
0Likes
Like

Posted 13 May 2012 - 06:09 PM

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.

### #3Trienco  Members

2555
Like
0Likes
Like

Posted 13 May 2012 - 10:38 PM

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

### #4belfegor  Members

2833
Like
0Likes
Like

Posted 14 May 2012 - 03:00 AM

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:

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;

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

Edited by belfegor, 14 May 2012 - 03:07 AM.

### #5Fredericvo  Members

1566
Like
0Likes
Like

Posted 14 May 2012 - 04:12 AM

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.

### #6belfegor  Members

2833
Like
0Likes
Like

Posted 14 May 2012 - 04:25 AM

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, 14 May 2012 - 04:29 AM.

### #7belfegor  Members

2833
Like
0Likes
Like

Posted 15 May 2012 - 05:50 AM

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?