Skip to main content
GameDev.net gamedev.net
🔒 Locked

triangles not displayed

Started by tri-thanatos Jan 8, 2004 at 6:48 PM 1 replies 300+ views
Original Post
tri-thanatos
tri-thanatos
ok, I am new to direct x programming, and attempted to do my first vertex/index buffers and display primitives. The buffers are created and filled without any trouble, however, they fail to display, here is the class declaration :
#define BUFTYPE_TRI 0
#define BUFTYPE_QUAD 1
class Vertex
{
public:
	Vertex();
	Vertex(float X,float Y,float Z)
	{
		x=X;y=Y;z=Z;
	};

	float x,y,z;
};

class GObject
{
public:
int nVertices;
Vertex* Vertices;
WORD* Indices;
IDirect3DVertexBuffer9* vb;
IDirect3DIndexBuffer9* ib;
void CreateBuffer(int NumOfVertices);
void FillBuffer(char* szFileName);
void DisplayBuffer(unsigned int uType);
};
 
here is the code for my creation/filling

void GObject::CreateBuffer(int NumOfVertices)
{

	HRESULT hRes;
	nVertices = NumOfVertices;
	
	hRes = g_EchD3D.g_d3d_device->CreateVertexBuffer(nVertices * sizeof(Vertex),
											   D3DUSAGE_WRITEONLY,
											   D3DFVF_XYZ,
											   D3DPOOL_MANAGED,
											   &vb,
											   0);

	if(hRes != D3D_OK)
		MessageBox(NULL,"Could Not Initialize VertexBuffer","D3D Init Error",MB_OK);

	hRes = g_EchD3D.g_d3d_device->CreateIndexBuffer(nVertices * sizeof(Vertex),
											 D3DUSAGE_WRITEONLY,
											 D3DFMT_INDEX16,
											 D3DPOOL_MANAGED,
											 &ib,
											 0);
	if(hRes != D3D_OK)
		MessageBox(NULL,"Could Not Initialize IndexBuffer","D3D Init Error",MB_OK);
};



void GObject::FillBuffer(char* szFileName)
{
	HRESULT hRes;
   
	hRes = vb->Lock(0,0,(void**)&Vertices,0);

	if(hRes != D3D_OK)
	{
		MessageBeep(0);
		MessageBox(NULL,"Could Not Lock VertexBuffer","D3D Init Error",MB_OK);
	}

	Vertices[0] = Vertex(-1.0f,0.0f,3.0f);
	Vertices[2] = Vertex(0.0f,1.0f,3.0f);
	Vertices[3] = Vertex(1.0f,0.0f,3.0f);

	hRes = vb->Unlock();

	if(hRes != D3D_OK)
	{
		MessageBeep(0);
		MessageBox(NULL,"Could Not unlock VertexBuffer","D3D Init Error",MB_OK);
	}

	hRes = ib->Lock(0,0,(void**)&Indices,0);
	if(hRes != D3D_OK)
	{
		MessageBeep(0);
		MessageBox(NULL,"Could Not Lock IndexBuffer","D3D Init Error",MB_OK);
	}
	Indices[0] = 0;
	Indices[1] = 1;
	Indices[2] = 2;

	hRes = ib->Unlock();

	if(hRes != D3D_OK)
	{
		MessageBeep(0);
		MessageBox(NULL,"Could Not unLock IndexBuffer","D3D Init Error",MB_OK);
	}
};

 
and the display code:

void GObject::DisplayBuffer(unsigned int uType)
{

g_EchD3D.g_d3d_device->SetStreamSource(0,vb,0,sizeof(Vertex));
g_EchD3D.g_d3d_device->SetIndices(ib);
g_EchD3D.g_d3d_device->SetFVF(D3DFVF_XYZ);
g_EchD3D.g_d3d_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,3,0,1);
		return;

};
 
and here is the rendering code:
void InitScene()
{
	CameraAtOrig();
    SetDefaultProj();
	gObj.CreateBuffer(3);
	gObj.FillBuffer((char*) 0);
};
void EchD3DxGUI::Render()
{

	g_d3d_device->Clear(0,0,D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET,0x00000000,1.0f,0);
	g_d3d_device->BeginScene();

    gObj.DisplayBuffer(BUFTYPE_TRI);
	g_d3d_device->EndScene();
	g_d3d_device->Present(0,0,0,0);
};
 
It loads up and starts fine, however, nothing is blitted to the screen.
Supernat02
Supernat02
My first intuition is that it is drawing the triangle, but it''s black because you aren''t providing a color for it. The FFP(Fixed Function Pipeline) in the video card first determines where to put a vertex (defined by your stream), then it determines the color of it (passed through the stream also). If your stream (your vertex structures) does not contain color information, a diffuse in this case because you aren''t using lighting, then it probably just sets the output color to black.

A quick test would be to clear your background to a different color, try blue or red. See if the triangle shows up then.

Also, you defined your vertices in counter clockwise order, so you''ll need to set the render state for Culling to CULL_CW. Or set it to CULL_NONE for testing purposes. Also, set your ambient light to (255, 255, 255), full white.

Lastly, make sure your projection, view, and world matrices are set appropriately. What have you set them to?

Good luck,
Chris
Chris ByersMicrosoft DirectX MVP - 2005
tri-thanatos
tri-thanatos
what do you mean I am declaring them CCW? what is the proper way to do it for CW in D3D, as I am doing it the way OGL has it CCW culling.

EDIT:nvm, figured it out

[edited by - tri-thanatos on January 8, 2004 12:02:01 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.