XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

Transform vertex coordinate to pixel coordinate?

Last post 7/10/2009 12:35 AM by The Joss Master. 5 replies.
  • 7/9/2009 1:17 AM

    Transform vertex coordinate to pixel coordinate?

    Hi everyone,

    I have the vertex of an object and I would like to calculate what the pixel coordinate will be in my backbuffer.  I tried to multiply the homogenous coordinate by the product of multiplying World * View * Projection matrices, but that doesn't work.  If I leave out the projection matrix it works perfectly for points of the form  (x, y, 0, 1), but if the point isn't on the z == 0 plane, the lack of a projection transform screws everything up. 

    To adjust for the projection I tried doing something like:
    x += z * projection._11;
    y += z * projection._22;

    but it didn't work.

    Thanks!
  • 7/9/2009 1:38 AM In reply to

    Re: Transform vertex coordinate to pixel coordinate?

    You had it right by multiplying by the WorldViewProjection.  You just need to divide the product by it's w.

    float4 screenpos = mul(pos, WorldViewProjection); 
    screenpos /= screenpos.w; 

  • 7/9/2009 4:18 PM In reply to

    Re: Transform vertex coordinate to pixel coordinate?

    I forgot to mention that I was doing that : \  it's really weird
  • 7/9/2009 5:34 PM In reply to

    Re: Transform vertex coordinate to pixel coordinate?

    Well that's really strange.  Maybe there's something wrong with your projection matrix.  Anyway, if you don't need the position until the pixel shader, you could just use VPOS and move that back into projection space.
  • 7/10/2009 12:00 AM In reply to

    Re: Transform vertex coordinate to pixel coordinate?

    I'm kind of new to this, what does VPOS mean?

    I set the view and projection matrices when initializing Direct3D.  Then when rendering I do this:
          World = i->second->getTransform() * m_BufferToWorld;
          hr = m_pD3DDevice9->SetTransform(D3DTS_WORLD, &World);
    The buffer to world matrix is used because I use buffer-space coordinates to define the vertices in my polygons.

    Here's the code I'm using to convert from object coordinates to window coordinates:

       D3DXMATRIX matProj;
       D3DXMATRIX matView;
       hr = m_pD3DDevice9->GetTransform(D3DTS_PROJECTION, &matProj);
       hr = m_pD3DDevice9->GetTransform(D3DTS_VIEW, &matView);
       D3DXMATRIX mWorldView = obj->getTransform() * m_BufferToWorld * matView * matProj;
      
       multiplyMatrixVector(mWorldView, Coord);
       
       Coord /= Coord.w;

    This should give me world coordinates, right?  Nothing comes out right this way.  If I leave out the projection matrix, I get x, y coordinates that are fine (for an orthogonal projection), but the z coordinate is always off. 
  • 7/10/2009 12:35 AM In reply to

    Re: Transform vertex coordinate to pixel coordinate?

    Oh sorry.  I assumed this was for a shader.  In this case there is no VPOS.  I'm a bit confused as to why you have obj->getTransform() as well as m_BufferToWorld.  What exactly is obj?  Also make sure that Coord.w = 1 before the multiplication.  This projection matrix works fine for rendering right?
Page 1 of 1 (6 items) Previous Next