Jump to content

  • Log In with Google      Sign In   
  • Create Account

cube2 overrides cube1's vertex data


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 mosweg   Members   -  Reputation: 131

Like
0Likes
Like

Posted 18 February 2013 - 10:50 AM

I have been working a long time to get 2 cubes instead of 1 with the Direct3d11 tutorial 7
Every time I end up with 2 cubes but when changing the vertex data they seem to override each other ending with 2 equal cubes, let me make a clear example:

Object.Create(1); // take vertices1 as vertex buffer
Object2.Create(1); // take vertices2 as vertex buffer
//This ends in 2 cubes with vertices1 data result
Object.Create(1); // take vertices1 as vertex buffer
Object2.Create(2); // take vertices2 as vertex buffer
//This ends in 2 cubes with vertices2 data result
Object.Create(2); // take vertices1 as vertex buffer
Object2.Create(1); // take vertices2 as vertex buffer
//This ends in 2 cubes with vertices1 data result

So the last one does something wrong that makes the first cube have the same data as the last one…

 


After multiple failed attempts with starting over again I’ve decided to make the code so clear as possible and present it to the Gamedev community in the hope that someone is able to find an answer to my problem.
What do I need to change/add/remove from my code to get the 2 cubes with both a different vertex buffer?
If someone can find out where I went wrong I would be so grateful, I’ve been trying to fix this for weeks now and I’m really starting to think I just suck at DirectX11

Attach there is the Tutorial07.txt file containing the whole code to load and render the 2 cubes


Cube struct + create & render function:


struct _Object
{

	XMMATRIX				g_World;
	CBChangesEveryFrame		cb;
	ID3D11Buffer*			g_pVertexBuffer;
	ID3D11Buffer*			g_pIndexBuffer;
	D3D11_SUBRESOURCE_DATA	InitData;
	D3D11_BUFFER_DESC		bd;

	void Render();
	void Create(int vertices_number);
};

void _Object::Render()
{

	//
	// Update variables that change once per frame
	//
	cb.mWorld = XMMatrixTranspose( g_World );
	cb.vMeshColor = g_vMeshColor;
	g_pImmediateContext->UpdateSubresource( g_pCBChangesEveryFrame, 0, NULL, &cb, 0, 0 );

	//
	// Render the cube
	//
	g_pImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 );
	g_pImmediateContext->VSSetConstantBuffers( 0, 1, &g_pCBNeverChanges );
	g_pImmediateContext->VSSetConstantBuffers( 1, 1, &g_pCBChangeOnResize );
	g_pImmediateContext->VSSetConstantBuffers( 2, 1, &g_pCBChangesEveryFrame );
	g_pImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 );
	g_pImmediateContext->PSSetConstantBuffers( 2, 1, &g_pCBChangesEveryFrame );
	g_pImmediateContext->PSSetShaderResources( 0, 1, &g_pTextureRV );
	g_pImmediateContext->PSSetSamplers( 0, 1, &g_pSamplerLinear );
	g_pImmediateContext->DrawIndexed( 36, 0, 0 );
}

