# Help with custom vertex creation

This topic is 3823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

For some reason, Direct3D doesn't seem to be able to render my custom vertex format that i created. here is the code.

#define TRIANGLEFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw;    // from the D3DFVF_XYZRHW flag
DWORD color;    // from the D3DFVF_DIFFUSE flag
};

CUSTOMVERTEX Verts[] =
{
{10.0f, 50.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255),},
{50.0f, 400.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0),},
{120.0f, 400.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},
};
void createandDrawTriangle()
{
LPDIRECT3DVERTEXBUFFER9 VertexBuffer;

HRESULT result = Direct3DDevice9->CreateVertexBuffer(
3*sizeof(CUSTOMVERTEX),
0,
TRIANGLEFVF,
D3DPOOL_DEFAULT,
&VertexBuffer,
NULL);

if(FAILED(result))
{
return;
}

VOID** pVoid;
memcpy(pVoid,Verts,sizeof(Verts));
VertexBuffer->Unlock();

Direct3DDevice9->SetFVF(TRIANGLEFVF);
Direct3DDevice9->SetStreamSource(0,VertexBuffer,0,sizeof(CUSTOMVERTEX));
Direct3DDevice9->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
}


for some reason, Direct3D wont render the triangle. I wonder what do i have to do to make it work? I know you can't construct models from vertices as that would take too long but i just want to be able to render a triangle and for some reason, Direct3D isn't doing that.

##### Share on other sites
Never mind......sorry for this useless post.

##### Share on other sites
Offhand, a couple things quite wrong I can see:

1 - Your triangle is defined counter-clockwise, so it is probably culled out by the GPU (unless providing pre-transformed vertices disables back-face culling, but I don't think so. Maybe someone can confirm)

=> Invert last two vertices or disable backface culling (D3DRS_CULLMODE)

2 - You vertex buffer belongs to the MANAGED pool unless you plan to lock, modify, unlock it every frame (I don't think so)

=> Change D3DPOOL_DEFAULT to D3DPOOL_MANAGED

3 - How do you plan to use your function? For your triangle to be rendered correctly you need to call

Direct3DDevice9->SetFVF(TRIANGLEFVF);Direct3DDevice9->SetStreamSource(0,VertexBuffer,0,sizeof(CUSTOMVERTEX));Direct3DDevice9->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);

every frame, properly between BeginScene() and EndScene() (and a Clear() will most likely be required too). This is the bare minimum. Your function is unfit for this, as it will recreate the vertex buffer every frame (d00h!!!). Just split createandDrawTriangle into two functions.

Have fun then [smile]

Edit: "nevermind"? Sounds like you spotted #1 by yourself. You have to fix #2 and #3 now, eventhough they don't prevent your triangle from rendering. ;)

##### Share on other sites
Oh and "VOID** pVoid;" should be "VOID* pVoid = NULL". Your compiler probably complained about that but you STFU'ed it with your cast [smile]

##### Share on other sites
Ok, i'm working with getting back buffers to work as in working with more then one but when i try it seems that it like messes up, you know? it seems like Direct3D is trying to put the back buffer while drawing the old one. it blinks. here is my code.

	Direct3DDevice9->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(100,100,100),1.0f,1);			if(Direct3DDevice9->BeginScene())	{		createandDrawTriangle();		Direct3DDevice9->EndScene();	}	Direct3DDevice9->Present(NULL,NULL,NULL,NULL);	Direct3DDevice9->Clear(1,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);		SwapChain->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&BackBuffer);					g_pFonts->DrawTextA(NULL,				"Hello World....FINALLY",				22,				&text,				DT_CENTER | DT_VCENTER,				D3DCOLOR_XRGB(255,255,255));		SwapChain->Present(NULL,NULL,NULL,NULL,NULL)also i've added a 3d camera to my front buffer or the fist back buffer that i've used. Also my SwapEffect is set to copy, should i put it to flip? not really sure i'm getting the back buffer stuff. I hope to though

##### Share on other sites
Quote:
 Original post by EnerjakI hope to though

You hope to? Ahah. programming is not about hoping. It's about understanding what you do. The code you posted doesn't make much sense (I can see what you're trying to achieve though)
Hint: In most simple situations like yours, you only call Clear(), BeginScene(), EndScene() and Present() once perframe, in that order. Direct3D will take about the back buffer / front buffer things for you.

Direct3DDevice9->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(100,100,100),1.0f,1);		if(Direct3DDevice9->BeginScene()){	createandDrawTriangle(); // If this still does the same as 3 posts ago, it sux.	g_pFonts->DrawTextA(NULL,                            "Hello World....FINALLY",                            22,                            &text,                            DT_CENTER | DT_VCENTER,                            D3DCOLOR_XRGB(255,255,255));	Direct3DDevice9->EndScene();}Direct3DDevice9->Present(NULL,NULL,NULL,NULL);

##### Share on other sites
but what about when you are making a game based on thinks like advanture fields? like Sonic Adventure. didn't they use ack buffers? cause they had to be able to allow you to go to different places at the same time. not sure if i'm making sense. but thanks, i'll try it. I also have a 3D camera put in, does that matter at all? and also a menu type thing where it goes from options to sound then back to the menu again, you know? don't they use the back buffer? or do they simply make a function with that in it and call it when ever they need it? this is why i used DarkBasic for so damn long, it's easy to use

##### Share on other sites
I mean no offense Enerjak, but no, it doesn't make sense to me (but I may be misunderstanding) I have the feeling that you are trying to understand advanced concepts (like managing multiple swap chains) without a solid comprehension of the fundamentals. I suggest you to take more time studying on how direct3d works, possibly getting a good book or stepping through the sdk tutorials.

Good luck
JA

##### Share on other sites
I'm not offended, just trying to understand this. I might be aiming too high though. So i need to keep learning, thanks.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631706
• Total Posts
3001833
×