2014-01-15 61 views
0

我正在使用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

我也有一個迭代器調用所有的立方體繪製功能,設置它的個人世界matrtix後。

任何幫助,非常感謝。

編輯

立方類是這樣的 -

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); 
} 
+0

您需要展示使用立方體的定義 – ltjax

+0

立即添加立方體類 – Wikaman1

+0

聞起來像立方體位置的全局變量或通過立方體數組的不正確循環。 –

回答

0

你可能設置自己的個人世界矩陣,但你永遠不會讀它們。你必須得到這個矩陣到着色器。

+0

顯然不能在這裏粘貼代碼,但是我在調​​用draw = function = XMLoadFloat4x4(&cube-> world)時設置了它們。這是在遍歷所有立方體的循環中完成的。然後在繪製之前使用SetMatrix設置矩陣 – Wikaman1