Sign in to follow this  
Jaapio

OpenGL problem with texcoords (using multiple buffers)

Recommended Posts

Hi, I am trying to render a mesh using two buffers, one of the vertexes the other for the uv coords. http://www.jaapvanderwulp.nl/tech/doom3.jpg This is my code but as you can see in the image, it does not work. Could it be that directx uses texture coords in another way that openGL does because the mesh does look normal in openGL. Or am I forgetting something? Here is the draw code.
LPDIRECT3DVERTEXBUFFER9 Vertex_Buffer;
LPDIRECT3DVERTEXBUFFER9 Coord_Buffer;
LPDIRECT3DVERTEXDECLARATION9 VertexDeclaration = NULL;

for(uint i=0; i<NumSubMeshes; i++)
{
	uint vertexsize = SubMeshes[i].VertexCount*sizeof(CVector3);
	uint coordsize = SubMeshes[i].VertexCount*sizeof(CTexCoord);

	// Create buffer
	if(FAILED(gEngine.pDirect3DDevice->CreateVertexBuffer(vertexsize, 0, 0,
                                        D3DPOOL_DEFAULT, &Vertex_Buffer, NULL)))
	{
		MessageBox(NULL, L"CreateVertexBuffer", L"Vertex_Buffer", MB_OK);
		return;
	}

	// Lock
	CVector3 *Vertices;

	// Lock the buffer we can write to it.
	if(FAILED(Vertex_Buffer->Lock(0, vertexsize, (void**)&Vertices, 0)))
	{
		MessageBox(NULL, L"Lock", L"Vertex_Buffer", MB_OK);
		return;
	}

	// Here we copy the square's data into the vertex buffer.
	memcpy(Vertices, SubMeshes[i].Vertexes, vertexsize);

	// Unlock when your done coping data into the buffer.
	if(FAILED(Vertex_Buffer->Unlock()))
	{
		MessageBox(NULL, L"Lock", L"Vertex_Buffer", MB_OK);
		return;
	}

	// Create buffer
	if(FAILED(gEngine.pDirect3DDevice->CreateVertexBuffer(coordsize, 0, 0,
                                        D3DPOOL_DEFAULT, &Coord_Buffer, NULL)))
	{
		MessageBox(NULL, L"CreateVertexBuffer", L"Coord_Buffer", MB_OK);
		return;
	}

	// Lock
	CTexCoord *pCoords;

	// Lock the buffer we can write to it.
	if(FAILED(Coord_Buffer->Lock(0, coordsize, (void**)&pCoords, 0)))
	{
		MessageBox(NULL, L"Lock", L"Coord_Buffer", MB_OK);
		return;
	}

	// Here we copy the square's data into the vertex buffer.
	memcpy(pCoords, SubMeshes[i].UVs, coordsize);

	// Unlock when your done coping data into the buffer.
	if(FAILED(Coord_Buffer->Unlock()))
	{
		MessageBox(NULL, L"Unlock", L"Coord_Buffer", MB_OK);
		return;
	}


	//Declare
	D3DVERTEXELEMENT9 Declaration[] = 
	{
		{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		{1, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
		D3DDECL_END()
	};

	// Create the vertex declaration so DirectX knows how our vertex is made up.
	if(FAILED(gEngine.pDirect3DDevice->CreateVertexDeclaration(Declaration, &VertexDeclaration)))
	{
		MessageBox(NULL, L"CreateVertexDeclaration", L"", MB_OK);
		return;
	}

	if(!VertexDeclaration)
	{
		MessageBox(NULL, L"!VertexDeclaration", L"", MB_OK);
		return;
	}

	//Set the stream sources
	if(FAILED(gEngine.pDirect3DDevice->SetStreamSource(0, Vertex_Buffer, 0, sizeof(CVector3))))
	{
		MessageBox(NULL, L"SetStreamSource", L"Vertex_Buffer", MB_OK);
		return;
	}

	if(FAILED(gEngine.pDirect3DDevice->SetStreamSource(1, Coord_Buffer, 0, sizeof(CTexCoord))))
	{
		MessageBox(NULL, L"SetStreamSource", L"Coord_Buffer", MB_OK);
		return;
	}

	// Set the vertex stream declaration.
	if(FAILED(gEngine.pDirect3DDevice->SetVertexDeclaration(VertexDeclaration)))
	{
		MessageBox(NULL, L"SetVertexDeclaration", L"", MB_OK);
		return;
	}

	HRESULT r = gEngine.pDirect3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, SubMeshes[i].VertexCount/3);
	if(FAILED(r))
	{
		LPWSTR t = (LPWSTR)malloc(512);
		wsprintfW(t, L"Error %u", r);
		MessageBox(NULL, t, L"DrawPrimitive", MB_OK);
		delete []t;
		return;
	}

	VertexDeclaration->Release();
	Vertex_Buffer->Release();
	Coord_Buffer->Release();

}

Share this post


Link to post
Share on other sites
I can't see anything obviously wrong with the code you've posted. Are you getting any debug warnings whilst running your app? It's quite possible a state is invalid but the HRESULT's will not return an error.

Have you checked against the reference rasterizer? Shouldn't be necessary, but can't help to verify that its not an odd characteristic of your hardware.

Have you done a frame capture and analyzed it in PIX? You should be able to dig down to the pipeline configuration and see how it's assembling the data it actually renders.

Also, have you double-checked against the MultiStreamRendering Sample? Ignore the D3D10 stuff...

hth
Jack

Share this post


Link to post
Share on other sites
Well I just found out that it isn't because I'm using multiple buffers.
I put everything into one buffer and still the result is the same.
On to the harder part.

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