DrawPrimitive Question

Started by
2 comments, last by Paul65 12 years, 10 months ago
Hi,

My DirectX C++ engine works fine with Sprites, Textures, etc. And this call to DrawPrimitiveUP works fine too:

device->DrawPrimitiveUP(D3DPT_LINESTRIP, 3, vertices, sizeof(Vertex));


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

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


Here are my vertices:

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)},
};


Using that Shader I attempt this 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);


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

Any clues?
Advertisement
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.
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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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 D3DXMatrixPerspectiveFovLH.


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.

This topic is closed to new replies.

Advertisement