   2D ortho "z" in a 3D scene
  I finally got a shape to show up on screen after projecting it to 2D screen coordinates.My problem is this white square, although drawn with ortho projection matrix, still needs to be between my ship and the background. I want to place it right in front of the background, if possible.It is a shape I will be using for an occlusion test. I have it drawn right now so I can see where the future occlusion test will take place.So, what position Z value do I need to give it?As I understand, using the ortho projection, I have a clip range of 0 to 1:   // Render the occlusion shape centered at the current 2D light position   m_occlusionShape->SetPosition(D3DXVECTOR3(lightPosition.x, lightPosition.y, 0.0f));   D3DXMATRIX view;   D3DXMatrixIdentity(&view);   m_effectManager.SetViewMatrix(view);   D3DXMATRIX projection;   D3DXMatrixOrthoOffCenterLH(&projection,0.0f,static_cast<float>(viewport.Width),static_cast<float>(viewport.Height),0.0f,0.0f,1.0f);   m_effectManager.SetProjectionMatrix(projection);I've tried moving the position in the world matrix that draws it from 0.1 to 0.9999, and it still draws on top of my ship.My ship is drawn using regular 3D projection, which has 0 near and 1000 far clip, and should have a bounding box of about 100x100x100 units.// Create a camera   RECT rect;   GetClientRect(m_hwnd, &rect);   LONG client_width  = rect.right  - rect.left;   LONG client_height = rect.bottom - rect.top;  m_camera = new FlightCamera(client_width,  client_height, 0.1f, 1000.0f, D3DXVECTOR3(  0.0f,  0.0f,    0.0f ), D3DXVECTOR3(  0.0f,  0.0f,    1.0f ), D3DXVECTOR3(  0.0f,  1.0f,    0.0f ));// SNIP//---------------------------------------------------------------------------------FlightCamera::FlightCamera(const unsigned int client_width,   const unsigned int client_height,  const float nearClip,  const float farClip,  const D3DXVECTOR3 & position,  const D3DXVECTOR3 & lookat,  const D3DXVECTOR3 & up)   :   BaseCamera(),   Transformable(){   // Set the camera's perspective matrix   D3DXMatrixPerspectiveFovLH(&m_projection,static_cast<float>(D3DX_PI) * 0.5f,static_cast<float>(client_width) / static_cast<float>(client_height),nearClip,farClip);// SNIPI've drawn the background at a 3D z of 998, with the same projection as the ship.All states are the defaults, except alpha which I turned off.Do I even have the ability to control the Z of a shape drawn with an ortho projection in relation to a shape drawn with a 3D projection?   
  Draw the background first, and then your ortho quad with zwrite set to off, this will not write any depth in the depth buffer. Then, render the ship and rest of the scene as usual.device->setRenderState(D3DRS_ZWRITEENABLE, FALSE);   ;  I could probably do that, but I would still like to be able to control the Z.What if I was trying to draw text above a player's head or something along those lines?I couldn't predict and order a render every frame, it would eat up my cpu.;  Generally you dont mix ortho and 3d the way that you are trying to, because as you are finding ortho is good for 2d and not for 3d.What you can do though is if you are really trying to draw a square on the screen that is a fixed size on the screen, but resides in 3d space you can...#1) Find the corners of the box in SCREEN SPACE (ie 2d)#2) Figure out what depth you want the rectangle to be at#3) Project rays into the screen from the corners of your box into the 3d world and figure out the 3d points where those 4 rays are the depth that you want for your rectangle.#4) ta da! you have the 4 3d points to make your rectangle.I'm really betting though this is way more complicated than you need or had in mind.Could you give some more info about what you are trying to do?  What does that white rectangle represent anyhow?   ;  It might one day be step 1 of a lens flare.It is also represents any attempt of understanding how to draw 2D in 3D. (Which I thought was easy). I will eventually have to draw a UI over the top of my scene as well as billboards.This particular white square was an occlusion test shape, but I don't have any of that code in right now. I just want to draw a 2D shape in 3D space.I will later need to determine how much of it is visible in order to control the alpha of my "star" graphic.I took all the occlusion crap out, I am just trying to see where the test would occur right now and succesfully draw it.I can't even get it transformed right now. I am so frustrated!Now the dern thing is not only off in the Z direction, but it is also rendering at the top right of my screen when it should be in the middle of it.I don't know how -50, -50, 0  is turning out to be .922, .902, .01World looks OK.View is IdentityProjection is now:D3DXMATRIX projection;   D3DXMatrixOrthoLH(&projection, static_cast<float>(viewport.Width), static_cast<float>(viewport.Height), 0.0f, 1.0f);The width and Height were correct when I checked in the debugger:1280x1024.This can't be that hard. I did it in an hour with DX9 years ago. Man, I wish I had that code still.;Quote:Original post by Atrix256Generally you dont mix ortho and 3d the way that you are trying to, because as you are finding ortho is good for 2d and not for 3d.What you can do though is if you are really trying to draw a square on the screen that is a fixed size on the screen, but resides in 3d space you can...#1) Find the corners of the box in SCREEN SPACE (ie 2d)#2) Figure out what depth you want the rectangle to be at#3) Project rays into the screen from the corners of your box into the 3d world and figure out the 3d points where those 4 rays are the depth that you want for your rectangle.#4) ta da! you have the 4 3d points to make your rectangle.I'm really betting though this is way more complicated than you need or had in mind.Could you give some more info about what you are trying to do?  What does that white rectangle represent anyhow?If I can't mix 2D and 3D, can I draw the test shape in 3D and billboard it somehow? Your algorithm above would work if I was allowed to change the vertices every single frame, but I'd hate to go locking vertex buffers every frame if I don't have to.Every billboarding example I'd found requires a static shape size and hardcodes it in the shader, which would suck. I would want to billboard any 3d shape, just make it face the camera and retain it original x,y dimensions.If I could so that I could also use it for my lens flare occlusion test shape. I'd just have to make it really big and hope the floating point error didn't get out of control. ;  Another example would be if I wanted to replace a 3D model with an explosion using a textured quad and the following image:Which is also something I want to do.I want it to be occluded if some other object is in front of it.   ;  when i was using FVF XYZRHW coordinates for rendering my gui, when i was set Z component to 0 it was rendered behind 3d objects. (i am using right handed coords)so if we convert it to ortho projection and discarding RHW, we need to know z component of ship's center point. also you should use a znear zfar parameters that same with perspective projection. so calculating correct z, you need to know center of your object (space ship), and take the distance between camera and ship center. then you should use this z component for your 2d rendering's z.if taking distance doesn't work you can calculate real z value of ship with projecting it like rendering. so output of shipcenter * perpesctiveprojmatrix * viewmatrix * worldmatrix will get you center coordinates of screen space ship. so you can use z component of it for rendering in 2d.so if you don't wanna change the coordinates of your quad each time you can use translated worldmatrix with that depth component. so it will automaticly translate your vertices before rendering.summary:z of your 2d rendering should be bigger than z of ship vertices to make it behind it. and for avoiding z culling, zfar of ortho projection should be bigger than 2d rendering's z.i hope this might help.   ;  Should not be a problem.  I have an application where I freely mix 2D and 3D data, using the Z coordinate to set whether they appear in front or behind the 3D objects.To render behind everything, use a number close to 1 (eg 0.9999999).To render in front of everything, use a number close to 0.To render at a 3D objects depth, use the vector3.project function to find the z coordinate.To render slightly in front of a 3D object, at a small vector distance back towards the camera position before using vector3.projectBuild your scene using the 2D objects first (it really shouldn't matter, but may be an issue if you are using alpha blending)Finally, make sure the W Buffer value of the 4th parameter in your Vector4 transformed point is set to 1.  Even though you are not using W buffers, I've noticed some graphics cards don't like this set to another value such as zero.   ;  I'll give it a shot, thanks.