Archived

This topic is now archived and is closed to further replies.

Camera and Object Orientation Problems

This topic is 5498 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

i'm building a D3D engine, but i'm having difficulties getting the camera position and the object positions to set right. i'm currently just rendering a triangle to do some initial testing. Basically, if i enter the coordinates for the triangle in a clockwise manner (LHS orientation), i can't see it b/c i'm looking at the back of the triangle. If i reverse the coordinates (counter-clockwise), the triangle shows up. EDIT: As the code below stands, the triangle *should* be facing the camera, but it isn't, and that is my dilemna. Here are the details:
          

m_pCamera->m_vPosition = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
m_pCamera->m_vLookAt = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
m_pCamera->m_vUp = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
m_pCamera->m_vRight = D3DXVECTOR3( 1.0f, 0.0f, 0.0f );

pTriangle->m_vPt0 = D3DXVECTOR3( -1.0f, 1.0f, 0.0f );
pTriangle->m_vPt0 = D3DXVECTOR3( 1.0f, 1.0f, 0.0f );
pTriangle->m_vPt0 = D3DXVECTOR3( 1.0f, 0.0f, 0.0f );
pTriangle->m_vPosition = D3DXVECTOR3( 0.0f, 0.0f, 5.0f );

//in the triangle constructor:

{...
     D3DUtil_InitMaterial( m_mtrlMaterial, 1.0f, 1.0f, 1.0f );
     D3DXMatrixTranslation( &m_matLocal, m_vPosition.x, m_vPosition.y, m_vPosition.z );
...}

//initializing the triangle:

{...
	m_pVerticies[m_nVertexBufferIndex + 0].p = m_vPt0;
	m_pVerticies[m_nVertexBufferIndex + 0].n = vNormal;
	m_pVerticies[m_nVertexBufferIndex + 0].tu = 0.0f;
	m_pVerticies[m_nVertexBufferIndex + 0].tv = 0.0f;

	m_pVerticies[m_nVertexBufferIndex + 1].p = m_vPt1;
	m_pVerticies[m_nVertexBufferIndex + 1].n = vNormal;
	m_pVerticies[m_nVertexBufferIndex + 1].tu = 1.0f;
	m_pVerticies[m_nVertexBufferIndex + 1].tv = 0.0f;

	m_pVerticies[m_nVertexBufferIndex + 2].p = m_vPt2;
	m_pVerticies[m_nVertexBufferIndex + 2].n = vNormal;
	m_pVerticies[m_nVertexBufferIndex + 2].tu = 1.0f;
	m_pVerticies[m_nVertexBufferIndex + 2].tv = 1.0f;

	m_pIndicies[m_nIndexBufferIndex + 0] = m_nVertexBufferIndex + 0;
	m_pIndicies[m_nIndexBufferIndex + 1] = m_nVertexBufferIndex + 1;
	m_pIndicies[m_nIndexBufferIndex + 2] = m_nVertexBufferIndex + 2;
...}

//example:

HRESULT CMyD3DApp::CameraRestoreDeviceObjects()
{
	D3DXMatrixLookAtLH(&m_matLocal,	 &m_vPosition,		//Camera Position

									 &m_vLookAt,		//Look At Position

									 &m_vUp);			//Up Direction

	GetD3DDevice()->SetTransform( D3DTS_VIEW, &m_matLocal );

	D3DXMatrixPerspectiveFovLH( &m_matProjection, m_fFOV, m_fAspect, m_fZNear, m_fZFar );
	GetD3DDevice()->SetTransform( D3DTS_PROJECTION, &m_matProjection );

        return S_OK;
}

