Jump to content
  • Advertisement
Sign in to follow this  
icethelog

Direct X 9 C++ - Texture turning black when moving away from object

This topic is 3300 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

Hi, I have a problem in my application where I am creating a texture of a grid using D3DXCreateTexture, but the further away I move from the textured object it becomes black: Photobucket I'm not really sure what could be causing this. I tried changing my shader to output a colour rather than the texture, and it no longer turns black when you move away and stays the correct colour. My code for creating the texture is as follows:
D3DLOCKED_RECT rect;
	BYTE bCol;
	vector <float> fvCol;

	D3DXCreateTexture(m_pDevice,512,512,D3DX_DEFAULT,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED, &m_pTexture);

	srand((unsigned)time(0));

	for(int i = 0; i < 512; i++)
	{
		for(int j = 0; j <512; j++)
		{
			//Reset colours
			if(i % 32 == 0 && j == 0)
			{
				//When you move down a window clear the array
				fvCol.clear();
			}

			//Assign colours
			if(j % 32 == 0)
			{
				//Add new window colour to array when you change along horizontal axis
				fvCol.push_back((rand() % 101) / 100.0f);

				//Set current colour to border colour
				bCol = 0.0f * 255.0f;
			}
			else if(i % 32 == 0)
			{
				//Set current colour to border colour
				bCol = 0.0f * 255.0f;
			}
			else
			{
				//Set current colour to row in the array
				int number = (j - (j % 32))/32;

				bCol = fvCol[(j - (j % 32))/32] * 255.0f;
			}

			//Set pixel colour
			m_pfaImage[j] = D3DCOLOR_ARGB((BYTE)(1 * 255.0f), (BYTE)(1 * 255.0f), bCol, bCol);
		}
	}

	//Lock the texture and copy over the contents of the array
	m_pTexture->LockRect(0, &rect, NULL, NULL);    
		memcpy(rect.pBits, &m_pfaImage, sizeof(m_pfaImage));  
	m_pTexture->UnlockRect(0);



Here's my shader: VS_OUTPUT vs_main(VS_INPUT Input ) { VS_OUTPUT Output; float4x4 matViewProjection = mul(View,Projection); float4x4 matWorldViewProjection = mul(World,matViewProjection); Output.Position = mul(Input.Position, matWorldViewProjection); Output.Texcoord = Input.Texcoord; return Output; } struct PS_INPUT { float4 Position : POSITION0; float2 Texcoord : TEXCOORD0; }; float4 ps_main( PS_INPUT Input ) : COLOR0 { float4 colour = tex2D(TextureSampler, Input.Texcoord); return colour; } Unfortunately I'm not even sure if I'm looking in the right place for the cause of this problem. Any help would be appreciated. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Is it a mipmap problem? You have specified default for MipLevels which means create a full mipmap chain but only filled in level 0

Share this post


Link to post
Share on other sites
Thanks, that's sorted it. Passing in D3DUSAGE_AUTOGENMIPMAP to Usage parameter seemed to work, although I am consfused as why it's not mentioned in the documentation

http://msdn.microsoft.com/en-us/library(d=default)/bb172800(l=en-us,v=VS.85).aspx

Share this post


Link to post
Share on other sites
Quote:
Original post by icethelog
Thanks, that's sorted it. Passing in D3DUSAGE_AUTOGENMIPMAP to Usage parameter seemed to work, although I am consfused as why it's not mentioned in the documentation

http://msdn.microsoft.com/en-us/library(d=default)/bb172800(l=en-us,v=VS.85).aspx
It is:
Quote:
MipLevels
[in] Number of mip levels requested. If this value is zero or D3DX_DEFAULT, a complete mipmap chain is created.
If you create more than one mip level, then you need to fill more than one.

Share this post


Link to post
Share on other sites
I presumed if I created a complete mipmap chain all the levels would be filled automatically, without having to pass in D3DUSAGE_AUTOGENMIPMAP as it isn't mentioned anywhere.

Edit: I just realised the answer to my own question, when you call LockRect you can pass in the mipmap level there.

Share this post


Link to post
Share on other sites
Quote:
Original post by icethelog
I presumed if I created a complete mipmap chain all the levels would be filled automatically, without having to pass in D3DUSAGE_AUTOGENMIPMAP as it isn't mentioned anywhere.
That would partially defeat the point in mipmaps; if that was the case, then you could only specify a single image to be displayed as a texture, and not a chain of mipmaps.

For the best looking textures, the mip maps should be generated manually (Or at least tweaked) offline, to remove as much blur as possible - that keeps textures in the distance from looking blurry.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!