Jump to content
  • Advertisement
Sign in to follow this  
DotDLL

2D Rendering Errors with Direct3D 9

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

Hello, I'm running into a problem creating a 2D game with Direct3D 9. I have a function, render_texture, that is supposed to render a texture onto the screen. Each texture being drawn is part of a TextureGrid struct, whereby a larger texture is broken down into a series of smaller textures. When I try to render a single texture (grid dimensions = 1x1), I do not encounter any problems. When I try to render a larger grid, in this case a 4x4 grid of textures (which should render as if it were a single image), I get rendering errors. More specifically, I get a series of lines that start from the top-left of the screen and end somewhere in the image. The weird part is, if I add a few dummy variables to my "render_texture" function, the lines go away. Here is the function I am using, along with the definition of the vertex structure I am using:
const DWORD tri_fvf = D3DFVF_XYZRHW | D3DFVF_TEX1 | D3DFVF_DIFFUSE;

struct textured_vertex {
	float x, y, z, rhw;
	DWORD diffuse;
	float tu,tv;
};

static HRESULT render_texture(IDirect3DDevice9 *device, IDirect3DTexture9 *texture, int x, int y, int width, int height)
{
	HRESULT result;
	float realX = (float)x + 0.5f;
	float realY = (float)y + 0.5f;
	float u1 = 0.0f;
	float v1 = 0.0f;
	float u2 = 1.0f;
	float v2 = 1.0f;

	/* If I comment out the following line, I get rendering errors. */
	Uint8 dummy_variable;

	struct textured_vertex vertices[] = {
		{ realX,         realY,          0, 1, 0, u1, v1 },
		{ realX + width, realY,          0, 1, 0, u2, v1 },
		{ realX,         realY + height, 0, 1, 0, u1, v2 },
		{ realX + width, realY + height, 0, 1, 0, u2, v2 }
	};

	result = IDirect3DDevice9_SetTexture(
		device,
		0,
		(IDirect3DBaseTexture9 *) texture);
	if ( FAILED(result) ) {
		return result;
	}

	result = IDirect3DDevice9_DrawPrimitiveUP(
		device,
		D3DPT_TRIANGLESTRIP,
		4,
		vertices,
		sizeof(struct textured_vertex));
	if ( FAILED(result) ) {
		return result;
	}

	return S_OK;
}


When dummy_variable is defined, the rendering looks like: this. This is what things should look like. When dummy_variable is not defined, things look like: this. If I call render_texture with different x and y values, the lines I am not terribly against having an extra variable in render_texture, however it strikes me as really odd that this is happening at all. My question is this: is this indicative of a larger problem? If so, does anything in the code sample above look odd? A few extra notes: * This occurs both in Direct3D's software renderer (D3DDEVTYPE_REF), as well as the accelerated one (D3DDEVTYPE_HAL). * dummy_variable cannot be defined anywhere else for this hack to work. * Uint8 is a typedef for a single-byte, unsigned integer. * dummy_variable's type seems to be irrelevant. Defining it to be a 4 byte value works just as well. I've also been able to define 2, 3, 4, 5, and 6 of them at once, in which case the hack still works. * If I define dummy_variable in between any of the floats, the rendering errors get worse. * I'm using MSVC++ 2003 to compile and link this code. The code is also being compiled not as C++ code but as straight C. Any help and/or thoughts would be greatly appreciated. This bug/oddity has been driving me nuts for a good day now!

Share this post


Link to post
Share on other sites
Advertisement
Hi, DotDLL

struct textured_vertex vertices[] = {
{ realX, realY, 0, 1, 0, u1, v1 },
{ realX + width, realY, 0, 1, 0, u2, v1 },
{ realX, realY + height, 0, 1, 0, u1, v2 },
{ realX + width, realY + height, 0, 1, 0, u2, v2 }};

X, Y, Z, R, D, TU, TV

Make sure that 'd'(Diffuse) is four bytes long (a DWORD), otherwise, it could screw things up. It could be compiling as a integer, not a unsigned long. As how this relates to the dummy variable, I don't know.


PS Your FVF declares diffuse as a four byte long(DWORD), thats why 'd' needs to be four bytes long in your vertices struct.

Hope this helps,
ProgrammingNerd

Share this post


Link to post
Share on other sites
Might also be the primitive count you pass in as four, although your array only contains two triangles. It's a bit confusing, the primitive count is the number of triangles on rendering trianglelists, triangle fans and strips.

It's not the number of vertices but the number of the primitives. Which is dependant on the primitive type.

Share this post


Link to post
Share on other sites
It was the primitive count. Setting that to 2 instead of 4 fixed the problem. I'm surprised it even worked without it!

Thanks for y'alls help!

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!