Sign in to follow this  

Very strange Index buffer (solved)

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

I tried to render a grid using an index buffer and was getting some very strange results * it looked like half the triangles weren't being rendered, * some of the triangles were not what I specified, attaching random vertices together * and the overall dimensions of the grid weren't in line with my vertices. * there was a horizontal offset of 50% and a linear skew Rendering the vertex buffer as a point list showed the vertices to be in the correct place. Spitting out the individual values for the index buffer, I saw that they all appeared to be correct too. Ok...so I set up a very simple test case. I would just manually build a VB and IB for 2 quads (4 triangles). I have stripped out all the unnecessary code to show you just what I'm doing...
#include "stdafx.h"
#include "NovaClipmaps.h"

namespace Nova
{
	extern LPDIRECT3DDEVICE9 dxDevice;

	IDirect3DVertexBuffer9 *clipVB = NULL;
	IDirect3DIndexBuffer9 *clipIB = NULL;

	static const DWORD D3DFVF_TLVERTEX = D3DFVF_XYZ | D3DFVF_TEX1;
	struct TLVERTEX
	{
		float x,y,z,u,v;
	};

	ClipmapManager::ClipmapManager()
	{
	}

	ClipmapManager::~ClipmapManager()
	{
		SAFE_RELEASE( clipVB );
		SAFE_RELEASE( clipIB );

	}

	void ClipmapManager::RenderClipmaps()
	{
		dxDevice->SetStreamSource(0, clipVB, 0, sizeof(TLVERTEX));
		dxDevice->SetFVF(D3DFVF_TLVERTEX);
		dxDevice->SetIndices( clipIB );

		dxDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
		dxDevice->DrawPrimitive(D3DPT_POINTLIST, 0, 6);
		dxDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 6, 0, 4);
	}

	void ClipmapManager::BuildClipmaps(const char *file)
	{
		UINT numTris = 4, numVerts = 6;

		dxDevice->CreateVertexBuffer(sizeof(TLVERTEX)*numVerts, D3DUSAGE_WRITEONLY, D3DFVF_TLVERTEX, D3DPOOL_MANAGED, &clipVB, NULL);
		dxDevice->CreateIndexBuffer(sizeof(WORD)*3*numTris, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &clipIB, NULL);

		TLVERTEX* verts;
		clipVB->Lock(0, 0, (void**)&verts, NULL);			
			verts[0].x = 0;
			verts[0].y = 10;
			verts[0].z = 0;

			verts[1].x = 15;
			verts[1].y = 15;
			verts[1].z = 0;

			verts[2].x = 20;
			verts[2].y = 10;
			verts[2].z = 0;

			verts[3].x = 0;
			verts[3].y = 0;
			verts[3].z = 0;

			verts[4].x = 10;
			verts[4].y = 0;
			verts[4].z = 0;

			verts[5].x = 20;
			verts[5].y = 0;
			verts[5].z = 0;
		clipVB->Unlock();

		WORD *inds;
		clipIB->Lock(0, 0, (void**)&inds, NULL);
			inds[0] = 0;
			inds[1] = 4;
			inds[2] = 3;

			inds[3] = 4;
			inds[5] = 0;
			inds[6] = 1;

			inds[7] = 5;
			inds[8] = 4;
			inds[9] = 1;

			inds[10] = 5;
			inds[11] = 1;
			inds[12] = 2;
		clipIB->Unlock();
	}
};


This should make 2 quads(4 tris). But when I rendered, it only shows 2 of the triangles. The two that it shows are correct. All of them are defined in clockwise fashion, and culling is disabled. Ok...I render just the first triangle. Looks good. I render just the second triangle...weird, I see what should be the 3rd triangle. I render the 3rd triangle, and I see a new triangle that wasn't even specified! I've noticed some other weird things going on that seem to be inconsistent... [Edited by - yahastu on October 26, 2007 1:20:23 PM]

Share this post


Link to post
Share on other sites

Hi,

maybe you already noticed the little bug in your code ?



inds[0] = 0;
inds[1] = 4;
inds[2] = 3;

inds[3] = 4;
inds[5] = 0;
inds[6] = 1;

inds[7] = 5;
inds[8] = 4;
inds[9] = 1;

inds[10] = 5;
inds[11] = 1;
inds[12] = 2;



If I give you a hint that 4 comes after 3, not 5 ?

You are also indexing outside of your index array, which may result corrupted graphics.

Good luck!

Share this post


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