• Advertisement
Sign in to follow this  

2D in D3D9 Problem

This topic is 4745 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 guys, I read both of the 2D in D3D tutorials in the articles section and I'm having a problem. When I was setting the rhw to 1.0 and not doing any transformations it was working perfectly; but I changed it over to use an orthogonal perspective and I have a slight problem: If I have a quad that's 128x128 and I put it at x=800-128=672,y=600-128=472 the bottom row gets cut off (as if I put it at y=473), the right side is fine (I'm in 800x600 resolution.) I got the texture to line up nicely without the double top and left borders by using the -0.5 trick, but I don't know what to do to get the position right. What should I do ? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
I changed it over to use an orthogonal perspective

You mean changing the D3DTS_PROJECTION matrix? Unless I'm being stupid - that has no effect on 2D geometry, so changing it really shouldn't affect your 2D rendering in the slightest.

Also, the screen should be measured as 0 < X < 800 and 0 < Y < 600, which is actually 0..799 and 0..699 - sure this isn't your problem?

hth
Jack

Share this post


Link to post
Share on other sites
Are the texture coordinates for the right and bottom side off by one maybe?
Remember, you need to set them one pixel higher than the output is supposed to be (e.g. 256.0f / 256.0f for the rightmost pixel in a texture).

Share this post


Link to post
Share on other sites
Hi guys,

I think I got it perfect now. Does this seem right:

// setup matrices for 800x600 screen

D3DXMATRIX Ortho2D;
D3DXMATRIX Identity;

D3DXMatrixOrthoLH(&Ortho2D,800.0f,600.0f,0.0f,1.0f);
D3DXMatrixIdentity(&Identity);

g_pDevice->SetTransform(D3DTS_PROJECTION,&Ortho2D);
g_pDevice->SetTransform(D3DTS_WORLD,&Identity);
g_pDevice->SetTransform(D3DTS_VIEW,&Identity);

// I setup the vertex buffer so the top left is at 0,0 like so:
// (only x,y coordinates posted here)
// (setup for 128x128 quad)

// bottom left
pVertices[0].x=-400.0f;
pVertices[0].y=300.0f-128.0f;

// top left
pVertices[1].x=-400.0f;
pVertices[1].y=300.0f;

// bottom right
pVertices[2].x=-400.0f+128.0f;
pVertices[2].y=300.0f-128.0f;

// top right
pVertices[3].x=-400.0f+128.0f;
pVertices[3].y=300.0f;

// now to blit I just do this:

D3DXMATRIX matPos;
D3DXMatrixTranslation(&matPos,xDest,-yDest,0.0f);
g_pDevice->SetTransform(D3DTS_WORLD,&matPos);

It seems to work great but maybe I am missing something ? What do you think ?

PS - I have it set to use a TRIANGLESTRIP, maybe a FAN is faster ?

Take care.

Share this post


Link to post
Share on other sites
I'm not too sure where I read this, but I think a triangle fan would run faster on most cards...something about driver assumptions...

Share this post


Link to post
Share on other sites
Man, I'm having the same problem when I texture it.

The texture doesn't line up unless I subtract 0.5 from each vertex, but when I do that the position is off one pixel in the Y-direction.

If I don't subtract the 0.5 the position is perfect and the texture is bad, if I subtract the 0.5 the texture is perfect but the position is bad.

What should I do ?

Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Endemoniada
Man, I'm having the same problem when I texture it.

The texture doesn't line up unless I subtract 0.5 from each vertex, but when I do that the position is off one pixel in the Y-direction.

If I don't subtract the 0.5 the position is perfect and the texture is bad, if I subtract the 0.5 the texture is perfect but the position is bad.

What should I do ?

Thanks.

Have you read the "Directly mapping texels to pixels" article in the SDK documentation (See also: FAQ entry)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

/* your 2d vertex struct */

typedef struct tagCUSTOMVERTEX2D
{
FLOAT x, y, z;
FLOAT rhw;
D3DCOLOR color;
FLOAT u, v;
} CUSTOMVERTEX2D;

#define D3DFVF_CUSTOMVERTEX2D (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1)


/* 2d render */

CUSTOMVERTEX2D p[4];

p[0].x = x1; p[0].y = y1;
p[1].x = x2; p[1].y = y1;
p[2].x = x1; p[2].y = y2;
p[3].x = x2; p[3].y = y2;

p[0].z = p[1].z = p[2].z = p[3].z = 0.5f;
p[0].rhw = p[1].rhw = p[2].rhw = p[3].rhw = 1.0f;
p[0].color = p[1].color = p[2].color = p[3].color = 0xFFFFFFFF;

p[0].u = 0.0f; p[0].v = 0.0f;
p[1].u = 1.0f; p[1].v = 0.0f;
p[2].u = 0.0f; p[2].v = 1.0f;
p[3].u = 1.0f; p[3].v = 1.0f;

pd3dDevice->SetTexture(0, pTexture);
pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX2D);
pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, &p[0], sizeof
(CUSTOMVERTEX2D));


Made In Taiwan

Share this post


Link to post
Share on other sites
If you are using RHW, you can't do transformations, because you say to Direct3D to put not verticles trought transformations...
PS: At least I think.

Share this post


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

  • Advertisement