Thanks Jason. Im using the DirectXMath variables and functions. I'm not sure where to add those flags, but i do use XMMatrixTranspose(); on each matrix before i set them to the constant buffer, which i think does the same thing. I've tried with and without transposing the matrices. Im almost certain the problem is the projection matrix. I'll show the calculations, see what you think.
This is the three vertices of the triangle.
{ XMFLOAT3( -1.0f, -1.0f, 0.0f ) },
{ XMFLOAT3( +0.0f, +1.0f, 0.0f ) },
{ XMFLOAT3( +1.0f, -1.0f, 0.0f ) }
The view matrix: ( Rotation is zero so it can be ignored. )
XMFLOAT3 up, position, lookAt;
XMVECTOR vUp, vPosition, vLookAt;
float yaw, pitch, roll;
XMMATRIX rotationMatrix;
// Set the vector that points upwards.
up.x = 0.0f;
up.y = 1.0f;
up.z = 0.0f;
vUp = XMLoadFloat3( &up );
// Set the position of the camera in the world.
position.x = 0.0f;
position.y = 0.0f;
position.z = -10.0f;
vPosition = XMLoadFloat3( &position );
// Set where the camera is looking at by default.
lookAt.x = 0.0f;
lookAt.y = 0.0f;
lookAt.z = 1.0f;
vLookAt = XMLoadFloat3( &lookAt);
// Convert the rotation angles to radians. ( these are all 0, no rotation )
pitch = XMConvertToRadians( m_rotationX );
yaw = XMConvertToRadians( m_rotationY );
roll = XMConvertToRadians( m_rotationZ );
// Create the rotation matix based on the Yaw Pitch Roll. ( again this comes out as an identity matrix, no rotation )
rotationMatrix = XMMatrixRotationRollPitchYaw( pitch, yaw, roll );
// Transform the "lookAt" and "up" vectors to rotate the camera at its origin.
vUp = XMVector3TransformCoord( vUp, rotationMatrix );
vLookAt = XMVector3TransformCoord( vLookAt, rotationMatrix );
// Translate the rotated camera position to the position of the viewer.
vLookAt = vPosition + vLookAt;
// Finaly create the view matrix from the three updated vectors.
m_viewMatrix = XMMatrixLookAtLH( vPosition, vLookAt, vUp );
The world matrix is just set as an identity matrix, the triangle is at the origin of the world.
m_worldMatrix = XMMatrixIdentity();
The projection matrix:
screenWidth = 800;
screenHeight = 600;
screenNear = 0.1f;
screenDepth = 1000.0f;
float fieldOfView = (float)XM_PI / 4.0f;
float screenAspect = (float)screenWidth / (float)screenHeight;
m_projectionMatrix = DirectX::XMMatrixPerspectiveFovLH( fieldOfView, screenAspect, screenNear, screenDepth );
Transpose the matrices and set them to the constant buffer:
// Transpose the matrices to prepare them for the shader.
XMMatrixTranspose( worldMatrix );
XMMatrixTranspose( viewMatrix );
XMMatrixTranspose( projectionMatrix );
// Lock the constant buffer so it can be writen to.
deviceContext->Map( m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource );
// Get a pointer to the data in the constant buffer.
dataPtr = (MatrixBufferType*)mappedResource.pData;
// Copy the matrices into the constant buffer.
dataPtr->world = worldMatrix;
dataPtr->view = viewMatrix;
dataPtr->projection = projectionMatrix;
// Unlock the constant buffer.
deviceContext->Unmap( m_matrixBuffer, 0 );
// Set the constant buffer in the vertex shader with the updated values.
deviceContext->VSSetConstantBuffers( 0, 1, &m_matrixBuffer );
The think final vertices positions comes out as this.
{-1.81066000, -2.41421342, 9.90099049, 10.0000000}
{0.000000000, 2.41421342, 9.90099049, 10.0000000}
{1.81066000, -2.41421342, 9.90099049, 10.0000000}