Orthographic camera

Started by
13 comments, last by DividedByZero 9 years, 10 months ago

I would recommend that you work with one matrix at a time until you get each of them proven out. Start with only your projection matrix, and make the other two identity matrices. You should be able to draw your sprite within the bounds of your orthographic camera volume, and you should see it appear even if the view and world matrices are identity.

If you can get that working, I think the rest will be manageable...

Also, you have a comment up above about why you would transpose a matrix. The reason is that the row / column order by default is not the same for the GPU and for the CPU (at least for their runtimes...) so you either have to transpose the matrices, or you can compile your shaders with an additional flag that flips the row / column order.

EDIT: Do you also have the debug device enabled? When you create your D3D11 device, use the flag D3D11_CREATE_DEVICE_DEBUG. You will get warnings if you try to use the API incorrectly, which can be really helpful too.

Advertisement

Thanks again,

Yeah, I have debug device enabled and surprisingly no errors / warnings at all (I have seen a few of those today - LOL)

I just made the view and world matrices 'identity' and I get a 1 pixel dot at bottom left of the screen (which is roughly what i'd expect due to the removal of scaling, except for bottom being zero and top being 640 - that seems to be upside down).

Not sure where to go from here though.

Here is my current code...

// Set up the view
XMMATRIX viewMatrix=XMMatrixIdentity();
XMMATRIX projMatrix=XMMatrixOrthographicOffCenterLH(0.0f,(float)width,0.0f,(float)height,0.0f,100.0f); // 800 x 600
viewMatrix=XMMatrixTranspose(viewMatrix);
projMatrix=XMMatrixTranspose(projMatrix);
 
// position the object
//XMMATRIX scaleMatrix=XMMatrixScaling(1.0f*256.0f,1.0f*256.0f,0.0f);
//XMMATRIX translationMatrix=XMMatrixTranslation(0.0f,0.0f,0.0f);
//XMMATRIX worldMat=scaleMatrix*translationMatrix
 
XMATRIX worldMat=XMMatrixIdentity(); 
 
d3dContext->UpdateSubresource(worldCB,0,0,&worldMat,0,0);
d3dContext->UpdateSubresource(viewCB,0,0,&viewMatrix,0,0);
d3dContext->UpdateSubresource(projCB,0,0,&projMatrix,0,0);
 
d3dContext->VSSetConstantBuffers(0,1,&worldCB);
d3dContext->VSSetConstantBuffers(1,1,&viewCB);
d3dContext->VSSetConstantBuffers(2,1,&projCB);
Ok, I made some decent progress. The code now works perfectly, except the fact that the y axis starts from the bottom of the screen and upwards is positive.

// Set up the view
XMMATRIX viewMatrix=XMMatrixIdentity();
XMMATRIX projMatrix=XMMatrixOrthographicOffCenterLH(0.0f,(float)width,0.0f,(float)height,0.0f,100.0f);		// 800 x 450
viewMatrix=XMMatrixTranspose(viewMatrix);
projMatrix=XMMatrixTranspose(projMatrix);

// position the object
XMMATRIX scaleMatrix=XMMatrixScaling(1.0f*128.0f,1.0f*128.0f,0.0f);  // This is correct for 256px sprite as verts are 1 to -1 (fix later)
XMMATRIX rotationMatrix=XMMatrixRotationZ(0.0f);
XMMATRIX translationMatrix=XMMatrixTranslation(0.0f,0.0f,0.0f);
XMMATRIX worldMat=scaleMatrix*rotationMatrix*translationMatrix;
worldMat=XMMatrixTranspose(worldMat);

d3dContext->UpdateSubresource(worldCB,0,0,&worldMat,0,0);
d3dContext->UpdateSubresource(viewCB,0,0,&viewMatrix,0,0);
d3dContext->UpdateSubresource(projCB,0,0,&projMatrix,0,0);

d3dContext->VSSetConstantBuffers(0,1,&worldCB);
d3dContext->VSSetConstantBuffers(1,1,&viewCB);
d3dContext->VSSetConstantBuffers(2,1,&projCB);
So, overall I am pretty happy as I had no clue on shaders and DX11 just 24 hours ago. If I can nail this last issue (the y axis upside down) I'll be extremely happy.

If you want to invert your y axis, just scale by a negative number along that axis. Just remember that you will be flipping everything along there, so you will need to also apply a translation accordingly to put the object where you want it to be.

Thanks Jason.

I also got around it using this method as well


XMMATRIX translationMatrix=XMMatrixTranslation(128.0f,(float)height-128.0f,0.0f);
(float)height being the height of the window. Just seemed a little hacky though as all of the documentation I can find says that 0,0 should be top-left of the screen.

At least I have a working solution though. smile.png

But, on the bright side. I came here yesterday with a blank window and now I have a working camera system, so I am glad I persisted with the shader method rather than persisting with dynamic vertex buffers. In the end, I know it was a far better method to go this way smile.png

Thanks for all of the help and guidance along the way too.

No doubt, I'll be posting in a weeks time asking how to do pixel perfect collisions (I am shuddering allready - LOL).

This topic is closed to new replies.

Advertisement