Sign in to follow this  
sriam

DrawPrimitive Question

Recommended Posts

Hi,

My DirectX C++ engine works fine with Sprites, Textures, etc. And this call to DrawPrimitiveUP works fine too:
[code]
device->DrawPrimitiveUP(D3DPT_LINESTRIP, 3, vertices, sizeof(Vertex));[/code]

So my next step is to use a shader, which I got out of a book:

[code]const char * strShader =
"vs_1_1 \n"
"dcl_position v0 \n"
"dcl_color v1 \n"
"m4x4 oPos, v0, c0 \n"
"mov oD0, v1 \n"
"";[/code]

Here are my vertices:

[code] Vertex vertices[] =
{
{20, 40, 0, D3DCOLOR_RGBA(0, 255, 255, 255)},
{40, 20, 0, D3DCOLOR_RGBA(255, 0, 0, 255)},
{60, 60, 0, D3DCOLOR_RGBA(0, 255, 255, 255)},
{20, 40, 0, D3DCOLOR_RGBA(0, 255, 255, 255)},
};[/code]

Using that Shader I attempt this code:
[code]
result = device->CreateVertexBuffer(
4 * sizeof(Vertex), 0, 0, D3DPOOL_DEFAULT, &mVertexBuffer, 0);

// lock the buffer to prepare for copying info into shader
mVertexBuffer->Lock(0, sizeof(vertices), (void**)&pVertices, 0);

memcpy(pVertices, vertices, sizeof(vertices));

mVertexBuffer->Unlock();

// Create the vertex declaration
D3DVERTEXELEMENT9 decl[]=
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
D3DDECL_END()
};

result = device->CreateVertexDeclaration(
decl, &mVertexDeclaration);

// ready to assemble the shader
result = D3DXAssembleShader(
strShader, (UINT)strlen(strShader), 0, 0, D3DXSHADER_DEBUG, &mShader, 0 );

// create a vertex shader
result = device->CreateVertexShader(
(DWORD*) mShader->GetBufferPointer(), &mVertexShader );

D3DXMatrixIdentity( &mMatWorld );

result = device->SetVertexShaderConstantF( 0, (float *)&mMatWorld, 4 );
result = device->SetVertexDeclaration( mVertexDeclaration );
result = device->SetVertexShader( mVertexShader );
result = device->SetStreamSource(0, mVertexBuffer, 0, sizeof(Vertex));

// now we get the magic list of triangles back from the shader!
result = device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

device->SetVertexShader( 0 );
device->SetTexture(0, NULL);
[/code]

I check the result and it's fine all the way through, but my screen is empty!

Any clues?

Share this post


Link to post
Share on other sites
One thing is to make sure you are setting the cull mode and fill mode properly. Cant see any reference to them in the code. Could also check all the function calls are working. Another thing is when call DrawPrimitive you are drawing a triangle list with one primitive in it. A triangle list expects 3 vertices for each face. Your vertex buffer has 4 vertices so the last one isnt used.

Share this post


Link to post
Share on other sites
You should simplify and make one change at a time. You don't have to use a vertex buffer with a shader, you can still continue using DrawPrimitiveUP. You can even use a vertex declaration with DPUP. So I'd revert that part of the code back and just focus on getting the shader working.

I'd also advise that you really should consider moving to HLSL/SM 2.0; the only reason not to is if you're on really really ancient hardware (10 years old!!!) that doesn't support it. Life will be a lot less painful, and you'll find many more tutorials and working examples online.

Share this post


Link to post
Share on other sites
Actually one big obvious thing I noticed on looking at the code a second time, is that you have forgotten the projection matrix. The matrix you give to the shader should be the combination of the camera, projection and world matrices. You can get a projection matrix using some of the DirectX utility functions, like [b]D3DXMatrixPerspectiveFovLH.[/b]

[quote name='mhagain' timestamp='1306713349' post='4817281']
You should simplify and make one change at a time. You don't have to use a vertex buffer with a shader, you can still continue using DrawPrimitiveUP. You can even use a vertex declaration with DPUP. So I'd revert that part of the code back and just focus on getting the shader working.

I'd also advise that you really should consider moving to HLSL/SM 2.0; the only reason not to is if you're on really really ancient hardware (10 years old!!!) that doesn't support it. Life will be a lot less painful, and you'll find many more tutorials and working examples online.
[/quote]

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