HRESULT CMyD3DApp::CameraFrameMove()
{
	float fYaw, fPitch, fRoll;
	fYaw = fPitch = fRoll = 0.0f;
	
	// base vector regeneration

    D3DXVec3Normalize(pvLookAt, pvLookAt);
	D3DXVec3Cross(pvRight, pvUp, pvLookAt);  // Cross Produkt of the UP and LOOK Vector

    D3DXVec3Normalize(pvRight, pvRight);
	D3DXVec3Cross(pvUp, pvLookAt, pvRight); // Cross Produkt of the RIGHT and LOOK Vector

    D3DXVec3Normalize(pvUp, pvUp);

    // Matrices for pitch, yaw and roll

	D3DXMATRIX matPitch, matYaw, matRoll;
	D3DXMatrixRotationAxis(&matPitch, &m_vRight, fPitch );
	D3DXMatrixRotationAxis(&matYaw, &m_vUp, fYaw ); 
    D3DXMatrixRotationAxis(&matRoll, &m_vLookAt, fRoll);
	
	// rotate the LOOK & RIGHT Vectors about the UP Vector

	D3DXVec3TransformCoord(&m_vLookAt, &m_vLookAt, &matYaw); 
	D3DXVec3TransformCoord(&m_vRight, &m_vRight, &matYaw); 

	// rotate the LOOK & UP Vectors about the RIGHT Vector

	D3DXVec3TransformCoord(&m_vLookAt, &m_vLookAt, &matPitch); 
	D3DXVec3TransformCoord(&m_vUp, &m_vUp, &matPitch); 

	// rotate the RIGHT & UP Vectors about the LOOK Vector

	D3DXVec3TransformCoord(&m_vRight, &m_vRight, &matRoll); 
	D3DXVec3TransformCoord(&m_vUp, &m_vUp, &matRoll); 

	D3DXMatrixIdentity( &m_matLocal );
	m_matLocal._11 = m_vRight.x; m_matLocal._12 = m_vUp.x; m_matLocal._13 = m_vLookAt.x;
	m_matLocal._21 = m_vRight.y; m_matLocal._22 = m_vUp.y; m_matLocal._23 = m_vLookAt.y;
	m_matLocal._31 = m_vRight.z; m_matLocal._32 = m_vUp.z; m_matLocal._33 = m_vLookAt.z;
	m_matLocal._41 = - D3DXVec3Dot( &m_vPosition, &m_vRight ); 
	m_matLocal._42 = - D3DXVec3Dot( &m_vPosition, &m_vUp );
	m_matLocal._43 = - D3DXVec3Dot( &m_vPosition, &m_vLookAt );

	GetD3DDevice()->SetTransform( D3DTS_VIEW, &m_matLocal );
}

HRESULT CMyD3DApp::TriangleFrameMove()
{
	D3DXMatrixTranslation( &m_matLocal, m_vPosition.x, m_vPosition.y, m_vPosition.z);

    D3DXMATRIX matTemp, matRotateX, matRotateY, matRotateZ;
    D3DXMatrixRotationY( &matRotateY, m_vRotation.x );
	D3DXMatrixRotationX( &matRotateX, m_vRotation.y );
	D3DXMatrixRotationZ( &matRotateZ, m_vRotation.z );

	D3DXMatrixMultiply( &matTemp, &matRotateX, &matRotateY );
	D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp );
	D3DXMatrixMultiply( &m_matLocal, &matTemp, &m_matLocal ); 
	return S_OK;
}

//the triangle's normal is 0.0f, 0.0f, -1.0f

HRESULT CMyD3DApp::Render()
{
	GetD3DDevice()->SetTransform( D3DTS_WORLD, &m_matLocal );

	GetD3DDevice()->SetStreamSource( 0, m_pVB, sizeof( CUSTOMVERTEX ));
	GetD3DDevice()->SetVertexShader( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 );
	GetD3DDevice()->SetIndices( m_pIB, m_nIndexBufferIndex );
	GetD3DDevice()->SetMaterial( &m_mtrlMaterial );

	GetD3DDevice()->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, m_nVertexBufferIndex, m_nNumVertices, m_nIBStartIndex, m_nIBPrimitiveCount );

	return S_OK;     
}

          
Currently none of the vector positions, rotations, etc. are changed. If i put a spin on the triangle, i can see it spinning, but it still starts facing the opposite direction from the camera. Any help is appreciated. "You call him Dr. Grip, doll!" [edited by - the_grip on November 20, 2002 12:52:22 PM] [edited by - the_grip on November 20, 2002 5:12:40 PM]

Share this post


Link to post
Share on other sites
I''m not sure exactly what the problem is, it sounds like your code is behaving to me because if you rotate your triangle around it should face your camera half the time and the other half of the time face away from your camera. If you want your triangle to be visible from both front and back sides you have to disable back face culling:

SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE)

If it is that you''re wanting your camera to rotate around the triangle as it spins (so the camera is always looking at the front of the triangle) well that is something I don''t have experience with yet so I can''t help you there. Good luck.

Share this post


Link to post
Share on other sites
Thanks for the reply.

That''s not exactly what i''m wanting... the triangle shouldn''t show up from the back side.

However, i''m not doing any changes to the triangle or camera sizes, rotation, position, etc. Basically i''m just trying to show the triangle in its initial state - which should point towards the camera, but it actually is pointing away (thus i only see the back face which is culled so it is not visible).