void _Object::Create(int vertices_number)
{
	HRESULT hr = S_OK;

	SimpleVertex vertices1[] =
	{
		{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
		{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },

		{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
		{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },

		{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
		{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
		{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
		{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },

		{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
		{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },

		{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
		{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 1.0f ) },

		{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
		{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
	};

	SimpleVertex vertices2[] =
	{
		{ XMFLOAT3( -1.0f, 2.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, 2.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, 2.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
		{ XMFLOAT3( -1.0f, 2.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },

		{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
		{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },

		{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
		{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
		{ XMFLOAT3( -1.0f, 2.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
		{ XMFLOAT3( -1.0f, 2.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },

		{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, 2.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
		{ XMFLOAT3( 1.0f, 2.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },

		{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, 2.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
		{ XMFLOAT3( -1.0f, 2.0f, -1.0f ), XMFLOAT2( 0.0f, 1.0f ) },

		{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
		{ XMFLOAT3( 1.0f, 2.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
		{ XMFLOAT3( -1.0f, 2.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
	};


    ZeroMemory( &bd, sizeof(bd) );
    bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof( SimpleVertex ) * 24;
    bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    bd.CPUAccessFlags = 0;
    ZeroMemory( &InitData, sizeof(InitData) );
	
	if(vertices_number == 1)
	{
		InitData.pSysMem = &vertices1[0];
	}
	else if(vertices_number == 2)
	{
		InitData.pSysMem = &vertices2[0];
	}
    hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &g_pVertexBuffer );
    if( FAILED( hr ) )
        { }

    // Set vertex buffer
    UINT stride = sizeof( SimpleVertex );
    UINT offset = 0;
    g_pImmediateContext->IASetVertexBuffers( 0, 1, &g_pVertexBuffer, &stride, &offset );

    // Create index buffer
    WORD indices[] =
    {
        3,1,0,
        2,1,3,

        6,4,5,
        7,4,6,

        11,9,8,
        10,9,11,

        14,12,13,
        15,12,14,

        19,17,16,
        18,17,19,

        22,20,21,
        23,20,22
    };

    bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof( WORD ) * 36;
    bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
    bd.CPUAccessFlags = 0;
    InitData.pSysMem = &indices[0];
    hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &g_pIndexBuffer );
    if( FAILED( hr ) )
        { }

    // Set index buffer
    g_pImmediateContext->IASetIndexBuffer( g_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0 );

    // Set primitive topology
    g_pImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

    // Create the constant buffers
    bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof(CBNeverChanges);
    bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    bd.CPUAccessFlags = 0;
    hr = g_pd3dDevice->CreateBuffer( &bd, NULL, &g_pCBNeverChanges );
    if( FAILED( hr ) )
        { }
    
    bd.ByteWidth = sizeof(CBChangeOnResize);
    hr = g_pd3dDevice->CreateBuffer( &bd, NULL, &g_pCBChangeOnResize );
    if( FAILED( hr ) )
        { }
    
    bd.ByteWidth = sizeof(CBChangesEveryFrame);
    hr = g_pd3dDevice->CreateBuffer( &bd, NULL, &g_pCBChangesEveryFrame );
    if( FAILED( hr ) )
        { }

    // Load the Texture
    hr = D3DX11CreateShaderResourceViewFromFile( g_pd3dDevice, L"seafloor.dds", NULL, NULL, &g_pTextureRV, NULL );
    if( FAILED( hr ) )
        { }

    // Create the sample state
    D3D11_SAMPLER_DESC sampDesc;
    ZeroMemory( &sampDesc, sizeof(sampDesc) );
    sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
    sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
    sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
    sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
    sampDesc.MinLOD = 0;
    sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
    hr = g_pd3dDevice->CreateSamplerState( &sampDesc, &g_pSamplerLinear );
    if( FAILED( hr ) )
        { }

    // Initialize the world matrices
    g_World = XMMatrixIdentity();
}
_Object Object;
_Object Object2;

 

Attached Files



Sponsor:

#2 phil_t   Crossbones+   -  Reputation: 3928

Like
0Likes
Like

Posted 18 February 2013 - 11:35 AM

I've never used DX11, but it looks like you are using a single global variable for your vertex buffer. Each Object needs its own vertex buffer if it has different vertices.

 

And of course, you need to bind the Object's vertex buffer in its Render method.



#3 mosweg   Members   -  Reputation: 131

Like
0Likes
Like

Posted 18 February 2013 - 02:37 PM

I also think it’s an global variable, but I can’t manage to find it.
I’ve altered my starting post because I already tried to alter the global variable for the vertex buffer weeks ago, but after multiple tries some backups aren’t fully up to date with the ‘getting rid of the global variables’
The new post contains a lot less global variables but still the same problem arises



#4 Nik02   Crossbones+   -  Reputation: 2838

Like
0Likes
Like

Posted 18 February 2013 - 03:00 PM

If you use Visual Studio, right-click on the vertex buffer's variable and go with "go to declaration" to find the context of said variable.


Edited by Nik02, 18 February 2013 - 03:01 PM.

Niko Suni


#5 phil_t   Crossbones+   -  Reputation: 3928

Like
0Likes
Like

Posted 18 February 2013 - 04:42 PM

The new post contains a lot less global variables but still the same problem arises

 

As I mentioned before, you're not binding the vertex buffer before the DrawIndexed call in your Render method. So it's just rendering whatever was the last vertex buffer you set on the device.


Edited by phil_t, 18 February 2013 - 04:43 PM.


#6 mosweg   Members   -  Reputation: 131

Like
0Likes
Like

Posted 19 February 2013 - 03:04 PM

Well, I got it to work with your help, you're a natural in Dictex11 without ever using it ;)
In the tutorial it wasn’t mention once about putting other buffers intro the device.
I thought it was already done with g_pImmediateContext->UpdateSubresource but it seems g_pImmediateContext->IASetVertexBuffers was also required.
I found that line in some far away example about 2D things so I’m not really happy with the lack of tutorial/clear information about this subject what is part of the real basic of Direct3D11…






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS