向量中的独立对象位置

时间:2014-01-15 13:28:36

标签: c++ arrays vector directx-11

我正在使用DirectX 11并且目前使用户可以将对象放置在他们所站立的位置(立方体,暂时)。

我有一个这样的矢量 -

 std::vector<Cube> cubes; 

当用户按下某个键时,此代码块会执行以向矢量添加新的立方体(并将其位置设置为相机位置) -

    cube = new Cube();
    cube->Init(md3dDevice, md3dImmediateContext);
    cube->SetOffset(eyePos.x, eyePos.y, eyePos.z);
    cubeOffset = XMMatrixTranslation(cube->GetOffset().x, cube->GetOffset().y, cube->GetOffset().z);
    XMStoreFloat4x4(&cube->world, XMMatrixMultiply(boxScale, cubeOffset));
    cubes.push_back(*cube);

这会向矢量添加一个新的立方体,但所有先前的立方体位置都会被覆盖到当前摄像机位置(移动后),使其看起来只创建了一个立方体,并且每按一次按钮就会移动到摄像机位置。

这是我的控制台窗口的屏幕截图,用于演示问题:

http://imgur.com/aXEKPxE

我设置了一个迭代器,在设置它的个别世界mattix之后调用所有立方体绘制函数。

非常感谢任何帮助。

修改

Cube类是这样的 -

Cube.h

#include "d3dUtil.h"
#include "GameTimer.h"
#include <string>
#include "Point3f.h"
//-- simple vertex structure
struct Vertex
{
XMFLOAT3 Pos;
XMFLOAT4 Color;
};

 class Cube{

public:

Cube();
~Cube();

public:
void Init(ID3D11Device* device, ID3D11DeviceContext* deviceContext);
void SetOffset(float x, float y, float z);
Point3f GetOffset();

void BuildGeometryBuffers();
void Draw();

XMFLOAT4X4 world;

private:

//-- Vertex and Index Buffers
ID3D11Buffer* mCubeVB;
ID3D11Buffer* mCubeIB;

ID3D11Device* md3dDevice;
ID3D11DeviceContext* md3dDeviceContext;

Point3f mPosition;
  };

Cube.cpp

#include "Cube.h"

 //-- Constructor
 Cube::Cube(){


 }

 //-- Deconstructor
 Cube::~Cube(){
//-- safely release
ReleaseCOM(mCubeVB);
ReleaseCOM(mCubeIB);
 }

 void Cube::Init(ID3D11Device* device, ID3D11DeviceContext* deviceContext)
 {
md3dDevice = device;
md3dDeviceContext = deviceContext;

//-- define vectors and indices 
BuildGeometryBuffers();

 }
 void Cube::SetOffset(float x, float y, float z)
 { 
 mPosition.x =  x;
 mPosition.y =  y;
 mPosition.z =  z;

 }
   Point3f Cube::GetOffset()
   {
 return mPosition;
  }
void Cube::BuildGeometryBuffers()
{
//-- Create vertex buffer
Vertex vertices[] =
{
    { XMFLOAT3(-1.0f, -1.0f, -1.0f), (const float*)&Colors::White   },
    { XMFLOAT3(-1.0f, +1.0f, -1.0f), (const float*)&Colors::Black   },
    { XMFLOAT3(+1.0f, +1.0f, -1.0f), (const float*)&Colors::Red     },
    { XMFLOAT3(+1.0f, -1.0f, -1.0f), (const float*)&Colors::Green   },
    { XMFLOAT3(-1.0f, -1.0f, +1.0f), (const float*)&Colors::Blue    },
    { XMFLOAT3(-1.0f, +1.0f, +1.0f), (const float*)&Colors::Yellow  },
    { XMFLOAT3(+1.0f, +1.0f, +1.0f), (const float*)&Colors::Cyan    },
    { XMFLOAT3(+1.0f, -1.0f, +1.0f), (const float*)&Colors::Magenta }
};

D3D11_BUFFER_DESC vbd;                      //-- Holds the vertex buffer resource description
vbd.Usage = D3D11_USAGE_IMMUTABLE;          //-- Only accessable by GPU
vbd.ByteWidth = sizeof(Vertex) * 8;         //-- Size of buffer
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;   //-- Bind the buffer as a vertex buffer to the input-assembler stage of the pipeline
vbd.CPUAccessFlags = 0;                     //-- No CPU access
vbd.MiscFlags = 0;
vbd.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA vinitData;           //-- Holds subresource data
vinitData.pSysMem = vertices;               //-- Use the vertices as initialisation data
HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mCubeVB)); //-- Create final buffer using all information


//-- Create the index buffer
UINT indices[] = {

    0, 1, 2,        //-- Front face
    0, 2, 3,

    4, 6, 5,        //-- Back face
    4, 7, 6,

    4, 5, 1,        //-- Left face
    4, 1, 0,

    3, 2, 6,        //-- right face
    3, 6, 7,

    1, 5, 6,        //-- top face
    1, 6, 2,

    4, 0, 3,        //-- bottom face
    4, 3, 7
};

D3D11_BUFFER_DESC ibd;                      //-- index buffer description, simple to vertex buffer
ibd.Usage = D3D11_USAGE_IMMUTABLE;
ibd.ByteWidth = sizeof(UINT) * 36;          //-- 36 indices
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0;
ibd.MiscFlags = 0;
ibd.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA iinitData;
iinitData.pSysMem = indices;
HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mCubeIB)); //-- create final index buffer using all information
}

void Cube::Draw()
{
UINT stride = sizeof(Vertex);
UINT offset = 0;
//-- Set the created buffers
md3dDeviceContext->IASetVertexBuffers(0, 1, &mCubeVB, &stride, &offset);
md3dDeviceContext->IASetIndexBuffer(mCubeIB, DXGI_FORMAT_R32_UINT, 0);

//-- Draw the cube
md3dDeviceContext->DrawIndexed(36, 0, 0);
}

1 个答案:

答案 0 :(得分:0)

您可能正在设置他们各自的世界矩阵,但您永远不会阅读它们。您必须将此矩阵添加到着色器。