"You call him Dr. Grip, doll!"

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I can''t see but you''re doing an awful lot of matrix calculations on your camera. Try setting up a simple LookAt matrix for your camera, so you know for sure it is in the right place.

Share this post


Link to post
Share on other sites
Good idea - but the problem still persists. The matrix calculations should not have any effect currently b/c there are no changes to the vectors or matricies.

Anyone else offer any help?

Thanks!

"You call him Dr. Grip, doll!"

Share this post


Link to post
Share on other sites
*bump*

i can''t figure this out... the triangle is:

position:
x = 0.0f
y = 0.0f
z = 5.0f

normal:
x = 0.0f
y = 0.0f
z = -1.0f

the camera is:

position:
x = 0.0f
y = 0.0f
z = -5.0f

look at:
x = 0.0f
y = 0.0f
z = 1.0f

up:
x = 0.0f
y = 1.0f
z = 0.0f

right:
x = 1.0f
y = 0.0f
z = 0.0f

The triangle is pointing towards the negative axis, yet unless i change the camera''s position to be on the other side of the triangle and pointed towards the negative axis (changing the right x coord to point to -1.0f), it never shows up (or unless i change the triangle''s coordinates to have a positive z axis normal).


"You call him Dr. Grip, doll!"

Share this post


Link to post
Share on other sites
Was this intended or is it typo:

pTriangle->m_vPt0 = D3DXVECTOR3( -1.0f, 1.0f, 0.0f );
pTriangle->m_vPt0 = D3DXVECTOR3( 1.0f, 1.0f, 0.0f );
pTriangle->m_vPt0 = D3DXVECTOR3( 1.0f, 0.0f, 0.0f );

i.e. not vPt0..vPt2?

Share this post


Link to post
Share on other sites
You're right - it's a typo.

pTriangle->m_vPt0( -1.0f, 1.0f, 0.0f ); //pt0
pTriangle->m_vPt1( 1.0f, 1.0f, 0.0f ); //pt1
pTriangle->m_vPt2( 1.0f, 0.0f, 0.0f ); //pt2

"You call him Dr. Grip, doll!"

[edited by - the_grip on November 21, 2002 6:18:40 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''m inclined to believe this is a culling problem, as previously stated. If you can see it from one side and not from the other, then it is almost certainly a culling problem.

Share this post


Link to post
Share on other sites
i'm using the common files, and i don't change the culling from the default (backface). So yes, the back should be culled, but i draw the verticies in a clockwise manner with a LHS perspective.

Any more help? This is driving me crazy. i've been thinking the problem is somehow in the camera positioning, but who knows - it could be the culling. Why would it be culled though if the verticies are drawn clockwise?

"You call him Dr. Grip, doll!"

[edited by - the_grip on November 25, 2002 10:42:53 AM]

Share this post


Link to post
Share on other sites
x = 0.0f
y = 0.0f
z = -1.0f

EDIT:

Yes, when i turn off backface culling i can see the triangle as it should be.

My problem, however, is that i *should be* looking at the front face.

The camera should be pointing towards the positive z plane. The triangle's normal should be pointing to the negative z plane (normal z value is -1.0f). The verticies are drawn in clockwise order, and the perspective is LHS.

Can someone tell me why the back face is pointing towards the camera? That's my real problem.

ANOTHER EDIT:

Ok, this is weird. When i specify to cull faces with a clockwise order (m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW)), the triangle shows up. Is clockwise not the default?

YET ANOTHER EDIT:
i checked the render state:

DWORD dwRenderState = 0L;
m_pd3dDevice->GetRenderState( D3DRS_CULLMODE, &dwRenderState );
ASSERT( dwRenderState & D3DCULL_CCW );

It is counterclockwise! i don't set this at all... can someone verify that counterclockwise is the default?

[edited by - the_grip on November 25, 2002 12:34:41 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
from the DX8.1 docs:

D3DRS_CULLMODE
Specifies how back-facing triangles are culled, if at all. This can be set to one member of the D3DCULL enumerated type. The default value is D3DCULL_CCW.

Share this post


Link to post
Share on other sites
That explains it... i was reading a website tutorial that says the default is clockwise.

i had looked through the SDK under D3DCULL and couldn''t find what the default is... instead of looking at D3DRENDERSTATETYPE. MSFT would have saved me a ton of agony if they would have mentioned that in both places.

"You call him Dr. Grip, doll!"

Share this post


Link to post
Share on other sites