• Advertisement
Sign in to follow this  

Render at a specific size / z plane problem

This topic is 4520 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 am currently writing a small 2D game. I wrote a 2D engine with DirectX9, which works fair well. However, I want to be able to have some sprite rotate on their X axis, in some animations (card flipping). I set up my rendering to have a classic perspective left handed field of view, rendered on the middle of a render-target texture my sprite with the rotation effect. And then I render this texture on my 2D engine. Everything works well, except that I need to set my rotated sprite a specific size : the sprite without rotation should be centered on the texture, and have a projected size of 128x128 pixels. No problem, D3DXVec3Unproject does this for me ! I can then transform the unprojected vectors of my quad with my rotation. Here comes the problem : The unprojected vectors D3DXVec3Unproject returns have a z value mostly equals to the z-far plane of my projection matrix. After applying a rotation to them, I only see the lower part of the rotated quad. Logical, since the upper part is beyond the z-far plane. But I of course want the complete quad... I really don't think any solution to this problem, that's why I need your help ! The source code (doesn't come from my game, but from a separate little test I made, that's why the rendering is classically made on a 640x480 backbuffer, and not on a texture, but this doesn't change anything) :
// Projection
D3DXMatrixPerspectiveFovLH(&matProj, D3DXToRadian(45.0f), 640.0f / 480.0f, 0.1f, 100.0f);
Dev->SetTransform(D3DTS_PROJECTION, &matProj);

// View
Dev->SetTransform(D3DTS_VIEW, &matView);

// Vectors
D3DVIEWPORT9 Viewport;
D3DXVECTOR3 vUnproj[4];
D3DXVECTOR3 vProj[4];
vProj[0] = D3DXVECTOR3(256.0f, 176.0f, 1.0f);
vProj[1] = D3DXVECTOR3(384.0f, 176.0f, 1.0f);
vProj[2] = D3DXVECTOR3(256.0f, 304.0f, 1.0f);
vProj[3] = D3DXVECTOR3(384.0f, 304.0f, 1.0f);
for (int i = 0; i < 4; ++i)
	D3DXVec3Unproject(&vUnproj, &vProj, &Viewport, &matProj, &matView, &matView); // unproject with identity matrix for world, that's why we have matView for world matrix
Vertex Quad[] = {
	{ vUnproj[0].x, vUnproj[0].y, 0.0f, 0.0f, 0.0f },
	{ vUnproj[1].x, vUnproj[1].y, 0.0f, 1.0f, 0.0f },
	{ vUnproj[2].x, vUnproj[2].y, 0.0f, 0.0f, 1.0f },
	{ vUnproj[3].x, vUnproj[3].y, 0.0f, 1.0f, 1.0f }

// World
D3DXMATRIX matTrans, matRot, matWorld;
D3DXMatrixTranslation(&matTrans, 0.0f, 0.0f, vUnproj[0].z); // vUnproj[n].z is always the same, whatever n is - we cannot set this directly in Quad[] because rotation needs to be done before z-translation
D3DXMatrixRotationX(&matRot, D3DXToRadian(45.0f));
matWorld = matRot * matTrans;
Dev->SetTransform(D3DTS_WORLD, &matWorld);

// VertexBuffer
char* Verts;
VB->Lock(0, 4 * sizeof(Quad), (void**) &Verts, 0);
memcpy(Verts, Quad, sizeof(Quad));

// Render
Dev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xFF000000, 1.0f, 0);
Dev->SetTexture(0, Tex);
Dev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
Dev->Present(NULL, NULL, NULL, NULL);

Thank you :)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement