Mysterious Rigids On Rendered Textures

Started by
5 comments, last by Hodgman 9 years, 10 months ago

Hello everyone,

I am trying to fix an issue that causes some (not all) sprites to have smudges/rigids when rendered. I am using a vertex buffer to render the sprites in 3D space. They all render with the correct size, etc. so that no resizing occurs during render-time. Here is an image of the issue I am clueless about (see "Character Info" dialog box):

2utp7qf.jpg

Here is the texture/spritesheet that the game is reading from (as you can see the original doesn't have these rigids on the "Character Info" dialog box):

30hvslv.jpg

I have been testing and discovered this issue may be related to non-power of 2 textures being resized to power 2 sizes. That being said since this texture (above spritesheet) is 619x598 and is forced to be resized to 1024x1024 when the texture is created, I decided to manually change the size of the spritesheet image file to 1024x1024 to see if resizing during texture creation was the issue; however, the issue still remained with no improvements.

Also the position of the vertexes on the screen and on the spritesheet are all consistent with the size of the dialog box (270x376):

Screen(-0.687500~0.156250, -0.775000~0.791667)

Spritesheet(0.000000~0.263672, 0.000000~0.367188)

Both the screen position and texture coordinates equate to 270x376 being the dialog box size so it is not stretching at all but it is definitely losing quality for some unknown reason. If you want to verify that it's not being stretched, the screen size is 640x480 in this case and the texture size is 1024x1024 after "extending" (not stretching) to a power-of-2 size.

I am using the following line to create the texture:


D3DXCreateTextureFromFileInMemoryEx(device, imageBuffer, m_imageSize, D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, format, D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &m_texture);

Since I am passing D3DX_FILTER_NONE as the filter parameters "No scaling or filtering will take place. Pixels outside the bounds of the source image are assumed to be transparent black." as stated by the official MSDN website. That being said I don't know why the image is losing quality even though its not being stretched rather only extended and no harm should be done, right? The weird thing is when I passed D3DX_DEFAULT_NONPOW2 as the image sizes as opposed to D3DX_DEFAULT it looks perfect. But other issues arise and besides I don't want to use non-power-2 textures since its not entirely cross-compatible at least with older graphics cards which is what I'm targeting.

Any help on the matter is greatly appreciated. Also feel free to ask any more questions to clear things up!

Edit: This just came to mind, is it possible the rigids/smudges are caused from a vertical flip of the sprites since the bottom and top are flipped to properly display the sprites? If so how can I approach this another way so DirectX won't flip the sprites upside-down?

Advertisement

Vertical flipping should not cause that problem. If you're using DX9 or below, it might depend on how you map texels to pixels though (offset the vertex position or offset the texture coordinates?)

This problem can also appear if your backbuffer size does not match the client size of the window.

You DID use AdjustWindowRect(Ex) to find the correct window size for the required client size?

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Vertical flipping should not cause that problem. If you're using DX9 or below, it might depend on how you map texels to pixels though (offset the vertex position or offset the texture coordinates?)

This problem can also appear if your backbuffer size does not match the client size of the window.

You DID use AdjustWindowRect(Ex) to find the correct window size for the required client size?

I'm using DX9. Also I don't think the issue is mapping texels to pixels although I cannot guarantee that since this is my first project using a vertex buffer. Here is how I do it:


//Here rtWidth and rtHeight are the width/height of the render target respectively.
//position.x/y are the pixel positions of the sprite
//sSpriteWidth/sSpriteHeight are the width/height of the sprite
// If you notice the texels range from -1.0 to +1.0 on the render screen; position (0,0) being the center of the screen

Vertex Positions Calculations:
positionLeft = (position.x - (rtWidth/2.0f))/(rtWidth/2.0f);
positionRight = (position.x+(float)sSpriteWidth - (rtWidth/2.0f))/(rtWidth/2.0f);
positionTop = ((rtHeight/2.0f) - position.y)/(rtHeight/2.0f); // top/bottom flipped
positionBottom = ((rtHeight/2.0f) - (position.y+(float)sSpriteHeight))/(rtHeight/2.0f); // top/bottom flipped


//m_wWidth/m_wHeight are the width/height of the spritesheet (1024x1024 after being resized into a power-2-texture from its original 619x598)
//here the texture coordinates range from 0.0 to 1.0

Texture Coordinate Calculations:
srcLeft = (float)srcRect.left/(float)m_wWidth;
srcTop = (float)srcRect.top/(float)m_wHeight;
srcRight = (float)srcRect.right/(float)m_wWidth;
srcBottom = (float)srcRect.bottom/(float)m_wHeight;

Do I still need to offset the vertex position and texture coordinates even though they range from -1.0 to +1.0 and 0.0 to 1.0 respectively? If so how much should I offset it by? Do I need to change them to range from the size of the backbuffer/spritesheet to implement a 0.5 offset?

Here is my vertex buffer array:


CUSTOM2DVERTEX vertices[] = {
{ D3DXVECTOR3(positionLeft, positionTop, 1.0f), D3DXVECTOR2(srcLeft, srcTop), D3DXCOLOR(255, 255, 255, alpha) }, // left top
{ D3DXVECTOR3(positionLeft, positionBottom, 1.0f), D3DXVECTOR2(srcLeft, srcBottom), D3DXCOLOR(255, 255, 255, alpha) }, // left bottom
{ D3DXVECTOR3(positionRight, positionTop, 1.0f), D3DXVECTOR2(srcRight, srcTop), D3DXCOLOR(255, 255, 255, alpha) }, // right top
{ D3DXVECTOR3(positionRight, positionBottom, 1.0f), D3DXVECTOR2(srcRight, srcBottom), D3DXCOLOR(255, 255, 255, alpha) }, // right bottom
};

// here is the draw call, I just skipped the vertex buffer lock/unlock (memory copying) and SetTexture steps for simplicity
DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

Also to clarify that my vertex position/texture coordinate calculations are correct, this is what is calculated for the specific "Character Info" dialog box sprite:

positionLeft = -0.687500
positionRight = 0.156250
positionTop = 0.791667
positionBottom = -0.775000

Since the actual size of the "Character Info" sprite is 270x376 pixels and the render screen/backbuffer is 640x480 pixels: abs(-0.687500)*(640/2) + abs(0.156250)*(640/2) = 270.00000 pixels AND abs(0.791667)*(480/2) + abs(-0.775000)*(480/2) = 376.00008 pixels. Therefore the vertex positions are calculated properly, right?

srcLeft = 0.000000
srcTop = 0.000000
srcRight = 0.263672
srcBottom = 0.367188

Since the actual size of the "Character Info" sprite is 270x376 pixels and the spritesheet is 1024x1024 pixels after being extended: 0.263672*1024 = 270.000128 pixels AND 0.367188*1024 = 376.000512 pixels. Therefore the texture coordinates are calculated properly, right?

I haven't used AdjustWindowRect, this is my first time hearing about this. Anyways I did what you told me so now I adjusted the window size to make the client size the same as the backbuffer (640x480). This didn't resolve the issue unfortunately sad.png, also the issue applies in fullscreen as well as window mode so it makes sense that this wasn't the problem. But I still thank you for letting me know about this function since I would prefer the client size to be the same as the backbuffer in window mode anyhow smile.png

Also for further investivation here is how it looks like when passing D3DX_DEFAULT_NONPOW2 into the image/texture size:

2q2eats.jpg

As you can see it looks perfect but other issues arise with the tiles in the background (black outlines, etc.). All the images here use the same process to render them so I don't see what could be causing this either when using non-power-2 textures because my graphics card supports all texture sizes without limitations (checked with device TextureCaps) although that could be a sticky statement since I expect nothing to be perfectly accurate at this point.

But if you look closely it seems as though something is wrong with the projection which may be causing these lines/rigids? It's as if everything is on an angle but not really, maybe more so magnified at a very small interval. Should I post my matrix transformations?

Changing my matrix transformations don't seem to be affecting anything at all :S I know 100% that my vertex/pixel shaders are used though. What could possibly be overwriting my matrix transformations? Is it the fact that I handle transformations outside of my shaders so that they have no effect at all? Will this make the difference in loss of quality if transformations aren't being applied by my calculations? If they're not being applied what is performing these transformations, is it mere luck that I so happen to be able to see the sprites rendered without calculating the matrices myself?

Sorry for asking so many questions, I just want to understand how these things work so I won't run into anymore problems in the future.

In my DX9 code I've offset the vector position by -0.5, -0.5 for all 2d textured rendering.

I recall having a few difficulties when the 2d rendering was performed not by pretransformed vertices (and ran through the matrices) In the worst case you may have to clamp the final coordinates to direct multiples of 0.5 (which can be tough when using matrices)

I see you're using shaders, I'm not sure if that makes a difference, I'm still using fixed function.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

In my DX9 code I've offset the vector position by -0.5, -0.5 for all 2d textured rendering.

I recall having a few difficulties when the 2d rendering was performed not by pretransformed vertices (and ran through the matrices) In the worst case you may have to clamp the final coordinates to direct multiples of 0.5 (which can be tough when using matrices)

I see you're using shaders, I'm not sure if that makes a difference, I'm still using fixed function.

I fixed it thanks to this suggestion! I adjusted my projection matrix and vertex positions to range from 0x0 to 640x480 instead of the original (-1,-1) to (1,1) and then made the -0.5 offsets in the vertex position values. This solved my issue, I can't tell you how thankful I am!

In case anyone else comes across this thread, here's the official explanation and workarounds for the dreaded Direct3D9 half pixel offset issue:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb219690(v=vs.85).aspx

If using -1:1 coordinates, then instead of -0.5, the offset would be -0.5/width and 0.5/height.

This topic is closed to new replies.

Advertisement