   Rendering 2d texture (HUD) correctly using d3d9
  Hi,For this project I'm working on, I display a geographical chart in 3D and I need to show symbols and texts on the chart. To make them look natural, I transform the 3D position of the symbols and texts to 2D screen coordinates and I render the textures with the symbols and texts on that position by using untransformed vertices. It's like rendering a game HUD.I am not allowed to use any D3DX interface like ID3DXFont and Sprite.The problem is that this works almost perfect except for sometimes the texture misses a pixel or scanline or horizontal line. If I move around a bit, the texture is shown correctly again but another texture is shown badly. This is of course mostly visible on texts.The texts are rendered on textures of the exact dimension as the text itself (calculated with windows GDI). Textures are stored in my TTexture class.Here's the code:    D3DXVECTOR3 XY = Transform3DTo2D(p); // transform to screen coordinates   int wd = tex->GetWidth();    int hg = tex->GetHeight();    int x1 = XY.x - wd / 2;    int y1 = XY.y - hg / 2;    int x2 = x1 + wd;    int y2 = y1 + hg;    DWORD color = (alpha << 24) | 0xFFFFFF;    struct vertex    { float x, y, z, rhw; DWORD color; float tu, tv;    };    vertex v[] =    { x1, y1, 0, 1.0, color, 0, 0, x2, y1, 0, 1.0, color, 1, 0, x1, y2, 0, 1.0, color, 0, 1, x2, y2, 0, 1.0, color, 1, 1,    };    m_pD3DDev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);    m_pD3DDev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);    m_pD3DDev->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);    m_pD3DDev->SetRenderState(D3DRS_ZENABLE, false);    m_pD3DDev->SetRenderState(D3DRS_ZWRITEENABLE, false);    m_pD3DDev->SetRenderState(D3DRS_LIGHTING, false);    m_pD3DDev->SetTexture(0, tex->GetTexture());    m_pD3DDev->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);    m_pD3DDev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);    m_pD3DDev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);    m_pD3DDev->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);    m_pD3DDev->SetRenderState(D3DRS_ALPHABLENDENABLE, true);    m_pD3DDev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);    m_pD3DDev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);    m_pD3DDev->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);    m_pD3DDev->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, &v, sizeof(vertex));Can someone help me on how to make this look always correct?D.   
  To show what I mean, here's a screenshot of a situation where it is at its worse. I marked some of the problems with a red arrow.Watch it unshrinked to see what I mean.;  how are you doing the transform from 2d to 3d. I use the formula of ((x/Width) * 2) -1 and ((y/Height)*2) -1. Of course I am using shaders so I can bypass the projection step. If you are using ray casting or some other complex math technique there will be small errors that would cause what you are seeing.   ;Quote:Original post by david_watt78how are you doing the transform from 2d to 3d. I use the formula of ((x/Width) * 2) -1 and ((y/Height)*2) -1. Of course I am using shaders so I can bypass the projection step. If you are using ray casting or some other complex math technique there will be small errors that would cause what you are seeing.I used this code at this moment (I know, it's with d3dx, this will need to change and maybe I'll try your method):D3DXVECTOR3 TVideoDriver::Transform3DTo2D(const D3DXVECTOR3 &v){  D3DXVECTOR3 out;  D3DVIEWPORT9 vp;  m_pD3DDev->GetViewport(&vp);  D3DXVec3Project(&out, &v, &vp, &m_MatProj, &m_MatView, &m_MatWorld);  return out;}But I don't think that is the problem because I draw the texture with it's complete width/height which takes up the exact amount of pixels on the screen (at least that's what I expect).I was thinking about this, the function returns floats, but they are rounded to int so that can't be the problem too...   ;  welcome to sub pixel accuracy issues :)whats happening is that your quad's position, in transformed screen space, is some fraction of a pixel from the edge of the nearest pixel, or there is a subtle scaling error going on. eg: the quad starts at 0.1, and finishes at 2, as opposed to starting at 0, and finishing at 2. in the X axis.This sort of thing also happens when you use a non power of 2 scale to shrink textures containing thin (ie: 1 pixel) vertical or horizontal lines. so, a few things to check- are you rendering your glyphs at the same size (in pixels) as they appear in the source texture?- use PIX to check things like the exact, transformed quad locations, and make sure your not accumulating errora cheap fix for this is to render the font glyphs in transformed device co-ordinates (eg: pixel space), and/or clamping the transformed position to whole numbers (depending on the actual size of the screen space). another dirty fix, is adding 0.5 pixels in size to the quad you use to render the font glyphs, as this will in most cases solve any minor error that may have crept in.;Quote:Original post by Matt_Dwelcome to sub pixel accuracy issues :)...so, a few things to check- are you rendering your glyphs at the same size (in pixels) as they appear in the source texture?I really do think so yes. I'm using borland c++ builder and I'm using a VCL bitmap to render the text on. The size of this bitmap is calculated by VCL too. The bitmap is then copied pixel per pixel to the texture converting some specific background color to fully transparent.Quote:Original post by Matt_D- use PIX to check things like the exact, transformed quad locations, and make sure your not accumulating errorWhat is PIX? Sorry if this is a really stupid question...Quote:Original post by Matt_Da cheap fix for this is to render the font glyphs in transformed device co-ordinates (eg: pixel space), and/or clamping the transformed position to whole numbers (depending on the actual size of the screen space).I thought I was doing that by converting the floating point coordinates to int's first. Can you check my code above to see if I'm doing that right?Quote:Original post by Matt_D another dirty fix, is adding 0.5 pixels in size to the quad you use to render the font glyphs, as this will in most cases solve any minor error that may have crept in.I tried that but it gives the same bad results.   ;  What are you using for world, View, and Projection matrices? You might be rendering these texts away from the screen a small amount. Since you are using fixed function and not shaders from what I can see you might need to set the matrices to identity matrices or use shaders to avoid transforming and thereby change your coordinates and the size of your text when rendered.   ;Quote:Original post by david_watt78What are you using for world, View, and Projection matrices? You might be rendering these texts away from the screen a small amount. Since you are using fixed function and not shaders from what I can see you might need to set the matrices to identity matrices or use shaders to avoid transforming and thereby change your coordinates and the size of your text when rendered.I am quite sure that when using the D3DFVF_XYZRHW flag in the SetFVF function, you inform the fixed function pipeline that no transformation should be done and that the coordinates are all screen-mapped coordinates. Therefor, there is no need to set matrices.Am I assuming this incorrectly? And if so, why does it work (although with very little error) while the matrices are set to 3D-stuff that don't get near to identity matrices?;Quote:Original post by david_watt78What are you using for world, View, and Projection matrices? You might be rendering these texts away from the screen a small amount. Since you are using fixed function and not shaders from what I can see you might need to set the matrices to identity matrices or use shaders to avoid transforming and thereby change your coordinates and the size of your text when rendered.By the way, I just tried it, setting the transformation matrices to identity. Doesn't do anything different.   ;  you are correct. Wish I knew about that FVF setting my text looked awful till I switched to d3d10 and shaders. I would guess there is still some issue with coordinate precision in there some where or the image is compressed when rendering the text to the output texture. Your text is clearly being compressed as for my fonts I used GDI to render the chars to a DC as white text on black background then copy that to a texture for each image. I then use a shader to multiply the image color by the text color when rendering it. It allows me to use any windows font I want and it doesn't take long to create the font set either.   