• Advertisement
Sign in to follow this  

Using 2 input slots and 2 vertex buffers

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

[color=#4A4A4A]I wanna send the position trough one input slot,and the color trough another input slot.Then i wanna create a vertex buffer for position,one for color,and use them.The problem is that I that the color doesn't get processed! The position gets there,but the color not,so everything remains black![/color]

[source lang="cpp"] // Create the vertex input layout.
D3D10_INPUT_ELEMENT_DESC vertexDesc[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D10_INPUT_PER_VERTEX_DATA, 0}
};

// Create the input layout
D3D10_PASS_DESC PassDesc;
mTech->GetPassByIndex(0)->GetDesc(&PassDesc);
HR(md3dDevice->CreateInputLayout(vertexDesc, 2, PassDesc.pIAInputSignature,
PassDesc.IAInputSignatureSize, &mVertexLayout));


mNumVertices = 8;
mNumFaces = 7; // 2 per quad

// Create vertex buffer
Vertex vertices[] =
{
{D3DXVECTOR3(+1.0f, 0.0f, -1.0f), WHITE},
{D3DXVECTOR3(-1.0f, 0.0f, -1.0f), BLACK},
{D3DXVECTOR3(+1.0f, 0.0f, +1.0f), RED},
{D3DXVECTOR3(-1.0f, 0.0f, +1.0f), GREEN},
{D3DXVECTOR3(0.0f, 2.0f, 0.0f), BLUE}, //varf


{D3DXVECTOR3(+4.0f, 0.0f, -1.0f), WHITE},
{D3DXVECTOR3(+6.0f, 0.0f, -1.0f), BLACK},
{D3DXVECTOR3(+5.0f, 0.0f, +1.0f), RED},

};

// Scale the box.
for(DWORD i = 0; i < mNumVertices; ++i)
vertices[i].pos *= scale;


D3D10_BUFFER_DESC vbd;
vbd.Usage = D3D10_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(Vertex) * mNumVertices;
vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = vertices;
HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB));

D3D10_BUFFER_DESC vbd2;
vbd2.Usage = D3D10_USAGE_IMMUTABLE;
vbd2.ByteWidth = sizeof(Vertex) * mNumVertices;
vbd2.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd2.CPUAccessFlags = 0;
vbd2.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData2;
vinitData2.pSysMem = vertices;
HR(md3dDevice->CreateBuffer(&vbd2, &vinitData2, &mVB2));
// Create the index buffer

DWORD indices[] = {
1,0,2,
1,3,2,

0,4,1,
0,4,2,
4,2,3,
4,3,1,





5,6,7
};

D3D10_BUFFER_DESC ibd;
ibd.Usage = D3D10_USAGE_IMMUTABLE;
ibd.ByteWidth = sizeof(DWORD) * mNumFaces*3;
ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0;
ibd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA iinitData;
iinitData.pSysMem = indices;
HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mIB));



ID3D10Buffer* g[] = {mVB,mVB2};

UINT stride = sizeof(Vertex);

UINT offset = 0;
md3dDevice->IASetVertexBuffers(0, 2, g, &stride, &offset);
md3dDevice->IASetIndexBuffer(mIB, DXGI_FORMAT_R32_UINT, 0);
md3dDevice->DrawIndexed(mNumFaces*3 - 3, 0, 0);
md3dDevice->DrawIndexed(3,mNumFaces * 3 - 3, 0);[/source]

Share this post


Link to post
Share on other sites
Advertisement
You have some errors in your code.

1-If you want data separated in two vertex buffers store it in different structures, in this case no need to use structures since each buffer only contains one type of data.
So separate position from color like this:

[CODE]
D3DXVECTOR3 verticesPos[] = { positions here };
D3DXCOLOR verticesColor[] = {colors here};
[/CODE]


2-The code you're using to create the buffers is creating 2 buffers each containing both positions and colors. So replace to this:
[CODE]
D3D10_BUFFER_DESC vbd;

vbd.Usage = D3D10_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof([b]D3DXVECTOR3[/b]) * mNumVertices;
vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = [b]verticesPos[/b];
HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB));

D3D10_BUFFER_DESC vbd2;
vbd2.Usage = D3D10_USAGE_IMMUTABLE;
vbd2.ByteWidth = sizeof([b]D3DXCOLOR[/b]) * mNumVertices;
vbd2.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd2.CPUAccessFlags = 0;
vbd2.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData2;
vinitData2.pSysMem = [b]verticesColor[/b];
HR(md3dDevice->CreateBuffer(&vbd2, &vinitData2, &mVB2));

[/CODE]

3-If you're using multiple vertex buffers you have to pass an array of strides/offsets:
[CODE]

UINT strides[] = { sizeof(D3DXVECTOR3), sizeof(D3DXCOLOR) };

UINT offsets[] = {0,0};
md3dDevice->IASetVertexBuffers(0, 2, g, strides, offsets);

[/CODE]

Share this post


Link to post
Share on other sites
You fill both of the buffers with the same interleaved data (that contains both positions and colors). Since the vertex stride (byte width) is still correct in both buffers, the input assembler treats the first 16 bytes of each vertex found in the second vertex buffer as color (which is presumably not what you want).

Also, enable the debug output to see if you get warnings for invalid states. An invalid pixel shader can cause black results.

Share this post


Link to post
Share on other sites
Tiago,can you also tell me how to use structures like you said above?

[left][background=rgb(250, 251, 252)]How would this work with structures if I would have 2 vertex buffers and the following elements:[/background][/left]


[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]position[/background][/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]normal[/background][/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]color[/background][/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]texcoord[/background][/size][/font][/color][/left]


[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]So I would send position and normal trough 1 buffer,and color and texcoord in another buffer.[/background][/size][/font][/color][/left]


[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]Please show me how I would use the structures in this.[/background][/size][/font][/color][/left]

Share this post


Link to post
Share on other sites
Create 2 structures and fill them:
[CODE]
struct PosNormal
{
D3DXVECTOR3 position;
D3DXVECTOR3 normal;
};


struct ColorTexC
{
D3DXCOLOR color;
D3DXVECTOR2 texC;
};

PosNormal positionNormals[] = { data here };
ColorTexC colorTexCoords[] = { data here };
[/CODE]

Replace the size of the buffers and the strides
[CODE]
D3D10_BUFFER_DESC vbd;
vbd.Usage = D3D10_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof([b]PosNormal[/b]) * mNumVertices;
vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = [b]positionNormals[/b];
HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB));

D3D10_BUFFER_DESC vbd2;
vbd2.Usage = D3D10_USAGE_IMMUTABLE;
vbd2.ByteWidth = sizeof([b]ColorTexC[/b]) * mNumVertices;
vbd2.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd2.CPUAccessFlags = 0;
vbd2.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData2;
vinitData2.pSysMem = [b]colorTexCoords[/b];
HR(md3dDevice->CreateBuffer(&vbd2, &vinitData2, &mVB2));


UINT strides[] = { sizeof(PosNormal), sizeof(ColorTexC) };
UINT offsets[] = {0,0};
md3dDevice->IASetVertexBuffers(0, 2, buffers, strides, offsets);
[/CODE]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement