I'm writing some code that has to transform a number of vertex coordinates to clip/projection space on the CPU (to integrate with an existing application; shaders are not an option here as the application provides its own shaders at a later point), for which I have a test case which intends to have the result be a basic 'camera-style' transformation + projection looking down a camera rotated around the X axis.
The test case is as follows:
#include <windows.h>
#include <xnamath.h>
#include <stdio.h>
int main()
{
XMVECTOR atPos = XMVectorSet(925.0f, -511.0f, 0.0f, 0.0f);
XMVECTOR eyePos = XMVectorSet(925.0f, -311.0f, 200.0f, 0.0f);
XMVECTOR upDir = XMVectorSet(0.f, 0.f, 1.f, 0.0f);
XMMATRIX viewMatrix = XMMatrixLookAtLH(eyePos, atPos, upDir);
XMMATRIX projectionMatrix = XMMatrixPerspectiveFovLH(XMConvertToRadians(55.f), 1.0f, 0.1f, 5000.0f);
XMMATRIX viewProjMatrix = viewMatrix * projectionMatrix;
XMVECTOR coord1 = XMVectorSet(925.0f, -500.0f, 0.0f, 0.0f);
XMVECTOR coord2 = XMVectorSet(925.0f, 0.0f, 0.0f, 0.0f);
XMVECTOR coord3 = XMVectorSet(925.0f, -1000.0f, 0.0f, 0.0f);
coord1 = XMVector3TransformCoord(coord1, viewProjMatrix);
coord2 = XMVector3TransformCoord(coord2, viewProjMatrix);
coord3 = XMVector3TransformCoord(coord3, viewProjMatrix);
printf(" 0: %g %g %g\n", XMVectorGetX(coord2), XMVectorGetY(coord2), XMVectorGetZ(coord2));
printf(" -500: %g %g %g\n", XMVectorGetX(coord1), XMVectorGetY(coord1), XMVectorGetZ(coord1));
printf("-1000: %g %g %g\n", XMVectorGetX(coord3), XMVectorGetY(coord3), XMVectorGetZ(coord3));
getc(stdin);
}
If I just transform by the view matrix, I'm noticing the Y value correctly moving correlated with the input Y value of the coordinates; but adding the projection matrix the Y value seems to wrap around making both the coordinate that are to the 'north' of the view and the coordinate to the 'south' of the view extend in the same direction in projection space, which results in the eventual faces transformed using similar code in the target application overlapping each other instead of drawing in the correct order.
What am I doing wrong in this regard, and how can I get a valid result for at least this input (i.e. extending in the same manner as the input Y is advancing, even though these vertices are out of the given projection range, which I doubt should be a problem)?
XMVECTORF32
type. For example,static const XMVECTORF32 atPos = { 925.0f, -511.0f, 0.0f, 0.0f };
The use ofXMVectorSet
is really for variables and not constant values. – Chuck WalbournXMFLOAT4A tmp; XMStoreFloat4( &tmp, coord2 ); printf(" 0: %g %g %g\n", tmp.x, tmp.y, tmp.z );
– Chuck Walbourn