Sign in to follow this  
SouthernMunk

[C++] Textured Quads

Recommended Posts

I was wondering if someone more experienced than myself is willing to show me what I'm doing wrong. I'm basically just drawing a rectangle (two triangles in a triangle strip) with a texture applied to it. I'm trying to make the whole program very object-orientated, so the source code is plucked from different areas.
[SOURCE]
// Defines:
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1|D3DFVF_DIFFUSE)

// Structures:
struct CUSTOMVERTEX
{
	float x, y, z;
	float u, v;
	DWORD colour;
};

// Initialise Direct3D and Device, etc.
// ...

// Initialise vertex buffer and texture.
void* pVertexBuffer = NULL;
CUSTOMVERTEX vertices[] =
{
	-1.0, 1.0, 1.0, 0.0, 0.0, 0x00ff0000,
	1.0, 1.0, 1.0, 1.0, 0.0, 0x0000ff00,
	-1.0, -1.0, 1.0, 0.0, 1.0, 0x0000ff00,
	1.0, -1.0, 1.0, 1.0, 1.0, 0x000000ff,
};

m_pDevice->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_pVB, NULL);

m_pVB->Lock(0, sizeof(vertices), &pData, 0);

memcpy(pData, vertices, sizeof(vertices));

m_pVB->Unlock();

D3DXCreateTextureFromFile(m_pDevice, "texture128.bmp", &m_pTexture);

// Rendering.
m_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, 0x00000000, 0.0, 0);

m_pDevice->BeginScene();
m_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
m_pDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
m_pDevice->SetStreamSource(0, m_pVB, 0, sizeof(CUSTOMVERTEX));
m_pDevice->SetTexture(0, m_pTexture);
m_pDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
m_pDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
m_pDevice->EndScene();

m_pDevice->Present(NULL, NULL, NULL, NULL);
[/SOURCE]
The image is stretched when rendered. Like 1 column for the whole thing.

Share this post


Link to post
Share on other sites
Quote:
Original post by Namethatnobodyelsetook
When using an FVF the data must come in a specific order. In this case, position, color, then UVs.

The order things must come in is here.


LOL! Thanks so much. I'm new to DirectX (and 3D programming in general) so I should have known it would be something obvious. Funny thing is, when I add:


m_pDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION );




it works (though it's kinda like the MIRROR texturing method).

Thanks again buddy!

BTW: I was going to choose your username when I signed up. ;)

EDIT: Also, the colours were turning out funny too. Guess I know why now! :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this