Example #1
0
	bool createIB(const Microsoft::WRL::ComPtr<ID3D11Device> &pDevice)
	{
		unsigned int pIndices[] =
		{
			0, 1, 2, 2, 3, 0,
			4, 5, 6, 6, 7, 4,
			8, 9, 10, 10, 11, 8,
			12, 13, 14, 14, 15, 12,
			16, 17, 18, 18, 19, 16,
			20, 21, 22, 22, 23, 20,
		};
		unsigned int isize = sizeof(pIndices);
		m_indices = isize / sizeof(pIndices[0]);

		D3D11_BUFFER_DESC idesc;
		ZeroMemory(&idesc, sizeof(idesc));
		idesc.ByteWidth = isize;
		idesc.Usage = D3D11_USAGE_DEFAULT;
		idesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
		idesc.CPUAccessFlags = 0;

		D3D11_SUBRESOURCE_DATA indexData;
		ZeroMemory(&indexData, sizeof(indexData));
		indexData.pSysMem = pIndices;

		HRESULT hr = pDevice->CreateBuffer(&idesc, &indexData, m_pIndexBuf.GetAddressOf());
		if (FAILED(hr)){
			return false;
		}

		return true;
	}
Example #2
0
	bool createVB(const Microsoft::WRL::ComPtr<ID3D11Device> &pDevice)
	{
		// Create VB
		auto size = 0.5f;
		Vertex pVertices[] =
		{
			// x
			{ DirectX::XMFLOAT4(-size, -size, -size, 1.0f), DirectX::XMFLOAT4(0.5f, 0.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(0, 1) },
			{ DirectX::XMFLOAT4(-size, -size, size, 1.0f), DirectX::XMFLOAT4(0.5f, 0.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(0, 0) },
			{ DirectX::XMFLOAT4(-size, size, size, 1.0f), DirectX::XMFLOAT4(0.5f, 0.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(1, 0) },
			{ DirectX::XMFLOAT4(-size, size, -size, 1.0f), DirectX::XMFLOAT4(0.5f, 0.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(1, 1) },

			{ DirectX::XMFLOAT4(size, -size, -size, 1.0f), DirectX::XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(0, 1) },
			{ DirectX::XMFLOAT4(size, size, -size, 1.0f), DirectX::XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(1, 1) },
			{ DirectX::XMFLOAT4(size, size, size, 1.0f), DirectX::XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(1, 0) },
			{ DirectX::XMFLOAT4(size, -size, size, 1.0f), DirectX::XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(0, 0) },
			// y
			{ DirectX::XMFLOAT4(-size, size, -size, 1.0f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(0, 1) },
			{ DirectX::XMFLOAT4(-size, size, size, 1.0f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(0, 0) },
			{ DirectX::XMFLOAT4(size, size, size, 1.0f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(1, 0) },
			{ DirectX::XMFLOAT4(size, size, -size, 1.0f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(1, 1) },

			{ DirectX::XMFLOAT4(-size, -size, -size, 1.0f), DirectX::XMFLOAT4(0.0f, 0.5f, 0.0f, 1.0f), DirectX::XMFLOAT2(0, 1) },
			{ DirectX::XMFLOAT4(size, -size, -size, 1.0f), DirectX::XMFLOAT4(0.0f, 0.5f, 0.0f, 1.0f), DirectX::XMFLOAT2(1, 1) },
			{ DirectX::XMFLOAT4(size, -size, size, 1.0f), DirectX::XMFLOAT4(0.0f, 0.5f, 0.0f, 1.0f), DirectX::XMFLOAT2(1, 0) },
			{ DirectX::XMFLOAT4(-size, -size, size, 1.0f), DirectX::XMFLOAT4(0.0f, 0.5f, 0.0f, 1.0f), DirectX::XMFLOAT2(0, 0) },
			// z
			{ DirectX::XMFLOAT4(-size, -size, -size, 1.0f), DirectX::XMFLOAT4(0.0f, 0.0f, 0.5f, 1.0f), DirectX::XMFLOAT2(0, 1) },
			{ DirectX::XMFLOAT4(-size, size, -size, 1.0f), DirectX::XMFLOAT4(0.0f, 0.0f, 0.5f, 1.0f), DirectX::XMFLOAT2(0, 0) },
			{ DirectX::XMFLOAT4(size, size, -size, 1.0f), DirectX::XMFLOAT4(0.0f, 0.0f, 0.5f, 1.0f), DirectX::XMFLOAT2(1, 0) },
			{ DirectX::XMFLOAT4(size, -size, -size, 1.0f), DirectX::XMFLOAT4(0.0f, 0.0f, 0.5f, 1.0f), DirectX::XMFLOAT2(1, 1) },

			{ DirectX::XMFLOAT4(-size, -size, size, 1.0f), DirectX::XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f), DirectX::XMFLOAT2(0, 1) },
			{ DirectX::XMFLOAT4(size, -size, size, 1.0f), DirectX::XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f), DirectX::XMFLOAT2(1, 1) },
			{ DirectX::XMFLOAT4(size, size, size, 1.0f), DirectX::XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f), DirectX::XMFLOAT2(1, 0) },
			{ DirectX::XMFLOAT4(-size, size, size, 1.0f), DirectX::XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f), DirectX::XMFLOAT2(0, 0) },
		};
		unsigned int vsize = sizeof(pVertices);

		D3D11_BUFFER_DESC vdesc;
		ZeroMemory(&vdesc, sizeof(vdesc));
		vdesc.ByteWidth = vsize;
		vdesc.Usage = D3D11_USAGE_DEFAULT;
		vdesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
		vdesc.CPUAccessFlags = 0;

		D3D11_SUBRESOURCE_DATA vertexData;
		ZeroMemory(&vertexData, sizeof(vertexData));
		vertexData.pSysMem = pVertices;

		HRESULT hr = pDevice->CreateBuffer(&vdesc, &vertexData, m_pVertexBuf.GetAddressOf());
		if (FAILED(hr)){
			return false;
		}

		return true;
	}
Example #3
0
void Cube::Create(Microsoft::WRL::ComPtr<ID3D11Device1> &d3dDevice)
{
    VertexPositionColor cubeVertices[] = 
    {
        {XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, 0.0f)},
        {XMFLOAT3(-0.5f, -0.5f,  0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
        {XMFLOAT3(-0.5f,  0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
        {XMFLOAT3(-0.5f,  0.5f,  0.5f), XMFLOAT3(0.0f, 1.0f, 1.0f)},
        {XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
        {XMFLOAT3( 0.5f, -0.5f,  0.5f), XMFLOAT3(1.0f, 0.0f, 1.0f)},
        {XMFLOAT3( 0.5f,  0.5f, -0.5f), XMFLOAT3(1.0f, 1.0f, 0.0f)},
        {XMFLOAT3( 0.5f,  0.5f,  0.5f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
    };

    D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
    vertexBufferData.pSysMem = cubeVertices;
    vertexBufferData.SysMemPitch = 0;
    vertexBufferData.SysMemSlicePitch = 0;
    DX::ThrowIfFailed(
        d3dDevice->CreateBuffer(
            &CD3D11_BUFFER_DESC(sizeof(cubeVertices), D3D11_BIND_VERTEX_BUFFER),
            &vertexBufferData,
            &m_vertexBuffer
            )
        );

    unsigned short cubeIndices[] = 
    {
        0,2,1, // -x
        1,2,3,

        4,5,6, // +x
        5,7,6,

        0,1,5, // -y
        0,5,4,

        2,6,7, // +y
        2,7,3,

        0,4,6, // -z
        0,6,2,

        1,3,7, // +z
        1,7,5,
    };

	m_indexCount = ARRAYSIZE(cubeIndices);

    D3D11_SUBRESOURCE_DATA indexBufferData = {0};
    indexBufferData.pSysMem = cubeIndices;
    indexBufferData.SysMemPitch = 0;
    indexBufferData.SysMemSlicePitch = 0;
    DX::ThrowIfFailed(
        d3dDevice->CreateBuffer(
            &CD3D11_BUFFER_DESC(sizeof(cubeVertices), D3D11_BIND_INDEX_BUFFER),
            &indexBufferData,
            &m_indexBuffer
            )
        );
}
Example #4
0
void glBufferData(GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage)
{
    if( g_pD3DDevice == 0 )
    {
        LOGInfo( LOGTag, "Trying to initialize a buffer before d3d is initialized!\n" );
        return;
    }

    int buffer = -1;

    if( target == GL_ARRAY_BUFFER )
        buffer = g_ActiveBufferObject_Array;
    else if( target == GL_ELEMENT_ARRAY_BUFFER )
        buffer = g_ActiveBufferObject_ElementArray;

    if( buffer == -1 )
        return;

    assert( g_D3DBufferObjects[buffer].m_InUse == true );

    if( g_D3DBufferObjects[buffer].m_Buffer == 0 )
    {
        D3D11_BUFFER_DESC BufferDesc;

        // TODO: handle usage
        BufferDesc.Usage = D3D11_USAGE_DYNAMIC;
        BufferDesc.ByteWidth = size;
        if( target == GL_ARRAY_BUFFER )
            BufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
        else if( target == GL_ELEMENT_ARRAY_BUFFER )
            BufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;    
        BufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
        BufferDesc.MiscFlags = 0;
        BufferDesc.StructureByteStride = 0;

        // Now create the vertex buffer.
        HRESULT result = g_pD3DDevice->CreateBuffer( &BufferDesc, 0, &g_D3DBufferObjects[buffer].m_Buffer );
        if( FAILED(result) )
            return;
    }

    // set the buffer data:
    if( data != 0 )
    {
        D3D11_MAPPED_SUBRESOURCE mappedResource;

        // Lock the vertex buffer so it can be written to.
        HRESULT result = g_pD3DContext->Map( g_D3DBufferObjects[buffer].m_Buffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource );
        if( FAILED(result) )
            return;

        // Get a pointer to the data in the vertex buffer.
        void* verticesPtr = (void*)mappedResource.pData;

        // Copy the data into the vertex buffer.
        memcpy( verticesPtr, (void*)data, size );

        // Unlock the vertex buffer.
        g_pD3DContext->Unmap( g_D3DBufferObjects[buffer].m_Buffer.Get(), 0 );
    }
}