Sign in to follow this  
Enerjak

Help with custom vertex creation

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;
	VertexBuffer->Lock(0,0,(VOID**)&pVoid,D3DLOCK_DISCARD);
	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 this post


Link to 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Enerjak
I 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 this post


Link to post
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 this post


Link to post
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 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